1919
2020package org .elasticsearch .bootstrap ;
2121
22- import org .apache .logging .log4j .Logger ;
2322import org .elasticsearch .common .SuppressForbidden ;
2423import org .elasticsearch .common .io .PathUtils ;
25- import org .elasticsearch .common .logging .Loggers ;
2624
2725import java .io .IOException ;
2826import java .net .MalformedURLException ;
4341import java .util .Locale ;
4442import java .util .Map ;
4543import java .util .Set ;
44+ import java .util .function .Consumer ;
4645import java .util .jar .JarEntry ;
4746import java .util .jar .JarFile ;
4847import java .util .jar .Manifest ;
@@ -68,25 +67,23 @@ private JarHell() {}
6867 @ SuppressForbidden (reason = "command line tool" )
6968 public static void main (String args []) throws Exception {
7069 System .out .println ("checking for jar hell..." );
71- checkJarHell ();
70+ checkJarHell (System . out :: println );
7271 System .out .println ("no jar hell found" );
7372 }
7473
7574 /**
7675 * Checks the current classpath for duplicate classes
76+ * @param output A {@link String} {@link Consumer} to which debug output will be sent
7777 * @throws IllegalStateException if jar hell was found
7878 */
79- public static void checkJarHell () throws IOException , URISyntaxException {
79+ public static void checkJarHell (Consumer < String > output ) throws IOException , URISyntaxException {
8080 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 ()));
8885 }
89- checkJarHell (parseClassPath ());
86+ checkJarHell (parseClassPath (), output );
9087 }
9188
9289 /**
@@ -152,31 +149,32 @@ static Set<URL> parseClassPath(String classPath) {
152149
153150 /**
154151 * 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
155154 * @throws IllegalStateException if jar hell was found
156155 */
157156 @ 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 {
160158 // we don't try to be sneaky and use deprecated/internal/not portable stuff
161159 // like sun.boot.class.path, and with jigsaw we don't yet have a way to get
162160 // a "list" at all. So just exclude any elements underneath the java home
163161 String javaHome = System .getProperty ("java.home" );
164- logger . debug ("java.home: {}" , javaHome );
162+ output . accept ("java.home: " + javaHome );
165163 final Map <String ,Path > clazzes = new HashMap <>(32768 );
166164 Set <Path > seenJars = new HashSet <>();
167165 for (final URL url : urls ) {
168166 final Path path = PathUtils .get (url .toURI ());
169167 // exclude system resources
170168 if (path .startsWith (javaHome )) {
171- logger . debug ("excluding system resource: {}" , path );
169+ output . accept ("excluding system resource: " + path );
172170 continue ;
173171 }
174172 if (path .toString ().endsWith (".jar" )) {
175173 if (!seenJars .add (path )) {
176174 throw new IllegalStateException ("jar hell!" + System .lineSeparator () +
177175 "duplicate jar on classpath: " + path );
178176 }
179- logger . debug ("examining jar: {}" , path );
177+ output . accept ("examining jar: " + path );
180178 try (JarFile file = new JarFile (path .toString ())) {
181179 Manifest manifest = file .getManifest ();
182180 if (manifest != null ) {
@@ -194,7 +192,7 @@ public static void checkJarHell(Set<URL> urls) throws URISyntaxException, IOExce
194192 }
195193 }
196194 } else {
197- logger . debug ("examining directory: {}" , path );
195+ output . accept ("examining directory: " + path );
198196 // case for tests: where we have class files in the classpath
199197 final Path root = PathUtils .get (url .toURI ());
200198 final String sep = root .getFileSystem ().getSeparator ();
0 commit comments