1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.test; |
16 |
|
|
17 |
|
import java.util.ArrayList; |
18 |
|
import java.util.HashMap; |
19 |
|
import java.util.Iterator; |
20 |
|
import java.util.List; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
24 |
|
import org.apache.hivemind.ClassResolver; |
25 |
|
import org.apache.hivemind.Location; |
26 |
|
import org.apache.hivemind.Resource; |
27 |
|
import org.apache.hivemind.impl.DefaultClassResolver; |
28 |
|
import org.apache.hivemind.service.ClassFactory; |
29 |
|
import org.apache.hivemind.service.impl.ClassFactoryImpl; |
30 |
|
import org.apache.hivemind.util.ClasspathResource; |
31 |
|
import org.apache.hivemind.util.PropertyUtils; |
32 |
|
import org.apache.tapestry.Tapestry; |
33 |
|
import org.apache.tapestry.enhance.AbstractPropertyWorker; |
34 |
|
import org.apache.tapestry.enhance.EnhancementOperationImpl; |
35 |
|
import org.apache.tapestry.enhance.EnhancementWorker; |
36 |
|
import org.apache.tapestry.enhance.InjectRenderWorker; |
37 |
|
import org.apache.tapestry.services.ComponentConstructor; |
38 |
|
import org.apache.tapestry.services.ComponentRenderWorker; |
39 |
|
import org.apache.tapestry.spec.ComponentSpecification; |
40 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
41 |
|
import org.apache.tapestry.util.DescribedLocation; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
81 |
public class Creator |
61 |
|
{ |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
81 |
private final Map _constructors = new HashMap(); |
66 |
|
|
67 |
81 |
private final ClassFactory _classFactory = new ClassFactoryImpl(); |
68 |
|
|
69 |
81 |
private final ClassResolver _classResolver = new DefaultClassResolver(); |
70 |
|
|
71 |
81 |
private final List _workers = new ArrayList(); |
72 |
|
|
73 |
81 |
private final Resource _creatorResource = new ClasspathResource(_classResolver, |
74 |
|
"/CreatorLocation"); |
75 |
|
|
76 |
81 |
private final Location _creatorLocation = new DescribedLocation(_creatorResource, |
77 |
|
"Creator Location"); |
78 |
|
|
79 |
81 |
private final ComponentRenderWorker _renderWorker = new MockComponentRenderWorker(); |
80 |
|
|
81 |
81 |
private final InjectRenderWorker _injectRender = new InjectRenderWorker(); |
82 |
|
|
83 |
|
{ |
84 |
|
|
85 |
81 |
_injectRender.setRenderWorker(_renderWorker); |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
81 |
_workers.add(new CreatePropertyWorker("messages", _creatorLocation)); |
91 |
81 |
_workers.add(new CreatePropertyWorker("specification", _creatorLocation)); |
92 |
81 |
_workers.add(_injectRender); |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
81 |
_workers.add(new AbstractPropertyWorker()); |
99 |
81 |
} |
100 |
|
|
101 |
|
private ComponentConstructor createComponentConstructor(Class inputClass) |
102 |
|
{ |
103 |
88 |
if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray()) |
104 |
1 |
throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass)); |
105 |
|
|
106 |
87 |
EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver, |
107 |
|
new ComponentSpecification(), inputClass, _classFactory, null); |
108 |
|
|
109 |
87 |
IComponentSpecification spec = new ComponentSpecification(); |
110 |
87 |
spec.setLocation(_creatorLocation); |
111 |
|
|
112 |
87 |
Iterator i = _workers.iterator(); |
113 |
435 |
while (i.hasNext()) |
114 |
|
{ |
115 |
348 |
EnhancementWorker worker = (EnhancementWorker) i.next(); |
116 |
|
|
117 |
348 |
worker.performEnhancement(op, spec); |
118 |
348 |
} |
119 |
|
|
120 |
87 |
return op.getConstructor(); |
121 |
|
} |
122 |
|
|
123 |
|
private ComponentConstructor getComponentConstructor(Class inputClass) |
124 |
|
{ |
125 |
219 |
ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass); |
126 |
|
|
127 |
219 |
if (result == null) |
128 |
|
{ |
129 |
88 |
result = createComponentConstructor(inputClass); |
130 |
|
|
131 |
87 |
_constructors.put(inputClass, result); |
132 |
|
} |
133 |
|
|
134 |
218 |
return result; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
public Object newInstance(Class abstractClass) |
142 |
|
{ |
143 |
219 |
ComponentConstructor constructor = getComponentConstructor(abstractClass); |
144 |
|
|
145 |
|
try |
146 |
|
{ |
147 |
218 |
return constructor.newInstance(); |
148 |
|
} |
149 |
0 |
catch (Exception ex) |
150 |
|
{ |
151 |
0 |
throw new ApplicationRuntimeException(ScriptMessages.unableToInstantiate( |
152 |
|
abstractClass, |
153 |
|
ex)); |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
public Object newInstance(Class abstractClass, Map properties) |
162 |
|
{ |
163 |
195 |
Object result = newInstance(abstractClass); |
164 |
|
|
165 |
195 |
if (properties != null) |
166 |
|
{ |
167 |
155 |
Iterator i = properties.entrySet().iterator(); |
168 |
|
|
169 |
641 |
while (i.hasNext()) |
170 |
|
{ |
171 |
486 |
Map.Entry e = (Map.Entry) i.next(); |
172 |
|
|
173 |
486 |
String propertyName = (String) e.getKey(); |
174 |
|
|
175 |
486 |
PropertyUtils.write(result, propertyName, e.getValue()); |
176 |
486 |
} |
177 |
|
} |
178 |
|
|
179 |
195 |
return result; |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
public Object newInstance(Class abstractClass, Object[] properties) |
188 |
|
{ |
189 |
195 |
Map propertyMap = Tapestry.convertArrayToMap(properties); |
190 |
|
|
191 |
195 |
return newInstance(abstractClass, propertyMap); |
192 |
|
} |
193 |
|
} |