1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.spec; |
16 |
|
|
17 |
|
import org.apache.commons.logging.Log; |
18 |
|
import org.apache.commons.logging.LogFactory; |
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.hivemind.ClassResolver; |
21 |
|
import org.apache.hivemind.util.PropertyUtils; |
22 |
|
import org.apache.tapestry.Tapestry; |
23 |
|
import org.apache.tapestry.coerce.ValueConverter; |
24 |
|
|
25 |
|
import java.util.Collections; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.Iterator; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class ExtensionSpecification extends LocatablePropertyHolder implements IExtensionSpecification |
39 |
|
{ |
40 |
2 |
private static final Log LOG = LogFactory.getLog(ExtensionSpecification.class); |
41 |
|
|
42 |
8 |
protected Map _configuration = new HashMap(); |
43 |
|
|
44 |
|
private String _className; |
45 |
|
|
46 |
|
private boolean _immediate; |
47 |
|
|
48 |
|
|
49 |
|
private ClassResolver _resolver; |
50 |
|
|
51 |
|
|
52 |
|
private ValueConverter _converter; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
public ExtensionSpecification(ClassResolver resolver, ValueConverter valueConverter) |
64 |
8 |
{ |
65 |
8 |
_resolver = resolver; |
66 |
8 |
_converter = valueConverter; |
67 |
8 |
} |
68 |
|
|
69 |
|
public String getClassName() |
70 |
|
{ |
71 |
0 |
return _className; |
72 |
|
} |
73 |
|
|
74 |
|
public void setClassName(String className) |
75 |
|
{ |
76 |
8 |
_className = className; |
77 |
8 |
} |
78 |
|
|
79 |
|
public void addConfiguration(String propertyName, String value) |
80 |
|
{ |
81 |
24 |
if (_configuration.containsKey(propertyName)) |
82 |
0 |
throw new IllegalArgumentException(Tapestry.format("ExtensionSpecification.duplicate-property", this, propertyName)); |
83 |
|
|
84 |
24 |
_configuration.put(propertyName, value); |
85 |
24 |
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
public Map getConfiguration() |
93 |
|
{ |
94 |
4 |
return Collections.unmodifiableMap(_configuration); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
public Object instantiateExtension() |
103 |
|
{ |
104 |
5 |
if (LOG.isDebugEnabled()) |
105 |
0 |
LOG.debug("Instantiating extension class " + _className + "."); |
106 |
|
|
107 |
5 |
Class extensionClass = null; |
108 |
5 |
Object result = null; |
109 |
|
|
110 |
|
try |
111 |
|
{ |
112 |
5 |
extensionClass = _resolver.findClass(_className); |
113 |
|
} |
114 |
0 |
catch (Exception ex) |
115 |
|
{ |
116 |
0 |
throw new ApplicationRuntimeException(Tapestry.format( |
117 |
|
"ExtensionSpecification.bad-class", |
118 |
|
_className), getLocation(), ex); |
119 |
5 |
} |
120 |
|
|
121 |
5 |
result = instantiateInstance(extensionClass, result); |
122 |
|
|
123 |
5 |
initializeProperties(result); |
124 |
|
|
125 |
5 |
return result; |
126 |
|
} |
127 |
|
|
128 |
|
private void initializeProperties(Object extension) |
129 |
|
{ |
130 |
|
|
131 |
5 |
Iterator i = _configuration.entrySet().iterator(); |
132 |
21 |
while (i.hasNext()) |
133 |
|
{ |
134 |
16 |
Map.Entry entry = (Map.Entry) i.next(); |
135 |
|
|
136 |
16 |
String propertyName = (String) entry.getKey(); |
137 |
16 |
String textValue = (String) entry.getValue(); |
138 |
|
|
139 |
|
try |
140 |
|
{ |
141 |
16 |
Class propertyType = PropertyUtils.getPropertyType(extension, propertyName); |
142 |
|
|
143 |
16 |
Object objectValue = _converter.coerceValue(textValue, propertyType); |
144 |
|
|
145 |
16 |
PropertyUtils.write(extension, propertyName, objectValue); |
146 |
|
} |
147 |
0 |
catch (Exception ex) |
148 |
|
{ |
149 |
0 |
throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); |
150 |
16 |
} |
151 |
16 |
} |
152 |
5 |
} |
153 |
|
|
154 |
|
private Object instantiateInstance(Class extensionClass, Object result) |
155 |
|
{ |
156 |
5 |
Object returnResult = result; |
157 |
|
try |
158 |
|
{ |
159 |
5 |
returnResult = extensionClass.newInstance(); |
160 |
|
} |
161 |
0 |
catch (Exception ex) |
162 |
|
{ |
163 |
0 |
throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); |
164 |
5 |
} |
165 |
|
|
166 |
5 |
return returnResult; |
167 |
|
} |
168 |
|
|
169 |
|
public String toString() |
170 |
|
{ |
171 |
0 |
StringBuffer buffer = new StringBuffer("ExtensionSpecification@"); |
172 |
0 |
buffer.append(Integer.toHexString(hashCode())); |
173 |
0 |
buffer.append('['); |
174 |
0 |
buffer.append(_className); |
175 |
|
|
176 |
0 |
if (_configuration != null) |
177 |
|
{ |
178 |
0 |
buffer.append(' '); |
179 |
0 |
buffer.append(_configuration); |
180 |
|
} |
181 |
|
|
182 |
0 |
buffer.append(']'); |
183 |
|
|
184 |
0 |
return buffer.toString(); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
public boolean isImmediate() |
194 |
|
{ |
195 |
8 |
return _immediate; |
196 |
|
} |
197 |
|
|
198 |
|
public void setImmediate(boolean immediate) |
199 |
|
{ |
200 |
8 |
_immediate = immediate; |
201 |
8 |
} |
202 |
|
|
203 |
|
} |