1 16 package org.apache.axis.utils; 17 18 import java.io.InputStream ; 19 import java.io.File ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.security.AccessController ; 24 import java.security.PrivilegedAction ; 25 26 32 public final class ClassUtils { 33 34 private static ClassLoader defaultClassLoader 35 = ClassUtils.class.getClassLoader(); 36 37 38 private static java.util.Hashtable classloaders = new java.util.Hashtable (); 39 40 46 public static void setDefaultClassLoader(ClassLoader loader) { 47 if (loader != null) 48 defaultClassLoader = loader; 49 } 50 51 public static ClassLoader getDefaultClassLoader() { 52 return defaultClassLoader; 53 } 54 55 62 public static void setClassLoader(String className, ClassLoader loader) { 63 if (className != null && loader != null) 64 classloaders.put(className, loader); 65 } 66 67 74 public static ClassLoader getClassLoader(String className) { 75 if (className == null) { 76 return null; 77 } 78 return (ClassLoader ) classloaders.get(className); 79 } 80 81 86 public static void removeClassLoader(String className) { 87 classloaders.remove(className); 88 } 89 90 91 98 public static Class forName(String className) 99 throws ClassNotFoundException { 100 return loadClass(className); 101 } 102 103 113 public static Class forName( 114 String _className, boolean init, ClassLoader _loader) 115 throws ClassNotFoundException { 116 117 final String className = _className; 119 final ClassLoader loader = _loader; 120 try { 121 Object ret = 123 AccessController.doPrivileged( 124 new PrivilegedAction () { 125 public Object run() { 126 try { 127 return Class.forName(className, true, loader); 128 } catch (Throwable e) { 129 return e; 130 } 131 } 132 }); 133 if (ret instanceof Class ) { 135 return (Class ) ret; 136 } else if (ret instanceof ClassNotFoundException ) { 137 throw (ClassNotFoundException ) ret; 138 } else { 139 throw new ClassNotFoundException (_className); 140 } 141 } catch (ClassNotFoundException cnfe) { 142 return loadClass(className); 143 } 144 } 145 146 154 private static Class loadClass(String _className) 155 throws ClassNotFoundException { 156 final String className = _className; 158 159 Object ret = 161 AccessController.doPrivileged( 162 new PrivilegedAction () { 163 public Object run() { 164 try { 165 ClassLoader classLoader = getClassLoader(className); 168 return Class.forName(className, true, classLoader); 169 } catch (ClassNotFoundException cnfe) { 170 } 171 172 try { 173 ClassLoader classLoader = 175 Thread.currentThread().getContextClassLoader(); 176 return Class.forName(className, true, classLoader); 177 } catch (ClassNotFoundException cnfe2) { 178 try { 179 ClassLoader classLoader = 181 ClassUtils.class.getClassLoader(); 182 return Class.forName(className, true, classLoader); 183 } catch (ClassNotFoundException cnfe3) { 184 try { 186 return defaultClassLoader.loadClass( 187 className); 188 } catch (Throwable e) { 189 return e; 191 } 192 } 193 } 194 } 195 }); 196 197 if (ret instanceof Class ) { 199 return (Class ) ret; 200 } else if (ret instanceof ClassNotFoundException ) { 201 throw (ClassNotFoundException ) ret; 202 } else { 203 throw new ClassNotFoundException (_className); 204 } 205 } 206 207 220 public static InputStream getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst) { 221 InputStream myInputStream = null; 222 223 if (checkThreadContextFirst && 224 Thread.currentThread().getContextClassLoader() != null) { 225 myInputStream = 227 Thread.currentThread().getContextClassLoader() 228 .getResourceAsStream(resource); 229 } 230 if (myInputStream == null) { 231 myInputStream = getResourceAsStream(clazz, resource); 233 } 234 return myInputStream; 235 } 236 237 249 public static InputStream getResourceAsStream(Class clazz, String resource) { 250 InputStream myInputStream = null; 251 252 if(clazz.getClassLoader()!=null) { 253 myInputStream = clazz.getClassLoader().getResourceAsStream(resource); 255 } else { 256 myInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resource); 258 } 259 if (myInputStream == null && Thread.currentThread().getContextClassLoader() != null) { 260 myInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); 262 } 263 if (myInputStream == null) { 264 myInputStream = clazz.getResourceAsStream(resource); 266 } 267 return myInputStream; 268 } 269 270 284 public static ClassLoader createClassLoader(String classpath, 285 ClassLoader parent) 286 throws SecurityException 287 { 288 String [] names = StringUtils.split(classpath, System.getProperty("path.separator").charAt(0)); 289 290 URL [] urls = new URL [names.length]; 291 try { 292 for(int i = 0; i < urls.length; i++) 293 urls[i] = new File (names[i]).toURL(); 294 } 295 catch (MalformedURLException e) { 296 throw (IllegalArgumentException ) new IllegalArgumentException ( 299 "Unable to parse classpath: " + classpath); 300 } 301 302 return new URLClassLoader (urls, parent); 303 } 304 } 305 | Popular Tags |