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