KickJava   Java API By Example, From Geeks To Geeks.

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


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.object.ClientObjectManager;
8 import com.tc.object.bytecode.Manager;
9 import com.tc.object.bytecode.ManagerImpl;
10 import com.tc.object.bytecode.hook.impl.ClassProcessorHelper;
11 import com.tc.object.bytecode.hook.impl.DSOContextImpl;
12 import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
13 import com.tc.object.config.DSOClientConfigHelper;
14 import com.tc.object.tx.ClientTransactionManager;
15
16 import java.net.URL JavaDoc;
17 import java.net.URLClassLoader JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 /**
21  * DSO Class loader for internal testing. The main purpose of this loader is to force test classes to be be defined
22  * specifically in this loader (and consequently within an isolated DSO context)
23  */

24 public class IsolationClassLoader extends URLClassLoader JavaDoc implements NamedClassLoader {
25   private static final ClassLoader JavaDoc SYSTEM_LOADER = ClassLoader.getSystemClassLoader();
26
27   private final Manager manager;
28   private final DSOClientConfigHelper config;
29   private final HashMap JavaDoc onLoadErrors;
30   private final StandardClassProvider classProvider;
31
32   public IsolationClassLoader(DSOClientConfigHelper config, PreparedComponentsFromL2Connection connectionComponents) {
33     this(config, true, null, null, connectionComponents);
34   }
35
36   public IsolationClassLoader(DSOClientConfigHelper config, ClientObjectManager objectManager,
37                               ClientTransactionManager txManager) {
38     this(config, false, objectManager, txManager, null);
39   }
40
41   private IsolationClassLoader(DSOClientConfigHelper config, boolean startClient, ClientObjectManager objectManager,
42                                ClientTransactionManager txManager,
43                                PreparedComponentsFromL2Connection connectionComponents) {
44     super(getSystemURLS(), null);
45     this.config = config;
46     this.classProvider = new StandardClassProvider();
47     this.manager = createManager(startClient, objectManager, txManager, config, connectionComponents);
48     this.onLoadErrors = new HashMap JavaDoc();
49   }
50
51   public void init() {
52     manager.init();
53     ClassProcessorHelper.setContext(this, DSOContextImpl.createContext(config, classProvider, manager));
54   }
55
56   private static URL JavaDoc[] getSystemURLS() {
57     return ((URLClassLoader JavaDoc) SYSTEM_LOADER).getURLs();
58   }
59
60   private Manager createManager(boolean startClient, ClientObjectManager objectManager,
61                                 ClientTransactionManager txManager, DSOClientConfigHelper theConfig,
62                                 PreparedComponentsFromL2Connection connectionComponents) {
63     classProvider.registerNamedLoader(this);
64     return new ManagerImpl(startClient, objectManager, txManager, theConfig, classProvider, connectionComponents, false);
65   }
66
67   public void stop() {
68     this.manager.stop();
69   }
70
71   public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
72     throwIfNeeded(name);
73
74     if (name.startsWith("com.tc.")) {
75       return SYSTEM_LOADER.loadClass(name);
76     } else {
77       return super.loadClass(name);
78     }
79   }
80
81   private void throwIfNeeded(String JavaDoc name) throws ClassNotFoundException JavaDoc {
82     // throw exception if one is registered
83
final String JavaDoc t = (String JavaDoc)onLoadErrors.get(name);
84     if (t != null) {
85       ClassNotFoundException JavaDoc rv = new ClassNotFoundException JavaDoc(t);
86       throw rv;
87     }
88   }
89
90   public String JavaDoc __tc_getClassLoaderName() {
91     return getClass().getName();
92   }
93
94   public void __tc_setClassLoaderName(String JavaDoc name) {
95     throw new AssertionError JavaDoc();
96   }
97
98   /**
99    * a ClassNotFoundException or NoClassDefFoundError with errorMessage will to be thrown referencing class className
100    */

101   public void throwOnLoad(String JavaDoc className, String JavaDoc errorMessage) {
102     onLoadErrors.put(className, errorMessage);
103   }
104
105   protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
106     throwIfNeeded(name);
107     return super.findClass(name);
108   }
109 }
110
Popular Tags