KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > tomcat > AbstractTomcatContainer


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.container.tomcat;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cactus.integration.ant.container.AbstractJavaContainer;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.taskdefs.Copy;
31 import org.apache.tools.ant.types.FileSet;
32 import org.apache.tools.ant.util.FileUtils;
33
34 /**
35  * Base support for Catalina based containers.
36  *
37  * @version $Id: AbstractTomcatContainer.java,v 1.8 2004/02/29 10:10:41 vmassol Exp $
38  */

39 public abstract class AbstractTomcatContainer extends AbstractJavaContainer
40 {
41
42     // Instance Variables ------------------------------------------------------
43

44     /**
45      * The Catalina installation directory.
46      */

47     private File JavaDoc dir;
48
49     /**
50      * List of filesets that contain user-specified files that should be added
51      * to the Tomcat conf directory.
52      */

53     private List JavaDoc confFileSets = new ArrayList JavaDoc();
54
55     /**
56      * A user-specific server.xml configuration file. If this variable is not
57      * set, the default configuration file from the JAR resources will be used.
58      */

59     private File JavaDoc serverXml;
60
61     /**
62      * The port to which the container should be bound.
63      */

64     private int port = 8080;
65
66     // Public Methods ----------------------------------------------------------
67

68     /**
69      * Adds a set of files to include in the Tomcat configuration directory.
70      *
71      * @param theConf The fileset to add
72      */

73     public final void addConf(FileSet theConf)
74     {
75         // Exclude the server.xml file as there is a "serverXml" specific
76
// property for it.
77
theConf.createExclude().setName("**/server.xml");
78
79         this.confFileSets.add(theConf);
80     }
81
82     /**
83      * Sets the Tomcat installation directory.
84      *
85      * @return The directory
86      */

87     public final File JavaDoc getDir()
88     {
89         return this.dir;
90     }
91
92     /**
93      * Sets the Tomcat installation directory.
94      *
95      * @param theDir The directory to set
96      */

97     public final void setDir(File JavaDoc theDir)
98     {
99         this.dir = theDir;
100     }
101
102     /**
103      * @return The server.xml file, if set or null otherwise
104      */

105     public final File JavaDoc getServerXml()
106     {
107         return this.serverXml;
108     }
109
110     /**
111      * Sets the server configuration file to use for the test installation of
112      * Tomcat.
113      *
114      * @param theServerXml The server.xml file
115      */

116     public final void setServerXml(File JavaDoc theServerXml)
117     {
118         this.serverXml = theServerXml;
119     }
120
121     /**
122      * Sets the port to which the container should listen.
123      *
124      * @param thePort The port to set
125      */

126     public final void setPort(int thePort)
127     {
128         this.port = thePort;
129     }
130
131     // AbstractContainer Implementation ----------------------------------------
132

133     /**
134      * Returns the port to which the container should listen.
135      *
136      * @return The port
137      */

138     public final int getPort()
139     {
140         return this.port;
141     }
142
143     /**
144      * @see org.apache.cactus.integration.ant.container.Container#init
145      */

146     public void init()
147     {
148         if (!this.dir.isDirectory())
149         {
150             throw new BuildException(this.dir + " is not a directory");
151         }
152
153         if (!getDeployableFile().isWar())
154         {
155             throw new BuildException("Tomcat doesn't support the "
156                 + "deployment of EAR files");
157         }
158     }
159
160     // Protected Methods -------------------------------------------------------
161

162     /**
163      * Copies the configuration files specified by nested <conf> filesets
164      * to the conf/ directory.
165      *
166      * @param theConfDir The Tomcat configuration directory
167      */

168     protected final void copyConfFiles(File JavaDoc theConfDir)
169     {
170         if (getServerXml() != null)
171         {
172             FileUtils fileUtils = FileUtils.newFileUtils();
173             try
174             {
175                 fileUtils.copyFile(getServerXml(),
176                     new File JavaDoc(theConfDir, "server.xml"));
177             }
178             catch (IOException JavaDoc ioe)
179             {
180                 throw new BuildException("Could not copy " + getServerXml()
181                     + " to directory " + theConfDir, ioe);
182             }
183         }
184
185         if (!this.confFileSets.isEmpty())
186         {
187             Copy copy = (Copy) createAntTask("copy");
188             copy.setTodir(theConfDir);
189             for (Iterator JavaDoc i = this.confFileSets.iterator(); i.hasNext();)
190             {
191                 copy.addFileset((FileSet) i.next());
192             }
193             copy.execute();
194         }
195     }
196
197 }
198
Popular Tags