1 9 package com.vladium.util; 10 11 23 public 24 abstract class ClassLoaderResolver 25 { 26 28 30 45 public static synchronized ClassLoader getClassLoader (final Class caller) 46 { 47 final ClassLoadContext ctx = new ClassLoadContext (caller); 48 49 return s_strategy.getClassLoader (ctx); 50 } 51 52 67 public static synchronized ClassLoader getClassLoader () 68 { 69 final Class caller = getCallerClass (1); final ClassLoadContext ctx = new ClassLoadContext (caller); 71 72 return s_strategy.getClassLoader (ctx); 73 } 74 75 82 public static Class getCallerClass (final int callerOffset) 83 { 84 if (CALLER_RESOLVER == null) return null; 86 return CALLER_RESOLVER.getClassContext () [CALL_CONTEXT_OFFSET + callerOffset]; 87 } 88 89 95 public static boolean isChild (final ClassLoader loader1, ClassLoader loader2) 96 { 97 if (loader1 == loader2) return true; 98 if (loader2 == null) return false; 99 if (loader1 == null) return true; 100 101 for ( ; loader2 != null; loader2 = loader2.getParent ()) 102 { 103 if (loader2 == loader1) return true; 104 } 105 106 return false; 107 } 108 109 112 public static synchronized IClassLoadStrategy getStrategy () 113 { 114 return s_strategy; 115 } 116 117 125 public static synchronized IClassLoadStrategy setStrategy (final IClassLoadStrategy strategy) 126 { 127 if (strategy == null) throw new IllegalArgumentException ("null input: strategy"); 128 129 final IClassLoadStrategy old = s_strategy; 130 s_strategy = strategy; 131 132 return old; 133 } 134 135 137 139 141 142 private static final class DefaultClassLoadStrategy implements IClassLoadStrategy 143 { 144 public ClassLoader getClassLoader (final ClassLoadContext ctx) 145 { 146 if (ctx == null) throw new IllegalArgumentException ("null input: ctx"); 147 148 final Class caller = ctx.getCallerClass (); 149 final ClassLoader contextLoader = Thread.currentThread ().getContextClassLoader (); 150 151 ClassLoader result; 152 153 if (caller == null) 154 result = contextLoader; 155 else 156 { 157 final ClassLoader callerLoader = caller.getClassLoader (); 158 159 162 165 if (isChild (callerLoader, contextLoader)) 166 result = contextLoader; 167 else 168 result = callerLoader; 169 } 170 171 final ClassLoader systemLoader = ClassLoader.getSystemClassLoader (); 172 173 if (isChild (result, systemLoader)) 175 result = systemLoader; 176 177 return result; 178 } 179 180 } 182 183 189 private static final class CallerResolver extends SecurityManager  190 { 191 protected Class [] getClassContext () 192 { 193 return super.getClassContext (); 194 } 195 196 } 198 199 private ClassLoaderResolver () {} 201 202 private static IClassLoadStrategy s_strategy; 204 private static final int CALL_CONTEXT_OFFSET = 2; private static final CallerResolver CALLER_RESOLVER; 208 static 209 { 210 CallerResolver temp = null; 211 try 212 { 213 216 temp = new CallerResolver (); 217 } 218 catch (Throwable t) 219 { 220 } 222 CALLER_RESOLVER = temp; 223 224 s_strategy = new DefaultClassLoadStrategy (); 225 } 226 227 }
| Popular Tags
|