1 16 package org.apache.juddi.util; 17 18 import java.io.InputStream ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.net.URL ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 35 public class Loader 36 { 37 private static Log log = LogFactory.getLog(Loader.class); 39 40 44 public static InputStream getResourceAsStream(String resource) 45 { 46 ClassLoader classLoader = null; 47 InputStream stream = null; 48 49 try 52 { 53 classLoader = getContextClassLoader(); 54 if (classLoader != null) 55 { 56 log.debug("Trying to find ["+resource+"] using context classloader "+classLoader+"."); 57 stream = classLoader.getResourceAsStream(resource); 58 if(stream != null) { 59 return stream; 60 } 61 } 62 63 classLoader = Loader.class.getClassLoader(); 66 if (classLoader != null) 67 { 68 log.debug("Trying to find ["+resource+"] using "+classLoader+" class loader."); 69 stream = classLoader.getResourceAsStream(resource); 70 if(stream != null) { 71 return stream; 72 } 73 } 74 } 75 catch(Throwable th) { 76 log.warn("Exception thrown from Loader.getResource(\""+resource+"\").",th); 77 } 78 79 log.debug("Trying to find ["+resource+"] using ClassLoader.getSystemResource()."); 83 84 return ClassLoader.getSystemResourceAsStream(resource); 85 } 86 87 91 public static URL getResource(String resource) 92 { 93 ClassLoader classLoader = null; 94 URL url = null; 95 96 try 99 { 100 classLoader = getContextClassLoader(); 101 if (classLoader != null) 102 { 103 log.debug("Trying to find ["+resource+"] using context classloader "+classLoader+"."); 104 url = classLoader.getResource(resource); 105 if(url != null) { 106 return url; 107 } 108 } 109 110 classLoader = Loader.class.getClassLoader(); 113 if (classLoader != null) 114 { 115 log.debug("Trying to find ["+resource+"] using "+classLoader+" class loader."); 116 url = classLoader.getResource(resource); 117 if(url != null) { 118 return url; 119 } 120 } 121 } 122 catch(Throwable th) { 123 log.warn("Exception thrown from Loader.getResource(\""+resource+"\").",th); 124 } 125 126 log.debug("Trying to find ["+resource+"] using ClassLoader.getSystemResource()."); 130 131 return ClassLoader.getSystemResource(resource); 132 } 133 134 139 private static ClassLoader getContextClassLoader() 140 throws IllegalAccessException , InvocationTargetException 141 { 142 Method method = null; 143 try { 144 method = Thread .class.getMethod("getContextClassLoader",null); 145 } 146 catch (NoSuchMethodException e) { 147 return null; } 149 150 return (ClassLoader )method.invoke(Thread.currentThread(),null); 151 } 152 153 160 public static Class getClassForName(String name) 161 throws ClassNotFoundException , NoClassDefFoundError 162 { 163 Class clazz = null; 164 165 try 166 { 167 log.info("Using the Context ClassLoader"); 168 ClassLoader ccl = Thread.currentThread().getContextClassLoader(); 169 clazz = Class.forName(name, true, ccl); 170 } 171 catch (Exception e) 172 { 173 log.warn("Failed to load the class " + name + " with context class loader " + e); 174 } 175 176 if (null == clazz) 177 { 178 ClassLoader scl = ClassLoader.getSystemClassLoader(); 179 180 try 181 { 182 log.info("Using the System ClassLoader"); 183 clazz = Class.forName(name, true, scl); 184 } 185 catch (Exception e) 186 { 187 log.warn("Failed to load the class " + name + " with system class loader " + e); 188 } 189 } 190 191 return clazz; 192 } 193 } | Popular Tags |