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.Resource; |
20 |
|
import org.apache.hivemind.impl.LocationImpl; |
21 |
|
import org.apache.tapestry.INamespace; |
22 |
|
import org.apache.tapestry.IRequestCycle; |
23 |
|
import org.apache.tapestry.PageNotFoundException; |
24 |
|
import org.apache.tapestry.Tapestry; |
25 |
|
import org.apache.tapestry.services.ComponentPropertySource; |
26 |
|
import org.apache.tapestry.spec.ComponentSpecification; |
27 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
28 |
|
|
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 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
14 |
public class PageSpecificationResolverImpl extends |
66 |
|
AbstractSpecificationResolver implements PageSpecificationResolver |
67 |
|
{ |
68 |
|
|
69 |
|
private static final String WEB_INF = "/WEB-INF/"; |
70 |
|
|
71 |
|
|
72 |
|
private Log _log; |
73 |
|
|
74 |
|
|
75 |
|
private String _simpleName; |
76 |
|
|
77 |
|
|
78 |
|
private INamespace _applicationNamespace; |
79 |
|
|
80 |
|
|
81 |
|
private INamespace _frameworkNamespace; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private ComponentPropertySource _componentPropertySource; |
86 |
|
|
87 |
|
public void initializeService() |
88 |
|
{ |
89 |
14 |
_applicationNamespace = getSpecificationSource() |
90 |
|
.getApplicationNamespace(); |
91 |
14 |
_frameworkNamespace = getSpecificationSource().getFrameworkNamespace(); |
92 |
|
|
93 |
14 |
super.initializeService(); |
94 |
14 |
} |
95 |
|
|
96 |
|
protected void reset() |
97 |
|
{ |
98 |
15 |
_simpleName = null; |
99 |
|
|
100 |
15 |
super.reset(); |
101 |
15 |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
public void resolve(IRequestCycle cycle, String prefixedName) |
113 |
|
{ |
114 |
15 |
reset(); |
115 |
|
|
116 |
15 |
INamespace namespace = null; |
117 |
|
|
118 |
15 |
int colonx = prefixedName.indexOf(':'); |
119 |
|
|
120 |
15 |
if (colonx > 0) |
121 |
|
{ |
122 |
3 |
_simpleName = prefixedName.substring(colonx + 1); |
123 |
3 |
String namespaceId = prefixedName.substring(0, colonx); |
124 |
|
|
125 |
3 |
namespace = findNamespaceForId(_applicationNamespace, namespaceId); |
126 |
3 |
} |
127 |
|
else |
128 |
|
{ |
129 |
12 |
_simpleName = prefixedName; |
130 |
|
|
131 |
12 |
namespace = _applicationNamespace; |
132 |
|
} |
133 |
|
|
134 |
15 |
setNamespace(namespace); |
135 |
|
|
136 |
15 |
if (namespace.containsPage(_simpleName)) |
137 |
|
{ |
138 |
4 |
setSpecification(namespace.getPageSpecification(_simpleName)); |
139 |
4 |
return; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
11 |
searchForPage(cycle); |
145 |
|
|
146 |
9 |
if (getSpecification() == null) |
147 |
1 |
throw new PageNotFoundException(_simpleName, |
148 |
|
ResolverMessages.noSuchPage(_simpleName, namespace)); |
149 |
8 |
} |
150 |
|
|
151 |
|
public String getSimplePageName() |
152 |
|
{ |
153 |
12 |
return _simpleName; |
154 |
|
} |
155 |
|
|
156 |
|
private void searchForPage(IRequestCycle cycle) |
157 |
|
{ |
158 |
11 |
INamespace namespace = getNamespace(); |
159 |
|
|
160 |
11 |
if (_log.isDebugEnabled()) |
161 |
9 |
_log.debug(ResolverMessages.resolvingPage(_simpleName, namespace)); |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
11 |
if (_simpleName.regionMatches(true, 0, WEB_INF, 0, WEB_INF.length()) |
166 |
|
|| _simpleName.regionMatches(true, 0, WEB_INF, 1, WEB_INF.length() - 1)) |
167 |
2 |
throw new ApplicationRuntimeException(ResolverMessages.webInfNotAllowed(_simpleName)); |
168 |
|
|
169 |
9 |
String expectedName = _simpleName + ".page"; |
170 |
|
|
171 |
9 |
Resource namespaceLocation = namespace.getSpecificationLocation(); |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
9 |
if (found(namespaceLocation, expectedName)) |
178 |
1 |
return; |
179 |
|
|
180 |
8 |
if (namespace.isApplicationNamespace()) |
181 |
|
{ |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
6 |
if (found(getWebInfAppLocation(), expectedName)) |
186 |
2 |
return; |
187 |
|
|
188 |
4 |
if (found(getWebInfLocation(), expectedName)) |
189 |
1 |
return; |
190 |
|
|
191 |
3 |
if (found(getContextRoot(), expectedName)) |
192 |
1 |
return; |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
2 |
String templateName = _simpleName + "." + getTemplateExtension(); |
199 |
|
|
200 |
2 |
Resource templateResource = getContextRoot().getRelativeResource(templateName); |
201 |
|
|
202 |
2 |
if (_log.isDebugEnabled()) |
203 |
2 |
_log.debug(ResolverMessages.checkingResource(templateResource)); |
204 |
|
|
205 |
2 |
if (templateResource.getResourceURL() != null) |
206 |
|
{ |
207 |
1 |
setupImplicitPage(templateResource, namespaceLocation); |
208 |
1 |
return; |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
1 |
if (_frameworkNamespace.containsPage(_simpleName)) |
215 |
|
{ |
216 |
1 |
if (_log.isDebugEnabled()) |
217 |
1 |
_log.debug(ResolverMessages.foundFrameworkPage(_simpleName)); |
218 |
|
|
219 |
1 |
setNamespace(_frameworkNamespace); |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
1 |
setSpecification(_frameworkNamespace.getPageSpecification(_simpleName)); |
226 |
1 |
return; |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
2 |
IComponentSpecification specification = getDelegate().findPageSpecification(cycle, namespace, _simpleName); |
234 |
|
|
235 |
2 |
if (specification != null) |
236 |
|
{ |
237 |
1 |
setSpecification(specification); |
238 |
1 |
install(); |
239 |
|
} |
240 |
2 |
} |
241 |
|
|
242 |
|
private void setupImplicitPage(Resource resource, Resource namespaceLocation) |
243 |
|
{ |
244 |
1 |
if (_log.isDebugEnabled()) |
245 |
1 |
_log.debug(ResolverMessages.foundHTMLTemplate(resource)); |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
1 |
Resource pageResource = namespaceLocation.getRelativeResource(_simpleName + ".page"); |
255 |
|
|
256 |
1 |
IComponentSpecification specification = new ComponentSpecification(); |
257 |
1 |
specification.setPageSpecification(true); |
258 |
1 |
specification.setSpecificationLocation(pageResource); |
259 |
1 |
specification.setLocation(new LocationImpl(resource)); |
260 |
|
|
261 |
1 |
setSpecification(specification); |
262 |
|
|
263 |
1 |
install(); |
264 |
1 |
} |
265 |
|
|
266 |
|
private boolean found(Resource baseResource, String expectedName) |
267 |
|
{ |
268 |
22 |
Resource resource = baseResource.getRelativeResource(expectedName); |
269 |
|
|
270 |
22 |
if (_log.isDebugEnabled()) |
271 |
22 |
_log.debug(ResolverMessages.checkingResource(resource)); |
272 |
|
|
273 |
22 |
if (resource.getResourceURL() == null) |
274 |
17 |
return false; |
275 |
|
|
276 |
5 |
setSpecification(getSpecificationSource().getPageSpecification(resource)); |
277 |
|
|
278 |
5 |
install(); |
279 |
|
|
280 |
5 |
return true; |
281 |
|
} |
282 |
|
|
283 |
|
private void install() |
284 |
|
{ |
285 |
7 |
INamespace namespace = getNamespace(); |
286 |
7 |
IComponentSpecification specification = getSpecification(); |
287 |
|
|
288 |
7 |
if (_log.isDebugEnabled()) |
289 |
5 |
_log.debug(ResolverMessages.installingPage(_simpleName, namespace, specification)); |
290 |
|
|
291 |
7 |
namespace.installPageSpecification(_simpleName, specification); |
292 |
7 |
} |
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
private String getTemplateExtension() |
301 |
|
{ |
302 |
2 |
return _componentPropertySource.getNamespaceProperty(getNamespace(), Tapestry.TEMPLATE_EXTENSION_PROPERTY); |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
public void setLog(Log log) |
308 |
|
{ |
309 |
10 |
_log = log; |
310 |
10 |
} |
311 |
|
|
312 |
|
|
313 |
|
public void setComponentPropertySource(ComponentPropertySource componentPropertySource) |
314 |
|
{ |
315 |
2 |
_componentPropertySource = componentPropertySource; |
316 |
2 |
} |
317 |
|
} |