001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-2004 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: import java.util.Properties;
025: import java.util.jar.JarFile;
026: import java.util.zip.ZipEntry;
027:
028: import org.apache.cactus.integration.ant.util.ResourceUtils;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.taskdefs.Java;
031: import org.apache.tools.ant.types.FilterChain;
032: import org.apache.tools.ant.types.Path;
033: import org.apache.tools.ant.util.FileUtils;
034:
035: /**
036: * Base support for Catalina based containers.
037: *
038: * @version $Id: AbstractCatalinaContainer.java 239003 2004-05-31 20:05:27Z vmassol $
039: */
040: public abstract class AbstractCatalinaContainer extends
041: AbstractTomcatContainer {
042: // Instance Variables ------------------------------------------------------
043:
044: /**
045: * The temporary directory from which the container will be started.
046: */
047: private File tmpDir;
048:
049: /**
050: * The Catalina version detected by reading a property file in the
051: * installation directory.
052: */
053: private String version;
054:
055: // Public Methods ----------------------------------------------------------
056:
057: /**
058: * Sets the temporary installation directory.
059: *
060: * @param theTmpDir The temporary directory to set
061: */
062: public final void setTmpDir(File theTmpDir) {
063: this .tmpDir = theTmpDir;
064: }
065:
066: // AbstractContainer Implementation ----------------------------------------
067:
068: /**
069: * @see org.apache.cactus.integration.ant.container.Container#getName
070: */
071: public final String getName() {
072: return "Tomcat " + this .version;
073: }
074:
075: /**
076: * @see org.apache.cactus.integration.ant.container.Container#init
077: */
078: public void init() {
079: // Check the installation directory
080: this .version = getVersion();
081: if (this .version == null) {
082: throw new BuildException(getDir()
083: + " not recognized as a Tomcat 4.x installation");
084: }
085: }
086:
087: // Protected Methods -------------------------------------------------------
088:
089: /**
090: * Returns the version of the Tomcat installation.
091: *
092: * @return The Tomcat version, or <code>null</code> if the verion number
093: * could not be retrieved
094: */
095: protected final String getVersion() {
096: if (this .version == null) {
097: try {
098: // Unfortunately, there's no safe way to find out the version of
099: // a Catalina installation, so we need to try multiple paths
100: // here
101:
102: // Tomcat 4.1.0 and later includes a ServerInfo.properties
103: // resource in catalina.jar that contains the version number. If
104: // that resource doesn't exist, we're on Tomcat 4.0.x
105: JarFile catalinaJar = new JarFile(new File(getDir(),
106: "server/lib/catalina.jar"));
107: ZipEntry entry = catalinaJar
108: .getEntry("org/apache/catalina/util/ServerInfo.properties");
109: if (entry != null) {
110: Properties props = new Properties();
111: props.load(catalinaJar.getInputStream(entry));
112: String serverInfo = props
113: .getProperty("server.info");
114: if (serverInfo.indexOf('/') > 0) {
115: this .version = serverInfo.substring(serverInfo
116: .indexOf('/') + 1);
117: }
118: } else {
119: this .version = "4.0.x";
120: }
121: } catch (IOException ioe) {
122: getLog().warn(
123: "Couldn't retrieve Tomcat version information",
124: ioe);
125: }
126: }
127: return this .version;
128: }
129:
130: /**
131: * Invokes the Catalina Bootstrap class to start or stop the container,
132: * depending on the value of the provided argument.
133: *
134: * @param theArg Either 'start' or 'stop'
135: */
136: protected final void invokeBootstrap(String theArg) {
137: Java java = null;
138: if ("start".equals(theArg)) {
139: java = createJavaForStartUp();
140: } else {
141: java = createJavaForShutDown();
142: }
143: java
144: .addSysproperty(createSysProperty("catalina.home",
145: getDir()));
146: java.addSysproperty(createSysProperty("catalina.base",
147: getTmpDir()));
148: Path classpath = java.createClasspath();
149: classpath.createPathElement().setLocation(
150: new File(getDir(), "bin/bootstrap.jar"));
151: addToolsJarToClasspath(classpath);
152: java.setClassname("org.apache.catalina.startup.Bootstrap");
153: java.createArg().setValue(theArg);
154: java.execute();
155: }
156:
157: /**
158: * Prepares a temporary installation of the container and deploys the
159: * web-application.
160: *
161: * @param theResourcePrefix The prefix to use when looking up container
162: * resource in the JAR
163: * @param theDirName The name of the temporary container installation
164: * directory
165: * @throws IOException If an I/O error occurs
166: */
167: protected void prepare(String theResourcePrefix, String theDirName)
168: throws IOException {
169: FileUtils fileUtils = FileUtils.newFileUtils();
170: FilterChain filterChain = createFilterChain();
171:
172: setTmpDir(setupTempDirectory(getTmpDir(), theDirName));
173: cleanTempDirectory(getTmpDir());
174:
175: File confDir = createDirectory(getTmpDir(), "conf");
176:
177: // Copy first the default configuration files so that they can be
178: // overriden by the user-provided ones.
179:
180: if (getServerXml() == null) {
181: ResourceUtils.copyResource(getProject(), RESOURCE_PATH
182: + theResourcePrefix + "/server.xml", new File(
183: confDir, "server.xml"), filterChain);
184: }
185:
186: ResourceUtils.copyResource(getProject(), RESOURCE_PATH
187: + theResourcePrefix + "/tomcat-users.xml", new File(
188: confDir, "tomcat-users.xml"));
189: fileUtils.copyFile(new File(getDir(), "conf/web.xml"),
190: new File(confDir, "web.xml"));
191:
192: // deploy the web-app by copying the WAR file into the webapps
193: // directory
194: File webappsDir = createDirectory(getTmpDir(), "webapps");
195: fileUtils.copyFile(getDeployableFile().getFile(), new File(
196: webappsDir, getDeployableFile().getFile().getName()),
197: null, true);
198:
199: // Copy user-provided configuration files into the temporary conf/
200: // container directory.
201: copyConfFiles(confDir);
202: }
203:
204: /**
205: * @return The temporary directory from which the container will be
206: * started.
207: */
208: protected final File getTmpDir() {
209: return this.tmpDir;
210: }
211: }
|