001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.container.tomcat;
021:
022: import java.io.File;
023: import java.io.IOException;
024:
025: import org.apache.cactus.integration.ant.util.ResourceUtils;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.taskdefs.Java;
028: import org.apache.tools.ant.types.FilterChain;
029: import org.apache.tools.ant.types.Path;
030: import org.apache.tools.ant.util.FileUtils;
031:
032: /**
033: * Special container support for the Apache Tomcat 3.x servlet container.
034: *
035: * @version $Id: Tomcat3xContainer.java 239003 2004-05-31 20:05:27Z vmassol $
036: */
037: public class Tomcat3xContainer extends AbstractTomcatContainer {
038:
039: // Instance Variables ------------------------------------------------------
040:
041: /**
042: * The temporary directory from which the container will be started.
043: */
044: private File tmpDir;
045:
046: // Public Methods ----------------------------------------------------------
047:
048: /**
049: * Sets the temporary installation directory.
050: *
051: * @param theTmpDir The temporary directory to set
052: */
053: public final void setTmpDir(File theTmpDir) {
054: this .tmpDir = theTmpDir;
055: }
056:
057: // AbstractContainer Implementation ----------------------------------------
058:
059: /**
060: * @see org.apache.cactus.integration.ant.container.Container#getName
061: */
062: public final String getName() {
063: return "Tomcat 3.x";
064: }
065:
066: /**
067: * @see org.apache.cactus.integration.ant.container.Container#startUp
068: */
069: public final void startUp() {
070: try {
071: prepare("cactus/tomcat3x");
072: invoke("start");
073: } catch (IOException ioe) {
074: getLog().error("Failed to startup the container", ioe);
075: throw new BuildException(ioe);
076: }
077: }
078:
079: /**
080: * @see org.apache.cactus.integration.ant.container.Container#shutDown
081: */
082: public final void shutDown() {
083: invoke("stop");
084: }
085:
086: // Private Methods ---------------------------------------------------------
087:
088: /**
089: * Invokes the Tomcat Main class to start or stop the container, depending
090: * on the value of the provided argument.
091: *
092: * @param theArg Either 'start' or 'stop'
093: */
094: private void invoke(String theArg) {
095: Java java = null;
096: if ("start".equals(theArg)) {
097: java = createJavaForStartUp();
098: } else {
099: java = createJavaForShutDown();
100: }
101: java.addSysproperty(createSysProperty("tomcat.install",
102: getDir()));
103: java.addSysproperty(createSysProperty("tomcat.home",
104: this .tmpDir));
105: Path classpath = java.createClasspath();
106: classpath.createPathElement().setLocation(
107: new File(getDir(), "lib/tomcat.jar"));
108:
109: // It seems that since Tomcat 3.3.2, the commons-logging jar is
110: // required in the Tomcat bootstrap classpath...
111: File commonsLoggingJarFile = new File(getDir(),
112: "lib/common/commons-logging-api.jar");
113: if (commonsLoggingJarFile.exists()) {
114: classpath.createPathElement().setLocation(
115: commonsLoggingJarFile);
116: }
117:
118: java.setClassname("org.apache.tomcat.startup.Main");
119: java.createArg().setValue(theArg);
120: java.execute();
121: }
122:
123: /**
124: * Prepares a temporary installation of the container and deploys the
125: * web-application.
126: *
127: * @param theDirName The name of the temporary container installation
128: * directory
129: * @throws IOException If an I/O error occurs
130: */
131: private void prepare(String theDirName) throws IOException {
132: FileUtils fileUtils = FileUtils.newFileUtils();
133: FilterChain filterChain = createFilterChain();
134:
135: this .tmpDir = setupTempDirectory(this .tmpDir, theDirName);
136: cleanTempDirectory(this .tmpDir);
137:
138: // copy configuration files into the temporary container directory
139: File confDir = createDirectory(tmpDir, "conf");
140: copyConfFiles(confDir);
141: if (getServerXml() == null) {
142: ResourceUtils.copyResource(getProject(), RESOURCE_PATH
143: + "tomcat3x/server.xml", new File(confDir,
144: "server.xml"), filterChain);
145: }
146: // TODO: only copy these files if they haven't been provided by the
147: // user as a conf fileset
148: ResourceUtils.copyResource(getProject(), RESOURCE_PATH
149: + "tomcat3x/tomcat-users.xml", new File(confDir,
150: "tomcat-users.xml"));
151: ResourceUtils.copyResource(getProject(), RESOURCE_PATH
152: + "tomcat3x/modules.xml", new File(confDir,
153: "modules.xml"));
154:
155: // deploy the web-app by copying the WAR file
156: File webappsDir = createDirectory(tmpDir, "webapps");
157: fileUtils.copyFile(getDeployableFile().getFile(), new File(
158: webappsDir, getDeployableFile().getFile().getName()),
159: null, true);
160: }
161:
162: }
|