View Javadoc

1   package org.apache.turbine.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.BufferedInputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.InputStream;
27  import java.net.MalformedURLException;
28  import java.net.URL;
29  import java.util.Enumeration;
30  import java.util.HashMap;
31  import java.util.Map;
32  import java.util.Set;
33  import java.util.Vector;
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.Servlet;
36  import javax.servlet.ServletConfig;
37  import javax.servlet.ServletContext;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.apache.avalon.framework.activity.Disposable;
42  import org.apache.avalon.framework.activity.Initializable;
43  import org.apache.turbine.Turbine;
44  
45  /***
46   * A class used for initialization of Turbine without a servlet container.
47   * <p>
48   * If you need to use Turbine outside of a servlet container, you can
49   * use this class for initialization of the Turbine servlet.
50   * <p>
51   * <blockquote><code><pre>
52   * TurbineConfig config = new TurbineConfig(".", "conf/TurbineResources.properties");
53   * </pre></code></blockquote>
54   * <p>
55   * All paths referenced in TurbineResources.properties and the path to
56   * the properties file itself (the second argument) will be resolved
57   * relative to the directory given as the first argument of the constructor,
58   * here - the directory where application was started. Don't worry about
59   * discarding the references to objects created above. They are not needed,
60   * once everything is initialized.
61   * <p>
62   * In order to initialize the Services Framework outside of the Turbine Servlet,
63   * you need to call the <code>init()</code> method. By default, this will
64   * initialize the Resource and Logging Services and any other services you
65   * have defined in your TurbineResources.properties file.
66   *
67   * @todo Make this class enforce the lifecycle contracts
68   *
69   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
70   * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
71   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
72   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
73   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
74   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
75   * @version $Id: TurbineConfig.java 534527 2007-05-02 16:10:59Z tv $
76   */
77  public class TurbineConfig
78          implements ServletConfig, ServletContext, Initializable, Disposable
79  {
80      /***
81       * Servlet initialization parameter name for the path to
82       * TurbineConfiguration.xml file used by Turbine
83       */
84      public static final String CONFIGURATION_PATH_KEY = "configuration";
85  
86      /***
87       * Servlet initialization parameter name for the path to
88       * Turbine.properties file used by Turbine
89       */
90      public static final String PROPERTIES_PATH_KEY = "properties";
91  
92      /***
93       * Default value of TurbineResources.properties file path
94       * (<code>/WEB-INF/conf/TurbineResources.properties</code>).
95       */
96      public static final String PROPERTIES_PATH_DEFAULT =
97              "/WEB-INF/conf/TurbineResources.properties";
98  
99      /*** Filenames are looked up in this directory. */
100     protected File root;
101 
102     /*** Servlet container (or emulator) attributes. */
103     protected Map attributes;
104 
105     /*** Turbine servlet initialization parameters. */
106     protected Map initParams;
107 
108     /*** The Turbine servlet instance used for initialization. */
109     private Turbine turbine;
110 
111     /*** Logging */
112     private Log log = LogFactory.getLog(this.getClass());
113 
114     /***
115      * Constructs a new TurbineConfig.
116      *
117      * This is the general form of the constructor. You can provide
118      * a path to search for files, and a name-value map of init
119      * parameters.
120      *
121      * <p> For the list of recognized init parameters, see
122      * {@link org.apache.turbine.Turbine} class.
123      *
124      * @param path The web application root (i.e. the path for file lookup).
125      * @param attributes Servlet container (or emulator) attributes.
126      * @param initParams initialization parameters.
127      */
128     public TurbineConfig(String path, Map attributes, Map initParams)
129     {
130         root = new File(path);
131         this.attributes = attributes;
132         this.initParams = initParams;
133     }
134 
135     /***
136      * @see #TurbineConfig(String path, Map attributes, Map initParams)
137      */
138     public TurbineConfig(String path, Map initParams)
139     {
140         this(path, new HashMap(0), initParams);
141     }
142 
143     /***
144      * Constructs a TurbineConfig.
145      *
146      * This is a specialized constructor that allows to configure
147      * Turbine easiliy in the common setups.
148      *
149      * @param path The web application root (i.e. the path for file lookup).
150      * @param properties the relative path to TurbineResources.properties file
151      */
152     public TurbineConfig(String path, String properties)
153     {
154         this(path, new HashMap(1));
155         initParams.put(PROPERTIES_PATH_KEY, properties);
156     }
157 
158     /***
159      * Causes this class to initialize itself which in turn initializes
160      * all of the Turbine Services that need to be initialized.
161      *
162      * @see org.apache.stratum.lifecycle.Initializable
163      */
164     public void initialize()
165     {
166         try
167         {
168             turbine = new Turbine();
169             turbine.init(this);
170         }
171         catch (Exception e)
172         {
173             log.error("TurbineConfig: Initialization failed", e);
174         }
175     }
176 
177     /***
178      * Initialization requiring a HTTP <code>GET</code> request.
179      */
180     public void init(RunData data)
181     {
182         if (turbine != null)
183         {
184             turbine.init(data);
185         }
186     }
187 
188     /***
189      * Shutdown the Turbine System, lifecycle style
190      *
191      */
192     public void dispose()
193     {
194         if (turbine != null)
195         {
196             turbine.destroy();
197         }
198     }
199 
200     /***
201      * Returns a reference to the object cast onto ServletContext type.
202      *
203      * @return a ServletContext reference
204      */
205     public ServletContext getServletContext()
206     {
207         return this;
208     }
209 
210     /***
211      * Translates a path relative to the web application root into an
212      * absolute path.
213      *
214      * @param path A path relative to the web application root.
215      * @return An absolute version of the supplied path, or <code>null</code>
216      * if the translated path doesn't map to a file or directory.
217      */
218     public String getRealPath(String path)
219     {
220         String result = null;
221         File f = new File(root, path);
222 
223         if (log.isDebugEnabled())
224         {
225             StringBuffer sb = new StringBuffer();
226 
227             sb.append("TurbineConfig.getRealPath: path '");
228             sb.append(path);
229             sb.append("' translated to '");
230             sb.append(f.getPath());
231             sb.append("' ");
232             sb.append(f.exists() ? "" : "not ");
233             sb.append("found");
234             log.debug(sb.toString());
235         }
236 
237         if (f.exists())
238         {
239           result = f.getPath();
240         }
241         else
242         {
243             log.error("getRealPath(\"" + path + "\") is undefined, returning null");
244         }
245 
246         return result;
247     }
248 
249     /***
250      * Retrieves an initialization parameter.
251      *
252      * @param name the name of the parameter.
253      * @return the value of the parameter.
254      */
255     public String getInitParameter(String name)
256     {
257         return (String) initParams.get(name);
258     }
259 
260     /***
261      * Retrieves an Enumeration of initialization parameter names.
262      *
263      * @return an Enumeration of initialization parameter names.
264      */
265     public Enumeration getInitParameterNames()
266     {
267         return new Vector(initParams.keySet()).elements();
268     }
269 
270     /***
271      * Returns the servlet name.
272      *
273      * Fixed value "Turbine" is returned.
274      *
275      * @return the servlet name.
276      */
277     public String getServletName()
278     {
279         return "Turbine";
280     }
281 
282     /***
283      * Returns the context name.
284      *
285      * Fixed value "Turbine" is returned
286      *
287      * @return the context name
288      */
289     public String getServletContextName()
290     {
291         return "Turbine";
292     }
293 
294     /***
295      * Returns a URL to the resource that is mapped to a specified
296      * path. The path must begin with a "/" and is interpreted
297      * as relative to the current context root.
298      *
299      * @param s the path to the resource
300      * @return a URL pointing to the resource
301      * @exception MalformedURLException
302      */
303     public URL getResource(String s)
304             throws MalformedURLException
305     {
306         return new URL("file://" + getRealPath(s));
307     }
308 
309     /***
310      * Returns the resource located at the named path as
311      * an <code>InputStream</code> object.
312      *
313      * @param s the path to the resource
314      * @return an InputStream object from which the resource can be read
315      */
316     public InputStream getResourceAsStream(String s)
317     {
318         try
319         {
320             FileInputStream fis = new FileInputStream(getRealPath(s));
321             return new BufferedInputStream(fis);
322         }
323         catch (FileNotFoundException e)
324         {
325             return null;
326         }
327     }
328 
329     /***
330      * Logs an error message.
331      *
332      * @param e an Exception.
333      * @param m a message.
334      * @deprecated use log(String,Throwable) instead
335      */
336     public void log(Exception e, String m)
337     {
338         log.info(m, e);
339     }
340 
341     /***
342      * Logs a message.
343      *
344      * @param m a message.
345      */
346     public void log(String m)
347     {
348         log.info(m);
349     }
350 
351     /***
352      * Logs an error message.
353      *
354      * @param t a Throwable object.
355      * @param m a message.
356      */
357     public void log(String m, Throwable t)
358     {
359         log.info(m, t);
360     }
361 
362     /***
363      * Returns the servlet container attribute with the given name, or
364      * null if there is no attribute by that name.
365      */
366     public Object getAttribute(String s)
367     {
368         return attributes.get(s);
369     }
370 
371     /***
372      * Returns an Enumeration containing the attribute names available
373      * within this servlet context.
374      */
375     public Enumeration getAttributeNames()
376     {
377         return new Vector(attributes.keySet()).elements();
378     }
379 
380     // Unimplemented methods follow
381 
382     /***
383      * Not implemented.
384      *
385      * A method in ServletConfig or ServletContext interface that is not
386      * implemented and will throw <code>UnsuportedOperationException</code>
387      * upon invocation
388      */
389     public ServletContext getContext(String s)
390     {
391         throw new UnsupportedOperationException();
392     }
393 
394     /***
395      * Not implemented.
396      *
397      * A method in ServletConfig or ServletContext interface that is not
398      * implemented and will throw <code>UnsuportedOperationException</code>
399      * upon invocation
400      */
401     public int getMajorVersion()
402     {
403         throw new UnsupportedOperationException();
404     }
405 
406     /***
407      * Not implemented.
408      *
409      * A method in ServletConfig or ServletContext interface that is not
410      * implemented and will throw <code>UnsuportedOperationException</code>
411      * upon invocation
412      */
413     public String getMimeType(String s)
414     {
415         throw new UnsupportedOperationException();
416     }
417 
418     /***
419      * Not implemented.
420      *
421      * A method in ServletConfig or ServletContext interface that is not
422      * implemented and will throw <code>UnsuportedOperationException</code>
423      * upon invocation
424      */
425     public int getMinorVersion()
426     {
427         throw new UnsupportedOperationException();
428     }
429 
430     /***
431      * Not implemented.
432      *
433      * A method in ServletConfig or ServletContext interface that is not
434      * implemented and will throw <code>UnsuportedOperationException</code>
435      * upon invocation
436      */
437     public RequestDispatcher getNamedDispatcher(String s)
438     {
439         throw new UnsupportedOperationException();
440     }
441 
442     /***
443      * Not implemented.
444      *
445      * A method in ServletConfig or ServletContext interface that is not
446      * implemented and will throw <code>UnsuportedOperationException</code>
447      * upon invocation
448      */
449     public RequestDispatcher getRequestDispatcher(String s)
450     {
451         throw new UnsupportedOperationException();
452     }
453 
454     /***
455      * Not implemented.
456      *
457      * A method in ServletContext (2.3) interface that is not implemented and
458      * will throw <code>UnsuportedOperationException</code> upon invocation
459      */
460     public Set getResourcePaths(String s)
461     {
462         throw new UnsupportedOperationException();
463     }
464 
465     /***
466      * Not implemented.
467      *
468      * A method in ServletContext (2.3) interface that is not implemented and
469      * will throw <code>UnsuportedOperationException</code> upon invocation
470      */
471     public String getServerInfo()
472     {
473         throw new UnsupportedOperationException();
474     }
475 
476     /***
477      * Not implemented.
478      *
479      * A method in ServletContext interface that is not implemented and will
480      * throw <code>UnsuportedOperationException</code> upon invocation
481      * @deprecated As of Java Servlet API 2.1, with no direct replacement.
482      */
483     public Servlet getServlet(String s)
484     {
485         throw new UnsupportedOperationException();
486     }
487 
488     /***
489      * Not implemented.
490      *
491      * A method in ServletContext interface that is not implemented and will
492      * throw <code>UnsuportedOperationException</code> upon invocation
493      * @deprecated As of Java Servlet API 2.1, with no replacement.
494      */
495     public Enumeration getServletNames()
496     {
497         throw new UnsupportedOperationException();
498     }
499 
500     /***
501      * Not implemented.
502      *
503      * A method in ServletContext interface that is not implemented and will
504      * throw <code>UnsuportedOperationException</code> upon invocation
505      * @deprecated As of Java Servlet API 2.0, with no replacement.
506      */
507     public Enumeration getServlets()
508     {
509         throw new UnsupportedOperationException();
510     }
511 
512     /***
513      * Not implemented.
514      *
515      * A method in ServletContext interface that is not implemented and will
516      * throw <code>UnsuportedOperationException</code> upon invocation
517      */
518     public void removeAttribute(String s)
519     {
520         throw new UnsupportedOperationException();
521     }
522 
523     /***
524      * Not implemented.
525      *
526      * A method in ServletContext interface that is not implemented and will
527      * throw <code>UnsuportedOperationException</code> upon invocation
528      */
529     public void setAttribute(String s, Object o)
530     {
531         throw new UnsupportedOperationException();
532     }
533 }