Skip to content

Commit

Permalink
Sporadic Gradle failure: access-filter.json already exists (still exi…
Browse files Browse the repository at this point in the history
…st in 0.10.4) #653
  • Loading branch information
ghilaima committed Dec 10, 2024
1 parent 355efa0 commit 2846bbc
Showing 1 changed file with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;

public class AgentConfiguration implements Serializable {
Expand Down Expand Up @@ -143,41 +144,29 @@ private void addDefaultAccessFilter() {
// this could only happen if we instantiated disabled agent configuration
return;
}
Path defaultAgentConfigTempFile = createDefaultAgentConfigTempFile();
try {
Files.writeString(defaultAgentConfigTempFile, getDefaultAgentConfigContent());
accessFilterFiles.add(defaultAgentConfigTempFile.toString());
} catch (IOException e) {
throw new RuntimeException("Cannot write default access filter to temporary file "+ defaultAgentConfigTempFile.toAbsolutePath(), e);
}
}

String tempDir = System.getProperty("java.io.tmpdir");
Path agentDir = Path.of(tempDir).resolve("agent-config");
Path accessFilterFile = agentDir.resolve(ACCESS_FILTER_PREFIX + ACCESS_FILTER_SUFFIX);
if (Files.exists(accessFilterFile)) {
accessFilterFiles.add(accessFilterFile.toString());
return;
private static Path createDefaultAgentConfigTempFile() {
try {
return Files.createTempFile(ACCESS_FILTER_PREFIX, ACCESS_FILTER_SUFFIX);
} catch (IOException e) {
throw new RuntimeException("Cannot create temporary file for access filter", e);
}
}

try(InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE_LOCATION)) {
if (accessFilterData == null) {
throw new IOException("Cannot access data from: " + DEFAULT_ACCESS_FILTER_FILE_LOCATION);
}

try {
Files.createDirectory(agentDir);
} catch (FileAlreadyExistsException e) {
logger.info("Skip creation of directory " + agentDir + " (already created).");
}

long pid = ProcessHandle.current().pid();
long time = System.currentTimeMillis();
Path tmpAccessFilter = agentDir.resolve(ACCESS_FILTER_PREFIX + '_' + pid + '_' + time + '_' + ACCESS_FILTER_SUFFIX);
Files.copy(accessFilterData, tmpAccessFilter);

try {
Files.move(tmpAccessFilter, accessFilterFile, StandardCopyOption.ATOMIC_MOVE);
} catch (FileAlreadyExistsException e) {
Files.delete(tmpAccessFilter);
logger.info(accessFilterFile + " already exists. Delete " + tmpAccessFilter);
}

accessFilterFiles.add(accessFilterFile.toString());
private static String getDefaultAgentConfigContent() {
try (InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE_LOCATION)) {
Objects.requireNonNull(accessFilterData, "Cannot access data from: " + DEFAULT_ACCESS_FILTER_FILE_LOCATION);
return new String(accessFilterData.readAllBytes());
} catch (IOException e) {
throw new RuntimeException("Cannot add default access-filter.json" ,e);
throw new RuntimeException("Cannot access default "+DEFAULT_ACCESS_FILTER_FILE_LOCATION+" from the classpath" ,e);
}
}

Expand Down

0 comments on commit 2846bbc

Please sign in to comment.