KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ========================================================================
3  *
4  * Copyright 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.net.URL JavaDoc;
24
25 import org.apache.cactus.integration.ant.container.ContainerRunner;
26 import org.apache.cactus.integration.ant.deployment.EarParser;
27 import org.apache.cactus.integration.ant.deployment.WarParser;
28 import org.apache.cactus.integration.ant.util.DefaultAntTaskFactory;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Task;
31 import org.apache.tools.ant.types.Path;
32
33 /**
34  * Task to start/stop a Resin instance.
35  *
36  * @since Cactus 1.7
37  * @version $Id: AbstractResinTask.java,v 1.2 2004/08/15 15:02:27 vmassol Exp $
38  */

39 public abstract class AbstractResinTask extends Task
40 {
41     /**
42      * The mandatory Resin installation directory.
43      */

44     private File JavaDoc dir;
45
46     /**
47      * The action that will be executed by this task
48      * @see #setAction(String)
49      */

50     private String JavaDoc action;
51
52     /**
53      * The archive that contains the enterprise application that will be
54      * deployed
55      */

56     private File JavaDoc earFile;
57
58     /**
59      * The archive that contains the web-app that will be deployed.
60      */

61     private File JavaDoc warFile;
62
63     /**
64      * URL used to verify if the container is started.
65      */

66     private URL JavaDoc testURL;
67
68     /**
69      * A user-specific resin.conf configuration file. If this variable is not
70      * set, the default configuration file from the JAR resources will be used.
71      */

72     private File JavaDoc resinConf;
73
74     /**
75      * The port to which the container should be bound.
76      */

77     private int port = 8080;
78
79     /**
80      * The temporary directory from which the container will be started.
81      */

82     private File JavaDoc tmpDir;
83
84     /**
85      * The file to which output of the container should be written.
86      */

87     private File JavaDoc output;
88
89     /**
90      * Whether output of the container should be appended to an existing file,
91      * or the existing file should be truncated.
92      */

93     private boolean append;
94
95     /**
96      * Additional classpath entries for the classpath that will be used to
97      * start the containers.
98      */

99     private Path containerClasspath;
100
101     /**
102      * Sets the Resin installation directory.
103      *
104      * @param theDir The directory to set
105      */

106     public final void setDir(File JavaDoc theDir)
107     {
108         this.dir = theDir;
109     }
110     
111     /**
112      * Sets the action to execute (either "start" or "stop").
113      *
114      * @param theAction the action that will be executed by this task
115      */

116     public void setAction(String JavaDoc theAction)
117     {
118         this.action = theAction;
119     }
120     
121     /**
122      * Sets a web application archive to deploy in the container.
123      *
124      * @param theWarFile The WAR file to deploy
125      */

126     public final void setWarFile(File JavaDoc theWarFile)
127     {
128         if (getEarFile() != null)
129         {
130             throw new BuildException(
131                 "You may only specify one of [earfile] and [warfile]");
132         }
133         this.warFile = theWarFile;
134     }
135
136     /**
137      * Sets an enterprise application aarchive to deploy.
138      *
139      * @param theEarFile The EAR file to deploy
140      */

141     public final void setEarFile(File JavaDoc theEarFile)
142     {
143         if (getWarFile() != null)
144         {
145             throw new BuildException(
146                 "You may only specify one of [earfile] and [warfile]");
147         }
148         this.earFile = theEarFile;
149     }
150
151     /**
152      * Sets the URL to call for testing if the server is running.
153      *
154      * @param theTestURL the test URL to ping
155      */

156     public void setTestURL(URL JavaDoc theTestURL)
157     {
158         this.testURL = theTestURL;
159     }
160
161     /**
162      * Sets the temporary directory from which the container is run.
163      *
164      * @param theTmpDir The temporary directory to set
165      */

166     public final void setTmpDir(File JavaDoc theTmpDir)
167     {
168         this.tmpDir = theTmpDir;
169     }
170     
171     /**
172      * Sets the configuration file to use for the test installation of Resin
173      *
174      * @param theResinConf The resin.conf file
175      */

176     public final void setResinConf(File JavaDoc theResinConf)
177     {
178         this.resinConf = theResinConf;
179     }
180
181     /**
182      * Sets the port to which the container should listen.
183      *
184      * @param thePort The port to set
185      */

186     public final void setPort(int thePort)
187     {
188         this.port = thePort;
189     }
190
191     /**
192      * Sets the file to which output of the container should be written.
193      *
194      * @param theOutput The output file to set
195      */

196     public final void setOutput(File JavaDoc theOutput)
197     {
198         this.output = theOutput;
199     }
200
201     /**
202      * Sets whether output of the container should be appended to an existing
203      * file, or the existing file should be truncated.
204      *
205      * @param isAppend Whether output should be appended
206      */

207     public final void setAppend(boolean isAppend)
208     {
209         this.append = isAppend;
210     }
211     
212     /**
213      * Checks if the task is correctly initialized.
214      *
215      * @param theContainer the Resin container to verify
216      */

217     private void verify(AbstractResinContainer theContainer)
218     {
219         theContainer.verify();
220         
221         if (getAction() == null)
222         {
223             throw new BuildException("You must specify an [action] attribute");
224         }
225         
226         if (!getAction().equalsIgnoreCase("start")
227            && !getAction().equalsIgnoreCase("stop"))
228         {
229             throw new BuildException(
230                 "Valid actions are: [start] and [stop]");
231         }
232     }
233
234     /**
235      * @return the instance of a Resin container to start/stop
236      */

237     protected abstract AbstractResinContainer getResinContainer();
238     
239     /**
240      * Start or stop the container depending on the action asked by the user.
241      * When starting the container, also prepare a valid container
242      * configuration and optionally deploy a war or ear in it.
243      *
244      * @see Task#execute()
245      */

246     public void execute()
247     {
248         // Resin container that prepares, starts and stops a Resin
249
// instance.
250
AbstractResinContainer container = getResinContainer();
251
252         // Sets the file to be deployed in the container.
253
if (getWarFile() != null)
254         {
255             container.setDeployableFile(
256                 WarParser.parse(getWarFile()));
257         }
258         else if (getEarFile() != null)
259         {
260             container.setDeployableFile(
261                 EarParser.parse(getEarFile()));
262         }
263
264         container.setDir(getDir());
265         container.setAntTaskFactory(new DefaultAntTaskFactory(
266             getProject(), getTaskName(), getLocation(), getOwningTarget()));
267         container.setPort(getPort());
268         
269         // Add specific additional user-defined classpath
270
container.setContainerClasspath(this.containerClasspath);
271         
272         if (getResinConf() != null)
273         {
274             container.setResinConf(getResinConf());
275         }
276
277         if (getTmpDir() != null)
278         {
279             container.setTmpDir(getTmpDir());
280         }
281
282         if (getOutput() != null)
283         {
284             container.setOutput(getOutput());
285         }
286
287         if (getAppend())
288         {
289             container.setAppend(getAppend());
290         }
291         
292         // Verify that the task is correctly set up.
293
verify(container);
294
295         // If the user has provided a test URL, we use a ContainerRunner
296
// that continuously polls this test URL to verify if the container
297
// is started. In that case, this task will only return when the
298
// container is up and running. If no test URL has been started
299
// we simply start the container and give back the control to the user
300
// without waiting for container to be up and running.
301

302         ContainerRunner runner = null;
303         if (getTestURL() != null)
304         {
305             runner = new ContainerRunner(container);
306             runner.setURL(getTestURL());
307         }
308         
309         // Decide whether to start or stop the container
310
if (getAction().equalsIgnoreCase("start"))
311         {
312             if (getTestURL() != null)
313             {
314                 runner.startUpContainer();
315             }
316             else
317             {
318                 container.startUp();
319             }
320         }
321         else if (getAction().equalsIgnoreCase("stop"))
322         {
323             if (getTestURL() != null)
324             {
325                 runner.shutDownContainer();
326             }
327             else
328             {
329                 container.shutDown();
330             }
331         }
332     }
333
334     /**
335      * @return the action to execute ("start" or "stop")
336      */

337     protected final String JavaDoc getAction()
338     {
339         return this.action;
340     }
341
342     /**
343      * @return the directory where Resin is installed
344      */

345     protected final File JavaDoc getDir()
346     {
347         return this.dir;
348     }
349
350     /**
351      * @return the test URL to ping to verify if the container is started
352      */

353     protected final URL JavaDoc getTestURL()
354     {
355         return this.testURL;
356     }
357
358     /**
359      * @return the port to use to start the container
360      */

361     protected final int getPort()
362     {
363         return this.port;
364     }
365
366     /**
367      * @return the configuration file to use for the test installation of Resin
368      */

369     protected final File JavaDoc getResinConf()
370     {
371         return this.resinConf;
372     }
373
374     /**
375      * @return the WAR file to deploy or null if none
376      */

377     protected final File JavaDoc getWarFile()
378     {
379         return this.warFile;
380     }
381
382     /**
383      * @return the EAR file to deploy or null if none
384      */

385     protected final File JavaDoc getEarFile()
386     {
387         return this.earFile;
388     }
389
390     /**
391      * @return the temporary directory from which the container is run
392      */

393     protected final File JavaDoc getTmpDir()
394     {
395         return this.tmpDir;
396     }
397
398     /**
399      * @return the file to which output of the container should be written
400      */

401     protected final File JavaDoc getOutput()
402     {
403         return this.output;
404     }
405
406     /**
407      * @return whether output of the container should be appended to an
408      * existing file, or the existing file should be truncated
409      */

410     protected final boolean getAppend()
411     {
412         return this.append;
413     }
414
415     /**
416      * Adds container classpath to the classpath that will be used for starting
417      * the container.
418      *
419      * @return reference to the classpath
420      */

421     public Path createContainerClasspath()
422     {
423         if (this.containerClasspath == null)
424         {
425             this.containerClasspath = new Path(this.project);
426         }
427         
428         return this.containerClasspath.createPath();
429     }
430 }
431
Popular Tags