Coverage Report - org.apache.tapestry.services.impl.SpecificationSourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SpecificationSourceImpl
0%
0/64
0%
0/12
1.733
 
 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 #resetEventDidOccur()} is invoked.
 44  
  * 
 45  
  * @author Howard Lewis Ship
 46  
  */
 47  0
 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  0
     private Map _componentCache = new HashMap();
 71  
 
 72  
     /**
 73  
      * Contains previously parsed page specifications.
 74  
      * 
 75  
      * @since 2.2
 76  
      */
 77  
 
 78  0
     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  0
     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  0
     private Map _namespaceCache = new HashMap();
 94  
 
 95  
     public void reportStatus(ReportStatusEvent event)
 96  
     {
 97  0
         event.title(_serviceId);
 98  
 
 99  0
         event.property("page specification count", _pageCache.size());
 100  0
         event.collection("page specifications", _pageCache.keySet());
 101  0
         event.property("component specification count", _componentCache.size());
 102  0
         event.collection("component specifications", _componentCache.keySet());
 103  0
     }
 104  
 
 105  
     public void initializeService()
 106  
     {
 107  0
         _namespaceResources = new NamespaceResourcesImpl(this, _assetSource);
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Clears the specification cache. This is used during debugging.
 112  
      */
 113  
 
 114  
     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  0
     }
 124  
 
 125  
     protected IComponentSpecification parseSpecification(Resource resource, boolean asPage)
 126  
     {
 127  0
         IComponentSpecification result = null;
 128  
 
 129  
         try
 130  
         {
 131  0
             if (asPage)
 132  0
                 result = _parser.parsePageSpecification(resource);
 133  
             else
 134  0
                 result = _parser.parseComponentSpecification(resource);
 135  
         }
 136  0
         catch (DocumentParseException ex)
 137  
         {
 138  0
             throw new ApplicationRuntimeException(
 139  
                     ImplMessages.unableToParseSpecification(resource), ex);
 140  0
         }
 141  
 
 142  0
         return result;
 143  
     }
 144  
 
 145  
     protected ILibrarySpecification parseLibrarySpecification(Resource resource)
 146  
     {
 147  
         try
 148  
         {
 149  0
             return _parser.parseLibrarySpecification(resource);
 150  
         }
 151  0
         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 resourceLocation
 163  
      *            the complete resource path to the specification.
 164  
      * @throws ApplicationRuntimeException
 165  
      *             if the specification cannot be obtained.
 166  
      */
 167  
 
 168  
     public synchronized IComponentSpecification getComponentSpecification(Resource resourceLocation)
 169  
     {
 170  0
         IComponentSpecification result = (IComponentSpecification) _componentCache
 171  
                 .get(resourceLocation);
 172  
 
 173  0
         if (result == null)
 174  
         {
 175  0
             result = parseSpecification(resourceLocation, false);
 176  
 
 177  0
             _componentCache.put(resourceLocation, result);
 178  
         }
 179  
 
 180  0
         return result;
 181  
     }
 182  
 
 183  
     public synchronized IComponentSpecification getPageSpecification(Resource resourceLocation)
 184  
     {
 185  0
         IComponentSpecification result = (IComponentSpecification) _pageCache.get(resourceLocation);
 186  
 
 187  0
         if (result == null)
 188  
         {
 189  0
             result = parseSpecification(resourceLocation, true);
 190  
 
 191  0
             _pageCache.put(resourceLocation, result);
 192  
         }
 193  
 
 194  0
         return result;
 195  
     }
 196  
 
 197  
     public synchronized ILibrarySpecification getLibrarySpecification(Resource resourceLocation)
 198  
     {
 199  0
         ILibrarySpecification result = (LibrarySpecification) _libraryCache.get(resourceLocation);
 200  
 
 201  0
         if (result == null)
 202  
         {
 203  0
             result = parseLibrarySpecification(resourceLocation);
 204  0
             _libraryCache.put(resourceLocation, result);
 205  
         }
 206  
 
 207  0
         return result;
 208  
     }
 209  
 
 210  
     public synchronized INamespace getApplicationNamespace()
 211  
     {
 212  0
         if (_applicationNamespace == null)
 213  0
             _applicationNamespace = new Namespace(null, null, _specification, _namespaceResources);
 214  
 
 215  0
         return _applicationNamespace;
 216  
     }
 217  
 
 218  
     public synchronized INamespace getFrameworkNamespace()
 219  
     {
 220  0
         if (_frameworkNamespace == null)
 221  
         {
 222  0
             Resource resource = new ClasspathResource(_classResolver,
 223  
                     "/org/apache/tapestry/Framework.library");
 224  
 
 225  0
             ILibrarySpecification ls = getLibrarySpecification(resource);
 226  
 
 227  0
             _frameworkNamespace = new Namespace(INamespace.FRAMEWORK_NAMESPACE, null, ls,
 228  
                     _namespaceResources);
 229  
         }
 230  
 
 231  0
         return _frameworkNamespace;
 232  
     }
 233  
 
 234  
     public void setParser(ISpecificationParser parser)
 235  
     {
 236  0
         _parser = parser;
 237  0
     }
 238  
 
 239  
     public void setClassResolver(ClassResolver resolver)
 240  
     {
 241  0
         _classResolver = resolver;
 242  0
     }
 243  
 
 244  
     public void setSpecification(IApplicationSpecification specification)
 245  
     {
 246  0
         _specification = specification;
 247  0
     }
 248  
 
 249  
     public void setAssetSource(AssetSource assetSource)
 250  
     {
 251  0
         _assetSource = assetSource;
 252  0
     }
 253  
 
 254  
     public void setServiceId(String serviceId)
 255  
     {
 256  0
         _serviceId = serviceId;
 257  0
     }
 258  
 
 259  
 }