001: package org.apache.turbine.util;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.BufferedInputStream;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.InputStream;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Enumeration;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Set;
030: import java.util.Vector;
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletConfig;
034: import javax.servlet.ServletContext;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.avalon.framework.activity.Disposable;
039: import org.apache.avalon.framework.activity.Initializable;
040: import org.apache.turbine.Turbine;
041:
042: /**
043: * A class used for initialization of Turbine without a servlet container.
044: * <p>
045: * If you need to use Turbine outside of a servlet container, you can
046: * use this class for initialization of the Turbine servlet.
047: * <p>
048: * <blockquote><code><pre>
049: * TurbineConfig config = new TurbineConfig(".", "conf/TurbineResources.properties");
050: * </pre></code></blockquote>
051: * <p>
052: * All paths referenced in TurbineResources.properties and the path to
053: * the properties file itself (the second argument) will be resolved
054: * relative to the directory given as the first argument of the constructor,
055: * here - the directory where application was started. Don't worry about
056: * discarding the references to objects created above. They are not needed,
057: * once everything is initialized.
058: * <p>
059: * In order to initialize the Services Framework outside of the Turbine Servlet,
060: * you need to call the <code>init()</code> method. By default, this will
061: * initialize the Resource and Logging Services and any other services you
062: * have defined in your TurbineResources.properties file.
063: *
064: * @todo Make this class enforce the lifecycle contracts
065: *
066: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
067: * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
068: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
069: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
070: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
071: * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
072: * @version $Id: TurbineConfig.java 264148 2005-08-29 14:21:04Z henning $
073: */
074: public class TurbineConfig implements ServletConfig, ServletContext,
075: Initializable, Disposable {
076: /**
077: * Servlet initialization parameter name for the path to
078: * TurbineConfiguration.xml file used by Turbine
079: */
080: public static final String CONFIGURATION_PATH_KEY = "configuration";
081:
082: /**
083: * Servlet initialization parameter name for the path to
084: * Turbine.properties file used by Turbine
085: */
086: public static final String PROPERTIES_PATH_KEY = "properties";
087:
088: /**
089: * Default value of TurbineResources.properties file path
090: * (<code>/WEB-INF/conf/TurbineResources.properties</code>).
091: */
092: public static final String PROPERTIES_PATH_DEFAULT = "/WEB-INF/conf/TurbineResources.properties";
093:
094: /** Filenames are looked up in this directory. */
095: protected File root;
096:
097: /** Servlet container (or emulator) attributes. */
098: protected Map attributes;
099:
100: /** Turbine servlet initialization parameters. */
101: protected Map initParams;
102:
103: /** The Turbine servlet instance used for initialization. */
104: private Turbine turbine;
105:
106: /** Logging */
107: private Log log = LogFactory.getLog(this .getClass());
108:
109: /**
110: * Constructs a new TurbineConfig.
111: *
112: * This is the general form of the constructor. You can provide
113: * a path to search for files, and a name-value map of init
114: * parameters.
115: *
116: * <p> For the list of recognized init parameters, see
117: * {@link org.apache.turbine.Turbine} class.
118: *
119: * @param path The web application root (i.e. the path for file lookup).
120: * @param attributes Servlet container (or emulator) attributes.
121: * @param initParams initialization parameters.
122: */
123: public TurbineConfig(String path, Map attributes, Map initParams) {
124: root = new File(path);
125: this .attributes = attributes;
126: this .initParams = initParams;
127: }
128:
129: /**
130: * @see #TurbineConfig(String path, Map attributes, Map initParams)
131: */
132: public TurbineConfig(String path, Map initParams) {
133: this (path, new HashMap(0), initParams);
134: }
135:
136: /**
137: * Constructs a TurbineConfig.
138: *
139: * This is a specialized constructor that allows to configure
140: * Turbine easiliy in the common setups.
141: *
142: * @param path The web application root (i.e. the path for file lookup).
143: * @param properties the relative path to TurbineResources.properties file
144: */
145: public TurbineConfig(String path, String properties) {
146: this (path, new HashMap(1));
147: initParams.put(PROPERTIES_PATH_KEY, properties);
148: }
149:
150: /**
151: * Causes this class to initialize itself which in turn initializes
152: * all of the Turbine Services that need to be initialized.
153: *
154: * @see org.apache.stratum.lifecycle.Initializable
155: */
156: public void initialize() {
157: try {
158: turbine = new Turbine();
159: turbine.init(this );
160: } catch (Exception e) {
161: log.error("TurbineConfig: Initialization failed", e);
162: }
163: }
164:
165: /**
166: * Initialization requiring a HTTP <code>GET</code> request.
167: */
168: public void init(RunData data) {
169: if (turbine != null) {
170: turbine.init(data);
171: }
172: }
173:
174: /**
175: * Shutdown the Turbine System, lifecycle style
176: *
177: */
178: public void dispose() {
179: if (turbine != null) {
180: turbine.destroy();
181: }
182: }
183:
184: /**
185: * Returns a reference to the object cast onto ServletContext type.
186: *
187: * @return a ServletContext reference
188: */
189: public ServletContext getServletContext() {
190: return this ;
191: }
192:
193: /**
194: * Translates a path relative to the web application root into an
195: * absolute path.
196: *
197: * @param path A path relative to the web application root.
198: * @return An absolute version of the supplied path, or <code>null</code>
199: * if the translated path doesn't map to a file or directory.
200: */
201: public String getRealPath(String path) {
202: String result = null;
203: File f = new File(root, path);
204:
205: if (log.isDebugEnabled()) {
206: StringBuffer sb = new StringBuffer();
207:
208: sb.append("TurbineConfig.getRealPath: path '");
209: sb.append(path);
210: sb.append("' translated to '");
211: sb.append(f.getPath());
212: sb.append("' ");
213: sb.append(f.exists() ? "" : "not ");
214: sb.append("found");
215: log.debug(sb.toString());
216: }
217:
218: if (f.exists()) {
219: result = f.getPath();
220: } else {
221: log.error("getRealPath(\"" + path
222: + "\") is undefined, returning null");
223: }
224:
225: return result;
226: }
227:
228: /**
229: * Retrieves an initialization parameter.
230: *
231: * @param name the name of the parameter.
232: * @return the value of the parameter.
233: */
234: public String getInitParameter(String name) {
235: return (String) initParams.get(name);
236: }
237:
238: /**
239: * Retrieves an Enumeration of initialization parameter names.
240: *
241: * @return an Enumeration of initialization parameter names.
242: */
243: public Enumeration getInitParameterNames() {
244: return new Vector(initParams.keySet()).elements();
245: }
246:
247: /**
248: * Returns the servlet name.
249: *
250: * Fixed value "Turbine" is returned.
251: *
252: * @return the servlet name.
253: */
254: public String getServletName() {
255: return "Turbine";
256: }
257:
258: /**
259: * Returns the context name.
260: *
261: * Fixed value "Turbine" is returned
262: *
263: * @return the context name
264: */
265: public String getServletContextName() {
266: return "Turbine";
267: }
268:
269: /**
270: * Returns a URL to the resource that is mapped to a specified
271: * path. The path must begin with a "/" and is interpreted
272: * as relative to the current context root.
273: *
274: * @param s the path to the resource
275: * @return a URL pointing to the resource
276: * @exception MalformedURLException
277: */
278: public URL getResource(String s) throws MalformedURLException {
279: return new URL("file://" + getRealPath(s));
280: }
281:
282: /**
283: * Returns the resource located at the named path as
284: * an <code>InputStream</code> object.
285: *
286: * @param s the path to the resource
287: * @return an InputStream object from which the resource can be read
288: */
289: public InputStream getResourceAsStream(String s) {
290: try {
291: FileInputStream fis = new FileInputStream(getRealPath(s));
292: return new BufferedInputStream(fis);
293: } catch (FileNotFoundException e) {
294: return null;
295: }
296: }
297:
298: /**
299: * Logs an error message.
300: *
301: * @param e an Exception.
302: * @param m a message.
303: * @deprecated use log(String,Throwable) instead
304: */
305: public void log(Exception e, String m) {
306: log.info(m, e);
307: }
308:
309: /**
310: * Logs a message.
311: *
312: * @param m a message.
313: */
314: public void log(String m) {
315: log.info(m);
316: }
317:
318: /**
319: * Logs an error message.
320: *
321: * @param t a Throwable object.
322: * @param m a message.
323: */
324: public void log(String m, Throwable t) {
325: log.info(m, t);
326: }
327:
328: /**
329: * Returns the servlet container attribute with the given name, or
330: * null if there is no attribute by that name.
331: */
332: public Object getAttribute(String s) {
333: return attributes.get(s);
334: }
335:
336: /**
337: * Returns an Enumeration containing the attribute names available
338: * within this servlet context.
339: */
340: public Enumeration getAttributeNames() {
341: return new Vector(attributes.keySet()).elements();
342: }
343:
344: // Unimplemented methods follow
345:
346: /**
347: * Not implemented.
348: *
349: * A method in ServletConfig or ServletContext interface that is not
350: * implemented and will throw <code>UnsuportedOperationException</code>
351: * upon invocation
352: */
353: public ServletContext getContext(String s) {
354: throw new UnsupportedOperationException();
355: }
356:
357: /**
358: * Not implemented.
359: *
360: * A method in ServletConfig or ServletContext interface that is not
361: * implemented and will throw <code>UnsuportedOperationException</code>
362: * upon invocation
363: */
364: public int getMajorVersion() {
365: throw new UnsupportedOperationException();
366: }
367:
368: /**
369: * Not implemented.
370: *
371: * A method in ServletConfig or ServletContext interface that is not
372: * implemented and will throw <code>UnsuportedOperationException</code>
373: * upon invocation
374: */
375: public String getMimeType(String s) {
376: throw new UnsupportedOperationException();
377: }
378:
379: /**
380: * Not implemented.
381: *
382: * A method in ServletConfig or ServletContext interface that is not
383: * implemented and will throw <code>UnsuportedOperationException</code>
384: * upon invocation
385: */
386: public int getMinorVersion() {
387: throw new UnsupportedOperationException();
388: }
389:
390: /**
391: * Not implemented.
392: *
393: * A method in ServletConfig or ServletContext interface that is not
394: * implemented and will throw <code>UnsuportedOperationException</code>
395: * upon invocation
396: */
397: public RequestDispatcher getNamedDispatcher(String s) {
398: throw new UnsupportedOperationException();
399: }
400:
401: /**
402: * Not implemented.
403: *
404: * A method in ServletConfig or ServletContext interface that is not
405: * implemented and will throw <code>UnsuportedOperationException</code>
406: * upon invocation
407: */
408: public RequestDispatcher getRequestDispatcher(String s) {
409: throw new UnsupportedOperationException();
410: }
411:
412: /**
413: * Not implemented.
414: *
415: * A method in ServletContext (2.3) interface that is not implemented and
416: * will throw <code>UnsuportedOperationException</code> upon invocation
417: */
418: public Set getResourcePaths(String s) {
419: throw new UnsupportedOperationException();
420: }
421:
422: /**
423: * Not implemented.
424: *
425: * A method in ServletContext (2.3) interface that is not implemented and
426: * will throw <code>UnsuportedOperationException</code> upon invocation
427: */
428: public String getServerInfo() {
429: throw new UnsupportedOperationException();
430: }
431:
432: /**
433: * Not implemented.
434: *
435: * A method in ServletContext interface that is not implemented and will
436: * throw <code>UnsuportedOperationException</code> upon invocation
437: * @deprecated As of Java Servlet API 2.1, with no direct replacement.
438: */
439: public Servlet getServlet(String s) {
440: throw new UnsupportedOperationException();
441: }
442:
443: /**
444: * Not implemented.
445: *
446: * A method in ServletContext interface that is not implemented and will
447: * throw <code>UnsuportedOperationException</code> upon invocation
448: * @deprecated As of Java Servlet API 2.1, with no replacement.
449: */
450: public Enumeration getServletNames() {
451: throw new UnsupportedOperationException();
452: }
453:
454: /**
455: * Not implemented.
456: *
457: * A method in ServletContext interface that is not implemented and will
458: * throw <code>UnsuportedOperationException</code> upon invocation
459: * @deprecated As of Java Servlet API 2.0, with no replacement.
460: */
461: public Enumeration getServlets() {
462: throw new UnsupportedOperationException();
463: }
464:
465: /**
466: * Not implemented.
467: *
468: * A method in ServletContext interface that is not implemented and will
469: * throw <code>UnsuportedOperationException</code> upon invocation
470: */
471: public void removeAttribute(String s) {
472: throw new UnsupportedOperationException();
473: }
474:
475: /**
476: * Not implemented.
477: *
478: * A method in ServletContext interface that is not implemented and will
479: * throw <code>UnsuportedOperationException</code> upon invocation
480: */
481: public void setAttribute(String s, Object o) {
482: throw new UnsupportedOperationException();
483: }
484: }
|