%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.servlet.TurbineServletService |
|
|
1 | package org.apache.turbine.services.servlet; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.InputStream; |
|
20 | import java.net.MalformedURLException; |
|
21 | import java.net.URL; |
|
22 | import javax.servlet.ServletConfig; |
|
23 | import javax.servlet.ServletContext; |
|
24 | ||
25 | import org.apache.commons.logging.Log; |
|
26 | import org.apache.commons.logging.LogFactory; |
|
27 | import org.apache.turbine.Turbine; |
|
28 | import org.apache.turbine.services.TurbineBaseService; |
|
29 | import org.apache.turbine.util.ServletUtils; |
|
30 | ||
31 | /** |
|
32 | * <p>This class provides a context service when the application |
|
33 | * is run in a ServletContainer. It is mainly a wrapper around |
|
34 | * the ServletContext API.</p> |
|
35 | * <p>This class requires Servlet API 2.1 or better.</p> |
|
36 | * |
|
37 | * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a> |
|
38 | * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a> |
|
39 | * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a> |
|
40 | * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a> |
|
41 | * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a> |
|
42 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> |
|
43 | * @version $Id: TurbineServletService.java 278958 2005-09-06 09:35:39Z henning $ |
|
44 | */ |
|
45 | 64 | public class TurbineServletService |
46 | extends TurbineBaseService implements ServletService |
|
47 | { |
|
48 | /** Logging */ |
|
49 | 48 | private static Log log = LogFactory.getLog(TurbineServletService.class); |
50 | ||
51 | /** The servlet context for this servlet */ |
|
52 | 32 | private ServletContext servletContext = null; |
53 | ||
54 | /** The servlet configuration for this servlet */ |
|
55 | 32 | private ServletConfig servletConfig = null; |
56 | ||
57 | /** |
|
58 | * Load all configured components and initialize them. This is |
|
59 | * a zero parameter variant which queries the Turbine Servlet |
|
60 | * for its config. |
|
61 | */ |
|
62 | public void init() |
|
63 | { |
|
64 | 26 | this.servletConfig = Turbine.getTurbineServletConfig(); |
65 | try |
|
66 | { |
|
67 | 26 | this.servletContext = servletConfig.getServletContext(); |
68 | ||
69 | 26 | log.debug("Initializing with ServletConfig"); |
70 | 13 | } |
71 | 0 | catch (Exception e) |
72 | { |
|
73 | 0 | log.error("Cannot initialize TurbineServletService.", e); |
74 | 13 | } |
75 | 26 | setInit(true); |
76 | 26 | } |
77 | ||
78 | /** |
|
79 | * Called during Turbine.init() |
|
80 | * |
|
81 | * @param servletConfig A ServletConfig. |
|
82 | * |
|
83 | * @deprecated use init() instead. |
|
84 | */ |
|
85 | public void init(ServletConfig servletConfig) |
|
86 | { |
|
87 | 0 | init(); |
88 | 0 | } |
89 | ||
90 | /** |
|
91 | * Returns an URL object for a given URI string. |
|
92 | * This URI is considered relative to the context. |
|
93 | * |
|
94 | * @see javax.servlet.ServletContext#getResource |
|
95 | * @param uri the URI to resolve as an URL |
|
96 | * @return an URL object or null is the uri is malformed or |
|
97 | * can't be resolved |
|
98 | */ |
|
99 | public URL getResource(String uri) |
|
100 | { |
|
101 | 0 | if (servletContext == null) |
102 | { |
|
103 | 0 | return null; |
104 | } |
|
105 | ||
106 | 0 | URL url = null; |
107 | ||
108 | try |
|
109 | { |
|
110 | 0 | url = getServletContext().getResource(uri); |
111 | // work-around for Websphere 3.52 |
|
112 | 0 | if (url != null && url.toString().startsWith("classloader:")) |
113 | { |
|
114 | 0 | url = new URL("file:" + url.toString().substring(12)); |
115 | } |
|
116 | 0 | else if (url == null) |
117 | { |
|
118 | 0 | url = new URL("file:" + getServletContext().getRealPath(uri)); |
119 | } |
|
120 | } |
|
121 | 0 | catch (MalformedURLException e) |
122 | { |
|
123 | //if the URL is wrong, return null |
|
124 | 0 | } |
125 | ||
126 | 0 | return url; |
127 | } |
|
128 | ||
129 | /** |
|
130 | * Same as getResource except that it returns an InputStream |
|
131 | * |
|
132 | * @see javax.servlet.ServletContext#getResourceAsStream |
|
133 | * @param uri the URI to resolve |
|
134 | * @return an InputStream on the URI content or null |
|
135 | */ |
|
136 | public InputStream getResourceAsStream(String uri) |
|
137 | { |
|
138 | 34 | if (servletContext == null) |
139 | { |
|
140 | 0 | return null; |
141 | } |
|
142 | ||
143 | 34 | InputStream is = null; |
144 | 34 | is = servletContext.getResourceAsStream(uri); |
145 | 0 | return is; |
146 | } |
|
147 | ||
148 | /** |
|
149 | * Returns the complete filesystem path for a |
|
150 | * given URI |
|
151 | * |
|
152 | * @see javax.servlet.ServletContext#getRealPath |
|
153 | * @param uri the URI to resolve |
|
154 | * @return the full system path of this URI |
|
155 | */ |
|
156 | public String getRealPath(String uri) |
|
157 | { |
|
158 | 0 | if (getServletContext() == null || uri == class="keyword">null) |
159 | { |
|
160 | 0 | return null; |
161 | } |
|
162 | else |
|
163 | { |
|
164 | 0 | return getServletContext().getRealPath(uri); |
165 | } |
|
166 | } |
|
167 | ||
168 | /** |
|
169 | * Returns the servlet config used by this |
|
170 | * Turbine web application. |
|
171 | * |
|
172 | * @return turbine servlet config |
|
173 | */ |
|
174 | public ServletConfig getServletConfig() |
|
175 | { |
|
176 | 0 | return servletConfig; |
177 | } |
|
178 | ||
179 | /** |
|
180 | * Returns the servlet context used by this |
|
181 | * Turbine web application. |
|
182 | * |
|
183 | * @return turbine servlet context |
|
184 | */ |
|
185 | public ServletContext getServletContext() |
|
186 | { |
|
187 | 0 | return servletContext; |
188 | } |
|
189 | ||
190 | /** |
|
191 | * Returns the server scheme for this |
|
192 | * Turbine application. This will either |
|
193 | * be http or https. |
|
194 | * |
|
195 | * @return String |
|
196 | */ |
|
197 | public String getServerScheme() |
|
198 | { |
|
199 | 0 | return Turbine.getServerScheme(); |
200 | } |
|
201 | ||
202 | /** |
|
203 | * Returns the server name that this |
|
204 | * Turbine application is running |
|
205 | * on. |
|
206 | * |
|
207 | * @return String |
|
208 | */ |
|
209 | public String getServerName() |
|
210 | { |
|
211 | 0 | return Turbine.getServerName(); |
212 | } |
|
213 | ||
214 | /** |
|
215 | * Returns the port that this Turbine |
|
216 | * application is running through |
|
217 | * on the server. |
|
218 | * |
|
219 | * @return String |
|
220 | */ |
|
221 | public String getServerPort() |
|
222 | { |
|
223 | 0 | return Turbine.getServerPort(); |
224 | } |
|
225 | ||
226 | /** |
|
227 | * Returns the context path for this |
|
228 | * Turbine application. |
|
229 | * |
|
230 | * @return String |
|
231 | */ |
|
232 | public String getContextPath() |
|
233 | { |
|
234 | 0 | return Turbine.getContextPath(); |
235 | } |
|
236 | ||
237 | /** |
|
238 | * Expands a string that points to a relative path or path list, |
|
239 | * leaving it as an absolute path based on the servlet context. |
|
240 | * It will return null if the text is empty or the config object |
|
241 | * is null. |
|
242 | * |
|
243 | * @param path The String containing a path or path list. |
|
244 | * @return A String with the expanded path or path list. |
|
245 | */ |
|
246 | public String expandRelative(String path) |
|
247 | { |
|
248 | 0 | return ServletUtils.expandRelative(getServletConfig(), path); |
249 | } |
|
250 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |