Coverage Report - org.apache.tapestry.services.impl.ApplicationSpecificationInitializer
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationSpecificationInitializer
100% 
100% 
2.5
 
 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 servlet service about it.
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  6
 public class ApplicationSpecificationInitializer implements ApplicationInitializer
 38  
 {
 39  
     public static final String APP_SPEC_PATH_PARAM = "org.apache.tapestry.application-specification";
 40  
 
 41  
     private Log _log;
 42  
 
 43  
     private ClasspathResourceFactory _classpathResourceFactory;
 44  
 
 45  
     private ApplicationGlobals _globals;
 46  
 
 47  
     private ISpecificationParser _parser;
 48  
 
 49  
     public void initialize(HttpServlet servlet)
 50  
     {
 51  6
         IApplicationSpecification spec = null;
 52  
 
 53  6
         Resource specResource = findApplicationSpecification(servlet);
 54  
 
 55  6
         if (specResource == null)
 56  
         {
 57  1
             _log.warn(ImplMessages.noApplicationSpecification(servlet));
 58  
             
 59  1
             spec = constructStandinSpecification(servlet);
 60  
         } else
 61  5
             spec = _parser.parseApplicationSpecification(specResource);
 62  
         
 63  6
         _globals.storeActivator(new HttpServletWebActivator(servlet));
 64  6
         _globals.storeSpecification(spec);
 65  6
     }
 66  
 
 67  
     private Resource findApplicationSpecification(HttpServlet servlet)
 68  
     {
 69  6
         String path = servlet.getInitParameter(APP_SPEC_PATH_PARAM);
 70  
 
 71  6
         if (path != null)
 72  1
             return _classpathResourceFactory.newResource(path);
 73  
 
 74  5
         ServletContext context = servlet.getServletContext();
 75  5
         String servletName = servlet.getServletName();
 76  5
         String expectedName = servletName + ".application";
 77  
 
 78  5
         Resource webInfLocation = new ContextResource(context, "/WEB-INF/");
 79  5
         Resource webInfAppLocation = webInfLocation.getRelativeResource(servletName + "/");
 80  
 
 81  5
         Resource result = check(webInfAppLocation, expectedName);
 82  5
         if (result != null)
 83  2
             return result;
 84  
         
 85  3
         result = check(webInfLocation, expectedName);
 86  3
         if (result != null)
 87  1
             return result;
 88  
         
 89  
         // Now look for it in classpath, just in case
 90  
         
 91  2
         result = _classpathResourceFactory.newResource(expectedName);
 92  2
         if (result != null && result.getResourceURL() != null)
 93  1
             return result;
 94  
         
 95  1
         return null;
 96  
     }
 97  
 
 98  
     private Resource check(Resource resource, String name)
 99  
     {
 100  8
         Resource result = resource.getRelativeResource(name);
 101  
 
 102  8
         if (_log.isDebugEnabled())
 103  3
             _log.debug("Checking for existence of " + result);
 104  
 
 105  8
         if (result.getResourceURL() != null)
 106  
         {
 107  3
             _log.debug("Found " + result);
 108  3
             return result;
 109  
         }
 110  
 
 111  5
         return null;
 112  
     }
 113  
 
 114  
     private IApplicationSpecification constructStandinSpecification(HttpServlet servlet)
 115  
     {
 116  1
         String servletName = servlet.getServletName();
 117  
 
 118  1
         ApplicationSpecification result = new ApplicationSpecification();
 119  
 
 120  
         // Pretend the file exists in the most common expected location.
 121  
 
 122  1
         Resource virtualLocation = new ContextResource(servlet.getServletContext(), "/WEB-INF/" + servletName + ".application");
 123  
 
 124  1
         result.setSpecificationLocation(virtualLocation);
 125  
 
 126  1
         result.setName(servletName);
 127  
 
 128  1
         return result;
 129  
     }
 130  
 
 131  
     public void setClasspathResourceFactory(ClasspathResourceFactory factory)
 132  
     {
 133  6
         _classpathResourceFactory = factory;
 134  6
     }
 135  
 
 136  
     public void setLog(Log log)
 137  
     {
 138  6
         _log = log;
 139  6
     }
 140  
 
 141  
     public void setGlobals(ApplicationGlobals globals)
 142  
     {
 143  6
         _globals = globals;
 144  6
     }
 145  
 
 146  
     public void setParser(ISpecificationParser parser)
 147  
     {
 148  5
         _parser = parser;
 149  5
     }
 150  
 
 151  
 }