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