KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > GenericContainer


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;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.TaskContainer;
29 import org.apache.tools.ant.taskdefs.CallTarget;
30
31 /**
32  * A generic container that can be nested in the
33  * {@link org.apache.cactus.integration.ant.CactusTask} to support complete
34  * customization of the container lifecycle from a build file.
35  *
36  * @version $Id: GenericContainer.java,v 1.9 2004/02/29 10:10:41 vmassol Exp $
37  */

38 public final class GenericContainer extends AbstractContainer
39 {
40
41     // Inner Classes -----------------------------------------------------------
42

43     /**
44      * Class that represents the nested 'startup' and 'shutdown' elements. It
45      * supports either an Ant target to delegate to, or a list of nested tasks
46      * that are to be executed in order to perform the operation.
47      */

48     public final class Hook implements TaskContainer
49     {
50         
51         // Instance Variables --------------------------------------------------
52

53         /**
54          * The target to call when the hook is executed.
55          */

56         private String JavaDoc target;
57
58         /**
59          * Ordered list of the contained tasks that should be invoked when the
60          * hook is executed.
61          */

62         private List JavaDoc tasks = new ArrayList JavaDoc();
63
64         // Public Methods ------------------------------------------------------
65

66         /**
67          * Sets the target to call.
68          *
69          * @param theTarget The name of the target
70          */

71         public void setTarget(String JavaDoc theTarget)
72         {
73             if (!this.tasks.isEmpty())
74             {
75                 throw new BuildException("The generic element supports either "
76                     + "a [target] attribute or nested tasks, but not both");
77             }
78             this.target = theTarget;
79         }
80
81         /**
82          * @see org.apache.tools.ant.TaskContainer#addTask
83          */

84         public void addTask(Task theTask) throws BuildException
85         {
86             if (this.target != null)
87             {
88                 throw new BuildException("The generic element supports either "
89                     + "a [target] attribute or nested tasks, but not both");
90             }
91             this.tasks.add(theTask);
92         }
93
94         /**
95          * Executes the hook by either calling the specified target, or invoking
96          * all nested tasks.
97          *
98          * @throws BuildException If thrown by the called target or one of the
99          * nested tasks
100          */

101         public void execute() throws BuildException
102         {
103             if (this.target != null)
104             {
105                 CallTarget antCall = (CallTarget) createAntTask("antcall");
106                 antCall.setInheritAll(true);
107                 antCall.setInheritRefs(true);
108                 antCall.init();
109                 antCall.setTarget(this.target);
110                 antCall.execute();
111             }
112             else
113             {
114                 for (Iterator JavaDoc i = this.tasks.iterator(); i.hasNext();)
115                 {
116                     Task task = (Task) i.next();
117                     task.perform();
118                 }
119             }
120         }
121
122     }
123
124     // Instance Variables ------------------------------------------------------
125

126     /**
127      * Name of the container for logging purposes.
128      */

129     private String JavaDoc name = "Unknown Container";
130
131     /**
132      * The hook that is called when the container should be started.
133      */

134     private Hook startUpHook;
135
136     /**
137      * The hook that is called when the container should be shut down.
138      */

139     private Hook shutDownHook;
140
141     /**
142      * The port to which the container should be bound.
143      */

144     private int port = 8080;
145
146     // Public Methods ----------------------------------------------------------
147

148     /**
149      * Creates a nested 'startup' element.
150      *
151      * @return The new hook element
152      * @throws BuildException If a startup hook has already been added
153      */

154     public Hook createStartUp() throws BuildException
155     {
156         if (isStartUpSet())
157         {
158             throw new BuildException("The container element supports only one"
159                 + "nested [startup] element");
160         }
161         this.startUpHook = new Hook();
162         return this.startUpHook;
163     }
164
165     /**
166      * Creates a nested 'shutdown' element.
167      *
168      * @return The new hook element
169      * @throws BuildException If a shutdown hook has already been added
170      */

171     public Hook createShutDown() throws BuildException
172     {
173         if (isShutDownSet())
174         {
175             throw new BuildException("The container element supports only one"
176                 + "nested [shutdown] element");
177         }
178         this.shutDownHook = new Hook();
179         return this.shutDownHook;
180     }
181
182     /**
183      * Returns whether a way to start the container has already been set, either
184      * as a target, or as a nested task container.
185      *
186      * @return <code>true</code> if the shut down procedure has been set
187      */

188     public boolean isShutDownSet()
189     {
190         return (this.shutDownHook != null);
191     }
192
193     /**
194      * Returns whether a way to stop the container has already been set, either
195      * as a target, or as a nested task container.
196      *
197      * @return <code>true</code> if the start up procedure has been set
198      */

199     public boolean isStartUpSet()
200     {
201         return (this.startUpHook != null);
202     }
203
204     /**
205      * Sets the name of the container for logging purposes.
206      *
207      * @param theName The container name
208      */

209     public void setName(String JavaDoc theName)
210     {
211         this.name = theName;
212     }
213
214     /**
215      * Sets the port to which the container should listen.
216      *
217      * @param thePort The port to set
218      */

219     public void setPort(int thePort)
220     {
221         this.port = thePort;
222     }
223
224     /**
225      * Sets the target to call to start the server.
226      *
227      * @param theStartUpTarget the Ant target to call
228      */

229     public void setStartUpTarget(String JavaDoc theStartUpTarget)
230     {
231         if (isStartUpSet())
232         {
233             throw new BuildException("Either specify the [startuptarget] "
234                 + "attribute or the nested [startup] element, but not both");
235         }
236         this.startUpHook = new Hook();
237         this.startUpHook.setTarget(theStartUpTarget);
238     }
239
240     /**
241      * Sets the target to call to stop the server.
242      *
243      * @param theShutDownTarget the Ant target to call
244      */

245     public void setShutDownTarget(String JavaDoc theShutDownTarget)
246     {
247         if (isShutDownSet())
248         {
249             throw new BuildException("Either specify the [shutdowntarget] "
250                 + "attribute or the nested [shutdown] element, but not both");
251         }
252         this.shutDownHook = new Hook();
253         this.shutDownHook.setTarget(theShutDownTarget);
254     }
255
256     // AbstractContainer Implementation ----------------------------------------
257

258     /**
259      * @see org.apache.cactus.integration.ant.container.Container#getName
260      */

261     public String JavaDoc getName()
262     {
263         return this.name;
264     }
265
266     /**
267      * Returns the port to which the container should listen.
268      *
269      * @return The port
270      */

271     public int getPort()
272     {
273         return this.port;
274     }
275
276     /**
277      * Starts up the container by delegating to the startup hook.
278      *
279      * @throws BuildException If thrown by the startup hook
280      */

281     public void startUp() throws BuildException
282     {
283         if (this.startUpHook != null)
284         {
285             this.startUpHook.execute();
286         }
287     }
288
289     /**
290      * Shuts down the container by delegating to the shutdown hook.
291      *
292      * @throws BuildException If thrown by the shutdown hook
293      */

294     public void shutDown() throws BuildException
295     {
296         if (this.shutDownHook != null)
297         {
298             this.shutDownHook.execute();
299         }
300     }
301
302 }
303
Popular Tags