KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > servlet > TurbineServletService


1 package org.apache.turbine.services.servlet;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
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,v 1.11.2.2 2004/05/20 03:06:49 seade Exp $
44  */

45 public class TurbineServletService
46         extends TurbineBaseService implements ServletService
47 {
48     /** Logging */
49     private static Log log = LogFactory.getLog(TurbineServletService.class);
50
51     /** The servlet context for this servlet */
52     private ServletContext JavaDoc servletContext = null;
53
54     /** The servlet configuration for this servlet */
55     private ServletConfig JavaDoc 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         this.servletConfig = Turbine.getTurbineServletConfig();
65         try
66         {
67             this.servletContext = servletConfig.getServletContext();
68
69             log.debug("Initializing with ServletConfig");
70         }
71         catch (Exception JavaDoc e)
72         {
73             log.error("Cannot initialize TurbineServletService.", e);
74         }
75         setInit(true);
76     }
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 JavaDoc servletConfig)
86     {
87         init();
88     }
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 JavaDoc getResource(String JavaDoc uri)
100     {
101         if (servletContext == null)
102         {
103             return null;
104         }
105
106         URL JavaDoc url = null;
107
108         try
109         {
110             url = getServletContext().getResource(uri);
111             // work-around for Websphere 3.52
112
if (url != null && url.toString().startsWith("classloader:"))
113             {
114                 url = new URL JavaDoc("file:" + url.toString().substring(12));
115             }
116             else if (url == null)
117             {
118                 url = new URL JavaDoc("file:" + getServletContext().getRealPath(uri));
119             }
120         }
121         catch (MalformedURLException JavaDoc e)
122         {
123             //if the URL is wrong, return null
124
}
125
126         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 JavaDoc getResourceAsStream(String JavaDoc uri)
137     {
138         if (servletContext == null)
139         {
140             return null;
141         }
142
143         InputStream JavaDoc is = null;
144         is = servletContext.getResourceAsStream(uri);
145         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 JavaDoc getRealPath(String JavaDoc uri)
157     {
158         if (getServletContext() == null || uri == null)
159         {
160             return null;
161         }
162         else
163         {
164             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 JavaDoc getServletConfig()
175     {
176         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 JavaDoc getServletContext()
186     {
187         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 JavaDoc getServerScheme()
198     {
199         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 JavaDoc getServerName()
210     {
211         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 JavaDoc getServerPort()
222     {
223         return Turbine.getServerPort();
224     }
225
226     /**
227      * Returns the context path for this
228      * Turbine application.
229      *
230      * @return String
231      */

232     public String JavaDoc getContextPath()
233     {
234         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 JavaDoc expandRelative(String JavaDoc path)
247     {
248         return ServletUtils.expandRelative(getServletConfig(), path);
249     }
250 }
251
Popular Tags