1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.services.impl; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.ClassResolver; |
19 |
|
import org.apache.hivemind.ErrorLog; |
20 |
|
import org.apache.hivemind.Resource; |
21 |
|
import org.apache.hivemind.service.ThreadLocale; |
22 |
|
import org.apache.hivemind.util.Defense; |
23 |
|
import org.apache.tapestry.IRequestCycle; |
24 |
|
import org.apache.tapestry.asset.AssetFactory; |
25 |
|
import org.apache.tapestry.coerce.ValueConverter; |
26 |
|
import org.apache.tapestry.describe.HTMLDescriber; |
27 |
|
import org.apache.tapestry.engine.IPageSource; |
28 |
|
import org.apache.tapestry.engine.IPropertySource; |
29 |
|
import org.apache.tapestry.engine.IScriptSource; |
30 |
|
import org.apache.tapestry.engine.ISpecificationSource; |
31 |
|
import org.apache.tapestry.engine.state.ApplicationStateManager; |
32 |
|
import org.apache.tapestry.error.ExceptionPresenter; |
33 |
|
import org.apache.tapestry.error.RequestExceptionReporter; |
34 |
|
import org.apache.tapestry.error.StaleLinkExceptionPresenter; |
35 |
|
import org.apache.tapestry.error.StaleSessionExceptionPresenter; |
36 |
|
import org.apache.tapestry.listener.ListenerInvoker; |
37 |
|
import org.apache.tapestry.listener.ListenerMapSource; |
38 |
|
import org.apache.tapestry.markup.MarkupWriterSource; |
39 |
|
import org.apache.tapestry.services.*; |
40 |
|
import org.apache.tapestry.spec.IApplicationSpecification; |
41 |
|
import org.apache.tapestry.web.WebContext; |
42 |
|
import org.apache.tapestry.web.WebContextResource; |
43 |
|
import org.apache.tapestry.web.WebRequest; |
44 |
|
import org.apache.tapestry.web.WebResponse; |
45 |
|
|
46 |
|
import java.util.*; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
7 |
public class InfrastructureImpl implements Infrastructure |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private List _normalContributions; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private List _overrideContributions; |
65 |
|
|
66 |
7 |
private Map _properties = new HashMap(); |
67 |
|
|
68 |
|
private boolean _initialized; |
69 |
|
|
70 |
|
private String _mode; |
71 |
|
|
72 |
|
private ErrorLog _errorLog; |
73 |
|
|
74 |
|
private ClassResolver _classResolver; |
75 |
|
|
76 |
|
private ThreadLocale _threadLocale; |
77 |
|
|
78 |
|
private String _outputEncoding; |
79 |
|
|
80 |
|
private RequestLocaleManager _localeManager; |
81 |
|
|
82 |
|
public void setLocale(Locale locale) |
83 |
|
{ |
84 |
0 |
_threadLocale.setLocale(locale); |
85 |
|
|
86 |
0 |
_localeManager.persistLocale(); |
87 |
0 |
} |
88 |
|
|
89 |
|
public String getApplicationId() |
90 |
|
{ |
91 |
0 |
return (String) getProperty("applicationId"); |
92 |
|
} |
93 |
|
|
94 |
|
public IPropertySource getApplicationPropertySource() |
95 |
|
{ |
96 |
0 |
return (IPropertySource) getProperty("applicationPropertySource"); |
97 |
|
} |
98 |
|
|
99 |
|
public IApplicationSpecification getApplicationSpecification() |
100 |
|
{ |
101 |
0 |
return (IApplicationSpecification) getProperty("applicationSpecification"); |
102 |
|
} |
103 |
|
|
104 |
|
public ApplicationStateManager getApplicationStateManager() |
105 |
|
{ |
106 |
0 |
return (ApplicationStateManager) getProperty("applicationStateManager"); |
107 |
|
} |
108 |
|
|
109 |
|
public ClassResolver getClassResolver() |
110 |
|
{ |
111 |
0 |
return _classResolver; |
112 |
|
} |
113 |
|
|
114 |
|
public ComponentMessagesSource getComponentMessagesSource() |
115 |
|
{ |
116 |
0 |
return (ComponentMessagesSource) getProperty("componentMessagesSource"); |
117 |
|
} |
118 |
|
|
119 |
|
public ComponentPropertySource getComponentPropertySource() |
120 |
|
{ |
121 |
0 |
return (ComponentPropertySource) getProperty("componentPropertySource"); |
122 |
|
} |
123 |
|
|
124 |
|
public String getContextPath() |
125 |
|
{ |
126 |
0 |
return getRequest().getContextPath(); |
127 |
|
} |
128 |
|
|
129 |
|
public Resource getContextRoot() |
130 |
|
{ |
131 |
0 |
WebContext context = (WebContext) getProperty("context"); |
132 |
|
|
133 |
0 |
return new WebContextResource(context, "/"); |
134 |
|
} |
135 |
|
|
136 |
|
public DataSqueezer getDataSqueezer() |
137 |
|
{ |
138 |
0 |
return (DataSqueezer) getProperty("dataSqueezer"); |
139 |
|
} |
140 |
|
|
141 |
|
public IPropertySource getGlobalPropertySource() |
142 |
|
{ |
143 |
0 |
return (IPropertySource) getProperty("globalPropertySource"); |
144 |
|
} |
145 |
|
|
146 |
|
public LinkFactory getLinkFactory() |
147 |
|
{ |
148 |
0 |
return (LinkFactory) getProperty("linkFactory"); |
149 |
|
} |
150 |
|
|
151 |
|
public ObjectPool getObjectPool() |
152 |
|
{ |
153 |
0 |
return (ObjectPool) getProperty("objectPool"); |
154 |
|
} |
155 |
|
|
156 |
|
public IPageSource getPageSource() |
157 |
|
{ |
158 |
0 |
return (IPageSource) getProperty("pageSource"); |
159 |
|
} |
160 |
|
|
161 |
|
public WebRequest getRequest() |
162 |
|
{ |
163 |
0 |
return (WebRequest) getProperty("request"); |
164 |
|
} |
165 |
|
|
166 |
|
public RequestCycleFactory getRequestCycleFactory() |
167 |
|
{ |
168 |
0 |
return (RequestCycleFactory) getProperty("requestCycleFactory"); |
169 |
|
} |
170 |
|
|
171 |
|
public RequestExceptionReporter getRequestExceptionReporter() |
172 |
|
{ |
173 |
0 |
return (RequestExceptionReporter) getProperty("requestExceptionReporter"); |
174 |
|
} |
175 |
|
|
176 |
|
public ResetEventHub getResetEventHub() |
177 |
|
{ |
178 |
0 |
return (ResetEventHub) getProperty("resetEventHub"); |
179 |
|
} |
180 |
|
|
181 |
|
public WebResponse getResponse() |
182 |
|
{ |
183 |
0 |
return (WebResponse) getProperty("response"); |
184 |
|
} |
185 |
|
|
186 |
|
public ResponseRenderer getResponseRenderer() |
187 |
|
{ |
188 |
0 |
return (ResponseRenderer) getProperty("responseRenderer"); |
189 |
|
} |
190 |
|
|
191 |
|
public IScriptSource getScriptSource() |
192 |
|
{ |
193 |
0 |
return (IScriptSource) getProperty("scriptSource"); |
194 |
|
} |
195 |
|
|
196 |
|
public ServiceMap getServiceMap() |
197 |
|
{ |
198 |
0 |
return (ServiceMap) getProperty("serviceMap"); |
199 |
|
} |
200 |
|
|
201 |
|
public ISpecificationSource getSpecificationSource() |
202 |
|
{ |
203 |
0 |
return (ISpecificationSource) getProperty("specificationSource"); |
204 |
|
} |
205 |
|
|
206 |
|
public TemplateSource getTemplateSource() |
207 |
|
{ |
208 |
0 |
return (TemplateSource) getProperty("templateSource"); |
209 |
|
} |
210 |
|
|
211 |
|
public String getOutputEncoding() |
212 |
|
{ |
213 |
0 |
if (_outputEncoding != null) |
214 |
0 |
return _outputEncoding; |
215 |
|
|
216 |
0 |
_outputEncoding = getApplicationPropertySource().getPropertyValue("org.apache.tapestry.output-encoding"); |
217 |
|
|
218 |
0 |
return _outputEncoding; |
219 |
|
} |
220 |
|
|
221 |
|
public MarkupWriterSource getMarkupWriterSource() |
222 |
|
{ |
223 |
0 |
return (MarkupWriterSource) getProperty("markupWriterSource"); |
224 |
|
} |
225 |
|
|
226 |
|
public HTMLDescriber getHTMLDescriber() |
227 |
|
{ |
228 |
0 |
return (HTMLDescriber) getProperty("HTMLDescriber"); |
229 |
|
} |
230 |
|
|
231 |
|
public ExceptionPresenter getExceptionPresenter() |
232 |
|
{ |
233 |
0 |
return (ExceptionPresenter) getProperty("exceptionPresenter"); |
234 |
|
} |
235 |
|
|
236 |
|
public ListenerMapSource getListenerMapSource() |
237 |
|
{ |
238 |
0 |
return (ListenerMapSource) getProperty("listenerMapSource"); |
239 |
|
} |
240 |
|
|
241 |
|
public StaleSessionExceptionPresenter getStaleSessionExceptionPresenter() |
242 |
|
{ |
243 |
0 |
return (StaleSessionExceptionPresenter) getProperty("staleSessionExceptionPresenter"); |
244 |
|
} |
245 |
|
|
246 |
|
public StaleLinkExceptionPresenter getStaleLinkExceptionPresenter() |
247 |
|
{ |
248 |
0 |
return (StaleLinkExceptionPresenter) getProperty("staleLinkExceptionPresenter"); |
249 |
|
} |
250 |
|
|
251 |
|
public ValueConverter getValueConverter() |
252 |
|
{ |
253 |
0 |
return (ValueConverter) getProperty("valueConverter"); |
254 |
|
} |
255 |
|
|
256 |
|
public ListenerInvoker getListenerInvoker() |
257 |
|
{ |
258 |
0 |
return (ListenerInvoker) getProperty("listenerInvoker"); |
259 |
|
} |
260 |
|
|
261 |
|
public AssetFactory getAssetFactory() |
262 |
|
{ |
263 |
0 |
return (AssetFactory) getProperty("assetFactory"); |
264 |
|
} |
265 |
|
|
266 |
|
public CookieSource getCookieSource() |
267 |
|
{ |
268 |
0 |
return (CookieSource) getProperty("cookieSource"); |
269 |
|
} |
270 |
|
|
271 |
|
public ClassFinder getClassFinder() |
272 |
|
{ |
273 |
0 |
return (ClassFinder) getProperty("classFinder"); |
274 |
|
} |
275 |
|
|
276 |
|
public IRequestCycle getRequestCycle() |
277 |
|
{ |
278 |
0 |
return (IRequestCycle) getProperty("requestCycle"); |
279 |
|
} |
280 |
|
|
281 |
|
public Object getProperty(String propertyName) |
282 |
|
{ |
283 |
6 |
Defense.notNull(propertyName, "propertyName"); |
284 |
|
|
285 |
6 |
if (!_initialized) |
286 |
1 |
throw new IllegalStateException(ImplMessages.infrastructureNotInitialized()); |
287 |
|
|
288 |
5 |
Object result = _properties.get(propertyName); |
289 |
|
|
290 |
5 |
if (result == null) |
291 |
1 |
throw new ApplicationRuntimeException(ImplMessages.missingInfrastructureProperty(propertyName)); |
292 |
|
|
293 |
4 |
return result; |
294 |
|
} |
295 |
|
|
296 |
|
public synchronized void initialize(String mode) |
297 |
|
{ |
298 |
7 |
Defense.notNull(mode, "mode"); |
299 |
|
|
300 |
7 |
if (_initialized) |
301 |
1 |
throw new IllegalStateException(ImplMessages.infrastructureAlreadyInitialized( |
302 |
|
mode, |
303 |
|
_mode)); |
304 |
|
|
305 |
6 |
Map normalByMode = buildMapFromContributions(_normalContributions, mode); |
306 |
6 |
Map normal = buildMapFromContributions(_normalContributions, null); |
307 |
6 |
Map overrideByMode = buildMapFromContributions(_overrideContributions, mode); |
308 |
6 |
Map override = buildMapFromContributions(_overrideContributions, null); |
309 |
|
|
310 |
6 |
addToProperties(overrideByMode); |
311 |
6 |
addToProperties(override); |
312 |
6 |
addToProperties(normalByMode); |
313 |
6 |
addToProperties(normal); |
314 |
|
|
315 |
6 |
_mode = mode; |
316 |
6 |
_initialized = true; |
317 |
6 |
} |
318 |
|
|
319 |
|
private Map buildMapFromContributions(List contributions, String mode) |
320 |
|
{ |
321 |
24 |
Map result = new HashMap(); |
322 |
|
|
323 |
24 |
Iterator i = contributions.iterator(); |
324 |
40 |
while (i.hasNext()) |
325 |
|
{ |
326 |
16 |
InfrastructureContribution ic = (InfrastructureContribution) i.next(); |
327 |
|
|
328 |
16 |
if (!ic.matchesMode(mode)) |
329 |
9 |
continue; |
330 |
|
|
331 |
7 |
String propertyName = ic.getProperty(); |
332 |
|
|
333 |
7 |
InfrastructureContribution existing = (InfrastructureContribution) result |
334 |
|
.get(propertyName); |
335 |
|
|
336 |
7 |
if (existing != null) |
337 |
|
{ |
338 |
1 |
_errorLog.error(ImplMessages.duplicateInfrastructureContribution(ic, existing |
339 |
|
.getLocation()), ic.getLocation(), null); |
340 |
1 |
continue; |
341 |
|
} |
342 |
|
|
343 |
6 |
result.put(propertyName, ic); |
344 |
6 |
} |
345 |
|
|
346 |
24 |
return result; |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
private void addToProperties(Map map) |
359 |
|
{ |
360 |
24 |
Iterator i = map.values().iterator(); |
361 |
30 |
while (i.hasNext()) |
362 |
|
{ |
363 |
6 |
InfrastructureContribution ic = (InfrastructureContribution) i.next(); |
364 |
6 |
String propertyName = ic.getProperty(); |
365 |
|
|
366 |
6 |
if (_properties.containsKey(propertyName)) |
367 |
2 |
continue; |
368 |
|
|
369 |
4 |
_properties.put(propertyName, ic.getObject()); |
370 |
4 |
} |
371 |
24 |
} |
372 |
|
|
373 |
|
public void setClassResolver(ClassResolver classResolver) |
374 |
|
{ |
375 |
0 |
_classResolver = classResolver; |
376 |
0 |
} |
377 |
|
|
378 |
|
public void setThreadLocale(ThreadLocale threadLocale) |
379 |
|
{ |
380 |
0 |
_threadLocale = threadLocale; |
381 |
0 |
} |
382 |
|
|
383 |
|
public void setNormalContributions(List normalContributions) |
384 |
|
{ |
385 |
6 |
_normalContributions = normalContributions; |
386 |
6 |
} |
387 |
|
|
388 |
|
public void setOverrideContributions(List overrideContributions) |
389 |
|
{ |
390 |
6 |
_overrideContributions = overrideContributions; |
391 |
6 |
} |
392 |
|
|
393 |
|
public void setLocaleManager(RequestLocaleManager manager) |
394 |
|
{ |
395 |
0 |
_localeManager = manager; |
396 |
0 |
} |
397 |
|
|
398 |
|
public void setErrorLog(ErrorLog errorLog) |
399 |
|
{ |
400 |
1 |
_errorLog = errorLog; |
401 |
1 |
} |
402 |
|
} |