Skip to content

Commit a0a2e69

Browse files
committed
Avoid adding inner classes of java agent jars to the classpath
The New Relic agent jar contains embedded sub jars, it gets confused if these are loaded both by the New Relic framework and later by EmJar. If recursion into jars that have manifests declaring Premain-Class: is desired, add these jars to the emjar.class.path property explicitly.
1 parent 041ac88 commit a0a2e69

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

emjar/src/main/java/com/comoyo/emjar/EmJarClassLoader.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.ConcurrentHashMap;
3434
import java.util.jar.JarEntry;
3535
import java.util.jar.JarFile;
36+
import java.util.jar.Manifest;
3637
import java.io.File;
3738
import java.io.IOException;
3839

@@ -56,6 +57,16 @@
5657
* must for obvious reasons be stored directly inside the bundle jar;
5758
* i.e not within an embedded jar.)
5859
*
60+
* <p/>
61+
* All jars given as part of the classpath will be recursively
62+
* inspected for embedded jars, and these will in turn be added to the
63+
* classpath. Jar files on the classpath having manifests declaring
64+
* <strong><code>Premain-Class:</code></strong> (i.e Java Agent jars)
65+
* will not be inspected by default, but can be added to the
66+
* EmJar-specific classpath contained in the
67+
* <strong><code>emjar.class.path</code></strong> property if this is
68+
* desired.
69+
5970
* <p/>
6071
* For a less manual approach that embeds all configuration in the
6172
* bundled jar, see {@link Boot}.
@@ -115,16 +126,19 @@ private static URL[] getClassPath(final Properties props, final Handler handler)
115126
DEBUG = "true".equalsIgnoreCase(props.getProperty(EMJAR_LOG_DEBUG_PROP, ""));
116127

117128
final ArrayList<URL> urls = new ArrayList<>();
118-
addClassPathUrls(props.getProperty(JAVA_CLASS_PATH_PROP), urls, handler);
119-
addClassPathUrls(props.getProperty(EMJAR_CLASS_PATH_PROP), urls, handler);
129+
addClassPathUrls(props.getProperty(JAVA_CLASS_PATH_PROP), urls, handler, false);
130+
addClassPathUrls(props.getProperty(EMJAR_CLASS_PATH_PROP), urls, handler, true);
120131
if (DEBUG) {
121132
System.err.println("EmJar: using classpath " + urls);
122133
}
123134
return urls.toArray(new URL[0]);
124135
}
125136

126137
private static void addClassPathUrls(
127-
final String classPath, final List<URL> urls, final Handler handler) {
138+
final String classPath,
139+
final List<URL> urls,
140+
final Handler handler,
141+
final boolean force) {
128142
if (classPath == null) {
129143
return;
130144
}
@@ -136,6 +150,15 @@ private static void addClassPathUrls(
136150
continue;
137151
}
138152
final JarFile jar = new JarFile(file);
153+
final Manifest mf = jar.getManifest();
154+
if (mf != null) {
155+
if ((mf.getMainAttributes().getValue("Premain-Class") != null) && !force) {
156+
if (DEBUG) {
157+
System.err.println("EmJar: skipping java agent jar: " + elem);
158+
}
159+
continue;
160+
}
161+
}
139162
final Enumeration<JarEntry> embedded = jar.entries();
140163
while (embedded.hasMoreElements()) {
141164
final JarEntry entry = embedded.nextElement();

0 commit comments

Comments
 (0)