Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
HiveMindClassPool |
|
| 1.6;1.6 |
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 | 35 | private ClassFactoryClassLoader _loader = new ClassFactoryClassLoader(); |
35 | ||
36 | /** |
|
37 | * Used to identify which class loaders have already been integrated into the pool. |
|
38 | */ |
|
39 | 35 | private Set _loaders = new HashSet(); |
40 | ||
41 | public HiveMindClassPool() |
|
42 | { |
|
43 | 35 | super(null); |
44 | ||
45 | 35 | appendClassLoader(Thread.currentThread().getContextClassLoader()); |
46 | 35 | } |
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 | 3363 | if (loader == null || loader == _loader || _loaders.contains(loader)) |
54 | 3328 | return; |
55 | ||
56 | 35 | _loader.addDelegateLoader(loader); |
57 | ||
58 | 35 | ClassPath path = new LoaderClassPath(loader); |
59 | ||
60 | 35 | appendClassPath(path); |
61 | ||
62 | 35 | _loaders.add(loader); |
63 | 35 | } |
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 | 299 | Class clazz = ctClass.toClass(_loader, getClass().getProtectionDomain()); |
80 | ||
81 | 299 | if (detach) |
82 | 15 | ctClass.detach(); |
83 | ||
84 | 299 | return clazz; |
85 | } |
|
86 | ||
87 | public Set getLoaders() |
|
88 | { |
|
89 | 0 | return _loaders; |
90 | } |
|
91 | } |