1 16 17 package org.apache.commons.jelly.util; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 25 public class ClassLoaderUtils { 26 27 28 private static final Log log = LogFactory.getLog(ClassLoaderUtils.class); 29 30 40 public static ClassLoader getClassLoader(ClassLoader specifiedLoader, boolean useContextClassLoader, Class callingClass) { 41 if (specifiedLoader != null) { 42 return specifiedLoader; 43 } 44 if (useContextClassLoader) { 45 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 46 if (classLoader != null) { 47 return classLoader; 48 } 49 } 50 return getClassLoader(callingClass); 51 } 52 53 62 public static ClassLoader getClassLoader(ClassLoader specifiedLoader, Class callingClass) { 63 if (specifiedLoader != null) { 64 return specifiedLoader; 65 } 66 return getClassLoader(callingClass); 67 } 68 69 74 public static ClassLoader getClassLoader(Class clazz) { 75 ClassLoader callersLoader = clazz.getClassLoader(); 76 if (callersLoader == null) { 77 callersLoader = ClassLoader.getSystemClassLoader(); 78 } 79 return callersLoader; 80 } 81 82 86 public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { 87 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 88 if (loader == null) { 89 return getClassLoader(callingClass).loadClass(className); 90 } else { 91 return loader.loadClass(className); 92 } 93 } 94 95 103 public static Class loadClass(String className, ClassLoader specifiedLoader, boolean useContextLoader, Class callingClass) throws ClassNotFoundException { 104 Class clazz = null; 105 if (specifiedLoader != null) { 106 try { 107 clazz = specifiedLoader.loadClass(className); 108 } catch (ClassNotFoundException e) { 109 log.debug("couldn't find class in specified loader", e); 110 } 111 } 112 if (clazz == null && useContextLoader) { 113 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 114 if (contextLoader != null) { 115 try { 116 clazz = contextLoader.loadClass(className); 117 } catch (ClassNotFoundException e) { 118 log.debug("couldn't find class in specified loader", e); 119 } 120 } 121 } 122 if (clazz == null) { 123 ClassLoader loader = getClassLoader(callingClass); 124 clazz = loader.loadClass(className); 125 } 126 return clazz; 127 } 128 } 129 | Popular Tags |