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