/*
* $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ApplicationContext.java,v 1.2 2000/02/13 01:43:44 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2000/02/13 01:43:44 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.core;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import org.apache.tomcat.Container;
import org.apache.tomcat.Context;
import org.apache.tomcat.Host;
import org.apache.tomcat.Logger;
import org.apache.tomcat.Resources;
import org.apache.tomcat.Wrapper;
import org.apache.tomcat.deployment.ContextParameter;
/**
* Standard implementation of <code>ServletContext</code> that represents
* a web application's execution environment. An instance of this class is
* associated with each instance of <code>StandardContext</code>.
*
* @author Craig R. McClanahan
* @version $Revision: 1.2 $ $Date: 2000/02/13 01:43:44 $
*/
final class ApplicationContext
implements ServletContext {
// ----------------------------------------------------------- Constructors
/**
* Construct a new instance of this class, associated with the specified
* Context instance.
*
* @param context The associated Host instance
*/
public ApplicationContext(StandardContext context) {
super();
this.context = context;
}
// ----------------------------------------------------- Instance Variables
/**
* The context attributes for this context.
*/
private Hashtable attributes = new Hashtable();
/**
* The Context instance with which we are associated.
*/
private StandardContext context = null;
// --------------------------------------------------------- Public Methods
/**
* Return the value of the specified context attribute, if any;
* otherwise return <code>null</code>.
*
* @param name Name of the context attribute to return
*/
public Object getAttribute(String name) {
return (attributes.get(name));
}
/**
* Return an enumeration of the names of the context attributes
* associated with this context.
*/
public Enumeration getAttributeNames() {
return (attributes.keys());
}
/**
* Return a <code>ServletContext</code> object that corresponds to a
* specified URI on the server. This method allows servlets to gain
* access to the context for various parts of the server, and as needed
* obtain <code>RequestDispatcher</code> objects or resources from the
* context. The given path must be absolute (beginning with a "/"),
* and is interpreted based on the server's document root.
* <p>
* <b>IMPLEMENTATION NOTE</b>: This implementation is dependent upon
* our associated Context having a parent that is a StandardHost. :-(
*
* @param uri Absolute URI of a resource on the server
*/
public ServletContext getContext(String uri) {
// Identify the parent Host of our Context, if any
StandardHost host = null;
try {
host = (StandardHost) context.getParent();
} catch (Throwable t) {
host = null;
}
if (host == null)
return (null);
if (!host.getRoot())
return (null);
// Return the appropriate Context for the specified URI
if (uri.equals("/"))
return (host.getRootContext());
else {
Context child = (Context) host.map(uri);
if (child != null)
return (child.getServletContext());
else
return (null);
}
}
/**
* Return the value of the specified initialization parameter, or
* <code>null</code> if this parameter does not exist.
*
* @param name Name of the initialization parameter to retrieve
*/
public String getInitParameter(String name) {
return (context.findParameter(name));
}
/**
* Return the names of the context's initialization parameters, or an
* empty enumeration if the context has no initialization parameters.
*/
public Enumeration getInitParameterNames() {
String parameters[] = context.findParameters();
Vector results = new Vector();
for (int i = 0; i < parameters.length; i++)
results.addElement(parameters[i]);
return (results.elements());
}
/**
* Return the major version of the Java Servlet API that we implement.
*/
public int getMajorVersion() {
return (Constants.MAJOR_VERSION);
}
/**
* Return the minor version of the Java Servlet API that we implement.
*/
public int getMinorVersion() {
return (Constants.MINOR_VERSION);
}
/**
* Return the MIME type of the specified file, or <code>null</code> if
* the MIME type cannot be determined.
*
* @param file Filename for which to identify a MIME type
*/
public String getMimeType(String file) {
Resources resources = context.getResources();
if (resources == null)
return (null);
else
return (resources.getMimeType(file));
}
/**
* Return a <code>RequestDispatcher</code> object that acts as a
* wrapper for the named servlet.
*
* @param name Name of the servlet for which a dispatcher is requested
*/
public RequestDispatcher getNamedDispatcher(String name) {
// Identify the Wrapper for the specified servlet, if any
Wrapper wrapper = (Wrapper) context.findChild(name);
if (wrapper == null)
return (null);
// Create and return a corresponding request dispatcher
return (null); // FIXME - getNamedDispatcher()
}
/**
* Return the real path for a given virtual path, if possible; otherwise
* return <code>null</code>.
*
* @param path The path to the desired resource
*/
public String getRealPath(String path) {
Resources resources = context.getResources();
if (resources == null)
return (null);
else
return (resources.getRealPath(path));
}
/**
* Return the 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 path The path to the desired resource
*
* @exception MalformedURLException if the path is not given
* in the correct form
*/
public URL getResource(String path) throws MalformedURLException {
Resources resources = context.getResources();
if (resources == null)
return (null);
else
return (resources.getResource(path));
}
/**
* Return a <code>RequestDispatcher</code> instance that acts as a
* wrapper for the resource at the given path. The path must begin
* with a "/" and is interpreted as relative to the current context root.
*
* @param path The path to the desired resource.
*/
public RequestDispatcher getRequestDispatcher(String path) {
// Identify the Wrapper for the servlet to process this request
Wrapper wrapper = context.map(path);
if (wrapper == null)
return (null);
// Create and return a corresponding request dispatcher
return (null); // FIXME - getRequestDispatcher()
}
/**
* Return the requested resource as an <code>InputStream</code>. The
* path must be specified according to the rules described under
* <code>getResource</code>. If no such resource can be identified,
* return <code>null</code>.
*
* @param path The path to the desired resource.
*/
public InputStream getResourceAsStream(String path) {
Resources resources = context.getResources();
if (resources == null)
return (null);
else
return (resources.getResourceAsStream(path));
}
/**
* Return the name and version of the servlet container.
*/
public String getServerInfo() {
return (Constants.SERVER_INFO);
}
/**
* @deprecated As of Java Servlet API 2.1, with no direct replacement.
*/
public Servlet getServlet(String name) {
return (null);
}
/**
* @deprecated As of Java Servlet API 2.1, with no direct replacement.
*/
public Enumeration getServletNames() {
return ((new Vector()).elements());
}
/**
* @deprectated As of Java Servlet API 2.1, with no direct replacement.
*/
public Enumeration getServlets() {
return ((new Vector()).elements());
}
/**
* Writes the specified message to a servlet log file.
*
* @param message Message to be written
*/
public void log(String message) {
Logger logger = context.getLogger();
if (logger != null)
logger.log(message);
}
/**
* Writes the specified exception and message to a servlet log file.
*
* @param exception Exception to be reported
* @param message Message to be written
*
* @deprecated As of Java Servlet API 2.1, use
* <code>log(String, Throwable)</code> instead
*/
public void log(Exception exception, String message) {
Logger logger = context.getLogger();
if (logger != null)
logger.log(exception, message);
}
/**
* Writes the specified message and exception to a servlet log file.
*
* @param message Message to be written
* @param throwable Exception to be reported
*/
public void log(String message, Throwable throwable) {
Logger logger = context.getLogger();
if (logger != null)
logger.log(message, throwable);
}
/**
* Remove the context attribute with the specified name, if any.
*
* @param name Name of the context attribute to be removed
*/
public void removeAttribute(String name) {
attributes.remove(name);
}
/**
* Bind the specified value with the specified context attribute name,
* replacing any existing value for that name.
*
* @param name Attribute name to be bound
* @param value New attribute value to be bound
*/
public void setAttribute(String name, Object value) {
attributes.put(name, value);
}
}