Coverage Report - org.apache.tapestry.services.impl.InfrastructureImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
InfrastructureImpl
0%
0/102
0%
0/18
1.271
 
 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 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  
  * Allows access to selected HiveMind services.
 50  
  * 
 51  
  * @author Howard Lewis Ship
 52  
  * @since 4.0
 53  
  */
 54  0
 public class InfrastructureImpl implements Infrastructure
 55  
 {
 56  
     /**
 57  
      * List of {@link org.apache.tapestry.services.impl.InfrastructureContribution}.
 58  
      */
 59  
     private List _normalContributions;
 60  
 
 61  
     /**
 62  
      * List of {@link org.apache.tapestry.services.impl.InfrastructureContribution}.
 63  
      */
 64  
     private List _overrideContributions;
 65  
 
 66  0
     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  0
         Defense.notNull(propertyName, "propertyName");
 284  
 
 285  0
         if (!_initialized)
 286  0
             throw new IllegalStateException(ImplMessages.infrastructureNotInitialized());
 287  
 
 288  0
         Object result = _properties.get(propertyName);
 289  
 
 290  0
         if (result == null)
 291  0
             throw new ApplicationRuntimeException(ImplMessages.missingInfrastructureProperty(propertyName));
 292  
 
 293  0
         return result;
 294  
     }
 295  
 
 296  
     public synchronized void initialize(String mode)
 297  
     {
 298  0
         Defense.notNull(mode, "mode");
 299  
 
 300  0
         if (_initialized)
 301  0
             throw new IllegalStateException(ImplMessages.infrastructureAlreadyInitialized(
 302  
                     mode,
 303  
                     _mode));
 304  
 
 305  0
         Map normalByMode = buildMapFromContributions(_normalContributions, mode);
 306  0
         Map normal = buildMapFromContributions(_normalContributions, null);
 307  0
         Map overrideByMode = buildMapFromContributions(_overrideContributions, mode);
 308  0
         Map override = buildMapFromContributions(_overrideContributions, null);
 309  
 
 310  0
         addToProperties(overrideByMode);
 311  0
         addToProperties(override);
 312  0
         addToProperties(normalByMode);
 313  0
         addToProperties(normal);
 314  
 
 315  0
         _mode = mode;
 316  0
         _initialized = true;
 317  0
     }
 318  
 
 319  
     private Map buildMapFromContributions(List contributions, String mode)
 320  
     {
 321  0
         Map result = new HashMap();
 322  
 
 323  0
         Iterator i = contributions.iterator();
 324  0
         while (i.hasNext())
 325  
         {
 326  0
             InfrastructureContribution ic = (InfrastructureContribution) i.next();
 327  
 
 328  0
             if (!ic.matchesMode(mode))
 329  0
                 continue;
 330  
 
 331  0
             String propertyName = ic.getProperty();
 332  
 
 333  0
             InfrastructureContribution existing = (InfrastructureContribution) result
 334  
                     .get(propertyName);
 335  
 
 336  0
             if (existing != null)
 337  
             {
 338  0
                 _errorLog.error(ImplMessages.duplicateInfrastructureContribution(ic, existing
 339  
                         .getLocation()), ic.getLocation(), null);
 340  0
                 continue;
 341  
             }
 342  
 
 343  0
             result.put(propertyName, ic);
 344  0
         }
 345  
 
 346  0
         return result;
 347  
     }
 348  
 
 349  
     /**
 350  
      * Adds to the master set of properties contributed objects that don't match an already existing
 351  
      * key.
 352  
      * 
 353  
      * @param map
 354  
      *            map of {@link org.apache.tapestry.services.impl.InfrastructureContribution}keyed
 355  
      *            on property name (String).
 356  
      */
 357  
 
 358  
     private void addToProperties(Map map)
 359  
     {
 360  0
         Iterator i = map.values().iterator();
 361  0
         while (i.hasNext())
 362  
         {
 363  0
             InfrastructureContribution ic = (InfrastructureContribution) i.next();
 364  0
             String propertyName = ic.getProperty();
 365  
 
 366  0
             if (_properties.containsKey(propertyName))
 367  0
                 continue;
 368  
 
 369  0
             _properties.put(propertyName, ic.getObject());
 370  0
         }
 371  0
     }
 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  0
         _normalContributions = normalContributions;
 386  0
     }
 387  
 
 388  
     public void setOverrideContributions(List overrideContributions)
 389  
     {
 390  0
         _overrideContributions = overrideContributions;
 391  0
     }
 392  
 
 393  
     public void setLocaleManager(RequestLocaleManager manager)
 394  
     {
 395  0
         _localeManager = manager;
 396  0
     }
 397  
 
 398  
     public void setErrorLog(ErrorLog errorLog)
 399  
     {
 400  0
         _errorLog = errorLog;
 401  0
     }
 402  
 }