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