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