KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 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
25 import org.apache.cactus.integration.ant.util.ResourceUtils;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.taskdefs.Java;
28 import org.apache.tools.ant.types.FilterChain;
29 import org.apache.tools.ant.types.Path;
30 import org.apache.tools.ant.util.FileUtils;
31
32 /**
33  * Special container support for the Apache Tomcat 3.x servlet container.
34  *
35  * @version $Id: Tomcat3xContainer.java,v 1.11 2004/05/31 20:05:25 vmassol Exp $
36  */

37 public class Tomcat3xContainer extends AbstractTomcatContainer
38 {
39
40     // Instance Variables ------------------------------------------------------
41

42     /**
43      * The temporary directory from which the container will be started.
44      */

45     private File JavaDoc tmpDir;
46
47     // Public Methods ----------------------------------------------------------
48

49     /**
50      * Sets the temporary installation directory.
51      *
52      * @param theTmpDir The temporary directory to set
53      */

54     public final void setTmpDir(File JavaDoc theTmpDir)
55     {
56         this.tmpDir = theTmpDir;
57     }
58
59     // AbstractContainer Implementation ----------------------------------------
60

61     /**
62      * @see org.apache.cactus.integration.ant.container.Container#getName
63      */

64     public final String JavaDoc getName()
65     {
66         return "Tomcat 3.x";
67     }
68
69     /**
70      * @see org.apache.cactus.integration.ant.container.Container#startUp
71      */

72     public final void startUp()
73     {
74         try
75         {
76             prepare("cactus/tomcat3x");
77             invoke("start");
78         }
79         catch (IOException JavaDoc ioe)
80         {
81             getLog().error("Failed to startup the container", ioe);
82             throw new BuildException(ioe);
83         }
84     }
85
86     /**
87      * @see org.apache.cactus.integration.ant.container.Container#shutDown
88      */

89     public final void shutDown()
90     {
91         invoke("stop");
92     }
93
94     // Private Methods ---------------------------------------------------------
95

96     /**
97      * Invokes the Tomcat Main class to start or stop the container, depending
98      * on the value of the provided argument.
99      *
100      * @param theArg Either 'start' or 'stop'
101      */

102     private void invoke(String JavaDoc theArg)
103     {
104         Java java = null;
105         if ("start".equals(theArg))
106         {
107             java = createJavaForStartUp();
108         }
109         else
110         {
111             java = createJavaForShutDown();
112         }
113         java.addSysproperty(createSysProperty("tomcat.install", getDir()));
114         java.addSysproperty(createSysProperty("tomcat.home", this.tmpDir));
115         Path classpath = java.createClasspath();
116         classpath.createPathElement().setLocation(
117             new File JavaDoc(getDir(), "lib/tomcat.jar"));
118
119         // It seems that since Tomcat 3.3.2, the commons-logging jar is
120
// required in the Tomcat bootstrap classpath...
121
File JavaDoc commonsLoggingJarFile =
122             new File JavaDoc(getDir(), "lib/common/commons-logging-api.jar");
123         if (commonsLoggingJarFile.exists())
124         {
125             classpath.createPathElement().setLocation(commonsLoggingJarFile);
126         }
127         
128         java.setClassname("org.apache.tomcat.startup.Main");
129         java.createArg().setValue(theArg);
130         java.execute();
131     }
132     
133     /**
134      * Prepares a temporary installation of the container and deploys the
135      * web-application.
136      *
137      * @param theDirName The name of the temporary container installation
138      * directory
139      * @throws IOException If an I/O error occurs
140      */

141     private void prepare(String JavaDoc theDirName) throws IOException JavaDoc
142     {
143         FileUtils fileUtils = FileUtils.newFileUtils();
144         FilterChain filterChain = createFilterChain();
145         
146         this.tmpDir = setupTempDirectory(this.tmpDir, theDirName);
147         cleanTempDirectory(this.tmpDir);
148
149         // copy configuration files into the temporary container directory
150
File JavaDoc confDir = createDirectory(tmpDir, "conf");
151         copyConfFiles(confDir);
152         if (getServerXml() == null)
153         {
154             ResourceUtils.copyResource(getProject(),
155                 RESOURCE_PATH + "tomcat3x/server.xml",
156                 new File JavaDoc(confDir, "server.xml"), filterChain);
157         }
158         // TODO: only copy these files if they haven't been provided by the
159
// user as a conf fileset
160
ResourceUtils.copyResource(getProject(),
161             RESOURCE_PATH + "tomcat3x/tomcat-users.xml",
162             new File JavaDoc(confDir, "tomcat-users.xml"));
163         ResourceUtils.copyResource(getProject(),
164             RESOURCE_PATH + "tomcat3x/modules.xml",
165             new File JavaDoc(confDir, "modules.xml"));
166         
167         // deploy the web-app by copying the WAR file
168
File JavaDoc webappsDir = createDirectory(tmpDir, "webapps");
169         fileUtils.copyFile(getDeployableFile().getFile(),
170             new File JavaDoc(webappsDir, getDeployableFile().getFile().getName()),
171             null, true);
172     }
173
174 }
175
Popular Tags