/*
* $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/RootContext.java,v 1.1 2000/01/20 06:34:49 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2000/01/20 06:34:49 $
*
* ====================================================================
*
* 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.Context;
import org.apache.tomcat.Host;
import org.apache.tomcat.Logger;
import org.apache.tomcat.Resources;
/**
* Specialized implementation of <code>ServletContext</code> that represents
* the "root" of a virtual host. An instance of this class will typically
* be returned by a call to <code>ServletContext.getContext("/")</code>, if
* allowed by the security configuration of this deployment.
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2000/01/20 06:34:49 $
*/
final class RootContext
implements ServletContext {
// ----------------------------------------------------------- Constructors
/**
* Construct a new instance of this class, associated with the specified
* Host instance.
*
* @param host The associated Host instance
*/
public RootContext(StandardHost host) {
super();
this.host = host;
}
// ----------------------------------------------------- Instance Variables
/**
* The context attributes for this context.
*/
private Hashtable attributes = new Hashtable();
/**
* The Host instance with which we are associated.
*/
private StandardHost host = 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.
*
* @param uri Absolute URI of a resource on the server
*/
public ServletContext getContext(String uri) {
Context context = host.map(uri);
if (context != null)
return (context.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 (null); // Not supported by this servlet context
}
/**
* Return the names of the context's initialization parameters, or an
* empty enumeration if the context has no initialization parameters.
*/
public Enumeration getInitParameterNames() {
return ((new Vector()).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 = host.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) {
return (null); // Not supported by this servlet context
}
/**
* 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 = host.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 = host.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) {
return (null); // Not supported by this servlet context
}
/**
* 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 = host.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 = host.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 = host.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 = host.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);
}
}