Skip to content

Commit acf1dcf

Browse files
committed
make detection explicit
1 parent 923e97a commit acf1dcf

File tree

1 file changed

+14
-7
lines changed
  • instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources

1 file changed

+14
-7
lines changed

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarPathFinder.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ class JarPathFinder {
2020
private final Function<String, String> getSystemProperty;
2121
private final Predicate<Path> fileExists;
2222

23-
private static Optional<Optional<Path>> jarPath = Optional.empty();
23+
private static class DetectionResult {
24+
private final Optional<Path> jarPath;
25+
26+
private DetectionResult(Optional<Path> jarPath) {
27+
this.jarPath = jarPath;
28+
}
29+
}
30+
31+
private static Optional<DetectionResult> detectionResult = Optional.empty();
2432

2533
public JarPathFinder() {
2634
this(ProcessArguments::getProcessArguments, System::getProperty, Files::isRegularFile);
@@ -38,18 +46,17 @@ public JarPathFinder() {
3846

3947
// visible for testing
4048
static void resetForTest() {
41-
jarPath = Optional.empty();
49+
detectionResult = Optional.empty();
4250
}
4351

4452
Optional<Path> getJarPath() {
45-
if (jarPath.isPresent()) {
46-
return jarPath.get();
53+
if (!detectionResult.isPresent()) {
54+
detectionResult = Optional.of(new DetectionResult(Optional.ofNullable(detectJarPath())));
4755
}
48-
jarPath = Optional.of(Optional.ofNullable(readJarPath()));
49-
return jarPath.get();
56+
return detectionResult.get().jarPath;
5057
}
5158

52-
private Path readJarPath() {
59+
private Path detectJarPath() {
5360
Path jarPath = getJarPathFromProcessHandle();
5461
if (jarPath != null) {
5562
return jarPath;

0 commit comments

Comments
 (0)