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