Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sporadic Gradle failure: access-filter.json already exists (still exist in 0.10.4) #653 #654

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,31 @@ 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 {
Path tempFile = Files.createTempFile(ACCESS_FILTER_PREFIX, ACCESS_FILTER_SUFFIX);
tempFile.toFile().deleteOnExit();
return tempFile;
} 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