You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by ep...@apache.org on 2003/06/07 11:22:45 UTC
cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/util TurbineXmlConfig.java
epugh 2003/06/07 02:22:44
Modified: src/test/org/apache/turbine TurbineConfigTest.java
ConfigurationTest.java
Added: src/java/org/apache/turbine/util TurbineXmlConfig.java
Log:
Added fixes for TurbineXmlConfig. TurbineXmlConfig
just duplicates TurbineConfig, however this is NOT how it remain.
I am having difficulties in getting it to properly compile while extending
from TurbineConfig.. However, I wanted to make sure all the testcases
worked.
Revision Changes Path
1.2 +6 -5 jakarta-turbine-2/src/test/org/apache/turbine/TurbineConfigTest.java
Index: TurbineConfigTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/test/org/apache/turbine/TurbineConfigTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TurbineConfigTest.java 6 Jun 2003 22:08:09 -0000 1.1
+++ TurbineConfigTest.java 7 Jun 2003 09:22:44 -0000 1.2
@@ -60,6 +60,7 @@
import org.apache.turbine.Turbine;
import org.apache.turbine.util.TurbineConfig;
+import org.apache.turbine.util.*;
/**
* This testcase verifies that TurbineConfig can be used to startup Turbine in a non
@@ -73,12 +74,12 @@
extends TestCase
{
private static TurbineConfig tc = null;
+ private static TurbineXmlConfig txc = null;
public TurbineConfigTest(String name)
{
super(name);
-
}
public static Test suite()
@@ -98,10 +99,10 @@
}
- public void testTurbineConfigWithconfigurationFile() throws Exception {
+ public void testTurbineXmlConfigWithconfigurationFile() throws Exception {
String value = "/conf/test/TurbineConfiguration.xml";
- tc = new TurbineConfig(".", value);
- ServletContext context = tc.getServletContext();
+ txc = new TurbineXmlConfig(".", value);
+ ServletContext context = txc.getServletContext();
ServletConfig config = (ServletConfig)tc;
String confFile= Turbine.findInitParameter(context, config,
TurbineConfig.CONFIGURATION_PATH_KEY,
1.2 +7 -8 jakarta-turbine-2/src/test/org/apache/turbine/ConfigurationTest.java
Index: ConfigurationTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/test/org/apache/turbine/ConfigurationTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ConfigurationTest.java 6 Jun 2003 22:08:09 -0000 1.1
+++ ConfigurationTest.java 7 Jun 2003 09:22:44 -0000 1.2
@@ -53,16 +53,14 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
-import javax.servlet.*;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.turbine.Turbine;
-import org.apache.turbine.util.TurbineConfig;
import org.apache.commons.configuration.Configuration;
-import org.apache.commons.configuration.ConfigurationFactory;
-import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.turbine.util.TurbineConfig;
+import org.apache.turbine.util.TurbineXmlConfig;
+
/**
* Tests that the ConfigurationFactory and regular old properties methods both work.
@@ -76,6 +74,7 @@
extends TestCase
{
private static TurbineConfig tc = null;
+ private static TurbineXmlConfig txc = null;
public ConfigurationTest(String name)
{
@@ -94,8 +93,8 @@
public void testCreateTurbineWithConfigurationXML() throws Exception
{
- tc = new TurbineConfig(".", "/conf/test/TurbineConfiguration.xml");
- tc.initialize();
+ txc = new TurbineXmlConfig(".", "/conf/test/TurbineConfiguration.xml");
+ txc.initialize();
Configuration configuration = Turbine.getConfiguration();
assertTrue("Make sure we have values", !configuration.isEmpty());
1.1 jakarta-turbine-2/src/java/org/apache/turbine/util/TurbineXmlConfig.java
Index: TurbineXmlConfig.java
===================================================================
package org.apache.turbine.util;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.turbine.Turbine;
/**
* A class used for initalization of Turbine without a servlet container.
* <p>
* If you need to use Turbine outside of a servlet container, you can
* use this class for initalization of the Turbine servlet.
* <p>
* <blockquote><code><pre>
* TurbineXmlConfig config = new TurbineXmlConfig(".", "/conf/TurbineResources.properties");
* </pre></code></blockquote>
* <p>
* All paths referenced in TurbineResources.properties and the path to
* the properties file itself (the second argument) will be resolved
* relative to the directory given as the first argument of the constructor,
* here - the directory where application was started. Don't worry about
* discarding the references to objects created above. They are not needed,
* once everything is initialized.
* <p>
* In order to initialize the Services Framework outside of the Turbine Servlet,
* you need to call the <code>init()</code> method. By default, this will
* initialize the Resource and Logging Services and any other services you
* have defined in your TurbineResources.properties file.
*
* @todo Make this class enforce the lifecycle contracts
*
* @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
* @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id: TurbineXmlConfig.java,v 1.1 2003/06/07 09:22:44 epugh Exp $
*/
public class TurbineXmlConfig
implements ServletConfig, ServletContext, Initializable, Disposable
{
/**
* Servlet initialization parameter name for the path to
* TurbineXmlConfiguration.xml file used by Turbine
*/
public static final String CONFIGURATION_PATH_KEY = "configuration";
/**
* Servlet initialization parameter name for the path to
* Turbine.properties file used by Turbine
*/
public static final String PROPERTIES_PATH_KEY = "properties";
/**
* Default value of TurbineResources.properties file path
* (<code>/WEB-INF/conf/TurbineResources.properties</code>).
*/
public static final String PROPERTIES_PATH_DEFAULT =
"/WEB-INF/conf/TurbineResources.properties";
/** Filenames are looked up in this directory. */
protected File root;
/** Servlet container (or emulator) attributes. */
protected Map attributes;
/** Turbine servlet initialization parameters. */
protected Map initParams;
/** The Turbine servlet instance used for initialization. */
private Turbine turbine;
/** Logging */
private Log log = LogFactory.getLog(this.getClass());
/**
* Constructs a new TurbineXmlConfig.
*
* This is the general form of the constructor. You can provide
* a path to search for files, and a name-value map of init
* parameters.
*
* <p> For the list of recognized init parameters, see
* {@link org.apache.turbine.Turbine} class.
*
* @param path The web application root (i.e. the path for file lookup).
* @param attributes Servlet container (or emulator) attributes.
* @param initParams initialization parameters.
*/
public TurbineXmlConfig(String path, Map attributes, Map initParams)
{
root = new File(path);
this.attributes = attributes;
this.initParams = initParams;
}
/**
* @see #TurbineXmlConfig(String path, Map attributes, Map initParams)
*/
public TurbineXmlConfig(String path, Map initParams)
{
this(path, new HashMap(0), initParams);
}
/**
* Constructs a TurbineXmlConfig.
*
* This is a specialized constructor that allows to configure
* Turbine easiliy in the common setups.
*
* @param path The web application root (i.e. the path for file lookup).
* @param properties the relative path to TurbineResources.properties file
*/
public TurbineXmlConfig(String path, String properties)
{
this(path, new HashMap(1));
initParams.put(CONFIGURATION_PATH_KEY, properties);
}
/**
* Causes this class to initialize itself which in turn initializes
* all of the Turbine Services that need to be initialized.
*
* @see org.apache.stratum.lifecycle.Initializable
*/
public void initialize()
{
try
{
turbine = new Turbine();
turbine.init(this);
}
catch (Exception e)
{
log.error("TurbineXmlConfig: Initialization failed", e);
}
}
/**
* Initialization requiring a HTTP <code>GET</code> request.
*/
public void init(RunData data)
{
if (turbine != null)
{
turbine.init(data);
}
}
/**
* Shutdown the Turbine System, lifecycle style
*
*/
public void dispose()
{
if (turbine != null)
{
turbine.destroy();
}
}
/**
* Returns a reference to the object cast onto ServletContext type.
*
* @return a ServletContext reference
*/
public ServletContext getServletContext()
{
return this;
}
/**
* Translates a path relative to the web application root into an
* absolute path.
*
* @param path A path relative to the web application root.
* @return An absolute version of the supplied path, or <code>null</code>
* if the translated path doesn't map to a file or directory.
*/
public String getRealPath(String path)
{
String result = null;
File f = new File(root, path);
if (log.isDebugEnabled())
{
StringBuffer sb = new StringBuffer();
sb.append("TurbineXmlConfig.getRealPath: path '");
sb.append(path);
sb.append("' translated to '");
sb.append(f.getPath());
sb.append("' ");
sb.append(f.exists() ? "" : "not ");
sb.append("found");
log.debug(sb.toString());
}
if (f.exists())
{
result = f.getPath();
}
else
{
log.error("getRealPath(\"" + path + "\") is undefined, returning null");
}
return result;
}
/**
* Retrieves an initialization parameter.
*
* @param name the name of the parameter.
* @return the value of the parameter.
*/
public String getInitParameter(String name)
{
return (String) initParams.get(name);
}
/**
* Retrieves an Enumeration of initialization parameter names.
*
* @return an Enumeration of initialization parameter names.
*/
public Enumeration getInitParameterNames()
{
return new Vector(initParams.keySet()).elements();
}
/**
* Returns the servlet name.
*
* Fixed value "Turbine" is returned.
*
* @return the servlet name.
*/
public String getServletName()
{
return "Turbine";
}
/**
* Returns the context name.
*
* Fixed value "Turbine" is returned
*
* @return the context name
*/
public String getServletContextName()
{
return "Turbine";
}
/**
* Returns a URL to the resource that is mapped to a specified
* path. The path must begin with a "/" and is interpreted
* as relative to the current context root.
*
* @param s the path to the resource
* @return a URL pointing to the resource
* @exception MalformedURLException
*/
public URL getResource(String s)
throws MalformedURLException
{
return new URL("file://" + getRealPath(s));
}
/**
* Returns the resource located at the named path as
* an <code>InputStream</code> object.
*
* @param s the path to the resource
* @return an InputStream object from which the resource can be read
*/
public InputStream getResourceAsStream(String s)
{
try
{
FileInputStream fis = new FileInputStream(getRealPath(s));
return new BufferedInputStream(fis);
}
catch (FileNotFoundException e)
{
return null;
}
}
/**
* Logs an error message.
*
* @param e an Exception.
* @param m a message.
* @deprecated use log(String,Throwable) instead
*/
public void log(Exception e, String m)
{
log.info(m, e);
}
/**
* Logs a message.
*
* @param m a message.
*/
public void log(String m)
{
log.info(m);
}
/**
* Logs an error message.
*
* @param t a Throwable object.
* @param m a message.
*/
public void log(String m, Throwable t)
{
log.info(m, t);
}
/**
* Returns the servlet container attribute with the given name, or
* null if there is no attribute by that name.
*/
public Object getAttribute(String s)
{
return attributes.get(s);
}
/**
* Returns an Enumeration containing the attribute names available
* within this servlet context.
*/
public Enumeration getAttributeNames()
{
return new Vector(attributes.keySet()).elements();
}
// Unimplemented methods follow
/**
* Not implemented.
*
* A method in ServletConfig or ServletContext interface that is not
* implemented and will throw <code>UnsuportedOperationException</code>
* upon invocation
*/
public ServletContext getContext(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletConfig or ServletContext interface that is not
* implemented and will throw <code>UnsuportedOperationException</code>
* upon invocation
*/
public int getMajorVersion()
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletConfig or ServletContext interface that is not
* implemented and will throw <code>UnsuportedOperationException</code>
* upon invocation
*/
public String getMimeType(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletConfig or ServletContext interface that is not
* implemented and will throw <code>UnsuportedOperationException</code>
* upon invocation
*/
public int getMinorVersion()
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletConfig or ServletContext interface that is not
* implemented and will throw <code>UnsuportedOperationException</code>
* upon invocation
*/
public RequestDispatcher getNamedDispatcher(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletConfig or ServletContext interface that is not
* implemented and will throw <code>UnsuportedOperationException</code>
* upon invocation
*/
public RequestDispatcher getRequestDispatcher(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext (2.3) interface that is not implemented and
* will throw <code>UnsuportedOperationException</code> upon invocation
*/
public Set getResourcePaths(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext (2.3) interface that is not implemented and
* will throw <code>UnsuportedOperationException</code> upon invocation
*/
public String getServerInfo()
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext interface that is not implemented and will
* throw <code>UnsuportedOperationException</code> upon invocation
* @deprecated As of Java Servlet API 2.1, with no direct replacement.
*/
public Servlet getServlet(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext interface that is not implemented and will
* throw <code>UnsuportedOperationException</code> upon invocation
* @deprecated As of Java Servlet API 2.1, with no replacement.
*/
public Enumeration getServletNames()
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext interface that is not implemented and will
* throw <code>UnsuportedOperationException</code> upon invocation
* @deprecated As of Java Servlet API 2.0, with no replacement.
*/
public Enumeration getServlets()
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext interface that is not implemented and will
* throw <code>UnsuportedOperationException</code> upon invocation
*/
public void removeAttribute(String s)
{
throw new UnsupportedOperationException();
}
/**
* Not implemented.
*
* A method in ServletContext interface that is not implemented and will
* throw <code>UnsuportedOperationException</code> upon invocation
*/
public void setAttribute(String s, Object o)
{
throw new UnsupportedOperationException();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/util TurbineXmlConfig.java
Posted by Мантатов Владимир <x3...@esstu.ru>.
Здравствуйте, epugh.
Вы писали 7 июня 2003 г., 18:22:45:
eao> epugh 2003/06/07 02:22:44
eao> Modified: src/test/org/apache/turbine TurbineConfigTest.java
eao> ConfigurationTest.java
eao> Added: src/java/org/apache/turbine/util TurbineXmlConfig.java
eao> Log:
eao> Added fixes for TurbineXmlConfig. TurbineXmlConfig
eao> just duplicates TurbineConfig, however this is NOT how it remain.
eao> I am having difficulties in getting it to properly compile while extending
eao> from TurbineConfig.. However, I wanted to make sure all the testcases
eao> worked.
eao> Revision Changes Path
eao> 1.2 +6 -5 jakarta-turbine-2/src/test/org/apache/turbine/TurbineConfigTest.java
eao> Index: TurbineConfigTest.java
eao> ===================================================================
eao> RCS file: /home/cvs/jakarta-turbine-2/src/test/org/apache/turbine/TurbineConfigTest.java,v
eao> retrieving revision 1.1
eao> retrieving revision 1.2
eao> diff -u -r1.1 -r1.2
eao> --- TurbineConfigTest.java 6 Jun 2003 22:08:09 -0000 1.1
eao> +++ TurbineConfigTest.java 7 Jun 2003 09:22:44 -0000 1.2
eao> @@ -60,6 +60,7 @@
eao> import org.apache.turbine.Turbine;
eao> import org.apache.turbine.util.TurbineConfig;
eao> +import org.apache.turbine.util.*;
eao> /**
eao> * This testcase verifies that TurbineConfig can be used to startup Turbine in a non
eao> @@ -73,12 +74,12 @@
eao> extends TestCase
eao> {
eao> private static TurbineConfig tc = null;
eao> + private static TurbineXmlConfig txc = null;
eao> public TurbineConfigTest(String name)
eao> {
eao> super(name);
eao> -
eao> }
eao> public static Test suite()
eao> @@ -98,10 +99,10 @@
eao> }
eao> - public void testTurbineConfigWithconfigurationFile() throws Exception {
eao> + public void testTurbineXmlConfigWithconfigurationFile() throws Exception {
eao> String value = "/conf/test/TurbineConfiguration.xml";
eao> - tc = new TurbineConfig(".", value);
eao> - ServletContext context = tc.getServletContext();
eao> + txc = new TurbineXmlConfig(".", value);
eao> + ServletContext context = txc.getServletContext();
eao> ServletConfig config = (ServletConfig)tc;
eao> String confFile= Turbine.findInitParameter(context, config,
eao> TurbineConfig.CONFIGURATION_PATH_KEY,
eao> 1.2 +7 -8 jakarta-turbine-2/src/test/org/apache/turbine/ConfigurationTest.java
eao> Index: ConfigurationTest.java
eao> ===================================================================
eao> RCS file: /home/cvs/jakarta-turbine-2/src/test/org/apache/turbine/ConfigurationTest.java,v
eao> retrieving revision 1.1
eao> retrieving revision 1.2
eao> diff -u -r1.1 -r1.2
eao> --- ConfigurationTest.java 6 Jun 2003 22:08:09 -0000 1.1
eao> +++ ConfigurationTest.java 7 Jun 2003 09:22:44 -0000 1.2
eao> @@ -53,16 +53,14 @@
eao> * information on the Apache Software Foundation, please see
eao> * <http://www.apache.org/>.
eao> */
eao> -import javax.servlet.*;
eao> import junit.framework.Test;
eao> import junit.framework.TestCase;
eao> import junit.framework.TestSuite;
eao> -import org.apache.turbine.Turbine;
eao> -import org.apache.turbine.util.TurbineConfig;
eao> import org.apache.commons.configuration.Configuration;
eao> -import org.apache.commons.configuration.ConfigurationFactory;
eao> -import org.apache.commons.configuration.PropertiesConfiguration;
eao> +import org.apache.turbine.util.TurbineConfig;
eao> +import org.apache.turbine.util.TurbineXmlConfig;
eao> +
eao> /**
eao> * Tests that the ConfigurationFactory and regular old properties methods both work.
eao> @@ -76,6 +74,7 @@
eao> extends TestCase
eao> {
eao> private static TurbineConfig tc = null;
eao> + private static TurbineXmlConfig txc = null;
eao> public ConfigurationTest(String name)
eao> {
eao> @@ -94,8 +93,8 @@
eao> public void testCreateTurbineWithConfigurationXML() throws Exception
eao> {
eao> - tc = new TurbineConfig(".", "/conf/test/TurbineConfiguration.xml");
eao> - tc.initialize();
eao> + txc = new TurbineXmlConfig(".", "/conf/test/TurbineConfiguration.xml");
eao> + txc.initialize();
eao> Configuration configuration = Turbine.getConfiguration();
eao> assertTrue("Make sure we have values", !configuration.isEmpty());
eao> 1.1 jakarta-turbine-2/src/java/org/apache/turbine/util/TurbineXmlConfig.java
eao> Index: TurbineXmlConfig.java
eao> ===================================================================
eao> package org.apache.turbine.util;
eao> /* ====================================================================
eao> * The Apache Software License, Version 1.1
eao> *
eao> * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
eao> * reserved.
eao> *
eao> * Redistribution and use in source and binary forms, with or without
eao> * modification, are permitted provided that the following conditions
eao> * are met:
eao> *
eao> * 1. Redistributions of source code must retain the above copyright
eao> * notice, this list of conditions and the following disclaimer.
eao> *
eao> * 2. Redistributions in binary form must reproduce the above copyright
eao> * notice, this list of conditions and the following disclaimer in
eao> * the documentation and/or other materials provided with the
eao> * distribution.
eao> *
eao> * 3. The end-user documentation included with the redistribution,
eao> * if any, must include the following acknowledgment:
eao> * "This product includes software developed by the
eao> * Apache Software Foundation (http://www.apache.org/)."
eao> * Alternately, this acknowledgment may appear in the software itself,
eao> * if and wherever such third-party acknowledgments normally appear.
eao> *
eao> * 4. The names "Apache" and "Apache Software Foundation" and
eao> * "Apache Turbine" must not be used to endorse or promote products
eao> * derived from this software without prior written permission. For
eao> * written permission, please contact apache@apache.org.
eao> *
eao> * 5. Products derived from this software may not be called "Apache",
eao> * "Apache Turbine", nor may "Apache" appear in their name, without
eao> * prior written permission of the Apache Software Foundation.
eao> *
eao> * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
eao> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
eao> * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
eao> * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
eao> * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
eao> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
eao> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
eao> * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
eao> * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
eao> * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
eao> * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
eao> * SUCH DAMAGE.
eao> * ====================================================================
eao> *
eao> * This software consists of voluntary contributions made by many
eao> * individuals on behalf of the Apache Software Foundation. For more
eao> * information on the Apache Software Foundation, please see
eao> * <http://www.apache.org/>.
eao> */
eao> import java.io.BufferedInputStream;
eao> import java.io.File;
eao> import java.io.FileInputStream;
eao> import java.io.FileNotFoundException;
eao> import java.io.InputStream;
eao> import java.net.MalformedURLException;
eao> import java.net.URL;
eao> import java.util.Enumeration;
eao> import java.util.HashMap;
eao> import java.util.Map;
eao> import java.util.Set;
eao> import java.util.Vector;
eao> import javax.servlet.RequestDispatcher;
eao> import javax.servlet.Servlet;
eao> import javax.servlet.ServletConfig;
eao> import javax.servlet.ServletContext;
eao> import org.apache.commons.logging.Log;
eao> import org.apache.commons.logging.LogFactory;
eao> import org.apache.avalon.framework.activity.Disposable;
eao> import org.apache.avalon.framework.activity.Initializable;
eao> import org.apache.turbine.Turbine;
eao> /**
eao> * A class used for initalization of Turbine without a servlet container.
eao> * <p>
eao> * If you need to use Turbine outside of a servlet container, you can
eao> * use this class for initalization of the Turbine servlet.
eao> * <p>
eao> * <blockquote><code><pre>
eao> * TurbineXmlConfig config = new TurbineXmlConfig(".", "/conf/TurbineResources.properties");
eao> * </pre></code></blockquote>
eao> * <p>
eao> * All paths referenced in TurbineResources.properties and the path to
eao> * the properties file itself (the second argument) will be resolved
eao> * relative to the directory given as the first argument of the constructor,
eao> * here - the directory where application was started. Don't worry about
eao> * discarding the references to objects created above. They are not needed,
eao> * once everything is initialized.
eao> * <p>
eao> * In order to initialize the Services Framework outside of the Turbine Servlet,
eao> * you need to call the <code>init()</code> method. By default, this will
eao> * initialize the Resource and Logging Services and any other services you
eao> * have defined in your TurbineResources.properties file.
eao> *
eao> * @todo Make this class enforce the lifecycle contracts
eao> *
eao> * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
eao> * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
eao> * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
eao> * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
eao> * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
eao> * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
eao> * @version $Id: TurbineXmlConfig.java,v 1.1 2003/06/07 09:22:44 epugh Exp $
eao> */
eao> public class TurbineXmlConfig
eao> implements ServletConfig, ServletContext, Initializable, Disposable
eao> {
eao> /**
eao> * Servlet initialization parameter name for the path to
eao> * TurbineXmlConfiguration.xml file used by Turbine
eao> */
eao> public static final String CONFIGURATION_PATH_KEY = "configuration";
eao> /**
eao> * Servlet initialization parameter name for the path to
eao> * Turbine.properties file used by Turbine
eao> */
eao> public static final String PROPERTIES_PATH_KEY = "properties";
eao> /**
eao> * Default value of TurbineResources.properties file path
eao> * (<code>/WEB-INF/conf/TurbineResources.properties</code>).
eao> */
eao> public static final String PROPERTIES_PATH_DEFAULT =
eao> "/WEB-INF/conf/TurbineResources.properties";
eao> /** Filenames are looked up in this directory. */
eao> protected File root;
eao> /** Servlet container (or emulator) attributes. */
eao> protected Map attributes;
eao> /** Turbine servlet initialization parameters. */
eao> protected Map initParams;
eao> /** The Turbine servlet instance used for initialization. */
eao> private Turbine turbine;
eao> /** Logging */
eao> private Log log = LogFactory.getLog(this.getClass());
eao> /**
eao> * Constructs a new TurbineXmlConfig.
eao> *
eao> * This is the general form of the constructor. You can provide
eao> * a path to search for files, and a name-value map of init
eao> * parameters.
eao> *
eao> * <p> For the list of recognized init parameters, see
eao> * {@link org.apache.turbine.Turbine} class.
eao> *
eao> * @param path The web application root (i.e. the path for file lookup).
eao> * @param attributes Servlet container (or emulator) attributes.
eao> * @param initParams initialization parameters.
eao> */
eao> public TurbineXmlConfig(String path, Map attributes, Map initParams)
eao> {
eao> root = new File(path);
eao> this.attributes = attributes;
eao> this.initParams = initParams;
eao> }
eao> /**
eao> * @see #TurbineXmlConfig(String path, Map attributes, Map initParams)
eao> */
eao> public TurbineXmlConfig(String path, Map initParams)
eao> {
eao> this(path, new HashMap(0), initParams);
eao> }
eao> /**
eao> * Constructs a TurbineXmlConfig.
eao> *
eao> * This is a specialized constructor that allows to configure
eao> * Turbine easiliy in the common setups.
eao> *
eao> * @param path The web application root (i.e. the path for file lookup).
eao> * @param properties the relative path to TurbineResources.properties file
eao> */
eao> public TurbineXmlConfig(String path, String properties)
eao> {
eao> this(path, new HashMap(1));
eao> initParams.put(CONFIGURATION_PATH_KEY, properties);
eao> }
eao> /**
eao> * Causes this class to initialize itself which in turn initializes
eao> * all of the Turbine Services that need to be initialized.
eao> *
eao> * @see org.apache.stratum.lifecycle.Initializable
eao> */
eao> public void initialize()
eao> {
eao> try
eao> {
eao> turbine = new Turbine();
eao> turbine.init(this);
eao> }
eao> catch (Exception e)
eao> {
eao> log.error("TurbineXmlConfig: Initialization failed", e);
eao> }
eao> }
eao> /**
eao> * Initialization requiring a HTTP <code>GET</code> request.
eao> */
eao> public void init(RunData data)
eao> {
eao> if (turbine != null)
eao> {
eao> turbine.init(data);
eao> }
eao> }
eao> /**
eao> * Shutdown the Turbine System, lifecycle style
eao> *
eao> */
eao> public void dispose()
eao> {
eao> if (turbine != null)
eao> {
eao> turbine.destroy();
eao> }
eao> }
eao> /**
eao> * Returns a reference to the object cast onto ServletContext type.
eao> *
eao> * @return a ServletContext reference
eao> */
eao> public ServletContext getServletContext()
eao> {
eao> return this;
eao> }
eao> /**
eao> * Translates a path relative to the web application root into an
eao> * absolute path.
eao> *
eao> * @param path A path relative to the web application root.
eao> * @return An absolute version of the supplied path, or <code>null</code>
eao> * if the translated path doesn't map to a file or directory.
eao> */
eao> public String getRealPath(String path)
eao> {
eao> String result = null;
eao> File f = new File(root, path);
eao> if (log.isDebugEnabled())
eao> {
eao> StringBuffer sb = new StringBuffer();
eao> sb.append("TurbineXmlConfig.getRealPath: path '");
eao> sb.append(path);
eao> sb.append("' translated to '");
eao> sb.append(f.getPath());
eao> sb.append("' ");
eao> sb.append(f.exists() ? "" : "not ");
eao> sb.append("found");
eao> log.debug(sb.toString());
eao> }
eao> if (f.exists())
eao> {
eao> result = f.getPath();
eao> }
eao> else
eao> {
eao> log.error("getRealPath(\"" + path + "\") is undefined, returning null");
eao> }
eao> return result;
eao> }
eao> /**
eao> * Retrieves an initialization parameter.
eao> *
eao> * @param name the name of the parameter.
eao> * @return the value of the parameter.
eao> */
eao> public String getInitParameter(String name)
eao> {
eao> return (String) initParams.get(name);
eao> }
eao> /**
eao> * Retrieves an Enumeration of initialization parameter names.
eao> *
eao> * @return an Enumeration of initialization parameter names.
eao> */
eao> public Enumeration getInitParameterNames()
eao> {
eao> return new Vector(initParams.keySet()).elements();
eao> }
eao> /**
eao> * Returns the servlet name.
eao> *
eao> * Fixed value "Turbine" is returned.
eao> *
eao> * @return the servlet name.
eao> */
eao> public String getServletName()
eao> {
eao> return "Turbine";
eao> }
eao> /**
eao> * Returns the context name.
eao> *
eao> * Fixed value "Turbine" is returned
eao> *
eao> * @return the context name
eao> */
eao> public String getServletContextName()
eao> {
eao> return "Turbine";
eao> }
eao> /**
eao> * Returns a URL to the resource that is mapped to a specified
eao> * path. The path must begin with a "/" and is interpreted
eao> * as relative to the current context root.
eao> *
eao> * @param s the path to the resource
eao> * @return a URL pointing to the resource
eao> * @exception MalformedURLException
eao> */
eao> public URL getResource(String s)
eao> throws MalformedURLException
eao> {
eao> return new URL("file://" + getRealPath(s));
eao> }
eao> /**
eao> * Returns the resource located at the named path as
eao> * an <code>InputStream</code> object.
eao> *
eao> * @param s the path to the resource
eao> * @return an InputStream object from which the resource can be read
eao> */
eao> public InputStream getResourceAsStream(String s)
eao> {
eao> try
eao> {
eao> FileInputStream fis = new FileInputStream(getRealPath(s));
eao> return new BufferedInputStream(fis);
eao> }
eao> catch (FileNotFoundException e)
eao> {
eao> return null;
eao> }
eao> }
eao> /**
eao> * Logs an error message.
eao> *
eao> * @param e an Exception.
eao> * @param m a message.
eao> * @deprecated use log(String,Throwable) instead
eao> */
eao> public void log(Exception e, String m)
eao> {
eao> log.info(m, e);
eao> }
eao> /**
eao> * Logs a message.
eao> *
eao> * @param m a message.
eao> */
eao> public void log(String m)
eao> {
eao> log.info(m);
eao> }
eao> /**
eao> * Logs an error message.
eao> *
eao> * @param t a Throwable object.
eao> * @param m a message.
eao> */
eao> public void log(String m, Throwable t)
eao> {
eao> log.info(m, t);
eao> }
eao> /**
eao> * Returns the servlet container attribute with the given name, or
eao> * null if there is no attribute by that name.
eao> */
eao> public Object getAttribute(String s)
eao> {
eao> return attributes.get(s);
eao> }
eao> /**
eao> * Returns an Enumeration containing the attribute names available
eao> * within this servlet context.
eao> */
eao> public Enumeration getAttributeNames()
eao> {
eao> return new Vector(attributes.keySet()).elements();
eao> }
eao> // Unimplemented methods follow
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletConfig or ServletContext interface that is not
eao> * implemented and will throw <code>UnsuportedOperationException</code>
eao> * upon invocation
eao> */
eao> public ServletContext getContext(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletConfig or ServletContext interface that is not
eao> * implemented and will throw <code>UnsuportedOperationException</code>
eao> * upon invocation
eao> */
eao> public int getMajorVersion()
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletConfig or ServletContext interface that is not
eao> * implemented and will throw <code>UnsuportedOperationException</code>
eao> * upon invocation
eao> */
eao> public String getMimeType(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletConfig or ServletContext interface that is not
eao> * implemented and will throw <code>UnsuportedOperationException</code>
eao> * upon invocation
eao> */
eao> public int getMinorVersion()
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletConfig or ServletContext interface that is not
eao> * implemented and will throw <code>UnsuportedOperationException</code>
eao> * upon invocation
eao> */
eao> public RequestDispatcher getNamedDispatcher(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletConfig or ServletContext interface that is not
eao> * implemented and will throw <code>UnsuportedOperationException</code>
eao> * upon invocation
eao> */
eao> public RequestDispatcher getRequestDispatcher(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext (2.3) interface that is not implemented and
eao> * will throw <code>UnsuportedOperationException</code> upon invocation
eao> */
eao> public Set getResourcePaths(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext (2.3) interface that is not implemented and
eao> * will throw <code>UnsuportedOperationException</code> upon invocation
eao> */
eao> public String getServerInfo()
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext interface that is not implemented and will
eao> * throw <code>UnsuportedOperationException</code> upon invocation
eao> * @deprecated As of Java Servlet API 2.1, with no direct replacement.
eao> */
eao> public Servlet getServlet(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext interface that is not implemented and will
eao> * throw <code>UnsuportedOperationException</code> upon invocation
eao> * @deprecated As of Java Servlet API 2.1, with no replacement.
eao> */
eao> public Enumeration getServletNames()
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext interface that is not implemented and will
eao> * throw <code>UnsuportedOperationException</code> upon invocation
eao> * @deprecated As of Java Servlet API 2.0, with no replacement.
eao> */
eao> public Enumeration getServlets()
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext interface that is not implemented and will
eao> * throw <code>UnsuportedOperationException</code> upon invocation
eao> */
eao> public void removeAttribute(String s)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> /**
eao> * Not implemented.
eao> *
eao> * A method in ServletContext interface that is not implemented and will
eao> * throw <code>UnsuportedOperationException</code> upon invocation
eao> */
eao> public void setAttribute(String s, Object o)
eao> {
eao> throw new UnsupportedOperationException();
eao> }
eao> }
eao> ---------------------------------------------------------------------
eao> To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
eao> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
--
С уважением,
Мантатов mailto:x3n@esstu.ru
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org