KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > loaders > StandardClassProvider


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.loaders;
6
7 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
8
9 import java.lang.ref.WeakReference JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 public class StandardClassProvider implements ClassProvider {
14
15   private static final String JavaDoc BOOT = Namespace.getStandardBootstrapLoaderName();
16   private static final String JavaDoc EXT = Namespace.getStandardExtensionsLoaderName();
17   private static final String JavaDoc SYSTEM = Namespace.getStandardSystemLoaderName();
18
19   private final ClassLoader JavaDoc systemLoader = ClassLoader.getSystemClassLoader();
20   private final Map JavaDoc loaders = new HashMap JavaDoc();
21
22   public StandardClassProvider() {
23     //
24
}
25
26   public ClassLoader JavaDoc getClassLoader(String JavaDoc desc) {
27     if (isStandardLoader(desc)) { return systemLoader; }
28
29     ClassLoader JavaDoc rv = lookupLoader(desc);
30     if (rv == null) { throw new AssertionError JavaDoc("No registered loader for description: " + desc); }
31     return rv;
32   }
33
34   public Class JavaDoc getClassFor(final String JavaDoc className, String JavaDoc desc) throws ClassNotFoundException JavaDoc {
35     final ClassLoader JavaDoc loader;
36
37     if (isStandardLoader(desc)) {
38       loader = systemLoader;
39     } else {
40       loader = lookupLoader(desc);
41       if (loader == null) { throw new ClassNotFoundException JavaDoc("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 JavaDoc 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 JavaDoc name = getName(loader);
59     synchronized (loaders) {
60       loaders.put(name, new WeakReference JavaDoc(loader));
61     }
62   }
63
64   private static String JavaDoc getName(NamedClassLoader loader) {
65     String JavaDoc name = loader.__tc_getClassLoaderName();
66     if (name == null || name.length() == 0) { throw new AssertionError JavaDoc("Invalid name [" + name + "] from loader "
67                                                                        + loader); }
68     return name;
69   }
70
71   public String JavaDoc getLoaderDescriptionFor(Class JavaDoc clazz) {
72     return getLoaderDescriptionFor(clazz.getClassLoader());
73   }
74
75   public String JavaDoc getLoaderDescriptionFor(ClassLoader JavaDoc 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 JavaDoc handleMissingLoader(ClassLoader JavaDoc loader) {
82     if ("org.apache.jasper.servlet.JasperLoader".equals(loader.getClass().getName())) {
83       // try to guve a better error message if you're trying to share a JSP
84
return new RuntimeException JavaDoc("JSP instances (and inner classes there of) cannot be distributed, loader = "
85                                   + loader);
86     }
87     return new RuntimeException JavaDoc("No loader description for " + loader);
88   }
89
90   private boolean isStandardLoader(String JavaDoc desc) {
91     if (BOOT.equals(desc) || EXT.equals(desc) || SYSTEM.equals(desc)) { return true; }
92     return false;
93   }
94
95   private ClassLoader JavaDoc lookupLoader(String JavaDoc desc) {
96     final ClassLoader JavaDoc rv;
97     synchronized (loaders) {
98       WeakReference JavaDoc ref = (WeakReference JavaDoc) loaders.get(desc);
99       if (ref != null) {
100         rv = (ClassLoader JavaDoc) 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