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,22 @@ 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
77
76
* @throws IllegalStateException if jar hell was found
78
77
*/
79
- public static void checkJarHell () throws IOException , URISyntaxException {
78
+ public static void checkJarHell (Consumer < String > output ) throws IOException , URISyntaxException {
80
79
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
- }
80
+ output .accept ("java.class.path: " + System .getProperty ("java.class.path" ));
81
+ output .accept ("sun.boot.class.path: " + System .getProperty ("sun.boot.class.path" ));
82
+ if (loader instanceof URLClassLoader ) {
83
+ output .accept ("classloader urls: " + Arrays .toString (((URLClassLoader )loader ).getURLs ()));
88
84
}
89
- checkJarHell (parseClassPath ());
85
+ checkJarHell (parseClassPath (), output );
90
86
}
91
87
92
88
/**
@@ -155,28 +151,27 @@ static Set<URL> parseClassPath(String classPath) {
155
151
* @throws IllegalStateException if jar hell was found
156
152
*/
157
153
@ 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 );
154
+ public static void checkJarHell (Set <URL > urls , Consumer <String > output ) throws URISyntaxException , IOException {
160
155
// we don't try to be sneaky and use deprecated/internal/not portable stuff
161
156
// like sun.boot.class.path, and with jigsaw we don't yet have a way to get
162
157
// a "list" at all. So just exclude any elements underneath the java home
163
158
String javaHome = System .getProperty ("java.home" );
164
- logger . debug ("java.home: {}" , javaHome );
159
+ output . accept ("java.home: " + javaHome );
165
160
final Map <String ,Path > clazzes = new HashMap <>(32768 );
166
161
Set <Path > seenJars = new HashSet <>();
167
162
for (final URL url : urls ) {
168
163
final Path path = PathUtils .get (url .toURI ());
169
164
// exclude system resources
170
165
if (path .startsWith (javaHome )) {
171
- logger . debug ("excluding system resource: {}" , path );
166
+ output . accept ("excluding system resource: " + path );
172
167
continue ;
173
168
}
174
169
if (path .toString ().endsWith (".jar" )) {
175
170
if (!seenJars .add (path )) {
176
171
throw new IllegalStateException ("jar hell!" + System .lineSeparator () +
177
172
"duplicate jar on classpath: " + path );
178
173
}
179
- logger . debug ("examining jar: {}" , path );
174
+ output . accept ("examining jar: " + path );
180
175
try (JarFile file = new JarFile (path .toString ())) {
181
176
Manifest manifest = file .getManifest ();
182
177
if (manifest != null ) {
@@ -194,7 +189,7 @@ public static void checkJarHell(Set<URL> urls) throws URISyntaxException, IOExce
194
189
}
195
190
}
196
191
} else {
197
- logger . debug ("examining directory: {}" , path );
192
+ output . accept ("examining directory: " + path );
198
193
// case for tests: where we have class files in the classpath
199
194
final Path root = PathUtils .get (url .toURI ());
200
195
final String sep = root .getFileSystem ().getSeparator ();
0 commit comments