|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.resources;
|
7 | 7 |
|
8 |
| -import static java.util.logging.Level.WARNING; |
9 |
| - |
10 |
| -import java.io.InputStream; |
11 |
| -import java.net.URL; |
12 | 8 | import java.nio.file.Files;
|
13 | 9 | import java.nio.file.InvalidPathException;
|
14 | 10 | import java.nio.file.Path;
|
|
17 | 13 | import java.util.function.Function;
|
18 | 14 | import java.util.function.Predicate;
|
19 | 15 | import java.util.function.Supplier;
|
20 |
| -import java.util.jar.Manifest; |
21 |
| -import java.util.logging.Logger; |
22 | 16 | import javax.annotation.Nullable;
|
23 | 17 |
|
24 | 18 | class JarPathFinder {
|
25 | 19 | private final Supplier<String[]> getProcessHandleArguments;
|
26 | 20 | private final Function<String, String> getSystemProperty;
|
27 | 21 | private final Predicate<Path> fileExists;
|
28 |
| - private final Function<Path, Optional<Manifest>> manifestReader; |
29 | 22 |
|
30 |
| - private static final Logger logger = Logger.getLogger(JarPathFinder.class.getName()); |
| 23 | + private static Optional<Optional<Path>> jarPath = Optional.empty(); |
31 | 24 |
|
32 | 25 | public JarPathFinder() {
|
33 |
| - this( |
34 |
| - ProcessArguments::getProcessArguments, |
35 |
| - System::getProperty, |
36 |
| - Files::isRegularFile, |
37 |
| - JarPathFinder::readManifest); |
| 26 | + this(ProcessArguments::getProcessArguments, System::getProperty, Files::isRegularFile); |
38 | 27 | }
|
39 | 28 |
|
40 | 29 | // visible for tests
|
41 | 30 | JarPathFinder(
|
42 | 31 | Supplier<String[]> getProcessHandleArguments,
|
43 | 32 | Function<String, String> getSystemProperty,
|
44 |
| - Predicate<Path> fileExists, |
45 |
| - Function<Path, Optional<Manifest>> manifestReader) { |
| 33 | + Predicate<Path> fileExists) { |
46 | 34 | this.getProcessHandleArguments = getProcessHandleArguments;
|
47 | 35 | this.getSystemProperty = getSystemProperty;
|
48 | 36 | this.fileExists = fileExists;
|
49 |
| - this.manifestReader = manifestReader; |
50 | 37 | }
|
51 | 38 |
|
52 |
| - @Nullable |
53 |
| - Path getJarPath() { |
54 |
| - return ResourceDiscoveryCache.get( |
55 |
| - "jarPath", |
56 |
| - () -> { |
57 |
| - Path jarPath = getJarPathFromProcessHandle(); |
58 |
| - if (jarPath != null) { |
59 |
| - return jarPath; |
60 |
| - } |
61 |
| - return getJarPathFromSunCommandLine(); |
62 |
| - }); |
| 39 | + // visible for testing |
| 40 | + static void resetForTest() { |
| 41 | + jarPath = Optional.empty(); |
63 | 42 | }
|
64 | 43 |
|
65 |
| - Optional<Manifest> getManifestFromJarFile() { |
66 |
| - Path jarPath = getJarPath(); |
67 |
| - if (jarPath == null) { |
68 |
| - return Optional.empty(); |
| 44 | + Optional<Path> getJarPath() { |
| 45 | + if (jarPath.isPresent()) { |
| 46 | + return jarPath.get(); |
69 | 47 | }
|
70 |
| - return manifestReader.apply(jarPath); |
| 48 | + jarPath = Optional.of(Optional.ofNullable(readJarPath())); |
| 49 | + return jarPath.get(); |
71 | 50 | }
|
72 | 51 |
|
73 |
| - private static Optional<Manifest> readManifest(Path jarPath) { |
74 |
| - try (InputStream s = |
75 |
| - new URL(String.format("jar:%s!/META-INF/MANIFEST.MF", jarPath.toUri())).openStream()) { |
76 |
| - Manifest manifest = new Manifest(); |
77 |
| - manifest.read(s); |
78 |
| - return Optional.of(manifest); |
79 |
| - } catch (Exception e) { |
80 |
| - logger.log(WARNING, "Error reading manifest", e); |
81 |
| - return Optional.empty(); |
| 52 | + private Path readJarPath() { |
| 53 | + Path jarPath = getJarPathFromProcessHandle(); |
| 54 | + if (jarPath != null) { |
| 55 | + return jarPath; |
82 | 56 | }
|
| 57 | + return getJarPathFromSunCommandLine(); |
83 | 58 | }
|
84 | 59 |
|
85 | 60 | @Nullable
|
|
0 commit comments