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