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