View Javadoc

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