Skip to content

Commit 808750a

Browse files
authored
Add method to get JAR and Java agent paths as list (#24)
1 parent b2b2e71 commit 808750a

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

src/main/java/com/nordstrom/common/jar/JarUtils.java

+51-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.io.UnsupportedEncodingException;
88
import java.net.URLDecoder;
99
import java.nio.charset.Charset;
10+
import java.util.ArrayList;
1011
import java.util.HashSet;
12+
import java.util.List;
1113
import java.util.Set;
1214
import java.util.jar.JarInputStream;
1315
import java.util.jar.Manifest;
@@ -51,31 +53,68 @@ public class JarUtils {
5153
* @return assembled classpath string (see <b>NOTE</b>)
5254
*/
5355
public static String getClasspath(final String[] dependencyContexts) {
54-
Set<String> agentList = new HashSet<>();
56+
// get dependency context paths
57+
List<String> contextPaths = getContextPaths(false, dependencyContexts);
58+
// pop classpath from collection
59+
String classPath = contextPaths.remove(0);
60+
// if no agents were found
61+
if (contextPaths.isEmpty()) {
62+
// classpath only
63+
return classPath;
64+
} else {
65+
// classpath plus tab-delimited list of agent paths
66+
return classPath + "\n" + Joiner.on("\t").join(contextPaths);
67+
}
68+
}
69+
70+
/**
71+
* Assemble a list of context paths from the specified array of dependencies.
72+
* <p>
73+
* <b>NOTE</b>: The first item of the returned list contains a path-delimited string of JAR file paths suitable
74+
* for use with the Java {@code -cp} command line option. If any of the specified dependency contexts names the
75+
* {@code premain} class of a Java agent, subsequent items contain fully-formed {@code -javaagent} specifications.
76+
*
77+
* @param dependencyContexts array of dependency contexts
78+
* @return list of classpath/javaagent specifications (see <b>NOTE</b>)
79+
*/
80+
public static List<String> getContextPaths(final String[] dependencyContexts) {
81+
return getContextPaths(true, dependencyContexts);
82+
}
83+
84+
/**
85+
* Assemble a list of context paths from the specified array of dependencies.
86+
* <p>
87+
* <b>NOTE</b>: The first item of the returned list contains a path-delimited string of JAR file paths suitable
88+
* for use with the Java {@code -cp} command line option. If any of the specified dependency contexts names the
89+
* {@code premain} class of a Java agent, subsequent items contain Java agent JAR paths with optional prefix.
90+
*
91+
* @param prefixAgents {@code true} to request prefixing Java agent paths with {@code -javaagent:}
92+
* @param dependencyContexts array of dependency contexts
93+
* @return list of classpath/javaagent paths (see <b>NOTE</b>)
94+
*/
95+
private static List<String> getContextPaths(final boolean prefixAgents, final String[] dependencyContexts) {
5596
Set<String> pathList = new HashSet<>();
97+
Set<String> agentList = new HashSet<>();
98+
List<String> contextPaths = new ArrayList<>();
99+
final String prefix = prefixAgents ? "-javaagent:" : "";
56100
for (String contextClassName : dependencyContexts) {
57101
// get JAR path for this dependency context
58102
String jarPath = findJarPathFor(contextClassName);
59103
// if this context names the premain class of a Java agent
60104
if (contextClassName.equals(getJarPremainClass(jarPath))) {
61105
// collect agent path
62-
agentList.add(jarPath);
106+
agentList.add(prefix + jarPath);
63107
// otherwise
64108
} else {
65109
// collect class path
66110
pathList.add(jarPath);
67111
}
68112
}
69-
// assemble classpath string
70-
String classPath = Joiner.on(File.pathSeparator).join(pathList);
71-
// if no agents were found
72-
if (agentList.isEmpty()) {
73-
// classpath only
74-
return classPath;
75-
} else {
76-
// classpath plus tab-delimited list of agent paths
77-
return classPath + "\n" + Joiner.on("\t").join(agentList);
78-
}
113+
// add assembled classpath string
114+
contextPaths.add(Joiner.on(File.pathSeparator).join(pathList));
115+
// add Java agent paths
116+
contextPaths.addAll(agentList);
117+
return contextPaths;
79118
}
80119

81120
/**

0 commit comments

Comments
 (0)