Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HiveMindClassPool |
|
| 2.0;2 |
1 | // Copyright 2007 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 | package org.apache.tapestry.enhance; | |
15 | ||
16 | import java.util.HashSet; | |
17 | import java.util.Set; | |
18 | ||
19 | import javassist.CannotCompileException; | |
20 | import javassist.ClassPath; | |
21 | import javassist.ClassPool; | |
22 | import javassist.CtClass; | |
23 | import javassist.LoaderClassPath; | |
24 | ||
25 | /** | |
26 | * Used to ensure that {@link javassist.ClassPool#appendClassPath(javassist.ClassPath)} is invoked | |
27 | * with a synchronized lock. Additionally, wraps around a shared | |
28 | * {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader}. | |
29 | * | |
30 | * @author Howard Lewis Ship | |
31 | */ | |
32 | public class HiveMindClassPool extends ClassPool | |
33 | { | |
34 | 0 | private ClassFactoryClassLoader _loader = new ClassFactoryClassLoader(); |
35 | ||
36 | /** | |
37 | * Used to identify which class loaders have already been integrated into the pool. | |
38 | */ | |
39 | 0 | private Set _loaders = new HashSet(); |
40 | ||
41 | public HiveMindClassPool() | |
42 | { | |
43 | 0 | super(null); |
44 | ||
45 | 0 | appendClassLoader(Thread.currentThread().getContextClassLoader()); |
46 | 0 | } |
47 | ||
48 | /** | |
49 | * Convienience method for adding to the ClassPath for a particular class loader. | |
50 | */ | |
51 | public synchronized void appendClassLoader(ClassLoader loader) | |
52 | { | |
53 | 0 | if (loader == null || loader == _loader || _loaders.contains(loader)) |
54 | 0 | return; |
55 | ||
56 | 0 | _loader.addDelegateLoader(loader); |
57 | ||
58 | 0 | ClassPath path = new LoaderClassPath(loader); |
59 | ||
60 | 0 | appendClassPath(path); |
61 | ||
62 | 0 | _loaders.add(loader); |
63 | 0 | } |
64 | ||
65 | /** | |
66 | * Invoked to convert an fabricated class into a real class. The new classes' class loader will | |
67 | * be the delegating ClassFactoryClassLoader, which has visibility to all class loaders for all | |
68 | * modules. | |
69 | * | |
70 | * @since 1.1 | |
71 | */ | |
72 | public Class toClass(CtClass ctClass) throws CannotCompileException | |
73 | { | |
74 | 0 | return toClass(ctClass, false); |
75 | } | |
76 | ||
77 | public Class toClass(CtClass ctClass, boolean detach) throws CannotCompileException | |
78 | { | |
79 | 0 | Class clazz = ctClass.toClass(_loader, getClass().getProtectionDomain()); |
80 | ||
81 | 0 | if (detach) |
82 | 0 | ctClass.detach(); |
83 | ||
84 | 0 | return clazz; |
85 | } | |
86 | ||
87 | public Set getLoaders() | |
88 | { | |
89 | 0 | return _loaders; |
90 | } | |
91 | } |