|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ApplicationSpecificationInitializer.java | 100% | 100% | 100% | 100% |
|
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 | 50 |
public void initialize(HttpServlet servlet) |
51 |
{ |
|
52 | 50 |
IApplicationSpecification spec = null;
|
53 |
|
|
54 | 50 |
Resource specResource = findApplicationSpecification(servlet); |
55 |
|
|
56 | 50 |
if (specResource == null) |
57 |
{ |
|
58 | 17 |
_log.debug(ImplMessages.noApplicationSpecification(servlet)); |
59 |
|
|
60 | 17 |
spec = constructStandinSpecification(servlet); |
61 |
} |
|
62 |
else
|
|
63 | 33 |
spec = _parser.parseApplicationSpecification(specResource); |
64 |
|
|
65 | 50 |
_globals.storeActivator(new HttpServletWebActivator(servlet));
|
66 | 50 |
_globals.storeSpecification(spec); |
67 |
} |
|
68 |
|
|
69 | 50 |
private Resource findApplicationSpecification(HttpServlet servlet)
|
70 |
{ |
|
71 | 50 |
String path = servlet.getInitParameter(APP_SPEC_PATH_PARAM); |
72 |
|
|
73 | 50 |
if (path != null) |
74 | 26 |
return _classpathResourceFactory.newResource(path);
|
75 |
|
|
76 | 24 |
ServletContext context = servlet.getServletContext(); |
77 | 24 |
String servletName = servlet.getServletName(); |
78 | 24 |
String expectedName = servletName + ".application";
|
79 |
|
|
80 | 24 |
Resource webInfLocation = new ContextResource(context, "/WEB-INF/"); |
81 | 24 |
Resource webInfAppLocation = webInfLocation.getRelativeResource(servletName + "/");
|
82 |
|
|
83 | 24 |
Resource result = check(webInfAppLocation, expectedName); |
84 | 24 |
if (result != null) |
85 | 2 |
return result;
|
86 |
|
|
87 | 22 |
return check(webInfLocation, expectedName);
|
88 |
} |
|
89 |
|
|
90 | 46 |
private Resource check(Resource resource, String name)
|
91 |
{ |
|
92 | 46 |
Resource result = resource.getRelativeResource(name); |
93 |
|
|
94 | 46 |
if (_log.isDebugEnabled())
|
95 | 1 |
_log.debug("Checking for existence of " + result);
|
96 |
|
|
97 | 46 |
if (result.getResourceURL() != null) |
98 |
{ |
|
99 | 7 |
_log.debug("Found " + result);
|
100 | 7 |
return result;
|
101 |
} |
|
102 |
|
|
103 | 39 |
return null; |
104 |
} |
|
105 |
|
|
106 | 17 |
private IApplicationSpecification constructStandinSpecification(HttpServlet servlet)
|
107 |
{ |
|
108 | 17 |
String servletName = servlet.getServletName(); |
109 |
|
|
110 | 17 |
ApplicationSpecification result = new ApplicationSpecification();
|
111 |
|
|
112 |
// Pretend the file exists in the most common expected location.
|
|
113 |
|
|
114 | 17 |
Resource virtualLocation = new ContextResource(servlet.getServletContext(), "/WEB-INF/" |
115 |
+ servletName + ".application");
|
|
116 |
|
|
117 | 17 |
result.setSpecificationLocation(virtualLocation); |
118 |
|
|
119 | 17 |
result.setName(servletName); |
120 |
|
|
121 | 17 |
return result;
|
122 |
} |
|
123 |
|
|
124 | 50 |
public void setClasspathResourceFactory(ClasspathResourceFactory factory) |
125 |
{ |
|
126 | 50 |
_classpathResourceFactory = factory; |
127 |
} |
|
128 |
|
|
129 | 50 |
public void setLog(Log log) |
130 |
{ |
|
131 | 50 |
_log = log; |
132 |
} |
|
133 |
|
|
134 | 50 |
public void setGlobals(ApplicationGlobals globals) |
135 |
{ |
|
136 | 50 |
_globals = globals; |
137 |
} |
|
138 |
|
|
139 | 49 |
public void setParser(ISpecificationParser parser) |
140 |
{ |
|
141 | 49 |
_parser = parser; |
142 |
} |
|
143 |
|
|
144 |
} |
|