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