1 4 package com.tc.aspectwerkz.util; 5 6 import java.net.URL ; 7 8 13 public final class ContextClassLoader { 14 15 23 public static Class forName(final ClassLoader loader, final String name) throws ClassNotFoundException { 24 Class klass = null; 25 if (loader != null) { 26 klass = Class.forName(name, false, loader); 27 } else { 28 klass = Class.forName(name, false, ClassLoader.getSystemClassLoader()); 29 } 30 return klass; 31 } 32 33 34 41 public static Class forName(final String name) throws ClassNotFoundException { 42 Class cls = null; 43 try { 44 cls = Class.forName(name, false, Thread.currentThread().getContextClassLoader()); 45 } catch (Exception e) { 46 cls = Class.forName(name); 47 } 48 return cls; 49 } 50 51 57 public static URL loadResource(final String name) { 58 try { 59 return Thread.currentThread().getContextClassLoader().getResource(name); 60 } catch (Exception e) { 61 return ClassLoader .class.getClassLoader().getResource(name); 62 } 63 } 64 65 86 91 public static ClassLoader getLoader() { 92 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 93 if (loader == null) { 94 loader = ClassLoader .class.getClassLoader(); 95 } 96 return loader; 97 } 98 99 105 public static ClassLoader getLoaderOrSystemLoader(ClassLoader loader) { 106 if (loader != null) { 107 return loader; 108 } else { 109 return ClassLoader.getSystemClassLoader(); 110 } 111 } 112 } | Popular Tags |