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.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.cactus.integration.ant.container.AbstractJavaContainer;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.taskdefs.Copy;
031: import org.apache.tools.ant.types.FileSet;
032: import org.apache.tools.ant.util.FileUtils;
033:
034: /**
035: * Base support for Catalina based containers.
036: *
037: * @version $Id: AbstractTomcatContainer.java 238811 2004-02-29 10:10:42Z vmassol $
038: */
039: public abstract class AbstractTomcatContainer extends
040: AbstractJavaContainer {
041:
042: // Instance Variables ------------------------------------------------------
043:
044: /**
045: * The Catalina installation directory.
046: */
047: private File dir;
048:
049: /**
050: * List of filesets that contain user-specified files that should be added
051: * to the Tomcat conf directory.
052: */
053: private List confFileSets = new ArrayList();
054:
055: /**
056: * A user-specific server.xml configuration file. If this variable is not
057: * set, the default configuration file from the JAR resources will be used.
058: */
059: private File serverXml;
060:
061: /**
062: * The port to which the container should be bound.
063: */
064: private int port = 8080;
065:
066: // Public Methods ----------------------------------------------------------
067:
068: /**
069: * Adds a set of files to include in the Tomcat configuration directory.
070: *
071: * @param theConf The fileset to add
072: */
073: public final void addConf(FileSet theConf) {
074: // Exclude the server.xml file as there is a "serverXml" specific
075: // property for it.
076: theConf.createExclude().setName("**/server.xml");
077:
078: this .confFileSets.add(theConf);
079: }
080:
081: /**
082: * Sets the Tomcat installation directory.
083: *
084: * @return The directory
085: */
086: public final File getDir() {
087: return this .dir;
088: }
089:
090: /**
091: * Sets the Tomcat installation directory.
092: *
093: * @param theDir The directory to set
094: */
095: public final void setDir(File theDir) {
096: this .dir = theDir;
097: }
098:
099: /**
100: * @return The server.xml file, if set or null otherwise
101: */
102: public final File getServerXml() {
103: return this .serverXml;
104: }
105:
106: /**
107: * Sets the server configuration file to use for the test installation of
108: * Tomcat.
109: *
110: * @param theServerXml The server.xml file
111: */
112: public final void setServerXml(File theServerXml) {
113: this .serverXml = theServerXml;
114: }
115:
116: /**
117: * Sets the port to which the container should listen.
118: *
119: * @param thePort The port to set
120: */
121: public final void setPort(int thePort) {
122: this .port = thePort;
123: }
124:
125: // AbstractContainer Implementation ----------------------------------------
126:
127: /**
128: * Returns the port to which the container should listen.
129: *
130: * @return The port
131: */
132: public final int getPort() {
133: return this .port;
134: }
135:
136: /**
137: * @see org.apache.cactus.integration.ant.container.Container#init
138: */
139: public void init() {
140: if (!this .dir.isDirectory()) {
141: throw new BuildException(this .dir + " is not a directory");
142: }
143:
144: if (!getDeployableFile().isWar()) {
145: throw new BuildException("Tomcat doesn't support the "
146: + "deployment of EAR files");
147: }
148: }
149:
150: // Protected Methods -------------------------------------------------------
151:
152: /**
153: * Copies the configuration files specified by nested <conf> filesets
154: * to the conf/ directory.
155: *
156: * @param theConfDir The Tomcat configuration directory
157: */
158: protected final void copyConfFiles(File theConfDir) {
159: if (getServerXml() != null) {
160: FileUtils fileUtils = FileUtils.newFileUtils();
161: try {
162: fileUtils.copyFile(getServerXml(), new File(theConfDir,
163: "server.xml"));
164: } catch (IOException ioe) {
165: throw new BuildException("Could not copy "
166: + getServerXml() + " to directory "
167: + theConfDir, ioe);
168: }
169: }
170:
171: if (!this .confFileSets.isEmpty()) {
172: Copy copy = (Copy) createAntTask("copy");
173: copy.setTodir(theConfDir);
174: for (Iterator i = this .confFileSets.iterator(); i.hasNext();) {
175: copy.addFileset((FileSet) i.next());
176: }
177: copy.execute();
178: }
179: }
180:
181: }
|