1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.services.impl; |
16 |
|
|
17 |
|
import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock; |
18 |
|
|
19 |
|
import java.util.Collections; |
20 |
|
import java.util.HashMap; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import org.apache.commons.logging.Log; |
24 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
25 |
|
import org.apache.hivemind.ClassResolver; |
26 |
|
import org.apache.hivemind.service.ClassFactory; |
27 |
|
import org.apache.hivemind.util.Defense; |
28 |
|
import org.apache.tapestry.enhance.EnhancedClassValidator; |
29 |
|
import org.apache.tapestry.enhance.EnhancementOperationImpl; |
30 |
|
import org.apache.tapestry.enhance.EnhancementWorker; |
31 |
|
import org.apache.tapestry.event.ReportStatusEvent; |
32 |
|
import org.apache.tapestry.event.ReportStatusListener; |
33 |
|
import org.apache.tapestry.event.ResetEventListener; |
34 |
|
import org.apache.tapestry.services.ComponentConstructor; |
35 |
|
import org.apache.tapestry.services.ComponentConstructorFactory; |
36 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
0 |
public class ComponentConstructorFactoryImpl implements ComponentConstructorFactory, |
46 |
|
ResetEventListener, ReportStatusListener |
47 |
|
{ |
48 |
0 |
private final ReentrantLock _lock = new ReentrantLock(); |
49 |
|
|
50 |
|
private String _serviceId; |
51 |
|
|
52 |
|
private Log _log; |
53 |
|
|
54 |
|
private ClassFactory _classFactory; |
55 |
|
|
56 |
|
private ClassResolver _classResolver; |
57 |
|
|
58 |
|
private EnhancedClassValidator _validator; |
59 |
|
|
60 |
|
private EnhancementWorker _chain; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
0 |
private Map _cachedConstructors = Collections.synchronizedMap(new HashMap()); |
68 |
|
|
69 |
|
public void resetEventDidOccur() |
70 |
|
{ |
71 |
0 |
_cachedConstructors.clear(); |
72 |
0 |
} |
73 |
|
|
74 |
|
public synchronized void reportStatus(ReportStatusEvent event) |
75 |
|
{ |
76 |
0 |
event.title(_serviceId); |
77 |
|
|
78 |
0 |
event.property("enhanced class count", _cachedConstructors.size()); |
79 |
0 |
event.collection("enhanced classes", _cachedConstructors.keySet()); |
80 |
0 |
} |
81 |
|
|
82 |
|
public ComponentConstructor getComponentConstructor(IComponentSpecification specification, |
83 |
|
String className) |
84 |
|
{ |
85 |
0 |
Defense.notNull(specification, "specification"); |
86 |
|
|
87 |
|
try { |
88 |
|
|
89 |
0 |
_lock.lockInterruptibly(); |
90 |
|
|
91 |
0 |
ComponentConstructor result = (ComponentConstructor) _cachedConstructors.get(specification); |
92 |
|
|
93 |
0 |
if (result == null) { |
94 |
|
|
95 |
0 |
Class baseClass = _classResolver.findClass(className); |
96 |
|
|
97 |
0 |
EnhancementOperationImpl eo = new EnhancementOperationImpl(_classResolver, |
98 |
|
specification, baseClass, _classFactory, _log); |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
0 |
_chain.performEnhancement(eo, specification); |
104 |
|
|
105 |
0 |
result = eo.getConstructor(); |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
0 |
_validator.validate(baseClass, result.getComponentClass(), specification); |
110 |
|
|
111 |
0 |
_cachedConstructors.put(specification, result); |
112 |
|
} |
113 |
|
|
114 |
0 |
return result; |
115 |
|
|
116 |
0 |
} catch (InterruptedException e) { |
117 |
|
|
118 |
0 |
throw new ApplicationRuntimeException(e); |
119 |
|
} finally { |
120 |
|
|
121 |
0 |
_lock.unlock(); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
public void setClassFactory(ClassFactory classFactory) |
126 |
|
{ |
127 |
0 |
_classFactory = classFactory; |
128 |
0 |
} |
129 |
|
|
130 |
|
public void setClassResolver(ClassResolver classResolver) |
131 |
|
{ |
132 |
0 |
_classResolver = classResolver; |
133 |
0 |
} |
134 |
|
|
135 |
|
public void setValidator(EnhancedClassValidator validator) |
136 |
|
{ |
137 |
0 |
_validator = validator; |
138 |
0 |
} |
139 |
|
|
140 |
|
public void setChain(EnhancementWorker chain) |
141 |
|
{ |
142 |
0 |
_chain = chain; |
143 |
0 |
} |
144 |
|
|
145 |
|
public void setLog(Log log) |
146 |
|
{ |
147 |
0 |
_log = log; |
148 |
0 |
} |
149 |
|
|
150 |
|
public void setServiceId(String serviceId) |
151 |
|
{ |
152 |
0 |
_serviceId = serviceId; |
153 |
0 |
} |
154 |
|
} |