1 5 package com.tc.object.loaders; 6 7 import com.tc.aspectwerkz.transform.inlining.AsmHelper; 8 9 import java.lang.ref.WeakReference ; 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 public class StandardClassProvider implements ClassProvider { 14 15 private static final String BOOT = Namespace.getStandardBootstrapLoaderName(); 16 private static final String EXT = Namespace.getStandardExtensionsLoaderName(); 17 private static final String SYSTEM = Namespace.getStandardSystemLoaderName(); 18 19 private final ClassLoader systemLoader = ClassLoader.getSystemClassLoader(); 20 private final Map loaders = new HashMap (); 21 22 public StandardClassProvider() { 23 } 25 26 public ClassLoader getClassLoader(String desc) { 27 if (isStandardLoader(desc)) { return systemLoader; } 28 29 ClassLoader rv = lookupLoader(desc); 30 if (rv == null) { throw new AssertionError ("No registered loader for description: " + desc); } 31 return rv; 32 } 33 34 public Class getClassFor(final String className, String desc) throws ClassNotFoundException { 35 final ClassLoader loader; 36 37 if (isStandardLoader(desc)) { 38 loader = systemLoader; 39 } else { 40 loader = lookupLoader(desc); 41 if (loader == null) { throw new ClassNotFoundException ("No registered loader for description: " + desc 42 + ", trying to load " + className); } 43 } 44 45 try { 46 return Class.forName(className, false, loader); 47 } catch (ClassNotFoundException e) { 48 if (loader instanceof BytecodeProvider) { 49 BytecodeProvider provider = (BytecodeProvider) loader; 50 byte[] bytes = provider.__tc_getBytecodeForClass(className); 51 if (bytes != null && bytes.length != 0) { return AsmHelper.defineClass(loader, bytes, className); } 52 } 53 throw e; 54 } 55 } 56 57 public void registerNamedLoader(NamedClassLoader loader) { 58 final String name = getName(loader); 59 synchronized (loaders) { 60 loaders.put(name, new WeakReference (loader)); 61 } 62 } 63 64 private static String getName(NamedClassLoader loader) { 65 String name = loader.__tc_getClassLoaderName(); 66 if (name == null || name.length() == 0) { throw new AssertionError ("Invalid name [" + name + "] from loader " 67 + loader); } 68 return name; 69 } 70 71 public String getLoaderDescriptionFor(Class clazz) { 72 return getLoaderDescriptionFor(clazz.getClassLoader()); 73 } 74 75 public String getLoaderDescriptionFor(ClassLoader loader) { 76 if (loader == null) { return BOOT; } 77 if (loader instanceof NamedClassLoader) { return getName((NamedClassLoader) loader); } 78 throw handleMissingLoader(loader); 79 } 80 81 private RuntimeException handleMissingLoader(ClassLoader loader) { 82 if ("org.apache.jasper.servlet.JasperLoader".equals(loader.getClass().getName())) { 83 return new RuntimeException ("JSP instances (and inner classes there of) cannot be distributed, loader = " 85 + loader); 86 } 87 return new RuntimeException ("No loader description for " + loader); 88 } 89 90 private boolean isStandardLoader(String desc) { 91 if (BOOT.equals(desc) || EXT.equals(desc) || SYSTEM.equals(desc)) { return true; } 92 return false; 93 } 94 95 private ClassLoader lookupLoader(String desc) { 96 final ClassLoader rv; 97 synchronized (loaders) { 98 WeakReference ref = (WeakReference ) loaders.get(desc); 99 if (ref != null) { 100 rv = (ClassLoader ) ref.get(); 101 if (rv == null) { 102 loaders.remove(desc); 103 } 104 } else { 105 rv = null; 106 } 107 } 108 return rv; 109 } 110 } 111 | Popular Tags |