Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 239   Methods: 7
NCLOC: 106   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ComponentSpecificationResolverImpl.java 100% 100% 100% 100%
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.resolver;
 16   
 
 17   
 import org.apache.commons.logging.Log;
 18   
 import org.apache.hivemind.ApplicationRuntimeException;
 19   
 import org.apache.hivemind.Location;
 20   
 import org.apache.hivemind.Resource;
 21   
 import org.apache.tapestry.INamespace;
 22   
 import org.apache.tapestry.IRequestCycle;
 23   
 import org.apache.tapestry.spec.IComponentSpecification;
 24   
 
 25   
 /**
 26   
  * Utility class that understands the rules of component types (which may optionally have a library
 27   
  * prefix) and can resolve the type to a {@link org.apache.tapestry.INamespace}and a
 28   
  * {@link org.apache.tapestry.spec.IComponentSpecification}.
 29   
  * <p>
 30   
  * Like {@link org.apache.tapestry.resolver.PageSpecificationResolver}, if the component is not
 31   
  * defined explicitly in the namespace, a search may occur: Performs the tricky work of resolving a
 32   
  * page name to a page specification. The search for pages in the application namespace is the most
 33   
  * complicated, since Tapestry searches for pages that aren't explicitly defined in the application
 34   
  * specification. The search, based on the <i>simple-name </i> of the page, goes as follows:
 35   
  * <ul>
 36   
  * <li>As declared in the application specification
 37   
  * <li><i>type </i>.jwc in the same folder as the application specification
 38   
  * <li><i>type </i> jwc in the WEB-INF/ <i>servlet-name </i> directory of the context root
 39   
  * <li><i>type </i>.jwc in WEB-INF
 40   
  * <li><i>type </i>.jwc in the application root (within the context root)
 41   
  * <li>By searching the framework namespace
 42   
  * </ul>
 43   
  * The search for components in library namespaces is more abbreviated:
 44   
  * <li>As declared in the library specification
 45   
  * <li><i>type </i>.jwc in the same folder as the library specification
 46   
  * <li>By searching the framework namespace
 47   
  * </ul>
 48   
  * 
 49   
  * @author Howard Lewis Ship
 50   
  * @since 3.0
 51   
  */
 52   
 
 53   
 public class ComponentSpecificationResolverImpl extends AbstractSpecificationResolver implements
 54   
         ComponentSpecificationResolver
 55   
 {
 56   
     /** Set by container */
 57   
     private Log _log;
 58   
 
 59   
     /** Set by resolve() */
 60   
     private String _type;
 61   
 
 62  1542
     protected void reset()
 63   
     {
 64  1542
         _type = null;
 65   
 
 66  1542
         super.reset();
 67   
     }
 68   
 
 69   
     /**
 70   
      * Passed the namespace of a container (to resolve the type in) and the type to resolve,
 71   
      * performs the processing. A "bare type" (without a library prefix) may be in the
 72   
      * containerNamespace, or the framework namespace (a search occurs in that order).
 73   
      * 
 74   
      * @param cycle
 75   
      *            current request cycle
 76   
      * @param containerNamespace
 77   
      *            namespace that may contain a library referenced in the type
 78   
      * @param type
 79   
      *            the component specification to find, either a simple name, or prefixed with a
 80   
      *            library id (defined for the container namespace)
 81   
      * @see #getNamespace()
 82   
      * @see #getSpecification()
 83   
      */
 84   
 
 85  1034
     public void resolve(IRequestCycle cycle, INamespace containerNamespace, String type,
 86   
             Location location)
 87   
     {
 88  1034
         int colonx = type.indexOf(':');
 89   
 
 90  1034
         if (colonx > 0)
 91   
         {
 92  93
             String libraryId = type.substring(0, colonx);
 93  93
             String simpleType = type.substring(colonx + 1);
 94   
 
 95  93
             resolve(cycle, containerNamespace, libraryId, simpleType, location);
 96   
         }
 97   
         else
 98  941
             resolve(cycle, containerNamespace, null, type, location);
 99   
     }
 100   
 
 101   
     /**
 102   
      * Like
 103   
      * {@link #resolve(org.apache.tapestry.IRequestCycle, org.apache.tapestry.INamespace, java.lang.String, org.apache.tapestry.ILocation)},
 104   
      * but used when the type has already been parsed into a library id and a simple type.
 105   
      * 
 106   
      * @param cycle
 107   
      *            current request cycle
 108   
      * @param containerNamespace
 109   
      *            namespace that may contain a library referenced in the type
 110   
      * @param libraryId
 111   
      *            the library id within the container namespace, or null
 112   
      * @param type
 113   
      *            the component specification to find as a simple name (without a library prefix)
 114   
      * @param location
 115   
      *            of reference to be resolved
 116   
      * @throws ApplicationRuntimeException
 117   
      *             if the type cannot be resolved
 118   
      */
 119   
 
 120  1542
     public void resolve(IRequestCycle cycle, INamespace containerNamespace, String libraryId,
 121   
             String type, Location location)
 122   
     {
 123  1542
         reset();
 124  1542
         _type = type;
 125   
 
 126  1542
         INamespace namespace = null;
 127   
 
 128  1542
         if (libraryId != null)
 129  163
             namespace = containerNamespace.getChildNamespace(libraryId);
 130   
         else
 131  1379
             namespace = containerNamespace;
 132   
 
 133  1542
         setNamespace(namespace);
 134   
 
 135  1542
         if (namespace.containsComponentType(type))
 136  1350
             setSpecification(namespace.getComponentSpecification(type));
 137   
         else
 138  192
             searchForComponent(cycle);
 139   
 
 140   
         // If not found after search, check to see if it's in
 141   
         // the framework instead.
 142   
 
 143  1542
         if (getSpecification() == null)
 144   
         {
 145   
 
 146  1
             throw new ApplicationRuntimeException(ResolverMessages.noSuchComponentType(
 147   
                     type,
 148   
                     namespace), location, null);
 149   
 
 150   
         }
 151   
     }
 152   
 
 153   
     // TODO: This looks like a chain-of-command to me
 154   
 
 155  192
     private void searchForComponent(IRequestCycle cycle)
 156   
     {
 157  192
         INamespace namespace = getNamespace();
 158   
 
 159  192
         if (_log.isDebugEnabled())
 160  7
             _log.debug(ResolverMessages.resolvingComponent(_type, namespace));
 161   
 
 162  192
         String expectedName = _type + ".jwc";
 163  192
         Resource namespaceLocation = namespace.getSpecificationLocation();
 164   
 
 165   
         // Look for appropriate file in same folder as the library (or application)
 166   
         // specificaiton.
 167   
 
 168  192
         if (found(namespaceLocation.getRelativeResource(expectedName)))
 169  9
             return;
 170   
 
 171  183
         if (namespace.isApplicationNamespace())
 172   
         {
 173   
 
 174   
             // The application namespace gets some extra searching.
 175   
 
 176  138
             if (found(getWebInfAppLocation().getRelativeResource(expectedName)))
 177  1
                 return;
 178   
 
 179  137
             if (found(getWebInfLocation().getRelativeResource(expectedName)))
 180  1
                 return;
 181   
 
 182  136
             if (found(getContextRoot().getRelativeResource(expectedName)))
 183  1
                 return;
 184   
         }
 185   
 
 186   
         // Not in the library or app spec; does it match a component
 187   
         // provided by the Framework?
 188   
 
 189  180
         INamespace framework = getSpecificationSource().getFrameworkNamespace();
 190   
 
 191  180
         if (framework.containsComponentType(_type))
 192   
         {
 193  178
             setSpecification(framework.getComponentSpecification(_type));
 194   
             
 195  178
             install();
 196   
             
 197  178
             return;
 198   
         }
 199   
 
 200  2
         IComponentSpecification specification = getDelegate().findComponentSpecification(
 201   
                 cycle,
 202   
                 namespace,
 203   
                 _type);
 204   
         
 205  2
         setSpecification(specification);
 206   
     }
 207   
 
 208  603
     private boolean found(Resource resource)
 209   
     {
 210  603
         if (_log.isDebugEnabled())
 211  13
             _log.debug("Checking: " + resource);
 212   
 
 213  603
         if (resource.getResourceURL() == null)
 214  591
             return false;
 215   
 
 216  12
         setSpecification(getSpecificationSource().getComponentSpecification(resource));
 217   
 
 218  12
         install();
 219   
 
 220  12
         return true;
 221   
     }
 222   
 
 223  190
     private void install()
 224   
     {
 225  190
         INamespace namespace = getNamespace();
 226  190
         IComponentSpecification specification = getSpecification();
 227   
 
 228  190
         if (_log.isDebugEnabled())
 229  5
             _log.debug(ResolverMessages.installingComponent(_type, namespace, specification));
 230   
 
 231  190
         namespace.installComponentSpecification(_type, specification);
 232   
     }
 233   
 
 234  116
     public void setLog(Log log)
 235   
     {
 236  116
         _log = log;
 237   
     }
 238   
 
 239   
 }