1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.resolver; |
16 |
|
|
17 |
|
import org.apache.commons.logging.Log; |
18 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
19 |
|
import org.apache.hivemind.ClassResolver; |
20 |
|
import org.apache.hivemind.Location; |
21 |
|
import org.apache.hivemind.Resource; |
22 |
|
import org.apache.hivemind.impl.LocationImpl; |
23 |
|
import org.apache.hivemind.util.ClasspathResource; |
24 |
|
import org.apache.tapestry.INamespace; |
25 |
|
import org.apache.tapestry.IRequestCycle; |
26 |
|
import org.apache.tapestry.services.ClassFinder; |
27 |
|
import org.apache.tapestry.spec.ComponentSpecification; |
28 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
12 |
public class ComponentSpecificationResolverImpl extends AbstractSpecificationResolver implements ComponentSpecificationResolver |
62 |
|
{ |
63 |
|
|
64 |
|
private Log _log; |
65 |
|
|
66 |
|
|
67 |
|
private String _type; |
68 |
|
|
69 |
|
private ClassFinder _classFinder; |
70 |
|
|
71 |
|
private ClassResolver _classResolver; |
72 |
|
|
73 |
|
protected void reset() |
74 |
|
{ |
75 |
11 |
_type = null; |
76 |
|
|
77 |
11 |
super.reset(); |
78 |
11 |
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
public void resolve(IRequestCycle cycle, INamespace containerNamespace, String type, Location location) |
97 |
|
{ |
98 |
11 |
int colonx = type.indexOf(':'); |
99 |
|
|
100 |
11 |
if (colonx > 0) |
101 |
|
{ |
102 |
2 |
String libraryId = type.substring(0, colonx); |
103 |
2 |
String simpleType = type.substring(colonx + 1); |
104 |
|
|
105 |
2 |
resolve(cycle, containerNamespace, libraryId, simpleType, location); |
106 |
1 |
} |
107 |
|
else |
108 |
9 |
resolve(cycle, containerNamespace, null, type, location); |
109 |
|
|
110 |
9 |
IComponentSpecification spec = getSpecification(); |
111 |
|
|
112 |
9 |
if (spec.isDeprecated()) |
113 |
1 |
_log.warn(ResolverMessages.componentIsDeprecated(type, location)); |
114 |
9 |
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
public void resolve(IRequestCycle cycle, INamespace containerNamespace, String libraryId, |
136 |
|
String type, Location location) |
137 |
|
{ |
138 |
11 |
reset(); |
139 |
11 |
_type = type; |
140 |
|
|
141 |
|
INamespace namespace; |
142 |
|
try |
143 |
|
{ |
144 |
11 |
namespace = findNamespaceForId(containerNamespace, libraryId); |
145 |
|
} |
146 |
1 |
catch (ApplicationRuntimeException e) |
147 |
|
{ |
148 |
1 |
throw new ApplicationRuntimeException(e.getMessage(), location, e); |
149 |
10 |
} |
150 |
|
|
151 |
10 |
setNamespace(namespace); |
152 |
|
|
153 |
10 |
if (namespace.containsComponentType(type)) |
154 |
|
{ |
155 |
3 |
setSpecification(namespace.getComponentSpecification(type)); |
156 |
3 |
return; |
157 |
|
} |
158 |
|
|
159 |
7 |
IComponentSpecification spec = searchForComponent(cycle); |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
7 |
if (spec == null) |
165 |
|
{ |
166 |
1 |
throw new ApplicationRuntimeException(ResolverMessages.noSuchComponentType( |
167 |
|
type, |
168 |
|
namespace), location, null); |
169 |
|
|
170 |
|
} |
171 |
|
|
172 |
6 |
setSpecification(spec); |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
6 |
install(); |
177 |
6 |
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
private IComponentSpecification searchForComponent(IRequestCycle cycle) |
182 |
|
{ |
183 |
7 |
IComponentSpecification result = null; |
184 |
7 |
INamespace namespace = getNamespace(); |
185 |
|
|
186 |
7 |
if (_log.isDebugEnabled()) |
187 |
7 |
_log.debug(ResolverMessages.resolvingComponent(_type, namespace)); |
188 |
|
|
189 |
7 |
String expectedName = _type + ".jwc"; |
190 |
7 |
Resource namespaceLocation = namespace.getSpecificationLocation(); |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
7 |
result = check(namespaceLocation.getRelativeResource(expectedName)); |
196 |
|
|
197 |
7 |
if (result != null) |
198 |
1 |
return result; |
199 |
|
|
200 |
6 |
if (namespace.isApplicationNamespace()) { |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
3 |
result = check(getWebInfAppLocation().getRelativeResource(expectedName)); |
205 |
|
|
206 |
3 |
if (result == null) |
207 |
2 |
result = check(getWebInfLocation().getRelativeResource(expectedName)); |
208 |
|
|
209 |
3 |
if (result == null) |
210 |
1 |
result = check((getContextRoot().getRelativeResource(expectedName))); |
211 |
|
|
212 |
3 |
if (result != null) |
213 |
3 |
return result; |
214 |
|
} |
215 |
|
|
216 |
3 |
result = getDelegate().findComponentSpecification(cycle, namespace, _type); |
217 |
3 |
if (result != null) |
218 |
1 |
return result; |
219 |
|
|
220 |
2 |
result = searchForComponentClass(namespace, _type); |
221 |
|
|
222 |
2 |
if (result != null) |
223 |
0 |
return result; |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
2 |
INamespace framework = getSpecificationSource().getFrameworkNamespace(); |
229 |
|
|
230 |
2 |
if (framework.containsComponentType(_type)) |
231 |
1 |
return framework.getComponentSpecification(_type); |
232 |
|
|
233 |
1 |
return null; |
234 |
|
} |
235 |
|
|
236 |
|
IComponentSpecification searchForComponentClass(INamespace namespace, String type) |
237 |
|
{ |
238 |
3 |
String packages = namespace.getPropertyValue("org.apache.tapestry.component-class-packages"); |
239 |
|
|
240 |
3 |
String className = type.replace('/', '.'); |
241 |
|
|
242 |
3 |
Class componentClass = _classFinder.findClass(packages, className); |
243 |
3 |
if (componentClass == null) |
244 |
2 |
return null; |
245 |
|
|
246 |
1 |
IComponentSpecification spec = new ComponentSpecification(); |
247 |
|
|
248 |
1 |
Resource namespaceResource = namespace.getSpecificationLocation(); |
249 |
1 |
Resource componentResource = namespaceResource.getRelativeResource(type + ".jwc"); |
250 |
|
|
251 |
|
|
252 |
|
|
253 |
1 |
if (componentResource.getResourceURL() == null) { |
254 |
|
|
255 |
0 |
componentResource = new ClasspathResource(_classResolver, componentClass.getName().replace('.', '/')); |
256 |
|
} |
257 |
|
|
258 |
1 |
Location location = new LocationImpl(componentResource); |
259 |
|
|
260 |
1 |
spec.setLocation(location); |
261 |
1 |
spec.setSpecificationLocation(componentResource); |
262 |
1 |
spec.setComponentClassName(componentClass.getName()); |
263 |
|
|
264 |
1 |
return spec; |
265 |
|
} |
266 |
|
|
267 |
|
private IComponentSpecification check(Resource resource) |
268 |
|
{ |
269 |
13 |
if (_log.isDebugEnabled()) |
270 |
13 |
_log.debug("Checking: " + resource); |
271 |
|
|
272 |
13 |
if (resource.getResourceURL() == null) |
273 |
9 |
return null; |
274 |
|
|
275 |
4 |
return getSpecificationSource().getComponentSpecification(resource); |
276 |
|
} |
277 |
|
|
278 |
|
private void install() |
279 |
|
{ |
280 |
6 |
INamespace namespace = getNamespace(); |
281 |
6 |
IComponentSpecification specification = getSpecification(); |
282 |
|
|
283 |
6 |
if (_log.isDebugEnabled()) |
284 |
5 |
_log.debug(ResolverMessages.installingComponent(_type, namespace, specification)); |
285 |
|
|
286 |
6 |
namespace.installComponentSpecification(_type, specification); |
287 |
6 |
} |
288 |
|
|
289 |
|
public String getType() |
290 |
|
{ |
291 |
0 |
return _type; |
292 |
|
} |
293 |
|
|
294 |
|
public void setLog(Log log) |
295 |
|
{ |
296 |
8 |
_log = log; |
297 |
8 |
} |
298 |
|
|
299 |
|
public void setClassFinder(ClassFinder classFinder) |
300 |
|
{ |
301 |
3 |
_classFinder = classFinder; |
302 |
3 |
} |
303 |
|
|
304 |
|
public void setClassResolver(ClassResolver classResolver) |
305 |
|
{ |
306 |
0 |
_classResolver = classResolver; |
307 |
0 |
} |
308 |
|
} |