KickJava   Java API By Example, From Geeks To Geeks.

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


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.Properties JavaDoc;
25 import java.util.jar.JarFile JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27
28 import org.apache.cactus.integration.ant.util.ResourceUtils;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.taskdefs.Java;
31 import org.apache.tools.ant.types.FilterChain;
32 import org.apache.tools.ant.types.Path;
33 import org.apache.tools.ant.util.FileUtils;
34
35 /**
36  * Base support for Catalina based containers.
37  *
38  * @version $Id: AbstractCatalinaContainer.java,v 1.13 2004/05/31 20:05:25 vmassol Exp $
39  */

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

44     /**
45      * The temporary directory from which the container will be started.
46      */

47     private File JavaDoc tmpDir;
48
49     /**
50      * The Catalina version detected by reading a property file in the
51      * installation directory.
52      */

53     private String JavaDoc version;
54     
55     // Public Methods ----------------------------------------------------------
56

57     /**
58      * Sets the temporary installation directory.
59      *
60      * @param theTmpDir The temporary directory to set
61      */

62     public final void setTmpDir(File JavaDoc theTmpDir)
63     {
64         this.tmpDir = theTmpDir;
65     }
66     
67     // AbstractContainer Implementation ----------------------------------------
68

69     /**
70      * @see org.apache.cactus.integration.ant.container.Container#getName
71      */

72     public final String JavaDoc getName()
73     {
74         return "Tomcat " + this.version;
75     }
76
77     /**
78      * @see org.apache.cactus.integration.ant.container.Container#init
79      */

80     public void init()
81     {
82         // Check the installation directory
83
this.version = getVersion();
84         if (this.version == null)
85         {
86             throw new BuildException(getDir()
87                 + " not recognized as a Tomcat 4.x installation");
88         }
89     }
90
91     // Protected Methods -------------------------------------------------------
92

93     /**
94      * Returns the version of the Tomcat installation.
95      *
96      * @return The Tomcat version, or <code>null</code> if the verion number
97      * could not be retrieved
98      */

99     protected final String JavaDoc getVersion()
100     {
101         if (this.version == null)
102         {
103             try
104             {
105                 // Unfortunately, there's no safe way to find out the version of
106
// a Catalina installation, so we need to try multiple paths
107
// here
108

109                 // Tomcat 4.1.0 and later includes a ServerInfo.properties
110
// resource in catalina.jar that contains the version number. If
111
// that resource doesn't exist, we're on Tomcat 4.0.x
112
JarFile JavaDoc catalinaJar = new JarFile JavaDoc(
113                     new File JavaDoc(getDir(), "server/lib/catalina.jar"));
114                 ZipEntry JavaDoc entry = catalinaJar.getEntry(
115                     "org/apache/catalina/util/ServerInfo.properties");
116                 if (entry != null)
117                 {
118                     Properties JavaDoc props = new Properties JavaDoc();
119                     props.load(catalinaJar.getInputStream(entry));
120                     String JavaDoc serverInfo = props.getProperty("server.info");
121                     if (serverInfo.indexOf('/') > 0)
122                     {
123                         this.version =
124                             serverInfo.substring(serverInfo.indexOf('/') + 1);
125                     }
126                 }
127                 else
128                 {
129                     this.version = "4.0.x";
130                 }
131             }
132             catch (IOException JavaDoc ioe)
133             {
134                 getLog().warn("Couldn't retrieve Tomcat version information",
135                     ioe);
136             }
137         }
138         return this.version;
139     }
140
141     /**
142      * Invokes the Catalina Bootstrap class to start or stop the container,
143      * depending on the value of the provided argument.
144      *
145      * @param theArg Either 'start' or 'stop'
146      */

147     protected final void invokeBootstrap(String JavaDoc theArg)
148     {
149         Java java = null;
150         if ("start".equals(theArg))
151         {
152             java = createJavaForStartUp();
153         }
154         else
155         {
156             java = createJavaForShutDown();
157         }
158         java.addSysproperty(createSysProperty("catalina.home", getDir()));
159         java.addSysproperty(createSysProperty("catalina.base", getTmpDir()));
160         Path classpath = java.createClasspath();
161         classpath.createPathElement().setLocation(
162             new File JavaDoc(getDir(), "bin/bootstrap.jar"));
163         addToolsJarToClasspath(classpath);
164         java.setClassname("org.apache.catalina.startup.Bootstrap");
165         java.createArg().setValue(theArg);
166         java.execute();
167     }
168
169     /**
170      * Prepares a temporary installation of the container and deploys the
171      * web-application.
172      *
173      * @param theResourcePrefix The prefix to use when looking up container
174      * resource in the JAR
175      * @param theDirName The name of the temporary container installation
176      * directory
177      * @throws IOException If an I/O error occurs
178      */

179     protected void prepare(String JavaDoc theResourcePrefix, String JavaDoc theDirName)
180         throws IOException JavaDoc
181     {
182         FileUtils fileUtils = FileUtils.newFileUtils();
183         FilterChain filterChain = createFilterChain();
184
185         setTmpDir(setupTempDirectory(getTmpDir(), theDirName));
186         cleanTempDirectory(getTmpDir());
187
188         File JavaDoc confDir = createDirectory(getTmpDir(), "conf");
189         
190         // Copy first the default configuration files so that they can be
191
// overriden by the user-provided ones.
192

193         if (getServerXml() == null)
194         {
195             ResourceUtils.copyResource(getProject(),
196                 RESOURCE_PATH + theResourcePrefix + "/server.xml",
197                 new File JavaDoc(confDir, "server.xml"), filterChain);
198         }
199         
200         ResourceUtils.copyResource(getProject(),
201             RESOURCE_PATH + theResourcePrefix + "/tomcat-users.xml",
202             new File JavaDoc(confDir, "tomcat-users.xml"));
203         fileUtils.copyFile(new File JavaDoc(getDir(), "conf/web.xml"),
204             new File JavaDoc(confDir, "web.xml"));
205
206         // deploy the web-app by copying the WAR file into the webapps
207
// directory
208
File JavaDoc webappsDir = createDirectory(getTmpDir(), "webapps");
209         fileUtils.copyFile(getDeployableFile().getFile(),
210             new File JavaDoc(webappsDir, getDeployableFile().getFile().getName()),
211             null, true);
212         
213         // Copy user-provided configuration files into the temporary conf/
214
// container directory.
215
copyConfFiles(confDir);
216     }
217
218     /**
219      * @return The temporary directory from which the container will be
220      * started.
221      */

222     protected final File JavaDoc getTmpDir()
223     {
224         return this.tmpDir;
225     }
226 }
227
Popular Tags