KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > HiveMindClassPool


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service.impl;
16
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javassist.CannotCompileException;
21 import javassist.ClassPath;
22 import javassist.ClassPool;
23 import javassist.CtClass;
24 import javassist.LoaderClassPath;
25
26 /**
27  * Used to ensure that {@link javassist.ClassPool#appendClassPath(javassist.ClassPath)} is invoked
28  * with a synchronized lock. Additionally, wraps around a shared
29  * {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader}.
30  *
31  * @author Howard Lewis Ship
32  */

33 public class HiveMindClassPool extends ClassPool
34 {
35     private ClassFactoryClassLoader _loader = new ClassFactoryClassLoader(HiveMindClassPool.class.getClassLoader());
36     
37     /**
38      * Used to identify which class loaders have already been integrated into the pool.
39      */

40     private Set JavaDoc _loaders = new HashSet JavaDoc();
41
42     public HiveMindClassPool()
43     {
44         super(null);
45
46         appendClassLoader(Thread.currentThread().getContextClassLoader());
47     }
48
49     /**
50      * Convienience method for adding to the ClassPath for a particular class loader.
51      */

52     public synchronized void appendClassLoader(ClassLoader JavaDoc loader)
53     {
54         if (loader == null || loader == _loader || _loaders.contains(loader))
55             return;
56
57         _loader.addDelegateLoader(loader);
58
59         ClassPath path = new LoaderClassPath(loader);
60
61         appendClassPath(path);
62
63         _loaders.add(loader);
64     }
65
66     /**
67      * Invoked to convert an fabricated class into a real class. The new classes' class loader will
68      * be the delegating ClassFactoryClassLoader, which has visibility to all class loaders for all
69      * modules.
70      *
71      * @since 1.1
72      */

73     public Class JavaDoc toClass(CtClass ctClass) throws CannotCompileException
74     {
75         return ctClass.toClass(_loader, this.getClass().getProtectionDomain());
76     }
77 }
Popular Tags