package com.doc.jersey.loader;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarLoader {
	private static final Class<?>[] parameters = new Class[]{URL.class};

	/**
	 * Try to load a jar as comme file
	 * @param path
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static void load(String path) throws IOException {
		@SuppressWarnings("resource")
		JarFile file = new JarFile(path);
		Enumeration<JarEntry> e = file.entries();

		URL url = new URL("jar:file:" + path + "!/");
		
		URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
		Class<?> sysclass = URLClassLoader.class;

		try {
			Method method = sysclass.getDeclaredMethod("addURL", parameters);
			method.setAccessible(true);
			method.invoke(sysloader, new Object[]{url});
		} catch (Throwable t) {
			t.printStackTrace();
			throw new IOException("Error, could not add URL to system classloader");
		}
		
		while (e.hasMoreElements()) {
			JarEntry je = (JarEntry) e.nextElement();
			if(je.isDirectory() || !je.getName().endsWith(".class")){
				continue;
			}
			// -6 because of .class
			String className = je.getName().substring(0,je.getName().length()-6);
			className = className.replace('/', '.');
			try {
				sysloader.loadClass(className);
			}
			catch(ClassNotFoundException ex) {}
			catch(NoClassDefFoundError ez) {}
		}

	}
}

@@ -143,69 +143,8 @@
 er;%0A
-import java.util.Enumeration;%0Aimport java.util.jar.JarEntry;%0A
 impo
@@ -354,44 +354,40 @@
 %0A%09 *
- @throws ClassNotFoundException%0A%09 */
+/%0A%09@SuppressWarnings(%22resource%22)
 %0A%09pu
@@ -444,16 +444,18 @@
 ion %7B%0A%09%09
+/*
 @Suppres
@@ -511,19 +511,44 @@
 e(path);
+*/
 %0A%09%09
+new JarFile(path);%0A%09%09//
 Enumerat
@@ -1051,16 +1051,18 @@
 %09%7D%0A%09%09%0A%09%09
+/*
 while (e
@@ -1481,15 +1481,17 @@
 ) %7B%7D%0A%09%09%7D
+*/
 %0A%0A%09%7D%0A%7D%0A

Make the JAR loader faster.