Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 259   Methods: 15
NCLOC: 155   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SpecificationSourceImpl.java 75% 84% 93.3% 84.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.Map;
 19   
 20    import org.apache.hivemind.ApplicationRuntimeException;
 21    import org.apache.hivemind.ClassResolver;
 22    import org.apache.hivemind.Resource;
 23    import org.apache.hivemind.util.ClasspathResource;
 24    import org.apache.tapestry.INamespace;
 25    import org.apache.tapestry.asset.AssetSource;
 26    import org.apache.tapestry.engine.ISpecificationSource;
 27    import org.apache.tapestry.engine.Namespace;
 28    import org.apache.tapestry.event.ReportStatusEvent;
 29    import org.apache.tapestry.event.ReportStatusListener;
 30    import org.apache.tapestry.event.ResetEventListener;
 31    import org.apache.tapestry.parse.ISpecificationParser;
 32    import org.apache.tapestry.services.NamespaceResources;
 33    import org.apache.tapestry.spec.IApplicationSpecification;
 34    import org.apache.tapestry.spec.IComponentSpecification;
 35    import org.apache.tapestry.spec.ILibrarySpecification;
 36    import org.apache.tapestry.spec.LibrarySpecification;
 37    import org.apache.tapestry.util.xml.DocumentParseException;
 38   
 39    /**
 40    * Default implementation of {@link ISpecificationSource} that expects to use the normal class
 41    * loader to locate component specifications from within the classpath.
 42    * <p>
 43    * Caches specifications in memory forever, or until {@link #resetDidOccur()} is invoked.
 44    *
 45    * @author Howard Lewis Ship
 46    */
 47    public class SpecificationSourceImpl implements ISpecificationSource, ResetEventListener,
 48    ReportStatusListener
 49    {
 50    private ClassResolver _classResolver;
 51   
 52    private IApplicationSpecification _specification;
 53   
 54    private ISpecificationParser _parser;
 55   
 56    private NamespaceResources _namespaceResources;
 57   
 58    private INamespace _applicationNamespace;
 59   
 60    private INamespace _frameworkNamespace;
 61   
 62    private AssetSource _assetSource;
 63   
 64    private String _serviceId;
 65   
 66    /**
 67    * Contains previously parsed component specifications.
 68    */
 69   
 70    private Map _componentCache = new HashMap();
 71   
 72    /**
 73    * Contains previously parsed page specifications.
 74    *
 75    * @since 2.2
 76    */
 77   
 78    private Map _pageCache = new HashMap();
 79   
 80    /**
 81    * Contains previously parsed library specifications, keyed on specification resource path.
 82    *
 83    * @since 2.2
 84    */
 85   
 86    private Map _libraryCache = new HashMap();
 87   
 88    /**
 89    * Contains {@link INamespace} instances, keyed on id (which will be null for the application
 90    * specification).
 91    */
 92   
 93    private Map _namespaceCache = new HashMap();
 94   
 95  34 public void reportStatus(ReportStatusEvent event)
 96    {
 97  34 event.title(_serviceId);
 98   
 99  34 event.property("page specification count", _pageCache.size());
 100  34 event.collection("page specifications", _pageCache.keySet());
 101  34 event.property("component specification count", _componentCache.size());
 102  34 event.collection("component specifications", _componentCache.keySet());
 103    }
 104   
 105  78 public void initializeService()
 106    {
 107  78 _namespaceResources = new NamespaceResourcesImpl(this, _assetSource);
 108    }
 109   
 110    /**
 111    * Clears the specification cache. This is used during debugging.
 112    */
 113   
 114  0 public synchronized void resetEventDidOccur()
 115    {
 116  0 _componentCache.clear();
 117  0 _pageCache.clear();
 118  0 _libraryCache.clear();
 119  0 _namespaceCache.clear();
 120   
 121  0 _applicationNamespace = null;
 122  0 _frameworkNamespace = null;
 123    }
 124   
 125  750 protected IComponentSpecification parseSpecification(Resource resource, boolean asPage)
 126    {
 127  750 IComponentSpecification result = null;
 128   
 129  750 try
 130    {
 131  750 if (asPage)
 132  170 result = _parser.parsePageSpecification(resource);
 133    else
 134  580 result = _parser.parseComponentSpecification(resource);
 135    }
 136    catch (DocumentParseException ex)
 137    {
 138  0 throw new ApplicationRuntimeException(
 139    ImplMessages.unableToParseSpecification(resource), ex);
 140    }
 141   
 142  750 return result;
 143    }
 144   
 145  112 protected ILibrarySpecification parseLibrarySpecification(Resource resource)
 146    {
 147  112 try
 148    {
 149  112 return _parser.parseLibrarySpecification(resource);
 150    }
 151    catch (DocumentParseException ex)
 152    {
 153  0 throw new ApplicationRuntimeException(
 154    ImplMessages.unableToParseSpecification(resource), ex);
 155    }
 156   
 157    }
 158   
 159    /**
 160    * Gets a component specification.
 161    *
 162    * @param resourcePath
 163    * the complete resource path to the specification.
 164    * @throws ApplicationRuntimeException
 165    * if the specification cannot be obtained.
 166    */
 167   
 168  580 public synchronized IComponentSpecification getComponentSpecification(Resource resourceLocation)
 169    {
 170  580 IComponentSpecification result = (IComponentSpecification) _componentCache
 171    .get(resourceLocation);
 172   
 173  580 if (result == null)
 174    {
 175  580 result = parseSpecification(resourceLocation, false);
 176   
 177  580 _componentCache.put(resourceLocation, result);
 178    }
 179   
 180  580 return result;
 181    }
 182   
 183  170 public synchronized IComponentSpecification getPageSpecification(Resource resourceLocation)
 184    {
 185  170 IComponentSpecification result = (IComponentSpecification) _pageCache.get(resourceLocation);
 186   
 187  170 if (result == null)
 188    {
 189  170 result = parseSpecification(resourceLocation, true);
 190   
 191  170 _pageCache.put(resourceLocation, result);
 192    }
 193   
 194  170 return result;
 195    }
 196   
 197  112 public synchronized ILibrarySpecification getLibrarySpecification(Resource resourceLocation)
 198    {
 199  112 ILibrarySpecification result = (LibrarySpecification) _libraryCache.get(resourceLocation);
 200   
 201  112 if (result == null)
 202    {
 203  112 result = parseLibrarySpecification(resourceLocation);
 204  112 _libraryCache.put(resourceLocation, result);
 205    }
 206   
 207  112 return result;
 208    }
 209   
 210  190 public synchronized INamespace getApplicationNamespace()
 211    {
 212  190 if (_applicationNamespace == null)
 213  78 _applicationNamespace = new Namespace(null, null, _specification, _namespaceResources);
 214   
 215  190 return _applicationNamespace;
 216    }
 217   
 218  474 public synchronized INamespace getFrameworkNamespace()
 219    {
 220  474 if (_frameworkNamespace == null)
 221    {
 222  78 Resource resource = new ClasspathResource(_classResolver,
 223    "/org/apache/tapestry/Framework.library");
 224   
 225  78 ILibrarySpecification ls = getLibrarySpecification(resource);
 226   
 227  78 _frameworkNamespace = new Namespace(INamespace.FRAMEWORK_NAMESPACE, null, ls,
 228    _namespaceResources);
 229    }
 230   
 231  474 return _frameworkNamespace;
 232    }
 233   
 234  78 public void setParser(ISpecificationParser parser)
 235    {
 236  78 _parser = parser;
 237    }
 238   
 239  78 public void setClassResolver(ClassResolver resolver)
 240    {
 241  78 _classResolver = resolver;
 242    }
 243   
 244  78 public void setSpecification(IApplicationSpecification specification)
 245    {
 246  78 _specification = specification;
 247    }
 248   
 249  78 public void setAssetSource(AssetSource assetSource)
 250    {
 251  78 _assetSource = assetSource;
 252    }
 253   
 254  78 public void setServiceId(String serviceId)
 255    {
 256  78 _serviceId = serviceId;
 257    }
 258   
 259    }