KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > resin > AbstractResinContainer


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.resin;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.cactus.integration.ant.container.AbstractJavaContainer;
26 import org.apache.cactus.integration.ant.util.ResourceUtils;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.taskdefs.Java;
29 import org.apache.tools.ant.types.FileSet;
30 import org.apache.tools.ant.types.FilterChain;
31 import org.apache.tools.ant.types.Path;
32 import org.apache.tools.ant.util.FileUtils;
33
34 /**
35  * Common support for all Resin container versions.
36  *
37  * @version $Id: AbstractResinContainer.java,v 1.6 2004/05/31 20:05:22 vmassol Exp $
38  */

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

43     /**
44      * The mandatory Resin installation directory.
45      */

46     private File JavaDoc dir;
47
48     /**
49      * A user-specific resin.conf configuration file. If this variable is not
50      * set, the default configuration file from the JAR resources will be used.
51      */

52     private File JavaDoc resinConf;
53
54     /**
55      * The port to which the container should be bound.
56      */

57     private int port = 8080;
58
59     /**
60      * The temporary directory from which the container will be started.
61      */

62     private File JavaDoc tmpDir;
63     
64     // Public Methods ----------------------------------------------------------
65

66     /**
67      * Sets the Resin installation directory.
68      *
69      * @param theDir The directory to set
70      */

71     public final void setDir(File JavaDoc theDir)
72     {
73         this.dir = theDir;
74     }
75
76     /**
77      * Sets the temporary directory from which the container is run.
78      *
79      * @param theTmpDir The temporary directory to set
80      */

81     public final void setTmpDir(File JavaDoc theTmpDir)
82     {
83         this.tmpDir = theTmpDir;
84     }
85     
86     /**
87      * Sets the configuration file to use for the test installation of Resin
88      *
89      * @param theResinConf The resin.conf file
90      */

91     public final void setResinConf(File JavaDoc theResinConf)
92     {
93         this.resinConf = theResinConf;
94     }
95
96     /**
97      * Sets the port to which the container should listen.
98      *
99      * @param thePort The port to set
100      */

101     public final void setPort(int thePort)
102     {
103         this.port = thePort;
104     }
105
106     /**
107      * Checks if all mandatory properties have been set and that they
108      * contain valid values.
109      */

110     public void verify()
111     {
112         if (getDir() == null)
113         {
114             throw new BuildException(
115                 "You must specify the mandatory [dir] attribute");
116         }
117
118         if (!getDir().isDirectory())
119         {
120             throw new BuildException("[" + getDir() + "] is not a directory");
121         }
122     }
123     
124     // AbstractContainer Implementation ----------------------------------------
125

126     /**
127      * Returns the port to which the container should listen.
128      *
129      * @return The port
130      */

131     public final int getPort()
132     {
133         return this.port;
134     }
135
136     /**
137      * @see org.apache.cactus.integration.ant.container.Container#init
138      */

139     public final void init()
140     {
141         verify();
142     }
143
144     /**
145      * @see org.apache.cactus.integration.ant.container.Container#startUp
146      */

147     public final void startUp()
148     {
149         try
150         {
151             File JavaDoc installDir = setupTempDirectory(getTmpDir(),
152                 "cactus/" + getContainerDirName());
153             cleanTempDirectory(installDir);
154             
155             prepare(installDir);
156             
157             // Invoke the main class
158
Java java = createJavaForStartUp();
159             java.addSysproperty(createSysProperty("resin.home", installDir));
160             Path classpath = java.createClasspath();
161             classpath.createPathElement().setLocation(
162                 ResourceUtils.getResourceLocation("/"
163                 + ResinRun.class.getName().replace('.', '/') + ".class"));
164             FileSet fileSet = new FileSet();
165             fileSet.setDir(getDir());
166             fileSet.createInclude().setName("lib/*.jar");
167             classpath.addFileset(fileSet);
168             java.setClassname(ResinRun.class.getName());
169             java.createArg().setValue("-start");
170             java.createArg().setValue("-conf");
171             java.createArg().setFile(new File JavaDoc(installDir, "resin.conf"));
172     
173             // Add settings specific to a given container version
174
startUpAdditions(java, classpath);
175             
176             java.execute();
177         }
178         catch (IOException JavaDoc ioe)
179         {
180             getLog().error("Failed to startup the container", ioe);
181             throw new BuildException(ioe);
182         }
183     }
184
185     /**
186      * @see org.apache.cactus.integration.ant.container.Container#shutDown
187      */

