1 16 17 package org.apache.log4j.helpers; 18 19 import java.net.URL ; 20 import java.lang.IllegalAccessException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.InvocationTargetException ; 23 24 27 32 33 public class Loader { 34 35 static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous."; 36 37 static private boolean java1 = true; 39 40 static private boolean ignoreTCL = false; 41 42 static { 43 String prop = OptionConverter.getSystemProperty("java.version", null); 44 45 if(prop != null) { 46 int i = prop.indexOf('.'); 47 if(i != -1) { 48 if(prop.charAt(i+1) != '1') 49 java1 = false; 50 } 51 } 52 String ignoreTCLProp = OptionConverter.getSystemProperty("log4j.ignoreTCL", null); 53 if(ignoreTCLProp != null) { 54 ignoreTCL = OptionConverter.toBoolean(ignoreTCLProp, true); 55 } 56 } 57 58 77 static public URL getResource(String resource) { 78 ClassLoader classLoader = null; 79 URL url = null; 80 81 try { 82 if(!java1) { 83 classLoader = getTCL(); 84 if(classLoader != null) { 85 LogLog.debug("Trying to find ["+resource+"] using context classloader " 86 +classLoader+"."); 87 url = classLoader.getResource(resource); 88 if(url != null) { 89 return url; 90 } 91 } 92 } 93 94 classLoader = Loader.class.getClassLoader(); 97 if(classLoader != null) { 98 LogLog.debug("Trying to find ["+resource+"] using "+classLoader 99 +" class loader."); 100 url = classLoader.getResource(resource); 101 if(url != null) { 102 return url; 103 } 104 } 105 } catch(Throwable t) { 106 LogLog.warn(TSTR, t); 107 } 108 109 LogLog.debug("Trying to find ["+resource+ 114 "] using ClassLoader.getSystemResource()."); 115 return ClassLoader.getSystemResource(resource); 116 } 117 118 121 public 122 static 123 boolean isJava1() { 124 return java1; 125 } 126 127 133 private static ClassLoader getTCL() throws IllegalAccessException , 134 InvocationTargetException { 135 136 Method method = null; 138 try { 139 method = Thread .class.getMethod("getContextClassLoader", null); 140 } catch (NoSuchMethodException e) { 141 return null; 143 } 144 145 return (ClassLoader ) method.invoke(Thread.currentThread(), null); 146 } 147 148 149 150 157 static public Class loadClass (String clazz) throws ClassNotFoundException { 158 if(java1 || ignoreTCL) { 161 return Class.forName(clazz); 162 } else { 163 try { 164 return getTCL().loadClass(clazz); 165 } catch(Throwable e) { 166 return Class.forName(clazz); 170 } 171 } 172 } 173 } 174 | Popular Tags |