craigmcc 02/02/28 19:04:44 Modified: catalina/src/share/org/apache/catalina/core StandardHostDeployer.java catalina/src/share/org/apache/catalina/servlets ManagerServlet.java catalina/src/share/org/apache/catalina/startup HostConfig.java Log: Restore the unpackWARs behavior (but only for WAR files in the appBase subdirectory) that was broken by previous changes to deployment and the Manager webapp's servlet. The WAR-expansion logic was refactored from StandardHostDeployer to HostConfig, which is the only place that requires it. Applications that are added via the Manager webapp's "/install" or "/deploy" commands are *not* auto-expanded -- if you want your webapp to run from an unpacked directory, you should install an unpacked directory. NOTE: You can also get auto-unpack service by simply dropping a WAR file into the appBase directory while Tomcat is running - a background thread will notice this and unpack it in the same way that startup works. NOTE: As before, any auto-unpacked directory is *never* deleted, because the user has probably changed things and would be very annoyed if it was removed. I'm going to make a pass through the JavaDocs and the documentation files to make sure that they are all up to date with respect to the actual functionality. Revision Changes Path 1.5 +32 -226 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardHostDeployer.java Index: StandardHostDeployer.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardHostDeployer.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- StandardHostDeployer.java 27 Feb 2002 01:17:00 -0000 1.4 +++ StandardHostDeployer.java 1 Mar 2002 03:04:43 -0000 1.5 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardHostDeployer.java,v 1.4 2002/02/27 01:17:00 craigmcc Exp $ - * $Revision: 1.4 $ - * $Date: 2002/02/27 01:17:00 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardHostDeployer.java,v 1.5 2002/03/01 03:04:43 craigmcc Exp $ + * $Revision: 1.5 $ + * $Date: 2002/03/01 03:04:43 $ * * ==================================================================== * @@ -94,7 +94,7 @@ * <code>StandardHost</code> implementation class.</p> * * @author Craig R. McClanahan - * @version $Revision: 1.4 $ $Date: 2002/02/27 01:17:00 $ + * @version $Revision: 1.5 $ $Date: 2002/03/01 03:04:43 $ */ public class StandardHostDeployer implements Deployer { @@ -206,9 +206,10 @@ * @exception IllegalStateException if the specified context path * is already attached to an existing web application * @exception IOException if an input/output error was encountered - * during install + * during installation */ - public void install(String contextPath, URL war) throws IOException { + public synchronized void install(String contextPath, URL war) + throws IOException { // Validate the format and state of our arguments if (contextPath == null) @@ -224,49 +225,23 @@ throw new IllegalArgumentException (sm.getString("standardHost.warRequired")); - // Prepare the local variables we will require + // Calculate the document base for the new web application + host.log(sm.getString("standardHost.installing", + contextPath, war.toString())); String url = war.toString(); String docBase = null; - host.log(sm.getString("standardHost.installing", contextPath, url)); - - // Expand a WAR archive into an unpacked directory if needed - // NOTE: If the user supplies a "jar:file:" URL, assume that - // they do not want WAR expansion even if unpackWARs is set - if (host.isUnpackWARs() && !url.startsWith("jar:file:")) { - - if (url.startsWith("jar:")) - docBase = expand(war); - else 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)); - - // Make sure the document base directory exists and is readable - File docBaseDir = new File(docBase); - if (!docBaseDir.exists() || !docBaseDir.isDirectory() || - !docBaseDir.canRead()) - throw new IllegalArgumentException - (sm.getString("standardHost.accessBase", docBase)); - - } else { - - 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)); - + 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 + // Install the new web application try { Class clazz = Class.forName(host.getContextClass()); Context context = (Context) clazz.newInstance(); @@ -321,54 +296,26 @@ throw new IllegalArgumentException (sm.getString("standardHost.configRequired")); - // Prepare the local variables we will require + // Calculate the document base for the new web application (if needed) String docBase = null; // Optional override for value in config file - - // Expand a WAR archive into an unpacked directory if needed if (war != null) { - String url = war.toString(); host.log(sm.getString("standardHost.installingWAR", url)); - - if (host.isUnpackWARs()) { - - // Calculate the document base directory pathname - if (url.startsWith("jar:")) - docBase = expand(war); - else 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)); - - // Make sure the document base directory exists and is readable - File docBaseDir = new File(docBase); - if (!docBaseDir.exists() || !docBaseDir.isDirectory() || - !docBaseDir.canRead()) - throw new IllegalArgumentException - (sm.getString("standardHost.accessBase", docBase)); - - } else { - - // Calculate the WAR file absolute pathname - 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)); - + // Calculate the WAR file absolute pathname + 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 + // Install the new web application this.context = null; this.overrideDocBase = docBase; InputStream stream = null; @@ -608,147 +555,6 @@ digester.addRuleSet(namingRuleSet); } return (digester); - - } - - - /** - * Expand the WAR file found at the specified URL into an unpacked - * directory structure, and return the absolute pathname to the expanded - * directory. - * - * @param war URL of the web application archive to be expanded - * (must start with "jar:") - * - * @exception IllegalArgumentException if this is not a "jar:" URL - * @exception IOException if an input/output error was encountered - * during expansion - */ - protected String expand(URL war) throws IOException { - - // Calculate the directory name of the expanded directory - if (host.getDebug() >= 1) - host.log("expand(" + war.toString() + ")"); - String pathname = war.toString().replace('\\', '/'); - if (pathname.endsWith("!/")) - pathname = pathname.substring(0, pathname.length() - 2); - int period = pathname.lastIndexOf('.'); - if (period >= pathname.length() - 4) - pathname = pathname.substring(0, period); - int slash = pathname.lastIndexOf('/'); - if (slash >= 0) - pathname = pathname.substring(slash + 1); - if (host.getDebug() >= 1) - host.log(" Proposed directory name: " + pathname); - - // Make sure that there is no such directory already existing - File appBase = new File(host.getAppBase()); - if (!appBase.isAbsolute()) - appBase = new File(System.getProperty("catalina.base"), - host.getAppBase()); - if (!appBase.exists() || !appBase.isDirectory()) - throw new IOException - (sm.getString("standardHost.appBase", - appBase.getAbsolutePath())); - File docBase = new File(appBase, pathname); - if (docBase.exists()) { - // War file is already installed - return (docBase.getAbsolutePath()); - } - docBase.mkdir(); - if (host.getDebug() >= 2) - host.log(" Have created expansion directory " + - docBase.getAbsolutePath()); - - // Expand the WAR into the new document base directory - JarURLConnection juc = (JarURLConnection) war.openConnection(); - juc.setUseCaches(false); - JarFile jarFile = juc.getJarFile(); - if (host.getDebug() >= 2) - host.log(" Have opened JAR file successfully"); - Enumeration jarEntries = jarFile.entries(); - if (host.getDebug() >= 2) - host.log(" Have retrieved entries enumeration"); - while (jarEntries.hasMoreElements()) { - JarEntry jarEntry = (JarEntry) jarEntries.nextElement(); - String name = jarEntry.getName(); - if (host.getDebug() >= 2) - host.log(" Am processing entry " + name); - int last = name.lastIndexOf('/'); - if (last >= 0) { - File parent = new File(docBase, - name.substring(0, last)); - if (host.getDebug() >= 2) - host.log(" Creating parent directory " + parent); - parent.mkdirs(); - } - if (name.endsWith("/")) - continue; - if (host.getDebug() >= 2) - host.log(" Creating expanded file " + name); - InputStream input = jarFile.getInputStream(jarEntry); - expand(input, docBase, name); - input.close(); - } - jarFile.close(); - - // Return the absolute path to our new document base directory - return (docBase.getAbsolutePath()); - - } - - - /** - * Expand the specified input stream into the specified directory, creating - * a file named from the specified relative path. - * - * @param input InputStream to be copied - * @param docBase Document base directory into which we are expanding - * @param name Relative pathname of the file to be created - * - * @exception IOException if an input/output error occurs - */ - protected void expand(InputStream input, File docBase, String name) - throws IOException { - - File file = new File(docBase, name); - 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(); - - } - - - /** - * Remove the specified directory and all of its contents. - * - * @param dir Directory to be removed - * - * @exception IOException if an input/output error occurs - */ - protected void remove(File dir) throws IOException { - - String list[] = dir.list(); - for (int i = 0; i < list.length; i++) { - File file = new File(dir, list[i]); - if (file.isDirectory()) { - remove(file); - } else { - if (!file.delete()) - throw new IOException("Cannot delete file " + - file.getAbsolutePath()); - } - } - if (!dir.delete()) - throw new IOException("Cannot delete directory " + - dir.getAbsolutePath()); } 1.16 +7 -43 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/ManagerServlet.java Index: ManagerServlet.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/ManagerServlet.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- ManagerServlet.java 28 Feb 2002 06:10:55 -0000 1.15 +++ ManagerServlet.java 1 Mar 2002 03:04:44 -0000 1.16 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/ManagerServlet.java,v 1.15 2002/02/28 06:10:55 craigmcc Exp $ - * $Revision: 1.15 $ - * $Date: 2002/02/28 06:10:55 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/ManagerServlet.java,v 1.16 2002/03/01 03:04:44 craigmcc Exp $ + * $Revision: 1.16 $ + * $Date: 2002/03/01 03:04:44 $ * * ==================================================================== * @@ -182,7 +182,7 @@ * </ul> * * @author Craig R. McClanahan - * @version $Revision: 1.15 $ $Date: 2002/02/28 06:10:55 $ + * @version $Revision: 1.16 $ $Date: 2002/03/01 03:04:44 $ */ public class ManagerServlet @@ -453,7 +453,7 @@ return; } - // Upload the web application archive to a temporary JAR file + // Upload the web application archive to a local WAR file File tempDir = (File) getServletContext().getAttribute ("javax.servlet.context.tempdir"); File localWar = new File(tempDir, basename + ".war"); @@ -505,35 +505,6 @@ /** - * Expand the specified input stream into the specified dirctory, creating - * a file named from the specified relative path. - * - * @param istream InputStream to be copied - * @param docBaseDir Document base directory into which we are expanding - * @param name Relative pathname of the file to be created - * - * @exception IOException if an input/output error occurs - */ - protected void deployExpand(InputStream istream, File docBaseDir, - String name) throws IOException { - - File file = new File(docBaseDir, name); - BufferedOutputStream ostream = - new BufferedOutputStream(new FileOutputStream(file)); - byte buffer[] = new byte[2048]; - while (true) { - int n = istream.read(buffer); - if (n <= 0) { - break; - } - ostream.write(buffer, 0, n); - } - ostream.close(); - - } - - - /** * Install an application for the specified path from the specified * web application archive. * @@ -937,14 +908,7 @@ docBaseDir = new File(appBaseDir, docBase); } String docBasePath = docBaseDir.getCanonicalPath(); - boolean ok = false; - if (docBasePath.startsWith(tempDirPath)) { - ok = true; - } else if ((appBasePath != null) && - docBasePath.startsWith(appBasePath)) { - ok = true; - } - if (!ok) { + if (!docBasePath.startsWith(tempDirPath)) { writer.println(sm.getString("managerServlet.noDocBase", displayPath)); return; @@ -958,7 +922,7 @@ if (docBaseDir.isDirectory()) { undeployDir(docBaseDir); } else { - docBaseDir.delete(); // WAR file + docBaseDir.delete(); // Delete the WAR file } writer.println(sm.getString("managerServlet.undeployed", displayPath)); 1.17 +195 -14 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/HostConfig.java Index: HostConfig.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/HostConfig.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- HostConfig.java 1 Feb 2002 19:17:34 -0000 1.16 +++ HostConfig.java 1 Mar 2002 03:04:44 -0000 1.17 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/HostConfig.java,v 1.16 2002/02/01 19:17:34 remm Exp $ - * $Revision: 1.16 $ - * $Date: 2002/02/01 19:17:34 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/HostConfig.java,v 1.17 2002/03/01 03:04:44 craigmcc Exp $ + * $Revision: 1.17 $ + * $Date: 2002/03/01 03:04:44 $ * * ==================================================================== * @@ -73,6 +73,7 @@ import java.io.InputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; +import java.net.JarURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -101,7 +102,7 @@ * * @author Craig R. McClanahan * @author Remy Maucherat - * @version $Revision: 1.16 $ $Date: 2002/02/01 19:17:34 $ + * @version $Revision: 1.17 $ $Date: 2002/03/01 03:04:44 $ */ public class HostConfig @@ -174,6 +175,13 @@ /** + * Should we unpack WAR files when auto-deploying applications in the + * <code>appBase</code> directory? + */ + private boolean unpackWARs = false; + + + /** * Last modified dates of the web.xml files of the contexts, keyed by * context name. */ @@ -249,6 +257,28 @@ } + /** + * Return the unpack WARs flag. + */ + public boolean isUnpackWARs() { + + return (this.unpackWARs); + + } + + + /** + * Set the unpack WARs flag. + * + * @param unpackWARs The new unpack WARs flag + */ + public void setUnpackWARs(boolean unpackWARs) { + + this.unpackWARs = unpackWARs; + + } + + // --------------------------------------------------------- Public Methods @@ -264,8 +294,10 @@ host = (Host) event.getLifecycle(); if (host instanceof StandardHost) { int hostDebug = ((StandardHost) host).getDebug(); - if (hostDebug > this.debug) + if (hostDebug > this.debug) { this.debug = hostDebug; + } + setUnpackWARs(((StandardHost) host).isUnpackWARs()); } } catch (ClassCastException e) { log(sm.getString("hostConfig.cce", event.getLifecycle()), e); @@ -386,15 +418,35 @@ if (host.findChild(contextPath) != null) continue; - // Deploy the application in this WAR file - log(sm.getString("hostConfig.deployJar", files[i])); - try { - URL url = new URL("file", null, dir.getCanonicalPath()); - url = new URL("jar:" + url.toString() + "!/"); - ((Deployer) host).install(contextPath, url); - } catch (Throwable t) { - log(sm.getString("hostConfig.deployJar.error", files[i]), - t); + if (isUnpackWARs()) { + + // Expand and deploy this application as a directory + log(sm.getString("hostConfig.expand", files[i])); + try { + URL url = new URL("jar:file:" + + dir.getCanonicalPath() + "!/"); + String path = expand(url); + url = new URL("file:" + path); + ((Deployer) host).install(contextPath, url); + } catch (Throwable t) { + log(sm.getString("hostConfig.expand.error", files[i]), + t); + } + + } else { + + // Deploy the application in this WAR file + log(sm.getString("hostConfig.deployJar", files[i])); + try { + URL url = new URL("file", null, + dir.getCanonicalPath()); + url = new URL("jar:" + url.toString() + "!/"); + ((Deployer) host).install(contextPath, url); + } catch (Throwable t) { + log(sm.getString("hostConfig.deployJar.error", + files[i]), t); + } + } } @@ -512,6 +564,135 @@ } + + + /** + * Expand the WAR file found at the specified URL into an unpacked + * directory structure, and return the absolute pathname to the expanded + * directory. + * + * @param war URL of the web application archive to be expanded + * (must start with "jar:") + * + * @exception IllegalArgumentException if this is not a "jar:" URL + * @exception IOException if an input/output error was encountered + * during expansion + */ + protected String expand(URL war) throws IOException { + + // Calculate the directory name of the expanded directory + if (getDebug() >= 1) { + log("expand(" + war.toString() + ")"); + } + String pathname = war.toString().replace('\\', '/'); + if (pathname.endsWith("!/")) { + pathname = pathname.substring(0, pathname.length() - 2); + } + int period = pathname.lastIndexOf('.'); + if (period >= pathname.length() - 4) + pathname = pathname.substring(0, period); + int slash = pathname.lastIndexOf('/'); + if (slash >= 0) { + pathname = pathname.substring(slash + 1); + } + if (getDebug() >= 1) { + log(" Proposed directory name: " + pathname); + } + + // Make sure that there is no such directory already existing + File appBase = new File(host.getAppBase()); + if (!appBase.isAbsolute()) { + appBase = new File(System.getProperty("catalina.base"), + host.getAppBase()); + } + if (!appBase.exists() || !appBase.isDirectory()) { + throw new IOException + (sm.getString("standardHost.appBase", + appBase.getAbsolutePath())); + } + File docBase = new File(appBase, pathname); + if (docBase.exists()) { + // War file is already installed + return (docBase.getAbsolutePath()); + } + + // Create the new document base directory + docBase.mkdir(); + if (getDebug() >= 2) { + log(" Have created expansion directory " + + docBase.getAbsolutePath()); + } + + // Expand the WAR into the new document base directory + JarURLConnection juc = (JarURLConnection) war.openConnection(); + juc.setUseCaches(false); + JarFile jarFile = juc.getJarFile(); + if (getDebug() >= 2) { + log(" Have opened JAR file successfully"); + } + Enumeration jarEntries = jarFile.entries(); + if (getDebug() >= 2) { + log(" Have retrieved entries enumeration"); + } + while (jarEntries.hasMoreElements()) { + JarEntry jarEntry = (JarEntry) jarEntries.nextElement(); + String name = jarEntry.getName(); + if (getDebug() >= 2) { + log(" Am processing entry " + name); + } + int last = name.lastIndexOf('/'); + if (last >= 0) { + File parent = new File(docBase, + name.substring(0, last)); + if (getDebug() >= 2) { + log(" Creating parent directory " + parent); + } + parent.mkdirs(); + } + if (name.endsWith("/")) { + continue; + } + if (getDebug() >= 2) { + log(" Creating expanded file " + name); + } + InputStream input = jarFile.getInputStream(jarEntry); + expand(input, docBase, name); + input.close(); + } + jarFile.close(); + + // Return the absolute path to our new document base directory + return (docBase.getAbsolutePath()); + + } + + + /** + * Expand the specified input stream into the specified directory, creating + * a file named from the specified relative path. + * + * @param input InputStream to be copied + * @param docBase Document base directory into which we are expanding + * @param name Relative pathname of the file to be created + * + * @exception IOException if an input/output error occurs + */ + protected void expand(InputStream input, File docBase, String name) + throws IOException { + + File file = new File(docBase, name); + 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(); + + } /**
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>