/*
* $Header: /home/cvs/jakarta-slide/src/wrappers/catalina/AdminHost.java,v 1.3 2001/10/19 07:43:10 remm Exp $
* $Revision: 1.3 $
* $Date: 2001/10/19 07:43:10 $
*
* ====================================================================
*
* 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 wrappers.catalina;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Deployer;
import org.apache.catalina.Globals;
import org.apache.catalina.HttpRequest;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Request;
import org.apache.catalina.Response;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.ApplicationContext;
import org.apache.slide.common.EmbeddedDomain;
import org.apache.slide.common.Namespace;
import org.apache.slide.common.NamespaceAccessToken;
import org.apache.slide.webdav.WebdavServlet;
/**
* Slide administrator implementation of Host.
*
* @author Remy Maucherat
* @version $Revision: 1.3 $ $Date: 2001/10/19 07:43:10 $
*/
public class AdminHost
extends StandardHost
implements Deployer, Host {
// ----------------------------------------------------------- Constructors
/**
* Create a new AdminHost component with the default basic Valve.
*/
public AdminHost() {
super();
}
// ----------------------------------------------------- Instance Variables
/**
* Slide domain.
*/
EmbeddedDomain domain;
// ------------------------------------------------------------- Properties
/**
* Domain setter.
*/
public void setDomain(EmbeddedDomain domain) {
this.domain = domain;
}
/**
* Domain getter.
*/
public EmbeddedDomain getDomain() {
return domain;
}
// --------------------------------------------------------- Public Methods
/**
* Prepare for active use of the public methods of this Component.
*
* @exception IllegalStateException if this component has already been
* started
* @exception LifecycleException if this component detects a fatal error
* that prevents it from being started
*/
public synchronized void start() throws LifecycleException {
String defaultNamespace = domain.getDefaultNamespace();
Enumeration namespaceNames = domain.enumerateNamespaces();
while (namespaceNames.hasMoreElements()) {
String name = (String) namespaceNames.nextElement();
try {
URL adminWar = getAdminWebappURL();
install("/" + name, adminWar);
if (name.equals(defaultNamespace)) {
// Also install the deafult namespace as the root context
install("", adminWar);
}
} catch (IOException e) {
// Can't happen
e.printStackTrace();
}
}
super.start();
}
// ------------------------------------------------------- Deployer Methods
/**
* Install a new web application, whose web application archive is at the
* specified URL, into this container with the specified context path.
* A context path of "" (the empty string) should be used for the root
* application for this container. Otherwise, the context path must
* start with a slash.
* <p>
* If this application is successfully installed, a ContainerEvent of type
* <code>INSTALL_EVENT</code> will be sent to all registered listeners,
* with the newly created <code>Context</code> as an argument.
*
* @param contextPath The context path to which this application should
* be installed (must be unique)
* @param war A URL of type "jar:" that points to a WAR file, or type
* "file:" that points to an unpacked directory structure containing
* the web application to be installed
*
* @exception IllegalArgumentException if the specified context path
* is malformed (it must be "" or start with a slash)
* @exception IllegalArgumentException if the specified context path
* is already attached to an existing web application
* @exception IOException if an input/output error was encountered
* during install
*/
public void install(String contextPath, URL war) throws IOException {
// Validate the format and state of our arguments
if (contextPath == null)
throw new IllegalArgumentException
(sm.getString("standardHost.pathRequired"));
if (!contextPath.equals("") && !contextPath.startsWith("/"))
throw new IllegalArgumentException
(sm.getString("standardHost.pathFormat", contextPath));
if (findDeployedApp(contextPath) != null)
throw new IllegalArgumentException
(sm.getString("standardHost.pathUsed", contextPath));
if (war == null)
throw new IllegalArgumentException
(sm.getString("standardHost.warRequired"));
// Prepare the local variables we will require
String url = war.toString();
String docBase = null;
log(sm.getString("standardHost.installing", contextPath, url));
if (url.startsWith("jar:")) {
url = url.substring(4, url.length() - 2);
}
if (url.startsWith("file://"))
docBase = url.substring(7);
else if (url.startsWith("file:"))
docBase = url.substring(5);
else
throw new IllegalArgumentException
(sm.getString("standardHost.warURL", url));
// Install this new web application
try {
Class clazz = Class.forName(getContextClass());
Context context = (Context) clazz.newInstance();
context.setPath(contextPath);
context.setDocBase(docBase);
String namespaceName = contextPath;
while (namespaceName.startsWith("/")) {
namespaceName = namespaceName.substring(1);
}
if (namespaceName.equals("")) {
namespaceName = domain.getDefaultNamespace();
}
context.getServletContext().setAttribute
("org.apache.slide.NamespaceName", namespaceName);
((ApplicationContext) context.getServletContext())
.setAttributeReadOnly("org.apache.slide.NamespaceName");
NamespaceAccessToken accessToken =
domain.getNamespaceToken(namespaceName);
SlideRealm slideRealm = new SlideRealm();
slideRealm.setAccessToken(accessToken);
slideRealm.setNamespace(namespaceName);
context.setRealm(slideRealm);
context.getServletContext().setAttribute
(WebdavServlet.ATTRIBUTE_NAME, accessToken);
((ApplicationContext) context.getServletContext())
.setAttributeReadOnly(WebdavServlet.ATTRIBUTE_NAME);
Namespace namespace = domain.getNamespace(namespaceName);
context.getServletContext().setAttribute
("org.apache.slide.Namespace", namespace);
((ApplicationContext) context.getServletContext())
.setAttributeReadOnly("org.apache.slide.Namespace");
if (context instanceof Lifecycle) {
clazz = Class.forName(getConfigClass());
LifecycleListener listener =
(LifecycleListener) clazz.newInstance();
((Lifecycle) context).addLifecycleListener(listener);
}
addChild(context);
fireContainerEvent(INSTALL_EVENT, context);
} catch (Exception e) {
log(sm.getString("standardHost.installError", contextPath), e);
throw new IOException(e.toString());
}
}
// ------------------------------------------------------ Protected Methods
/**
* Return the full path to the admin webapp.
*/
protected URL getAdminWebappURL()
throws IOException {
File file = new File(System.getProperty("catalina.base"));
file = new File(file, "slide/slide-admin.war");
return new URL("file", null, file.getCanonicalPath());
}
}