KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > InfrastructureImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
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.ComponentMessagesSource;
45 import org.apache.tapestry.services.ComponentPropertySource;
46 import org.apache.tapestry.services.CookieSource;
47 import org.apache.tapestry.services.DataSqueezer;
48 import org.apache.tapestry.services.Infrastructure;
49 import org.apache.tapestry.services.LinkFactory;
50 import org.apache.tapestry.services.ObjectPool;
51 import org.apache.tapestry.services.RequestCycleFactory;
52 import org.apache.tapestry.services.ResetEventCoordinator;
53 import org.apache.tapestry.services.ResponseRenderer;
54 import org.apache.tapestry.services.ServiceMap;
55 import org.apache.tapestry.services.TemplateSource;
56 import org.apache.tapestry.spec.IApplicationSpecification;
57 import org.apache.tapestry.web.WebContext;
58 import org.apache.tapestry.web.WebContextResource;
59 import org.apache.tapestry.web.WebRequest;
60 import org.apache.tapestry.web.WebResponse;
61
62 /**
63  * Allows access to selected HiveMind services.
64  *
65  * @author Howard Lewis Ship
66  * @since 4.0
67  */

68 public class InfrastructureImpl implements Infrastructure
69 {
70     /**
71      * List of {@link org.apache.tapestry.services.impl.InfrastructureContribution}.
72      */

73     private List JavaDoc _normalContributions;
74
75     /**
76      * List of {@link org.apache.tapestry.services.impl.InfrastructureContribution}.
77      */

78     private List JavaDoc _overrideContributions;
79
80     private Map JavaDoc _properties = new HashMap JavaDoc();
81
82     private boolean _initialized;
83
84     private String JavaDoc _mode;
85
86     private ErrorLog _errorLog;
87
88     private ClassResolver _classResolver;
89
90     private ThreadLocale _threadLocale;
91
92     public void setLocale(Locale JavaDoc locale)
93     {
94         _threadLocale.setLocale(locale);
95     }
96
97     public String JavaDoc getApplicationId()
98     {
99         return (String JavaDoc) getProperty("applicationId");
100     }
101
102     public IPropertySource getApplicationPropertySource()
103     {
104         return (IPropertySource) getProperty("applicationPropertySource");
105     }
106
107     public IApplicationSpecification getApplicationSpecification()
108     {
109         return (IApplicationSpecification) getProperty("applicationSpecification");
110     }
111
112     public ApplicationStateManager getApplicationStateManager()
113     {
114         return (ApplicationStateManager) getProperty("applicationStateManager");
115     }
116
117     public ClassResolver getClassResolver()
118     {
119         return _classResolver;
120     }
121
122     public ComponentMessagesSource getComponentMessagesSource()
123     {
124         return (ComponentMessagesSource) getProperty("componentMessagesSource");
125     }
126
127     public ComponentPropertySource getComponentPropertySource()
128     {
129         return (ComponentPropertySource) getProperty("componentPropertySource");
130     }
131
132     public String JavaDoc getContextPath()
133     {
134         return getRequest().getContextPath();
135     }
136
137     public Resource getContextRoot()
138     {
139         WebContext context = (WebContext) getProperty("context");
140
141         return new WebContextResource(context, "/");
142     }
143
144     public DataSqueezer getDataSqueezer()
145     {
146         return (DataSqueezer) getProperty("dataSqueezer");
147     }
148
149     public IPropertySource getGlobalPropertySource()
150     {
151         return (IPropertySource) getProperty("globalPropertySource");
152     }
153
154     public LinkFactory getLinkFactory()
155     {
156         return (LinkFactory) getProperty("linkFactory");
157     }
158
159     public ObjectPool getObjectPool()
160     {
161         return (ObjectPool) getProperty("objectPool");
162     }
163
164     public IPageSource getPageSource()
165     {
166         return (IPageSource) getProperty("pageSource");
167     }
168
169     public WebRequest getRequest()
170     {
171         return (WebRequest) getProperty("request");
172     }
173
174     public RequestCycleFactory getRequestCycleFactory()
175     {
176         return (RequestCycleFactory) getProperty("requestCycleFactory");
177     }
178
179     public RequestExceptionReporter getRequestExceptionReporter()
180     {
181         return (RequestExceptionReporter) getProperty("requestExceptionReporter");
182     }
183
184     public ResetEventCoordinator getResetEventCoordinator()
185     {
186         return (ResetEventCoordinator) getProperty("resetEventCoordinator");
187     }
188
189     public WebResponse getResponse()
190     {
191         return (WebResponse) getProperty("response");
192     }
193
194     public ResponseRenderer getResponseRenderer()
195     {
196         return (ResponseRenderer) getProperty("responseRenderer");
197     }
198
199     public IScriptSource getScriptSource()
200     {
201         return (IScriptSource) getProperty("scriptSource");
202     }
203
204     public ServiceMap getServiceMap()
205     {
206         return (ServiceMap) getProperty("serviceMap");
207     }
208
209     public ISpecificationSource getSpecificationSource()
210     {
211         return (ISpecificationSource) getProperty("specificationSource");
212     }
213
214     public TemplateSource getTemplateSource()
215     {
216         return (TemplateSource) getProperty("templateSource");
217     }
218
219     public String JavaDoc getOutputEncoding()
220     {
221         return getApplicationPropertySource().getPropertyValue(
222                 "org.apache.tapestry.output-encoding");
223     }
224
225     public MarkupWriterSource getMarkupWriterSource()
226     {
227         return (MarkupWriterSource) getProperty("markupWriterSource");
228     }
229
230     public HTMLDescriber getHTMLDescriber()
231     {
232         return (HTMLDescriber) getProperty("HTMLDescriber");
233     }
234
235     public ExceptionPresenter getExceptionPresenter()
236     {
237         return (ExceptionPresenter) getProperty("exceptionPresenter");
238     }
239
240     public ListenerMapSource getListenerMapSource()
241     {
242         return (ListenerMapSource) getProperty("listenerMapSource");
243     }
244
245     public StaleSessionExceptionPresenter getStaleSessionExceptionPresenter()
246     {
247         return (StaleSessionExceptionPresenter) getProperty("staleSessionExceptionPresenter");
248     }
249
250     public StaleLinkExceptionPresenter getStaleLinkExceptionPresenter()
251     {
252         return (StaleLinkExceptionPresenter) getProperty("staleLinkExceptionPresenter");
253     }
254
255     public ValueConverter getValueConverter()
256     {
257         return (ValueConverter) getProperty("valueConverter");
258     }
259
260     public ListenerInvoker getListenerInvoker()
261     {
262         return (ListenerInvoker) getProperty("listenerInvoker");
263     }
264
265     public AssetFactory getAssetFactory()
266     {
267         return (AssetFactory) getProperty("assetFactory");
268     }
269     
270     public CookieSource getCookieSource()
271     {
272         return (CookieSource) getProperty("cookieSource");
273     }
274
275     public Object JavaDoc getProperty(String JavaDoc propertyName)
276     {
277         Defense.notNull(propertyName, "propertyName");
278
279         if (!_initialized)
280             throw new IllegalStateException JavaDoc(ImplMessages.infrastructureNotInitialized());
281
282         Object JavaDoc result = _properties.get(propertyName);
283
284         if (result == null)
285             throw new ApplicationRuntimeException(ImplMessages
286                     .missingInfrastructureProperty(propertyName));
287
288         return result;
289     }
290
291     public synchronized void initialize(String JavaDoc mode)
292     {
293         Defense.notNull(mode, "mode");
294
295         if (_initialized)
296             throw new IllegalStateException JavaDoc(ImplMessages.infrastructureAlreadyInitialized(
297                     mode,
298                     _mode));
299
300         Map JavaDoc normalByMode = buildMapFromContributions(_normalContributions, mode);
301         Map JavaDoc normal = buildMapFromContributions(_normalContributions, null);
302         Map JavaDoc overrideByMode = buildMapFromContributions(_overrideContributions, mode);
303         Map JavaDoc override = buildMapFromContributions(_overrideContributions, null);
304
305         addToProperties(overrideByMode);
306         addToProperties(override);
307         addToProperties(normalByMode);
308         addToProperties(normal);
309
310         _mode = mode;
311         _initialized = true;
312     }
313
314     private Map JavaDoc buildMapFromContributions(List JavaDoc contributions, String JavaDoc mode)
315     {
316         Map JavaDoc result = new HashMap JavaDoc();
317
318         Iterator JavaDoc i = contributions.iterator();
319         while (i.hasNext())
320         {
321             InfrastructureContribution ic = (InfrastructureContribution) i.next();
322
323             if (!ic.matchesMode(mode))
324                 continue;
325
326             String JavaDoc propertyName = ic.getProperty();
327
328             InfrastructureContribution existing = (InfrastructureContribution) result
329                     .get(propertyName);
330
331             if (existing != null)
332             {
333                 _errorLog.error(ImplMessages.duplicateInfrastructureContribution(ic, existing
334                         .getLocation()), ic.getLocation(), null);
335                 continue;
336             }
337
338             result.put(propertyName, ic);
339         }
340
341         return result;
342     }
343
344     /**
345      * Adds to the master set of properties contributed objects that don't match an already existing
346      * key.
347      *
348      * @param map
349      * map of {@link org.apache.tapestry.services.impl.InfrastructureContribution}keyed
350      * on property name (String).
351      */

352
353     private void addToProperties(Map JavaDoc map)
354     {
355         Iterator JavaDoc i = map.values().iterator();
356         while (i.hasNext())
357         {
358             InfrastructureContribution ic = (InfrastructureContribution) i.next();
359             String JavaDoc propertyName = ic.getProperty();
360
361             if (_properties.containsKey(propertyName))
362                 continue;
363
364             _properties.put(propertyName, ic.getObject());
365         }
366     }
367
368     public void setClassResolver(ClassResolver classResolver)
369     {
370         _classResolver = classResolver;
371     }
372
373     public void setThreadLocale(ThreadLocale threadLocale)
374     {
375         _threadLocale = threadLocale;
376     }
377
378     public void setNormalContributions(List JavaDoc normalContributions)
379     {
380         _normalContributions = normalContributions;
381     }
382
383     public void setOverrideContributions(List JavaDoc overrideContributions)
384     {
385         _overrideContributions = overrideContributions;
386     }
387
388     public void setErrorLog(ErrorLog errorLog)
389     {
390         _errorLog = errorLog;
391     }
392 }
Popular Tags