Skip to content

Commit 0f32985

Browse files
committed
prefactor
1 parent 88022ce commit 0f32985

1 file changed

Lines changed: 49 additions & 63 deletions

File tree

  • instrumentation/runtime-telemetry/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/runtimetelemetry

instrumentation/runtime-telemetry/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/runtimetelemetry/JarDetails.java

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ class JarDetails {
5454
});
5555

5656
private final URL url;
57-
protected final JarFile jarFile;
5857
@Nullable private final Properties pom;
5958
@Nullable private final Manifest manifest;
6059
private final String sha1Checksum;
6160

62-
private JarDetails(URL url, JarFile jarFile) throws IOException {
61+
private JarDetails(
62+
URL url, @Nullable Properties pom, @Nullable Manifest manifest, String sha1Checksum) {
6363
this.url = url;
64-
this.jarFile = jarFile;
65-
this.pom = getPom();
66-
this.manifest = getManifest();
67-
this.sha1Checksum = computeDigest(SHA1.get());
64+
this.pom = pom;
65+
this.manifest = manifest;
66+
this.sha1Checksum = sha1Checksum;
6867
}
6968

7069
static JarDetails forUrl(URL url) throws IOException {
@@ -82,11 +81,17 @@ static JarDetails forUrl(URL url) throws IOException {
8281
if (jarEntry == null) {
8382
throw new IOException("Embedded jar entry not found: " + targetEntry);
8483
}
85-
return new EmbeddedJarDetails(url, jarFile, jarEntry);
84+
return new JarDetails(
85+
url,
86+
getPom(jarFile, jarEntry),
87+
getManifest(jarFile, jarEntry),
88+
computeDigest(jarFile, jarEntry, SHA1.get()));
8689
}
8790
}
8891
}
89-
return new JarDetails(url, new JarFile(url.getFile()));
92+
JarFile jarFile = new JarFile(url.getFile());
93+
return new JarDetails(
94+
url, getPom(jarFile), getManifest(jarFile), computeDigest(url, SHA1.get()));
9095
}
9196

9297
/**
@@ -180,8 +185,15 @@ String computeSha1() {
180185
return sha1Checksum;
181186
}
182187

183-
private String computeDigest(MessageDigest md) throws IOException {
184-
try (InputStream inputStream = getInputStream()) {
188+
private static String computeDigest(URL url, MessageDigest md) throws IOException {
189+
try (InputStream inputStream = url.openStream()) {
190+
return computeDigest(inputStream, md);
191+
}
192+
}
193+
194+
private static String computeDigest(JarFile jarFile, JarEntry jarEntry, MessageDigest md)
195+
throws IOException {
196+
try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
185197
return computeDigest(inputStream, md);
186198
}
187199
}
@@ -196,29 +208,28 @@ private static String computeDigest(InputStream inputStream, MessageDigest md)
196208
return String.format(Locale.ROOT, "%040x", new BigInteger(1, digest));
197209
}
198210

199-
/**
200-
* Returns An open input stream for the associated url. It is the caller's responsibility to close
201-
* the stream on completion.
202-
*/
203-
protected InputStream getInputStream() throws IOException {
204-
return url.openStream();
205-
}
206-
207211
@Nullable
208-
protected Manifest getManifest() {
212+
private static Manifest getManifest(JarFile jarFile) {
209213
try {
210214
return jarFile.getManifest();
211-
} catch (IOException e) {
215+
} catch (IOException ignored) {
212216
return null;
213217
}
214218
}
215219

220+
@Nullable
221+
private static Manifest getManifest(JarFile jarFile, JarEntry jarEntry) throws IOException {
222+
try (JarInputStream jarInputStream = new JarInputStream(jarFile.getInputStream(jarEntry))) {
223+
return jarInputStream.getManifest();
224+
}
225+
}
226+
216227
/**
217228
* Returns the values from pom.properties if this file is found. If multiple pom.properties files
218229
* are found or there is an error reading the file, return null.
219230
*/
220231
@Nullable
221-
protected Properties getPom() throws IOException {
232+
private static Properties getPom(JarFile jarFile) throws IOException {
222233
Properties pom = null;
223234
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
224235
JarEntry jarEntry = entries.nextElement();
@@ -238,51 +249,26 @@ protected Properties getPom() throws IOException {
238249
return pom;
239250
}
240251

241-
private static class EmbeddedJarDetails extends JarDetails {
242-
243-
private final JarEntry jarEntry;
244-
245-
private EmbeddedJarDetails(URL url, JarFile jarFile, JarEntry jarEntry) throws IOException {
246-
super(url, jarFile);
247-
this.jarEntry = jarEntry;
248-
}
249-
250-
@Override
251-
protected InputStream getInputStream() throws IOException {
252-
return jarFile.getInputStream(jarEntry);
253-
}
254-
255-
@Override
256-
protected Manifest getManifest() {
257-
try (JarInputStream jarFile = new JarInputStream(getInputStream())) {
258-
return jarFile.getManifest();
259-
} catch (IOException e) {
260-
return null;
261-
}
262-
}
263-
264-
@Override
265-
@Nullable
266-
protected Properties getPom() throws IOException {
267-
Properties pom = null;
268-
// Need to navigate inside the embedded jar which can't be done via random access.
269-
try (JarInputStream jarFile = new JarInputStream(getInputStream())) {
270-
for (JarEntry entry = jarFile.getNextJarEntry();
271-
entry != null;
272-
entry = jarFile.getNextJarEntry()) {
273-
if (entry.getName().startsWith("META-INF/maven")
274-
&& entry.getName().endsWith("pom.properties")) {
275-
if (pom != null) {
276-
// we've found multiple pom files. bail!
277-
return null;
278-
}
279-
Properties props = new Properties();
280-
props.load(jarFile);
281-
pom = props;
252+
@Nullable
253+
private static Properties getPom(JarFile jarFile, JarEntry jarEntry) throws IOException {
254+
Properties pom = null;
255+
// Need to navigate inside the embedded jar which can't be done via random access.
256+
try (JarInputStream jarInputStream = new JarInputStream(jarFile.getInputStream(jarEntry))) {
257+
for (JarEntry entry = jarInputStream.getNextJarEntry();
258+
entry != null;
259+
entry = jarInputStream.getNextJarEntry()) {
260+
if (entry.getName().startsWith("META-INF/maven")
261+
&& entry.getName().endsWith("pom.properties")) {
262+
if (pom != null) {
263+
// we've found multiple pom files. bail!
264+
return null;
282265
}
266+
Properties props = new Properties();
267+
props.load(jarInputStream);
268+
pom = props;
283269
}
284-
return pom;
285270
}
271+
return pom;
286272
}
287273
}
288274
}

0 commit comments

Comments
 (0)