Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 301   Methods: 11
NCLOC: 149   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PageSpecificationResolverImpl.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.Resource;
 19    import org.apache.hivemind.impl.LocationImpl;
 20    import org.apache.tapestry.INamespace;
 21    import org.apache.tapestry.IRequestCycle;
 22    import org.apache.tapestry.PageNotFoundException;
 23    import org.apache.tapestry.Tapestry;
 24    import org.apache.tapestry.services.ComponentPropertySource;
 25    import org.apache.tapestry.spec.ComponentSpecification;
 26    import org.apache.tapestry.spec.IComponentSpecification;
 27   
 28    /**
 29    * Performs the tricky work of resolving a page name to a page specification. The search for pages
 30    * in the application namespace is the most complicated, since Tapestry searches for pages that
 31    * aren't explicitly defined in the application specification. The search, based on the
 32    * <i>simple-name </i> of the page, goes as follows:
 33    * <ul>
 34    * <li>As declared in the application specification
 35    * <li><i>simple-name </i>.page in the same folder as the application specification
 36    * <li><i>simple-name </i> page in the WEB-INF/ <i>servlet-name </i> directory of the context root
 37    * <li><i>simple-name </i>.page in WEB-INF
 38    * <li><i>simple-name </i>.page in the application root (within the context root)
 39    * <li><i>simple-name </i>.html as a template in the application root, for which an implicit
 40    * specification is generated
 41    * <li>By searching the framework namespace
 42    * <li>By invoking
 43    * {@link org.apache.tapestry.resolver.ISpecificationResolverDelegate#findPageSpecification(IRequestCycle, INamespace, String)}
 44    * </ul>
 45    * <p>
 46    * Pages in a component library are searched for in a more abbreviated fashion:
 47    * <ul>
 48    * <li>As declared in the library specification
 49    * <li><i>simple-name </i>.page in the same folder as the library specification
 50    * <li>By searching the framework namespace
 51    * <li>By invoking
 52    * {@link org.apache.tapestry.resolver.ISpecificationResolverDelegate#findPageSpecification(IRequestCycle, INamespace, String)}
 53    * </ul>
 54    *
 55    * @see org.apache.tapestry.engine.IPageSource
 56    * @author Howard Lewis Ship
 57    * @since 3.0
 58    */
 59   
 60    public class PageSpecificationResolverImpl extends AbstractSpecificationResolver implements
 61    PageSpecificationResolver
 62    {
 63    /** set by container */
 64    private Log _log;
 65   
 66    /** Set by resolve() */
 67    private String _simpleName;
 68   
 69    /** @since 4.0 * */
 70    private INamespace _applicationNamespace;
 71   
 72    /** @since 4.0 * */
 73    private INamespace _frameworkNamespace;
 74   
 75    /** @since 4.0 */
 76   
 77    private ComponentPropertySource _componentPropertySource;
 78   
 79  107 public void initializeService()
 80    {
 81  107 _applicationNamespace = getSpecificationSource().getApplicationNamespace();
 82  107 _frameworkNamespace = getSpecificationSource().getFrameworkNamespace();
 83   
 84  107 super.initializeService();
 85    }
 86   
 87  135 protected void reset()
 88    {
 89  135 _simpleName = null;
 90   
 91  135 super.reset();
 92    }
 93   
 94    /**
 95    * Resolve the name (which may have a library id prefix) to a namespace (see
 96    * {@link #getNamespace()}) and a specification (see {@link #getSpecification()}).
 97    *
 98    * @throws ApplicationRuntimeException
 99    * if the name cannot be resolved
 100    */
 101   
 102  135 public void resolve(IRequestCycle cycle, String prefixedName)
 103    {
 104  135 reset();
 105   
 106  135 INamespace namespace = null;
 107   
 108  135 int colonx = prefixedName.indexOf(':');
 109   
 110  135 if (colonx > 0)
 111    {
 112  5 _simpleName = prefixedName.substring(colonx + 1);
 113  5 String namespaceId = prefixedName.substring(0, colonx);
 114   
 115  5 namespace = findNamespaceForId(_applicationNamespace, namespaceId);
 116    }
 117    else
 118    {
 119  130 _simpleName = prefixedName;
 120   
 121  130 namespace = _applicationNamespace;
 122    }
 123   
 124  135 setNamespace(namespace);
 125   
 126  135 if (namespace.containsPage(_simpleName))
 127    {
 128  43 setSpecification(namespace.getPageSpecification(_simpleName));
 129  43 return;
 130    }
 131   
 132    // Not defined in the specification, so it's time to hunt it down.
 133   
 134  92 searchForPage(cycle);
 135   
 136  92 if (getSpecification() == null)
 137  2 throw new PageNotFoundException(ResolverMessages.noSuchPage(_simpleName, namespace));
 138    }
 139   
 140  133 public String getSimplePageName()
 141    {
 142  133 return _simpleName;
 143    }
 144   
 145  92 private void searchForPage(IRequestCycle cycle)
 146    {
 147  92 INamespace namespace = getNamespace();
 148   
 149  92 if (_log.isDebugEnabled())
 150  8 _log.debug(ResolverMessages.resolvingPage(_simpleName, namespace));
 151   
 152  92 String expectedName = _simpleName + ".page";
 153   
 154  92 Resource namespaceLocation = namespace.getSpecificationLocation();
 155   
 156    // See if there's a specification file in the same folder
 157    // as the library or application specification that's
 158    // supposed to contain the page.
 159   
 160  92 if (found(namespaceLocation.getRelativeResource(expectedName)))
 161  43 return;
 162   
 163  49 if (namespace.isApplicationNamespace())
 164    {
 165   
 166    // The application namespace gets some extra searching.
 167   
 168  47 if (found(getWebInfAppLocation().getRelativeResource(expectedName)))
 169  1 return;
 170   
 171  46 if (found(getWebInfLocation().getRelativeResource(expectedName)))
 172  1 return;
 173   
 174  45 if (found(getContextRoot().getRelativeResource(expectedName)))
 175  1 return;
 176   
 177    // The wierd one ... where we see if there's a template in the application root
 178    // location.
 179   
 180  44 String templateName = _simpleName + "." + getTemplateExtension();
 181   
 182  44 Resource templateResource = getContextRoot().getRelativeResource(templateName);
 183   
 184  44 if (_log.isDebugEnabled())
 185  2 _log.debug(ResolverMessages.checkingResource(templateResource));
 186   
 187  44 if (templateResource.getResourceURL() != null)
 188    {
 189  18 setupImplicitPage(templateResource, namespaceLocation);
 190  18 return;
 191    }
 192   
 193    // Not found in application namespace, so maybe its a framework page.
 194   
 195  26 if (_frameworkNamespace.containsPage(_simpleName))
 196    {
 197  25 if (_log.isDebugEnabled())
 198  1 _log.debug(ResolverMessages.foundFrameworkPage(_simpleName));
 199   
 200  25 setNamespace(_frameworkNamespace);
 201   
 202    // Note: This implies that normal lookup rules don't work
 203    // for the framework! Framework pages must be
 204    // defined in the framework library specification.
 205   
 206  25 setSpecification(_frameworkNamespace.getPageSpecification(_simpleName));
 207  25 return;
 208    }
 209    }
 210   
 211    // Not found by any normal rule, so its time to
 212    // consult the delegate.
 213   
 214  3 IComponentSpecification specification = getDelegate().findPageSpecification(
 215    cycle,
 216    namespace,
 217    _simpleName);
 218   
 219  3 if (specification != null)
 220    {
 221  1 setSpecification(specification);
 222  1 install();
 223    }
 224    }
 225   
 226  18 private void setupImplicitPage(Resource resource, Resource namespaceLocation)
 227    {
 228  18 if (_log.isDebugEnabled())
 229  1 _log.debug(ResolverMessages.foundHTMLTemplate(resource));
 230   
 231    // TODO The SpecFactory in Specification parser should be used in some way to
 232    // create an IComponentSpecification!
 233   
 234    // The virtual location of the page specification is relative to the
 235    // namespace (typically, the application specification). This will be used when
 236    // searching for the page's message catalog or other related assets.
 237   
 238  18 Resource pageResource = namespaceLocation.getRelativeResource(_simpleName + ".page");
 239   
 240  18 IComponentSpecification specification = new ComponentSpecification();
 241  18 specification.setPageSpecification(true);
 242  18 specification.setSpecificationLocation(pageResource);
 243  18 specification.setLocation(new LocationImpl(resource));
 244   
 245  18 setSpecification(specification);
 246   
 247  18 install();
 248    }
 249   
 250  230 private boolean found(Resource resource)
 251    {
 252  230 if (_log.isDebugEnabled())
 253  20 _log.debug(ResolverMessages.checkingResource(resource));
 254   
 255  230 if (resource.getResourceURL() == null)
 256  184 return false;
 257   
 258  46 setSpecification(getSpecificationSource().getPageSpecification(resource));
 259   
 260  46 install();
 261   
 262  46 return true;
 263    }
 264   
 265  65 private void install()
 266    {
 267  65 INamespace namespace = getNamespace();
 268  65 IComponentSpecification specification = getSpecification();
 269   
 270  65 if (_log.isDebugEnabled())
 271  4 _log.debug(ResolverMessages.installingPage(_simpleName, namespace, specification));
 272   
 273  65 namespace.installPageSpecification(_simpleName, specification);
 274    }
 275   
 276    /**
 277    * If the namespace defines the template extension (as property
 278    * {@link Tapestry#TEMPLATE_EXTENSION_PROPERTY}, then that is used, otherwise the default is
 279    * used.
 280    */
 281   
 282  44 private String getTemplateExtension()
 283    {
 284  44 return _componentPropertySource.getNamespaceProperty(
 285    getNamespace(),
 286    Tapestry.TEMPLATE_EXTENSION_PROPERTY);
 287    }
 288   
 289    /** @since 4.0 */
 290   
 291  103 public void setLog(Log log)
 292    {
 293  103 _log = log;
 294    }
 295   
 296    /** @since 4.0 */
 297  97 public void setComponentPropertySource(ComponentPropertySource componentPropertySource)
 298    {
 299  97 _componentPropertySource = componentPropertySource;
 300    }
 301    }