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