188     public final void shutDown()
189     {
190         File JavaDoc installDir = setupTempDirectory(getTmpDir(),
191             "cactus/" + getContainerDirName());
192
193         // Invoke the main class
194
Java java = createJavaForShutDown();
195         java.setFork(true);
196         java.addSysproperty(createSysProperty("resin.home", installDir));
197         Path classpath = java.createClasspath();
198         classpath.createPathElement().setLocation(
199             ResourceUtils.getResourceLocation("/"
200                 + ResinRun.class.getName().replace('.', '/') + ".class"));
201         FileSet fileSet = new FileSet();
202         fileSet.setDir(getDir());
203         fileSet.createInclude().setName("lib/*.jar");
204         classpath.addFileset(fileSet);
205         java.setClassname(ResinRun.class.getName());
206         java.createArg().setValue("-stop");
207         java.execute();
208     }
209     
210     // Protected Methods -------------------------------------------------------
211

212     /**
213      * Allow specific version implementations to add custom settings to the
214      * Java container that will be started.
215      *
216      * @param theJavaContainer the Ant Java object that will start the container
217      * @param theClasspath the classpath that will be used to start the
218      * container
219      */

220     protected abstract void startUpAdditions(Java theJavaContainer,
221         Path theClasspath);
222
223     /**
224      * Allow specific version implementations to add custom preparation steps
225      * before the container is started.
226      *
227      * @param theInstallDir The directory in which to create the temporary
228      * container installation
229      * @param theFilterChain the filter chain used to replace Ant tokens in
230      * configuration
231      * @exception IOException in case of an error
232      */

233     protected abstract void prepareAdditions(File JavaDoc theInstallDir,
234         FilterChain theFilterChain) throws IOException JavaDoc;
235
236     /**
237      * @return the name of the directory where the container will be set up
238      */

239     protected abstract String JavaDoc getContainerDirName();
240     
241     /**
242      * @return the directory where Resin is installed
243      */

244     protected final File JavaDoc getDir()
245     {
246         return this.dir;
247     }
248
249     /**
250      * @return The temporary directory from which the container will be
251      * started.
252      */

253     protected final File JavaDoc getTmpDir()
254     {
255         return this.tmpDir;
256     }
257     
258     // Private Methods ---------------------------------------------------------
259

260     /**
261      * Prepares a temporary installation of the container and deploys the
262      * web-application.
263      *
264      * @param theInstallDir The directory in which to create the temporary
265      * container installation
266      * @throws IOException If an I/O error occurs
267      */

268     private void prepare(File JavaDoc theInstallDir) throws IOException JavaDoc
269     {
270         FileUtils fileUtils = FileUtils.newFileUtils();
271         FilterChain filterChain = createFilterChain();
272
273         // Copy configuration files into the temporary container directory
274
if (this.resinConf != null)
275         {
276             fileUtils.copyFile(this.resinConf,
277                 new File JavaDoc(theInstallDir, "resin.conf"));
278         }
279         else
280         {
281             ResourceUtils.copyResource(getProject(),
282                 RESOURCE_PATH + getContainerDirName() + "/resin.conf",
283                 new File JavaDoc(theInstallDir, "resin.conf"), filterChain);
284         }
285
286         // Deploy the web-app by copying the WAR file into the webapps
287
// directory
288
if (getDeployableFile() != null)
289         {
290             File JavaDoc webappsDir = createDirectory(theInstallDir, "webapps");
291             fileUtils.copyFile(getDeployableFile().getFile(),
292                 new File JavaDoc(webappsDir, getDeployableFile().getFile().getName()),
293                 null, true);
294         }
295         
296         // Add preparation steps specific to a given container version
297
prepareAdditions(theInstallDir, filterChain);
298     }
299 }
300
Popular Tags