1 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 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 public void initialize(HttpServlet servlet) 51 { 52 IApplicationSpecification spec = null; 53 54 Resource specResource = findApplicationSpecification(servlet); 55 56 if (specResource == null) 57 { 58 _log.debug(ImplMessages.noApplicationSpecification(servlet)); 59 60 spec = constructStandinSpecification(servlet); 61 } 62 else 63 spec = _parser.parseApplicationSpecification(specResource); 64 65 _globals.storeActivator(new HttpServletWebActivator(servlet)); 66 _globals.storeSpecification(spec); 67 } 68 69 private Resource findApplicationSpecification(HttpServlet servlet) 70 { 71 String path = servlet.getInitParameter(APP_SPEC_PATH_PARAM); 72 73 if (path != null) 74 return _classpathResourceFactory.newResource(path); 75 76 ServletContext context = servlet.getServletContext(); 77 String servletName = servlet.getServletName(); 78 String expectedName = servletName + ".application"; 79 80 Resource webInfLocation = new ContextResource(context, "/WEB-INF/"); 81 Resource webInfAppLocation = webInfLocation.getRelativeResource(servletName + "/"); 82 83 Resource result = check(webInfAppLocation, expectedName); 84 if (result != null) 85 return result; 86 87 return check(webInfLocation, expectedName); 88 } 89 90 private Resource check(Resource resource, String name) 91 { 92 Resource result = resource.getRelativeResource(name); 93 94 if (_log.isDebugEnabled()) 95 _log.debug("Checking for existence of " + result); 96 97 if (result.getResourceURL() != null) 98 { 99 _log.debug("Found " + result); 100 return result; 101 } 102 103 return null; 104 } 105 106 private IApplicationSpecification constructStandinSpecification(HttpServlet servlet) 107 { 108 String servletName = servlet.getServletName(); 109 110 ApplicationSpecification result = new ApplicationSpecification(); 111 112 114 Resource virtualLocation = new ContextResource(servlet.getServletContext(), "/WEB-INF/" 115 + servletName + ".application"); 116 117 result.setSpecificationLocation(virtualLocation); 118 119 result.setName(servletName); 120 121 return result; 122 } 123 124 public void setClasspathResourceFactory(ClasspathResourceFactory factory) 125 { 126 _classpathResourceFactory = factory; 127 } 128 129 public void setLog(Log log) 130 { 131 _log = log; 132 } 133 134 public void setGlobals(ApplicationGlobals globals) 135 { 136 _globals = globals; 137 } 138 139 public void setParser(ISpecificationParser parser) 140 { 141 _parser = parser; 142 } 143 144 } | Popular Tags |