|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
HiveMindClassPool.java | 100% | 100% | 100% | 100% |
|
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; | |
18 | import java.util.Set; | |
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(); | |
36 | ||
37 | /** | |
38 | * Used to identify which class loaders have already been integrated into the pool. | |
39 | */ | |
40 | private Set _loaders = new HashSet(); | |
41 | ||
42 | 145 | public HiveMindClassPool() |
43 | { | |
44 | 145 | super(null); |
45 | ||
46 | 145 | appendClassLoader(Thread.currentThread().getContextClassLoader()); |
47 | } | |
48 | ||
49 | /** | |
50 | * Convienience method for adding to the ClassPath for a particular class loader. | |
51 | */ | |
52 | 23208 | public synchronized void appendClassLoader(ClassLoader loader) |
53 | { | |
54 | 23208 | if (loader == null || loader == _loader || _loaders.contains(loader)) |
55 | 23063 | return; |
56 | ||
57 | 145 | _loader.addDelegateLoader(loader); |
58 | ||
59 | 145 | ClassPath path = new LoaderClassPath(loader); |
60 | ||
61 | 145 | appendClassPath(path); |
62 | ||
63 | 145 | _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 | 2104 | public Class toClass(CtClass ctClass) throws CannotCompileException |
74 | { | |
75 | 2104 | return ctClass.toClass(_loader); |
76 | } | |
77 | } |
|