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.services.ComponentConstructor; |
37 |
| import org.apache.tapestry.spec.ComponentSpecification; |
38 |
| import org.apache.tapestry.spec.IComponentSpecification; |
39 |
| import org.apache.tapestry.util.DescribedLocation; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| public class Creator |
59 |
| { |
60 |
| |
61 |
| |
62 |
| |
63 |
| private final Map _constructors = new HashMap(); |
64 |
| |
65 |
| private final ClassFactory _classFactory = new ClassFactoryImpl(); |
66 |
| |
67 |
| private final ClassResolver _classResolver = new DefaultClassResolver(); |
68 |
| |
69 |
| private final List _workers = new ArrayList(); |
70 |
| |
71 |
| private final Resource _creatorResource = new ClasspathResource(_classResolver, |
72 |
| "/CreatorLocation"); |
73 |
| |
74 |
| private final Location _creatorLocation = new DescribedLocation(_creatorResource, |
75 |
| "Creator Location"); |
76 |
| |
77 |
| { |
78 |
| |
79 |
| |
80 |
| |
81 |
147
| _workers.add(new CreatePropertyWorker("messages", _creatorLocation));
|
82 |
147
| _workers.add(new CreatePropertyWorker("specification", _creatorLocation));
|
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
147
| _workers.add(new AbstractPropertyWorker());
|
89 |
| } |
90 |
| |
91 |
156
| private ComponentConstructor createComponentConstructor(Class inputClass)
|
92 |
| { |
93 |
156
| if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
|
94 |
1
| throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
|
95 |
| |
96 |
155
| EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver,
|
97 |
| new ComponentSpecification(), inputClass, _classFactory, null); |
98 |
| |
99 |
155
| IComponentSpecification spec = new ComponentSpecification();
|
100 |
155
| spec.setLocation(_creatorLocation);
|
101 |
| |
102 |
155
| Iterator i = _workers.iterator();
|
103 |
155
| while (i.hasNext())
|
104 |
| { |
105 |
465
| EnhancementWorker worker = (EnhancementWorker) i.next();
|
106 |
| |
107 |
465
| worker.performEnhancement(op, spec);
|
108 |
| } |
109 |
| |
110 |
155
| return op.getConstructor();
|
111 |
| } |
112 |
| |
113 |
157
| private ComponentConstructor getComponentConstructor(Class inputClass)
|
114 |
| { |
115 |
157
| ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass);
|
116 |
| |
117 |
157
| if (result == null)
|
118 |
| { |
119 |
156
| result = createComponentConstructor(inputClass);
|
120 |
| |
121 |
155
| _constructors.put(inputClass, result);
|
122 |
| } |
123 |
| |
124 |
156
| return result;
|
125 |
| } |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
157
| public Object newInstance(Class abstractClass)
|
132 |
| { |
133 |
157
| ComponentConstructor constructor = getComponentConstructor(abstractClass);
|
134 |
| |
135 |
156
| try
|
136 |
| { |
137 |
156
| return constructor.newInstance();
|
138 |
| } |
139 |
| catch (Exception ex) |
140 |
| { |
141 |
0
| throw new ApplicationRuntimeException(ScriptMessages.unableToInstantiate(
|
142 |
| abstractClass, |
143 |
| ex)); |
144 |
| } |
145 |
| } |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
130
| public Object newInstance(Class abstractClass, Map properties)
|
152 |
| { |
153 |
130
| Object result = newInstance(abstractClass);
|
154 |
| |
155 |
130
| if (properties != null)
|
156 |
| { |
157 |
103
| Iterator i = properties.entrySet().iterator();
|
158 |
| |
159 |
103
| while (i.hasNext())
|
160 |
| { |
161 |
232
| Map.Entry e = (Map.Entry) i.next();
|
162 |
| |
163 |
232
| String propertyName = (String) e.getKey();
|
164 |
| |
165 |
232
| PropertyUtils.write(result, propertyName, e.getValue());
|
166 |
| } |
167 |
| } |
168 |
| |
169 |
130
| return result;
|
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
130
| public Object newInstance(Class abstractClass, Object[] properties)
|
178 |
| { |
179 |
130
| Map propertyMap = Tapestry.convertArrayToMap(properties);
|
180 |
| |
181 |
130
| return newInstance(abstractClass, propertyMap);
|
182 |
| } |
183 |
| } |