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