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