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