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 | 0 | public class Creator |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | private final Map _constructors = new HashMap(); |
66 | |
|
67 | 0 | private final ClassFactory _classFactory = new ClassFactoryImpl(); |
68 | |
|
69 | 0 | private final ClassResolver _classResolver = new DefaultClassResolver(); |
70 | |
|
71 | 0 | private final List _workers = new ArrayList(); |
72 | |
|
73 | 0 | private final Resource _creatorResource = new ClasspathResource(_classResolver, |
74 | |
"/CreatorLocation"); |
75 | |
|
76 | 0 | private final Location _creatorLocation = new DescribedLocation(_creatorResource, |
77 | |
"Creator Location"); |
78 | |
|
79 | 0 | private final ComponentRenderWorker _renderWorker = new MockComponentRenderWorker(); |
80 | |
|
81 | 0 | private final InjectRenderWorker _injectRender = new InjectRenderWorker(); |
82 | |
|
83 | |
{ |
84 | |
|
85 | 0 | _injectRender.setRenderWorker(_renderWorker); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 0 | _workers.add(new CreatePropertyWorker("messages", _creatorLocation)); |
91 | 0 | _workers.add(new CreatePropertyWorker("specification", _creatorLocation)); |
92 | 0 | _workers.add(_injectRender); |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | _workers.add(new AbstractPropertyWorker()); |
99 | 0 | } |
100 | |
|
101 | |
private ComponentConstructor createComponentConstructor(Class inputClass) |
102 | |
{ |
103 | 0 | if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray()) |
104 | 0 | throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass)); |
105 | |
|
106 | 0 | EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver, |
107 | |
new ComponentSpecification(), inputClass, _classFactory, null); |
108 | |
|
109 | 0 | IComponentSpecification spec = new ComponentSpecification(); |
110 | 0 | spec.setLocation(_creatorLocation); |
111 | |
|
112 | 0 | Iterator i = _workers.iterator(); |
113 | 0 | while (i.hasNext()) |
114 | |
{ |
115 | 0 | EnhancementWorker worker = (EnhancementWorker) i.next(); |
116 | |
|
117 | 0 | worker.performEnhancement(op, spec); |
118 | 0 | } |
119 | |
|
120 | 0 | return op.getConstructor(); |
121 | |
} |
122 | |
|
123 | |
private ComponentConstructor getComponentConstructor(Class inputClass) |
124 | |
{ |
125 | 0 | ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass); |
126 | |
|
127 | 0 | if (result == null) |
128 | |
{ |
129 | 0 | result = createComponentConstructor(inputClass); |
130 | |
|
131 | 0 | _constructors.put(inputClass, result); |
132 | |
} |
133 | |
|
134 | 0 | return result; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public Object newInstance(Class abstractClass) |
142 | |
{ |
143 | 0 | ComponentConstructor constructor = getComponentConstructor(abstractClass); |
144 | |
|
145 | |
try |
146 | |
{ |
147 | 0 | 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 | 0 | Object result = newInstance(abstractClass); |
164 | |
|
165 | 0 | if (properties != null) |
166 | |
{ |
167 | 0 | Iterator i = properties.entrySet().iterator(); |
168 | |
|
169 | 0 | while (i.hasNext()) |
170 | |
{ |
171 | 0 | Map.Entry e = (Map.Entry) i.next(); |
172 | |
|
173 | 0 | String propertyName = (String) e.getKey(); |
174 | |
|
175 | 0 | PropertyUtils.write(result, propertyName, e.getValue()); |
176 | 0 | } |
177 | |
} |
178 | |
|
179 | 0 | return result; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public Object newInstance(Class abstractClass, Object[] properties) |
188 | |
{ |
189 | 0 | Map propertyMap = Tapestry.convertArrayToMap(properties); |
190 | |
|
191 | 0 | return newInstance(abstractClass, propertyMap); |
192 | |
} |
193 | |
} |