You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cr...@locus.apache.org on 2000/05/21 04:39:58 UTC

cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup HostConfig.java LocalStrings.properties

craigmcc    00/05/20 19:39:58

  Modified:    proposals/catalina/src/conf server.xml
               proposals/catalina/src/share/org/apache/tomcat/resources
                        LocalStrings.properties StandardResources.java
               proposals/catalina/src/share/org/apache/tomcat/startup
                        HostConfig.java LocalStrings.properties
  Log:
  Complete the implementation of the "application root" concept by making
  the "document root" settings for Contexts relative to the application root,
  rather than the "catalina.home" directory.
  
  Implement automatic expansion of JAR files found in the "application root"
  directory of a Host, if and only if there is no corresponding directory
  already in existence.
  
  Implement automatic deployment of web application directories found in the
  "application root", if and only if the directory appears to contain a web
  application.  For the purposes of this test, a WEB-INF/web.xml file must
  exist for the directory to be considered a web application.
  
  Revision  Changes    Path
  1.27      +3 -27     jakarta-tomcat/proposals/catalina/src/conf/server.xml
  
  Index: server.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/conf/server.xml,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- server.xml	2000/05/20 22:13:29	1.26
  +++ server.xml	2000/05/21 02:39:56	1.27
  @@ -44,7 +44,7 @@
         userRoleTable="user_roles" roleNameCol="role_name" />
   -->
   
  -    <Host name="localhost" debug="0" root="true" appBase=".">
  +    <Host name="localhost" debug="0" root="true" appBase="webapps">
   
         <!-- Access log processes all requests for this virtual host -->
         <Valve className="org.apache.tomcat.valves.AccessLogValve"
  @@ -57,39 +57,15 @@
   	      timestamp="true"/>
   
         <!-- Tomcat Root Context -->
  -      <Context path="" docBase="webapps/ROOT" debug="0"/>
  +      <Context path="" docBase="ROOT" debug="0"/>
   
         <!-- Tomcat Examples Context -->
  -      <Context path="/examples" docBase="webapps/examples" debug="0"
  +      <Context path="/examples" docBase="examples" debug="0"
                  reloadable="true">
   	<Logger className="org.apache.tomcat.logger.FileLogger"
   	        prefix="localhost_examples_log." suffix=".txt"
   		timestamp="true"/>
         </Context>
  -
  -      <!-- Watchdog JSP Tests Context -->
  -      <Context path="/jsp-tests" docBase="webapps/jsp-tests" debug="0">
  -        <Logger className="org.apache.tomcat.logger.FileLogger"
  -                prefix="localhost_jsp-tests_log." suffix=".txt"
  -                timestamp="true"/>
  -      </Context>
  -
  -      <!-- Watchdog Servlet Tests Context -->
  -      <Context path="/servlet-tests" docBase="webapps/servlet-tests" debug="0">
  -        <Logger className="org.apache.tomcat.logger.FileLogger"
  -                prefix="localhost_servlet-tests_log." suffix=".txt"
  -		timestamp="true"/>
  -      </Context>
  -
  -      <!-- Struts Example Context -->
  -<!--
  -      <Context path="/struts" docBase="/home/cmcclanahan/Source/build/struts"
  -               debug="0">
  -        <Logger className="org.apache.tomcat.logger.FileLogger"
  -                prefix="localhost_struts_log." suffix=".txt"
  -                timestamp="true"/>
  -      </Context>
  --->
   
       </Host>
   
  
  
  
  1.3       +1 -0      jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/LocalStrings.properties,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LocalStrings.properties	2000/04/27 05:55:17	1.2
  +++ LocalStrings.properties	2000/05/21 02:39:56	1.3
  @@ -1,5 +1,6 @@
   standardResources.alreadyStarted=Resources has already been started
   standardResources.directory=File base {0} is not a directory
  +standardResources.exists=File base {0} does not exist
   standardResources.notStarted=Resources has not yet been started
   standardResources.null=Document base cannot be null
   standardResources.slash=Document base {0} must not end with a slash
  
  
  
  1.9       +61 -14    jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/StandardResources.java
  
  Index: StandardResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/StandardResources.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- StandardResources.java	2000/05/16 20:25:55	1.8
  +++ StandardResources.java	2000/05/21 02:39:57	1.9
  @@ -1,13 +1,13 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/StandardResources.java,v 1.8 2000/05/16 20:25:55 craigmcc Exp $
  - * $Revision: 1.8 $
  - * $Date: 2000/05/16 20:25:55 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/resources/StandardResources.java,v 1.9 2000/05/21 02:39:57 craigmcc Exp $
  + * $Revision: 1.9 $
  + * $Date: 2000/05/21 02:39:57 $
    *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  + * Copyright (c) 1999 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -15,7 +15,7 @@
    * are met:
    *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  + *    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
  @@ -23,15 +23,15 @@
    *    distribution.
    *
    * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowlegement:  
  - *       "This product includes software developed by the 
  + *    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 
  + *    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"
  @@ -59,7 +59,7 @@
    *
    * [Additional notices, if required by prior licensing conditions]
    *
  - */ 
  + */
   
   
   package org.apache.tomcat.resources;
  @@ -77,6 +77,7 @@
   import java.util.Vector;
   import org.apache.tomcat.Container;
   import org.apache.tomcat.Context;
  +import org.apache.tomcat.Host;
   import org.apache.tomcat.Lifecycle;
   import org.apache.tomcat.LifecycleEvent;
   import org.apache.tomcat.LifecycleException;
  @@ -100,7 +101,7 @@
    * </ul>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.8 $ $Date: 2000/05/16 20:25:55 $
  + * @version $Revision: 1.9 $ $Date: 2000/05/21 02:39:57 $
    */
   
   public final class StandardResources
  @@ -294,15 +295,17 @@
   	if (fileBase != null) {
   	    File file = new File(fileBase);
   	    if (!file.isAbsolute()) {
  -		file = new File(System.getProperty("catalina.home"), fileBase);
  +		file = new File(hostBase(), fileBase);
   		fileBase = file.getAbsolutePath();
  -		file = new File(fileBase);
   	    }
   	    if (debug >= 1)
   		log(" Also setting fileBase to '" + fileBase + "'");
   	    if (!file.isDirectory())
   		throw new IllegalArgumentException
   		    (sm.getString("standardResources.directory", fileBase));
  +	    if (!file.exists())
  +	    	throw new IllegalArgumentException
  +	    	    (sm.getString("standardResources.exists", fileBase));
   	}
   
   
  @@ -384,10 +387,16 @@
        */
       public String getRealPath(String path) {
   
  -	if ((fileBase == null) || (path == null) || (!path.startsWith("/")))
  +	if ((fileBase == null) || (path == null) ||
  +	    (!path.startsWith("/") && !path.startsWith("\\"))) {
  +//	    if (debug >= 2)
  +//	    	log("getRealPath(" + path + ") --> null");
   	    return (null);
  -	else
  +	} else {
  +//	    if (debug >= 2)
  +//	        log("getRealPath(" + path + ") --> " + fileBase + path);
   	    return (fileBase + path);
  +	}
   
       }
   
  @@ -571,6 +580,44 @@
   
   
       // -------------------------------------------------------- Private Methods
  +
  +
  +    /**
  +     * Return a File object representing the base directory for the
  +     * entire servlet container (i.e. the Engine container if present).
  +     */
  +    private File engineBase() {
  +
  +	return (new File(System.getProperty("catalina.home")));
  +
  +    }
  +
  +
  +    /**
  +     * Return a File object representing the base directory for the
  +     * current virtual host (i.e. the Host container if present).
  +     */
  +    private File hostBase() {
  +
  +	// Locate our immediately surrounding Host (if any)
  +	Container container = this.container;
  +	while (container != null) {
  +	    if (container instanceof Host)
  +		break;
  +	    container = container.getParent();
  +	}
  +	if (container == null)
  +	    return (engineBase());
  +
  +	// Use the "appBase" property of this container
  +	String appBase = ((Host) container).getAppBase();
  +	File file = new File(appBase);
  +	if (!file.isAbsolute())
  +	    file = new File(engineBase(), appBase);
  +	return (file);
  +
  +    }
  +
   
   
       /**
  
  
  
  1.2       +232 -4    jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/HostConfig.java
  
  Index: HostConfig.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/HostConfig.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HostConfig.java	2000/05/20 22:13:35	1.1
  +++ HostConfig.java	2000/05/21 02:39:57	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/HostConfig.java,v 1.1 2000/05/20 22:13:35 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/05/20 22:13:35 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/HostConfig.java,v 1.2 2000/05/21 02:39:57 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/05/21 02:39:57 $
    *
    * ====================================================================
    *
  @@ -65,14 +65,20 @@
   package org.apache.tomcat.startup;
   
   
  +import java.io.BufferedOutputStream;
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
  +import java.io.FileOutputStream;
   import java.io.InputStream;
   import java.io.IOException;
   import java.lang.reflect.InvocationTargetException;
   import java.net.MalformedURLException;
   import java.net.URL;
  +import java.util.Enumeration;
  +import java.util.zip.ZipEntry;
  +import java.util.zip.ZipFile;
  +import org.apache.tomcat.Context;
   import org.apache.tomcat.Host;
   import org.apache.tomcat.Lifecycle;
   import org.apache.tomcat.LifecycleEvent;
  @@ -86,7 +92,7 @@
    * of that Host, and the associated defined contexts.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2000/05/20 22:13:35 $
  + * @version $Revision: 1.2 $ $Date: 2000/05/21 02:39:57 $
    */
   
   public final class HostConfig
  @@ -97,6 +103,18 @@
   
   
       /**
  +     * The Java class name of the Context configuration class we should use.
  +     */
  +    private String configClass = "org.apache.tomcat.startup.ContextConfig";
  +
  +
  +    /**
  +     * The Java class name of the Context implementation we should use.
  +     */
  +    private String contextClass = "org.apache.tomcat.core.StandardContext";
  +
  +
  +    /**
        * The debugging detail level for this component.
        */
       private int debug = 0;
  @@ -119,6 +137,50 @@
   
   
       /**
  +     * Return the Context configuration class name.
  +     */
  +    public String getConfigClass() {
  +
  +	return (this.configClass);
  +
  +    }
  +
  +
  +    /**
  +     * Set the Context configuration class name.
  +     *
  +     * @param configClass The new Context configuration class name.
  +     */
  +    public void setConfigClass(String configClass) {
  +
  +	this.configClass = configClass;
  +
  +    }
  +
  +
  +    /**
  +     * Return the Context implementation class name.
  +     */
  +    public String getContextClass() {
  +
  +	return (this.contextClass);
  +
  +    }
  +
  +
  +    /**
  +     * Set the Context implementation class name.
  +     *
  +     * @param contextClass The new Context implementation class name.
  +     */
  +    public void setContextClass(String contextClass) {
  +
  +	this.contextClass = contextClass;
  +
  +    }
  +
  +
  +    /**
        * Return the debugging detail level for this component.
        */
       public int getDebug() {
  @@ -171,6 +233,169 @@
   
   
       /**
  +     * Return a File object representing the "application root" directory
  +     * for our associated Host.
  +     */
  +    private File appBase() {
  +
  +	File file = new File(host.getAppBase());
  +	if (!file.isAbsolute())
  +	    file = new File(System.getProperty("catalina.home"),
  +	    		    host.getAppBase());
  +	return (file);
  +
  +    }
  +
  +
  +    /**
  +     * Deploy any directories found in our "application root" directory that
  +     * look like they contain a web application.  For the purposes of this
  +     * method, the directory must contain a WEB-INF subdirectory that contains
  +     * a web application deployment descriptor file (<code>web.xml</code>).
  +     */
  +    private void deploy() {
  +
  +	if (debug >= 1)
  +	    log(sm.getString("hostConfig.deploying"));
  +
  +	// Discover and deploy web application directories as necessary
  +	File appBase = appBase();
  +	if (!appBase.exists() || !appBase.isDirectory())
  +	    return;
  +	String files[] = appBase.list();
  +
  +	for (int i = 0; i < files.length; i++) {
  +
  +	    File dir = new File(appBase, files[i]);
  +	    if (!dir.isDirectory())
  +	        continue;
  +	    File webXml = new File(dir, "/WEB-INF/web.xml");
  +	    if (!webXml.exists() || !webXml.isFile() ||
  +	        !webXml.canRead())
  +	        continue;
  +	    String contextPath = "/" + files[i];
  +	    if (files[i].equals("ROOT"))
  +	        contextPath = "";
  +	    if (host.findChild(contextPath) != null)
  +	        continue;
  +
  +	    log(sm.getString("hostConfig.deploy", files[i]));
  +	    try {
  +		Class clazz = Class.forName(contextClass);
  +		Context context =
  +		  (Context) clazz.newInstance();
  +		context.setPath(contextPath);
  +		context.setDocBase(files[i]);
  +		if (context instanceof Lifecycle) {
  +		    clazz = Class.forName(configClass);
  +		    LifecycleListener listener =
  +		      (LifecycleListener) clazz.newInstance();
  +		    ((Lifecycle) context).addLifecycleListener(listener);
  +		}
  +		host.addChild(context);
  +	    } catch (Exception e) {
  +		log(sm.getString("hostConfig.deploy.error", files[i]), e);
  +	    }
  +
  +	}
  +
  +    }
  +
  +
  +    /**
  +     * Expand any JAR files found in our "application root" directory that
  +     * do not have a corresponding directory without the ".jar" extension.
  +     */
  +    private void expand() {
  +
  +	if (debug >= 1)
  +	    log(sm.getString("hostConfig.expanding"));
  +
  +	// Discover and expand JAR files as necessary
  +	File appBase = appBase();
  +	if (!appBase.exists() || !appBase.isDirectory())
  +	    return;
  +	String files[] = appBase.list();
  +
  +	for (int i = 0; i < files.length; i++) {
  +
  +	    if (!files[i].endsWith(".jar"))
  +		continue;
  +	    File dir = new File(appBase,
  +				files[i].substring(0, files[i].length() - 4));
  +	    if (dir.exists())
  +		continue;
  +
  +	    log(sm.getString("hostConfig.expand", files[i]));
  +	    ZipFile zipFile = null;
  +	    try {
  +		dir.mkdirs();
  +		zipFile = new ZipFile(new File(appBase, files[i]));
  +		Enumeration zipEntries = zipFile.entries();
  +		while (zipEntries.hasMoreElements()) {
  +		    ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement();
  +		    String name = zipEntry.getName();
  +		    int slash = name.lastIndexOf("/");
  +		    if (slash >= 0) {
  +			File parent = new File(dir,
  +					       name.substring(0, slash));
  +			if (debug >= 2)
  +			    log(" Creating parent directory " + parent);
  +			parent.mkdirs();
  +		    }
  +		    if (name.endsWith("/"))
  +		        continue;
  +		    if (debug >= 2)
  +		        log(" Creating expanded file " + name);
  +		    InputStream input = zipFile.getInputStream(zipEntry);
  +		    expand(input, dir, name);
  +		}
  +	    } catch (Exception e) {
  +		log(sm.getString("hostConfig.expand.error", files[i]), e);
  +		if (zipFile != null) {
  +		    try {
  +			zipFile.close();
  +		    } catch (Exception f) {
  +			;
  +		    }
  +		}
  +	    }
  +
  +	}
  +
  +    }
  +
  +
  +    /**
  +     * Expand the specified input stream into the specified directory, into
  +     * a file named from the specified relative filename path.
  +     *
  +     * @param input InputStream to be copied
  +     * @param directory Base directory into which the file is created
  +     * @param path Relative pathname of the file to be created
  +     *
  +     * @exception IOException if any processing exception occurs
  +     */
  +    private void expand(InputStream input, File directory, String path)
  +        throws IOException {
  +
  +	File file = new File(directory, path);
  +	BufferedOutputStream output =
  +	  new BufferedOutputStream(new FileOutputStream(file));
  +	byte buffer[] = new byte[2048];
  +	while (true) {
  +	    int n = input.read(buffer);
  +	    if (n <= 0)
  +	        break;
  +	    output.write(buffer, 0, n);
  +	}
  +	output.close();
  +	input.close();
  +
  +    }
  +
  +
  +    /**
        * Log a message on the Logger associated with our Host (if any)
        *
        * @param message Message to be logged
  @@ -220,6 +445,9 @@
   
   	if (debug > 0)
   	    log(sm.getString("hostConfig.start"));
  +
  +	expand();
  +	deploy();
   
       }
   
  
  
  
  1.7       +6 -0      jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/LocalStrings.properties,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- LocalStrings.properties	2000/05/20 22:13:35	1.6
  +++ LocalStrings.properties	2000/05/21 02:39:57	1.7
  @@ -20,5 +20,11 @@
   engineConfig.start=EngineConfig: Processing START
   engineConfig.stop=EngineConfig: Processing STOP
   hostConfig.cce=Lifecycle event data object {0} is not a Host
  +hostConfig.deploy=Deploying web application directory {0}
  +hostConfig.deploy.error=Exception while deploying web application directory {0}
  +hostConfig.deploying=Deploying discovered web application directories
  +hostConfig.expand=Expanding web application archive {0}
  +hostConfig.expand.error=Exception while expanding web application archive {0}
  +hostConfig.expanding=Expanding discovered web application archives
   hostConfig.start=HostConfig: Processing START
   hostConfig.stop=HostConfig: Processing STOP