1 45 46 package org.openejb.loader; 47 48 import java.io.File ; 49 import java.net.URL ; 50 import java.net.URLClassLoader ; 51 import java.security.AccessController ; 52 import java.security.PrivilegedAction ; 53 54 57 public abstract class BasicURLClassPath implements ClassPath { 58 public static ClassLoader getContextClassLoader() { 59 return (ClassLoader ) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction () { 60 public Object run() { 61 return Thread.currentThread().getContextClassLoader(); 62 } 63 }); 64 } 65 66 private java.lang.reflect.Field ucpField; 67 68 protected void addJarToPath(final URL jar, final URLClassLoader loader) throws Exception { 69 this.getURLClassPath(loader).addURL(jar); 70 } 71 72 protected void addJarsToPath(final File dir, final URLClassLoader loader) throws Exception { 73 if (dir == null || !dir.exists()) return; 74 String [] jarNames = dir.list(new java.io.FilenameFilter () { 77 public boolean accept(File dir, String name) { 78 return (name.endsWith(".jar") || name.endsWith(".zip")); 80 } 81 }); 82 83 final URL [] jars = new URL [jarNames.length]; 85 for (int j = 0; j < jarNames.length; j++) { 86 jars[j] = new File (dir, jarNames[j]).toURL(); 87 } 88 89 sun.misc.URLClassPath path = getURLClassPath(loader); 90 for (int i = 0; i < jars.length; i++) { 91 path.addURL(jars[i]); 93 } 94 } 95 96 protected sun.misc.URLClassPath getURLClassPath(URLClassLoader loader) throws Exception { 97 return (sun.misc.URLClassPath) getUcpField().get(loader); 98 } 99 100 private java.lang.reflect.Field getUcpField() throws Exception { 101 if (ucpField == null) { 102 ucpField = (java.lang.reflect.Field ) AccessController.doPrivileged(new PrivilegedAction () { 104 public Object run() { 105 java.lang.reflect.Field ucp = null; 106 try { 107 ucp = URLClassLoader .class.getDeclaredField("ucp"); 108 ucp.setAccessible(true); 109 } catch (Exception e2) { 110 e2.printStackTrace(); 111 } 112 return ucp; 113 } 114 }); 115 } 116 117 return ucpField; 118 } 119 120 } 121 | Popular Tags |