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