KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > TurbineConfig


1 package org.apache.turbine;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.BufferedInputStream JavaDoc;
58 import java.io.File JavaDoc;
59 import java.io.FileInputStream JavaDoc;
60 import java.io.FileNotFoundException JavaDoc;
61 import java.io.InputStream JavaDoc;
62 import java.net.MalformedURLException JavaDoc;
63 import java.net.URL JavaDoc;
64 import java.util.Enumeration JavaDoc;
65 import java.util.Hashtable JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.Map JavaDoc;
68 import java.util.Set JavaDoc;
69 import java.util.Vector JavaDoc;
70
71 import javax.servlet.RequestDispatcher JavaDoc;
72 import javax.servlet.Servlet JavaDoc;
73 import javax.servlet.ServletConfig JavaDoc;
74 import javax.servlet.ServletContext JavaDoc;
75
76 import org.apache.turbine.Turbine;
77 import org.apache.turbine.RunData;
78 import org.apache.commons.logging.LogFactory;
79 import org.apache.commons.logging.Log;
80
81 /**
82  * A class used for initalization of Turbine without a servlet container.
83  *
84  * If you need to use Turbine outside of a servlet container, you can
85  * use this class for initalization of the Turbine servlet.<br>
86  *
87  * <blockquote><code><pre>
88  * TurbineConfig config = new TurbineConfig(".", "/conf/TurbineResources.properties");
89  * </pre></code></blockquote>
90  *
91  * All paths referenced in TurbineResources.properties and the path to
92  * the properties file itself (the second argument) will be resolved
93  * relative to the directory given as the first argument of the constructor,
94  * here - the directory where application was started. Don't worry about
95  * discarding the references to objects created above. They are not needed,
96  * once everything is initialized.
97  *
98  * In order to initialize the Services Framework outside of the Turbine Servlet,
99  * you need to call the <code>init()</code> method. By default, this will
100  * initialize the Resource and Logging Services and any other services you
101  * have defined in your TurbineResources.properties file.
102  *
103  * @author <a HREF="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
104  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
105  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
106  * @version $Id: TurbineConfig.java,v 1.9 2004/02/02 11:20:13 epugh Exp $
107  */

