1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.commons.clazz.common; |
17 | |
|
18 | |
import java.lang.reflect.Constructor; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.clazz.ClazzAccessException; |
23 | |
import org.apache.commons.clazz.ClazzLoader; |
24 | |
import org.apache.commons.clazz.ClazzLoaderFactory; |
25 | |
import org.apache.commons.clazz.ModelClazzLoader; |
26 | |
import org.apache.commons.collections.ReferenceMap; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public abstract class ClazzLoaderFactorySupport implements ClazzLoaderFactory { |
34 | |
|
35 | 0 | private List clazzLoaderClasses = new ArrayList(); |
36 | |
|
37 | |
private ClazzLoader defaultClazzLoader; |
38 | |
|
39 | 0 | private ReferenceMap clazzLoaders = |
40 | |
new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD); |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | protected ClazzLoaderFactorySupport() { |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public synchronized ClazzLoader getClazzLoader( |
52 | |
ClassLoader contextClassLoader) |
53 | |
{ |
54 | 0 | if (contextClassLoader == null) { |
55 | 0 | if (defaultClazzLoader == null) { |
56 | 0 | defaultClazzLoader = createClazzLoader(null); |
57 | |
} |
58 | 0 | return defaultClazzLoader; |
59 | |
} |
60 | |
else { |
61 | 0 | ClazzLoader loader = |
62 | |
(ClazzLoader) clazzLoaders.get(contextClassLoader); |
63 | 0 | if (loader == null) { |
64 | 0 | loader = createClazzLoader(contextClassLoader); |
65 | 0 | clazzLoaders.put(contextClassLoader, loader); |
66 | |
} |
67 | 0 | return loader; |
68 | |
} |
69 | |
} |
70 | |
|
71 | |
protected ClazzLoader createClazzLoader(ClassLoader contextClassLoader) { |
72 | 0 | ModelClazzLoader modelClazzLoader = |
73 | |
new ModelClazzLoader(getClazzModel()); |
74 | |
|
75 | 0 | for (int i = 0; i < clazzLoaderClasses.size(); i++) { |
76 | 0 | Class javaClass = (Class) clazzLoaderClasses.get(i); |
77 | 0 | ClazzLoader clazzLoader = |
78 | |
allocateClazzLoader( |
79 | |
modelClazzLoader, |
80 | |
javaClass, |
81 | |
contextClassLoader); |
82 | 0 | modelClazzLoader.addClazzLoader(clazzLoader); |
83 | |
} |
84 | |
|
85 | 0 | return modelClazzLoader; |
86 | |
} |
87 | |
|
88 | |
protected ClazzLoader allocateClazzLoader( |
89 | |
ModelClazzLoader modelClazzLoader, |
90 | |
Class javaClass, |
91 | |
ClassLoader contextClassLoader) |
92 | |
{ |
93 | |
try { |
94 | 0 | Constructor constructor = null; |
95 | |
try { |
96 | 0 | constructor = |
97 | |
javaClass.getConstructor( |
98 | |
new Class[] { |
99 | 0 | ModelClazzLoader.class, |
100 | |
ClassLoader.class }); |
101 | |
} |
102 | 0 | catch (NoSuchMethodException e) { |
103 | |
|
104 | 0 | } |
105 | 0 | if (constructor != null) { |
106 | 0 | return (ClazzLoader) constructor.newInstance( |
107 | |
new Object[] { modelClazzLoader, contextClassLoader }); |
108 | |
} |
109 | |
|
110 | |
try { |
111 | 0 | constructor = |
112 | |
javaClass.getConstructor( |
113 | |
new Class[] { ModelClazzLoader.class }); |
114 | |
} |
115 | 0 | catch (NoSuchMethodException e) { |
116 | |
|
117 | 0 | } |
118 | 0 | if (constructor != null) { |
119 | 0 | return (ClazzLoader) constructor.newInstance( |
120 | |
new Object[] { modelClazzLoader }); |
121 | |
} |
122 | 0 | throw new IllegalArgumentException("Missing constructor"); |
123 | |
} |
124 | 0 | catch (Exception e) { |
125 | 0 | throw new ClazzAccessException( |
126 | |
"Cannot allocate ClazzLoader " + javaClass.getName(), |
127 | |
e); |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
public void addClazzLoaderClass(Class clazzLoaderClass) { |
132 | 0 | clazzLoaderClasses.add(clazzLoaderClass); |
133 | 0 | } |
134 | |
} |