|
7 | 7 | import java.io.UnsupportedEncodingException; |
8 | 8 | import java.net.URLDecoder; |
9 | 9 | import java.nio.charset.Charset; |
| 10 | +import java.util.ArrayList; |
10 | 11 | import java.util.HashSet; |
| 12 | +import java.util.List; |
11 | 13 | import java.util.Set; |
12 | 14 | import java.util.jar.JarInputStream; |
13 | 15 | import java.util.jar.Manifest; |
@@ -51,31 +53,68 @@ public class JarUtils { |
51 | 53 | * @return assembled classpath string (see <b>NOTE</b>) |
52 | 54 | */ |
53 | 55 | 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) { |
55 | 96 | Set<String> pathList = new HashSet<>(); |
| 97 | + Set<String> agentList = new HashSet<>(); |
| 98 | + List<String> contextPaths = new ArrayList<>(); |
| 99 | + final String prefix = prefixAgents ? "-javaagent:" : ""; |
56 | 100 | for (String contextClassName : dependencyContexts) { |
57 | 101 | // get JAR path for this dependency context |
58 | 102 | String jarPath = findJarPathFor(contextClassName); |
59 | 103 | // if this context names the premain class of a Java agent |
60 | 104 | if (contextClassName.equals(getJarPremainClass(jarPath))) { |
61 | 105 | // collect agent path |
62 | | - agentList.add(jarPath); |
| 106 | + agentList.add(prefix + jarPath); |
63 | 107 | // otherwise |
64 | 108 | } else { |
65 | 109 | // collect class path |
66 | 110 | pathList.add(jarPath); |
67 | 111 | } |
68 | 112 | } |
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; |
79 | 118 | } |
80 | 119 |
|
81 | 120 | /** |
|
0 commit comments