108 public class TurbineConfig implements ServletConfig JavaDoc, ServletContext JavaDoc
109 {
110     private static final Log log
111         = LogFactory.getLog( TurbineConfig.class );
112
113     /**
114      * Servlet initialization parameter name for the path to
115      * TurbineConfiguration.xml file used by Turbine
116      */

117     public static final String JavaDoc CONFIGURATION_PATH_KEY = "configuration";
118     
119     /**
120      * Servlet initialization parameter name for the path to the
121      * TurbineResources.properties file (<code>properties</code>).
122      */

123     public static final String JavaDoc PROPERTIES_KEY = "properties";
124
125     /** Enables output of debug messages (compile time option). */
126     protected final static boolean DEBUG = false;
127
128     /** Filenames are looked up in this directory. */
129     protected File JavaDoc root;
130
131     /** Servlet container (or emulator) attributes. */
132     protected Map JavaDoc attributes;
133
134     /** Turbine servlet initialization parameters. */
135     protected Map JavaDoc initParams;
136
137     /** The Turbine servlet instance used for initialization. */
138     protected Turbine turbine;
139
140     /**
141      * This is the general form of the constructor. You can provide
142      * a path to search for files, and a name-value map of init
143      * parameters.
144      *
145      * <p> For the list of recognized init parameters, see
146      * {@link org.apache.turbine.Turbine} class.
147      *
148      * @param path The web application root (i.e. the path for file lookup).
149      * @param attributes Servlet container (or emulator) attributes.
150      * @param initParams Global (i.e. psuedo-context wide)
151      * initialization parameters.
152      */

153     public TurbineConfig(String JavaDoc path, Map JavaDoc attributes, Map JavaDoc initParams)
154     {
155         root = new File JavaDoc(path);
156         // Since attributes are read/write, they need thread-safe
157
// storage.
158
this.attributes = (attributes instanceof Hashtable JavaDoc ? attributes :
159                            new Hashtable JavaDoc(attributes));
160         this.initParams = initParams;
161     }
162
163     /**
164      * @see #TurbineConfig(String path, Map attributes, Map initParams)
165      */

166     public TurbineConfig(String JavaDoc path, Map JavaDoc initParams)
167     {
168         this(path, new Hashtable JavaDoc(0), initParams);
169     }
170
171     /**
172      * This is a specialized constructor that allows to configure
173      * Turbine easiliy in the common setups.
174      *
175      * @param path The web application root (i.e. the path for file lookup).
176      * @param properties the relative path to TurbineResources.properties file
177      */

178     public TurbineConfig(String JavaDoc path, String JavaDoc properties)
179     {
180         this(path, new HashMap JavaDoc(1));
181         initParams.put(PROPERTIES_KEY, properties);
182     }
183
184     /**
185      * Causes this class to initialize itself which in turn initializes
186      * all of the Turbine Services that need to be initialized.
187      */

188     public void init()
189     {
190         try
191         {
192             turbine = new Turbine();
193             turbine.init(this);
194         }
195         catch (Exception JavaDoc e)
196         {
197             // Since we failed to initialize, the logger may not be
198
// set up yet.
199
String JavaDoc msg = "TurbineConfig: Initialization failed";
200             try
201             {
202                 log.error(msg, e);
203             }
204             catch (Throwable JavaDoc t)
205             {
206                 System.err.println(msg);
207                 e.printStackTrace();
208                 System.err.println("Couldn't write to log");
209                 t.printStackTrace();
210             }
211         }
212     }
213
214     /**
215      * Initialization requiring a HTTP <code>GET</code> request.
216      */

217     public void init(RunData data)
218     {
219         if (turbine != null)
220         {
221             turbine.init(data);
222         }
223     }
224
225     /**
226      * Returns a reference to the object cast onto ServletContext type.
227      *
228      * @return a ServletContext reference
229      */

230     public ServletContext JavaDoc getServletContext()
231     {
232         return this;
233     }
234
235     /**
236      * Translates a path relative to the web application root into an
237      * absolute path.
238      *
239      * @param path A path relative to the web application root.
240      * @return An absolute version of the supplied path, or <code>null</code>
241      * if the translated path doesn't map to a file or directory.
242      */

243     public String JavaDoc getRealPath( String JavaDoc path )
244     {
245         File JavaDoc f = new File JavaDoc(root, path);
246         if (DEBUG)
247         {
248             System.err.println("TurbineConfig.getRealPath: path '" + path +
249                                "' translated to '" + f.getPath() + "' " +
250                                (f.exists() ? "" : "not ") + "found");
251         }
252         return (f.exists() ? f.getPath() : null);
253     }
254
255     /**
256      * Retrieves an initialization parameter.
257      *
258      * @param name the name of the parameter.
259      * @return the value of the parameter.
260      */

261     public String JavaDoc getInitParameter(String JavaDoc name)
262     {
263         return (String JavaDoc)initParams.get(name);
264     }
265
266     /**
267      * Retrieves an Enumeration of initialization parameter names.
268      *
269      * @return an Enumeration of initialization parameter names.
270      */

271     public Enumeration JavaDoc getInitParameterNames()
272     {
273         return new Vector JavaDoc(initParams.keySet()).elements();
274     }
275
276     /**
277      * Returns the servlet name.
278      *
279      * Fixed value "Turbine" is returned.
280      *
281      * @return the servlet name.
282      */

283     public String JavaDoc getServletName()
284     {
285         return "Turbine";
286     }
287
288     /**
289      * Returns the context name.
290      *
291      * Fixed value "Turbine" is returned
292      *
293      * @return the context name
294      */

295     public String JavaDoc getServletContextName()
296     {
297         return "Turbine";
298     }
299
300     /**
301      * Returns a URL to the resource that is mapped to a specified
302      * path. The path must begin with a "/" and is interpreted
303      * as relative to the current context root.
304      *
305      * @param s the path to the resource
306      * @return a URL pointing to the resource
307      * @exception MalformedURLException
308      */

309     public URL JavaDoc getResource( String JavaDoc s )
310         throws MalformedURLException JavaDoc
311     {
312         return new URL JavaDoc("file://" + getRealPath(s));
313     }
314
315     /**
316      * Returns the resource located at the named path as
317      * an <code>InputStream</code> object.
318      *
319      * @param s the path to the resource
320      * @return an InputStream object from which the resource can be read
321      */

322     public InputStream JavaDoc getResourceAsStream( String JavaDoc s )
323     {
324         try
325         {
326             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(getRealPath(s));
327             return new BufferedInputStream JavaDoc(fis);
328         }
329         catch (FileNotFoundException JavaDoc e)
330         {
331             return null;
332         }
333     }
334
335     /**
336      * Logs an error message.
337      *
338      * @param e an Exception.
339      * @param m a message.
340      * @deprecated
341      */

342     public void log(Exception JavaDoc e, String JavaDoc m)
343     {
344         // cannot use Turbine logging yet.
345
System.err.println(m);
346         e.printStackTrace();
347     }
348
349     /**
350      * Logs a message.
351      *
352      * @param m a message.
353      */

354     public void log(String JavaDoc m)
355     {
356         // cannot use Turbine logging yet.
357
System.out.println(m);
358     }
359
360     /**
361      * Logs an error message.
362      *
363      * @param t a Throwable object.
364      * @param m a message.
365      */

366     public void log( String JavaDoc m, Throwable JavaDoc t )
367     {
368         // cannot use Turbine logging yet.
369
System.err.println(m);
370         t.printStackTrace();
371     }
372
373     /**
374      * Returns the servlet container attribute with the given name, or
375      * null if there is no attribute by that name.
376      */

377     public Object JavaDoc getAttribute(String JavaDoc name)
378     {
379         return attributes.get(name);
380     }
381
382     /**
383      * Returns an Enumeration containing the attribute names available
384      * within this servlet context.
385      */

386     public Enumeration JavaDoc getAttributeNames()
387     {
388         return new Vector JavaDoc(attributes.keySet()).elements();
389     }
390
391     // Unimplemented methods follow
392

393     /**
394      * Not implemented.
395      *
396      * A method in ServletConfig or ServletContext interface that is not
397      * implemented and will throw <code>UnsuportedOperationException</code>
398      * upon invocation
399      */

400     public ServletContext JavaDoc getContext(String JavaDoc s)
401     {
402         throw new UnsupportedOperationException JavaDoc();
403     }
404
405     /**
406      * Not implemented.
407      *
408      * A method in ServletConfig or ServletContext interface that is not
409      * implemented and will throw <code>UnsuportedOperationException</code>
410      * upon invocation
411      */

412     public int getMajorVersion()
413     {
414         throw new UnsupportedOperationException JavaDoc();
415     }
416
417     /**
418      * Not implemented.
419      *
420      * A method in ServletConfig or ServletContext interface that is not
421      * implemented and will throw <code>UnsuportedOperationException</code>
422      * upon invocation
423      */

424     public String JavaDoc getMimeType(String JavaDoc s)
425     {
426         throw new UnsupportedOperationException JavaDoc();
427     }
428
429     /**
430      * Not implemented.
431      *
432      * A method in ServletConfig or ServletContext interface that is not
433      * implemented and will throw <code>UnsuportedOperationException</code>
434      * upon invocation
435      */

436     public int getMinorVersion()
437     {
438         throw new UnsupportedOperationException JavaDoc();
439     }
440
441     /**
442      * Not implemented.
443      *
444      * A method in ServletConfig or ServletContext interface that is not
445      * implemented and will throw <code>UnsuportedOperationException</code>
446      * upon invocation
447      */

448     public RequestDispatcher JavaDoc getNamedDispatcher( String JavaDoc s)
449     {
450         throw new UnsupportedOperationException JavaDoc();
451     }
452
453     /**
454      * Not implemented.
455      *
456      * A method in ServletConfig or ServletContext interface that is not
457      * implemented and will throw <code>UnsuportedOperationException</code>
458      * upon invocation
459      */

460     public RequestDispatcher JavaDoc getRequestDispatcher( String JavaDoc s )
461     {
462         throw new UnsupportedOperationException JavaDoc();
463     }
464
465     /**
466      * Not implemented.
467      *
468      * A method in ServletConfig or ServletContext interface that is not
469      * implemented and will throw <code>UnsuportedOperationException</code>
470      * upon invocation
471      * @deprecated
472      */

473     public Set JavaDoc getResourcePaths()
474     {
475         throw new UnsupportedOperationException JavaDoc();
476     }
477
478     /**
479      * Not implemented.
480      *
481      * A method in ServletConfig or ServletContext interface that is not
482      * implemented and will throw <code>UnsuportedOperationException</code>
483      * upon invocation
484      * @deprecated
485      */

486     public Set JavaDoc getResourcePaths(String JavaDoc s)
487     {
488         throw new UnsupportedOperationException JavaDoc();
489     }
490
491     /**
492      * Not implemented.
493      *
494      * A method in ServletConfig or ServletContext interface that is not
495      * implemented and will throw <code>UnsuportedOperationException</code>
496      * upon invocation
497      */

498     public String JavaDoc getServerInfo()
499     {
500         throw new UnsupportedOperationException JavaDoc();
501     }
502
503     /**
504      * Not implemented.
505      *
506      * A method in ServletConfig or ServletContext interface that is not
507      * implemented and will throw <code>UnsuportedOperationException</code>
508      * upon invocation
509      * @deprecated
510      */

511     public Servlet JavaDoc getServlet(String JavaDoc s)
512     {
513         throw new UnsupportedOperationException JavaDoc();
514     }
515
516     /**
517      * Not implemented.
518      *
519      * A method in ServletConfig or ServletContext interface that is not
520      * implemented and will throw <code>UnsuportedOperationException</code>
521      * upon invocation
522      * @deprecated
523      */

524     public Enumeration JavaDoc getServletNames()
525     {
526         throw new UnsupportedOperationException JavaDoc();
527     }
528
529     /**
530      * Not implemented.
531      *
532      * A method in ServletConfig or ServletContext interface that is not
533      * implemented and will throw <code>UnsuportedOperationException</code>
534      * upon invocation
535      * @deprecated
536      */

537     public Enumeration JavaDoc getServlets()
538     {
539         throw new UnsupportedOperationException JavaDoc();
540     }
541
542     /**
543      * Sets the attribute identified by <code>name</code>.
544      */

545     public void removeAttribute(String JavaDoc name)
546     {
547         attributes.remove(name);
548     }
549
550     /**
551      * Sets the attribute identified by <code>name</code>.
552      */

553     public void setAttribute(String JavaDoc name, Object JavaDoc obj)
554     {
555         if (obj != null)
556         {
557             attributes.put(name, obj);
558         }
559         else
560         {
561             removeAttribute(name);
562         }
563     }
564 }
565
Popular Tags