|
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 |
486
| _workers.add(new CreatePropertyWorker("messages", _creatorLocation));
|
|
82 |
486
| _workers.add(new CreatePropertyWorker("specification", _creatorLocation));
|
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
486
| _workers.add(new AbstractPropertyWorker());
|
|
89 |
| } |
|
90 |
| |
|
91 |
513
| private ComponentConstructor createComponentConstructor(Class inputClass)
|
|
92 |
| { |
|
93 |
513
| if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
|
|
94 |
3
| throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
|
|
95 |
| |
|
96 |
510
| EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver,
|
|
97 |
| new ComponentSpecification(), inputClass, _classFactory, null); |
|
98 |
| |
|
99 |
510
| IComponentSpecification spec = new ComponentSpecification();
|
|
100 |
510
| spec.setLocation(_creatorLocation);
|
|
101 |
| |
|
102 |
510
| Iterator i = _workers.iterator();
|
|
103 |
510
| while (i.hasNext())
|
|
104 |
| { |
|
105 |
1530
| EnhancementWorker worker = (EnhancementWorker) i.next();
|
|
106 |
| |
|
107 |
1530
| worker.performEnhancement(op, spec);
|
|
108 |
| } |
|
109 |
| |
|
110 |
510
| return op.getConstructor();
|
|
111 |
| } |
|
112 |
| |
|
113 |
516
| private ComponentConstructor getComponentConstructor(Class inputClass)
|
|
114 |
| { |
|
115 |
516
| ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass);
|
|
116 |
| |
|
117 |
516
| if (result == null)
|
|
118 |
| { |
|
119 |
513
| result = createComponentConstructor(inputClass);
|
|
120 |
| |
|
121 |
510
| _constructors.put(inputClass, result);
|
|
122 |
| } |
|
123 |
| |
|
124 |
513
| return result;
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
516
| public Object newInstance(Class abstractClass)
|
|
132 |
| { |
|
133 |
516
| ComponentConstructor constructor = getComponentConstructor(abstractClass);
|
|
134 |
| |
|
135 |
513
| try
|
|
136 |
| { |
|
137 |
513
| 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 |
438
| public Object newInstance(Class abstractClass, Map properties)
|
|
152 |
| { |
|
153 |
438
| Object result = newInstance(abstractClass);
|
|
154 |
| |
|
155 |
438
| if (properties != null)
|
|
156 |
| { |
|
157 |
345
| Iterator i = properties.entrySet().iterator();
|
|
158 |
| |
|
159 |
345
| while (i.hasNext())
|
|
160 |
| { |
|
161 |
888
| Map.Entry e = (Map.Entry) i.next();
|
|
162 |
| |
|
163 |
888
| String propertyName = (String) e.getKey();
|
|
164 |
| |
|
165 |
888
| PropertyUtils.write(result, propertyName, e.getValue());
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| |
|
169 |
438
| return result;
|
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
438
| public Object newInstance(Class abstractClass, Object[] properties)
|
|
178 |
| { |
|
179 |
438
| Map propertyMap = Tapestry.convertArrayToMap(properties);
|
|
180 |
| |
|
181 |
438
| return newInstance(abstractClass, propertyMap);
|
|
182 |
| } |
|
183 |
| } |