Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 144   Methods: 8
NCLOC: 87   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ApplicationSpecificationInitializer.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.services.impl;
 16   
 17    import javax.servlet.ServletContext;
 18    import javax.servlet.http.HttpServlet;
 19   
 20    import org.apache.commons.logging.Log;
 21    import org.apache.hivemind.Resource;
 22    import org.apache.hivemind.util.ContextResource;
 23    import org.apache.tapestry.parse.ISpecificationParser;
 24    import org.apache.tapestry.services.ApplicationGlobals;
 25    import org.apache.tapestry.services.ApplicationInitializer;
 26    import org.apache.tapestry.services.ClasspathResourceFactory;
 27    import org.apache.tapestry.spec.ApplicationSpecification;
 28    import org.apache.tapestry.spec.IApplicationSpecification;
 29    import org.apache.tapestry.web.HttpServletWebActivator;
 30   
 31    /**
 32    * Locates the application specification and informs the
 33    * {@link org.apache.tapestry.services.ServletInfo}service about it.
 34    *
 35    * @author Howard Lewis Ship
 36    * @since 4.0
 37    */
 38    public class ApplicationSpecificationInitializer implements ApplicationInitializer
 39    {
 40    private Log _log;
 41   
 42    private ClasspathResourceFactory _classpathResourceFactory;
 43   
 44    private ApplicationGlobals _globals;
 45   
 46    private ISpecificationParser _parser;
 47   
 48    public static final String APP_SPEC_PATH_PARAM = "org.apache.tapestry.application-specification";
 49   
 50  46 public void initialize(HttpServlet servlet)
 51    {
 52  46 IApplicationSpecification spec = null;
 53   
 54  46 Resource specResource = findApplicationSpecification(servlet);
 55   
 56  46 if (specResource == null)
 57    {
 58  15 _log.debug(ImplMessages.noApplicationSpecification(servlet));
 59   
 60  15 spec = constructStandinSpecification(servlet);
 61    }
 62    else
 63  31 spec = _parser.parseApplicationSpecification(specResource);
 64   
 65  46 _globals.storeActivator(new HttpServletWebActivator(servlet));
 66  46 _globals.storeSpecification(spec);
 67    }
 68   
 69  46 private Resource findApplicationSpecification(HttpServlet servlet)
 70    {
 71  46 String path = servlet.getInitParameter(APP_SPEC_PATH_PARAM);
 72   
 73  46 if (path != null)
 74  24 return _classpathResourceFactory.newResource(path);
 75   
 76  22 ServletContext context = servlet.getServletContext();
 77  22 String servletName = servlet.getServletName();
 78  22 String expectedName = servletName + ".application";
 79   
 80  22 Resource webInfLocation = new ContextResource(context, "/WEB-INF/");
 81  22 Resource webInfAppLocation = webInfLocation.getRelativeResource(servletName + "/");
 82   
 83  22 Resource result = check(webInfAppLocation, expectedName);
 84  22 if (result != null)
 85  2 return result;
 86   
 87  20 return check(webInfLocation, expectedName);
 88    }
 89   
 90  42 private Resource check(Resource resource, String name)
 91    {
 92  42 Resource result = resource.getRelativeResource(name);
 93   
 94  42 if (_log.isDebugEnabled())
 95  1 _log.debug("Checking for existence of " + result);
 96   
 97  42 if (result.getResourceURL() != null)
 98    {
 99  7 _log.debug("Found " + result);
 100  7 return result;
 101    }
 102   
 103  35 return null;
 104    }
 105   
 106  15 private IApplicationSpecification constructStandinSpecification(HttpServlet servlet)
 107    {
 108  15 String servletName = servlet.getServletName();
 109   
 110  15 ApplicationSpecification result = new ApplicationSpecification();
 111   
 112    // Pretend the file exists in the most common expected location.
 113   
 114  15 Resource virtualLocation = new ContextResource(servlet.getServletContext(), "/WEB-INF/"
 115    + servletName + ".application");
 116   
 117  15 result.setSpecificationLocation(virtualLocation);
 118   
 119  15 result.setName(servletName);
 120   
 121  15 return result;
 122    }
 123   
 124  46 public void setClasspathResourceFactory(ClasspathResourceFactory factory)
 125    {
 126  46 _classpathResourceFactory = factory;
 127    }
 128   
 129  46 public void setLog(Log log)
 130    {
 131  46 _log = log;
 132    }
 133   
 134  46 public void setGlobals(ApplicationGlobals globals)
 135    {
 136  46 _globals = globals;
 137    }
 138   
 139  45 public void setParser(ISpecificationParser parser)
 140    {
 141  45 _parser = parser;
 142    }
 143   
 144    }