1 45 46 package org.openejb.loader; 47 48 import sun.misc.URLClassPath; 49 50 import java.io.File ; 51 import java.lang.reflect.Method ; 52 import java.net.URL ; 53 import java.net.URLClassLoader ; 54 import java.security.AccessController ; 55 import java.security.PrivilegedAction ; 56 57 60 61 62 63 public class TomcatClassPath extends BasicURLClassPath { 64 65 68 private final ClassLoader classLoader; 69 70 73 private Method addRepositoryMethod; 74 private Method addURLMethod; 75 private Method getLoaderMethod; 76 77 78 public TomcatClassPath() { 79 this(getCommonLoader(getContextClassLoader()).getParent()); 80 } 81 82 public TomcatClassPath(ClassLoader classLoader){ 83 this.classLoader = classLoader; 84 try { 85 addRepositoryMethod = getAddRepositoryMethod(); 86 } catch (Exception tomcat4Exception) { 87 try { 89 addURLMethod = getAddURLMethod(); 90 } catch (Exception tomcat5Exception) { 91 throw new RuntimeException ("Failed accessing classloader for Tomcat 4 or 5", tomcat5Exception); 92 } 93 try { 94 getLoaderMethod = getLoaderMethod(); 95 } catch (Exception e) { 96 } 97 } 98 } 99 100 private static ClassLoader getCommonLoader(ClassLoader loader) { 101 if (loader.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader")) { 102 return loader; 103 } else { 104 return getCommonLoader(loader.getParent()); 105 } 106 } 107 108 public ClassLoader getClassLoader() { 109 return classLoader; 110 } 111 112 public void addJarsToPath(File dir) throws Exception { 113 String [] jarNames = dir.list(new java.io.FilenameFilter () { 114 public boolean accept(File dir, String name) { 115 return (name.endsWith(".jar") || name.endsWith(".zip")); 116 } 117 }); 118 119 if (jarNames == null) { 120 return; 121 } 122 123 for (int j = 0; j < jarNames.length; j++) { 124 this.addJarToPath(new File (dir, jarNames[j]).toURL()); 125 } 126 rebuild(); 127 } 128 129 public void addJarToPath(URL jar) throws Exception { 130 this._addJarToPath(jar); 131 rebuild(); 132 } 133 134 public void _addJarToPath(URL jar) throws Exception { 135 if (addRepositoryMethod != null){ 136 String path = jar.toExternalForm(); 137 addRepositoryMethod.invoke(getClassLoader(), new Object []{path}); 138 } else { 139 addURLMethod.invoke(getClassLoader(), new Object []{jar}); 140 int index = 0; 141 while (getLoader(index++) != null); 142 } 143 } 144 145 private Object getLoader(int i) { 146 if (getLoaderMethod == null){ 147 return null; 148 } 149 150 try { 151 sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader ) getClassLoader()); 152 Object object = getLoaderMethod.invoke(cp, new Object []{new Integer (i)}); 153 return object; 154 } catch (Exception e) { 155 e.printStackTrace(); 156 return null; 157 } 158 } 159 160 protected void rebuild() { 161 try { 162 sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader ) getClassLoader()); 163 URL [] urls = cp.getURLs(); 164 if (urls.length < 1) 168 return; 169 170 StringBuffer path = new StringBuffer (urls.length * 32); 171 172 File s = new File (urls[0].getFile()); 173 path.append(s.getPath()); 174 176 for (int i = 1; i < urls.length; i++) { 177 path.append(File.pathSeparator); 178 179 s = new File (urls[i].getFile()); 180 path.append(s.getPath()); 182 } 183 System.setProperty("java.class.path", path.toString()); 184 } catch (Exception e) { 185 } 186 187 } 188 189 197 private java.lang.reflect.Method getAddURLMethod() throws Exception { 198 return (java.lang.reflect.Method ) AccessController.doPrivileged(new PrivilegedAction () { 199 public Object run() { 200 java.lang.reflect.Method method = null; 201 try { 202 Class clazz = URLClassLoader .class; 203 method = clazz.getDeclaredMethod("addURL", new Class []{URL .class}); 204 method.setAccessible(true); 205 return method; 206 } catch (Exception e2) { 207 e2.printStackTrace(); 208 } 209 return method; 210 } 211 }); 212 } 213 214 private java.lang.reflect.Method getLoaderMethod() throws Exception { 215 return (java.lang.reflect.Method ) AccessController.doPrivileged(new PrivilegedAction () { 216 public Object run() { 217 java.lang.reflect.Method method = null; 218 try { 219 Class clazz = URLClassPath.class; 220 method = clazz.getDeclaredMethod("getLoader", new Class []{Integer.TYPE}); 221 method.setAccessible(true); 222 return method; 223 } catch (Exception e2) { 224 e2.printStackTrace(); 225 } 226 return method; 227 } 228 }); 229 } 230 231 private Method getAddRepositoryMethod() throws Exception { 232 return (Method ) AccessController.doPrivileged(new PrivilegedAction () { 233 public Object run() { 234 Method method = null; 235 try { 236 Class clazz = getClassLoader().getClass(); 237 method = clazz.getDeclaredMethod("addRepository", new Class []{String .class}); 238 method.setAccessible(true); 239 return method; 240 } catch (Exception e2) { 241 throw (IllegalStateException ) new IllegalStateException ("Unable to find or access the addRepository method in StandardClassLoader").initCause(e2); 242 } 243 } 244 }); 245 } 246 247 } 248 | Popular Tags |