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