KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > ApplicationSpecificationInitializer


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 JavaDoc;
18 import javax.servlet.http.HttpServlet JavaDoc;
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 JavaDoc APP_SPEC_PATH_PARAM = "org.apache.tapestry.application-specification";
49
50     public void initialize(HttpServlet JavaDoc 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 JavaDoc servlet)
70     {
71         String JavaDoc path = servlet.getInitParameter(APP_SPEC_PATH_PARAM);
72
73         if (path != null)
74             return _classpathResourceFactory.newResource(path);
75
76         ServletContext JavaDoc context = servlet.getServletContext();
77         String JavaDoc servletName = servlet.getServletName();
78         String JavaDoc 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 JavaDoc 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 JavaDoc servlet)
107     {
108         String JavaDoc servletName = servlet.getServletName();
109
110         ApplicationSpecification result = new ApplicationSpecification();
111
112         // Pretend the file exists in the most common expected location.
113

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