19
19
20
20
package org .elasticsearch .bootstrap ;
21
21
22
- import org .apache .logging .log4j .Logger ;
23
22
import org .elasticsearch .common .SuppressForbidden ;
24
23
import org .elasticsearch .common .io .PathUtils ;
25
- import org .elasticsearch .common .logging .Loggers ;
26
24
27
25
import java .io .IOException ;
28
26
import java .net .MalformedURLException ;
43
41
import java .util .Locale ;
44
42
import java .util .Map ;
45
43
import java .util .Set ;
44
+ import java .util .function .Consumer ;
46
45
import java .util .jar .JarEntry ;
47
46
import java .util .jar .JarFile ;
48
47
import java .util .jar .Manifest ;
@@ -68,25 +67,23 @@ private JarHell() {}
68
67
@ SuppressForbidden (reason = "command line tool" )
69
68
public static void main (String args []) throws Exception {
70
69
System .out .println ("checking for jar hell..." );
71
- checkJarHell ();
70
+ checkJarHell (System . out :: println );
72
71
System .out .println ("no jar hell found" );
73
72
}
74
73
75
74
/**
76
75
* Checks the current classpath for duplicate classes
76
+ * @param output A {@link String} {@link Consumer} to which debug output will be sent
77
77
* @throws IllegalStateException if jar hell was found
78
78
*/
79
- public static void checkJarHell () throws IOException , URISyntaxException {
79
+ public static void checkJarHell (Consumer < String > output ) throws IOException , URISyntaxException {
80
80
ClassLoader loader = JarHell .class .getClassLoader ();
81
- Logger logger = Loggers .getLogger (JarHell .class );
82
- if (logger .isDebugEnabled ()) {
83
- logger .debug ("java.class.path: {}" , System .getProperty ("java.class.path" ));
84
- logger .debug ("sun.boot.class.path: {}" , System .getProperty ("sun.boot.class.path" ));
85
- if (loader instanceof URLClassLoader ) {
86
- logger .debug ("classloader urls: {}" , Arrays .toString (((URLClassLoader )loader ).getURLs ()));
87
- }
81
+ output .accept ("java.class.path: " + System .getProperty ("java.class.path" ));
82
+ output .accept ("sun.boot.class.path: " + System .getProperty ("sun.boot.class.path" ));
83
+ if (loader instanceof URLClassLoader ) {
84
+ output .accept ("classloader urls: " + Arrays .toString (((URLClassLoader )loader ).getURLs ()));
88
85
}
89
- checkJarHell (parseClassPath ());
86
+ checkJarHell (parseClassPath (), output );
90
87
}
91
88
92
89
/**
@@ -152,31 +149,32 @@ static Set<URL> parseClassPath(String classPath) {
152
149
153
150
/**
154
151
* Checks the set of URLs for duplicate classes
152
+ * @param urls A set of URLs from the classpath to be checked for conflicting jars
153
+ * @param output A {@link String} {@link Consumer} to which debug output will be sent
155
154
* @throws IllegalStateException if jar hell was found
156
155
*/
157
156
@ SuppressForbidden (reason = "needs JarFile for speed, just reading entries" )
158
- public static void checkJarHell (Set <URL > urls ) throws URISyntaxException , IOException {
159
- Logger logger = Loggers .getLogger (JarHell .class );
157
+ public static void checkJarHell (Set <URL > urls , Consumer <String > output ) throws URISyntaxException , IOException {
160
158
// we don't try to be sneaky and use deprecated/internal/not portable stuff
161
159
// like sun.boot.class.path, and with jigsaw we don't yet have a way to get
162
160
// a "list" at all. So just exclude any elements underneath the java home
163
161
String javaHome = System .getProperty ("java.home" );
164
- logger . debug ("java.home: {}" , javaHome );
162
+ output . accept ("java.home: " + javaHome );
165
163
final Map <String ,Path > clazzes = new HashMap <>(32768 );
166
164
Set <Path > seenJars = new HashSet <>();
167
165
for (final URL url : urls ) {
168
166
final Path path = PathUtils .get (url .toURI ());
169
167
// exclude system resources
170
168
if (path .startsWith (javaHome )) {
171
- logger . debug ("excluding system resource: {}" , path );
169
+ output . accept ("excluding system resource: " + path );
172
170
continue ;
173
171
}
174
172
if (path .toString ().endsWith (".jar" )) {
175
173
if (!seenJars .add (path )) {
176
174
throw new IllegalStateException ("jar hell!" + System .lineSeparator () +
177
175
"duplicate jar on classpath: " + path );
178
176
}
179
- logger . debug ("examining jar: {}" , path );
177
+ output . accept ("examining jar: " + path );
180
178
try (JarFile file = new JarFile (path .toString ())) {
181
179
Manifest manifest = file .getManifest ();
182
180
if (manifest != null ) {
@@ -194,7 +192,7 @@ public static void checkJarHell(Set<URL> urls) throws URISyntaxException, IOExce
194
192
}
195
193
}
196
194
} else {
197
- logger . debug ("examining directory: {}" , path );
195
+ output . accept ("examining directory: " + path );
198
196
// case for tests: where we have class files in the classpath
199
197
final Path root = PathUtils .get (url .toURI ());
200
198
final String sep = root .getFileSystem ().getSeparator ();
0 commit comments