1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.integration.ant.container;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.cactus.container.ContainerWrapper;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.TaskContainer;
31 import org.apache.tools.ant.taskdefs.CallTarget;
32 import org.codehaus.cargo.util.AntTaskFactory;
33
34 /**
35 * A generic container that can be nested in the
36 * {@link org.apache.cactus.integration.ant.CactusTask} to support complete
37 * customization of the container lifecycle from a build file.
38 *
39 * @version $Id: GenericContainer.java 238811 2004-02-29 10:10:42Z vmassol $
40 */
41 public final class GenericContainer extends ContainerWrapper
42 {
43 /**
44 * The factory for creating ant tasks.
45 */
46 private AntTaskFactory antTaskFactory = null;
47
48 // Inner Classes -----------------------------------------------------------
49
50 /**
51 * Class that represents the nested 'startup' and 'shutdown' elements. It
52 * supports either an Ant target to delegate to, or a list of nested tasks
53 * that are to be executed in order to perform the operation.
54 */
55 public final class Hook implements TaskContainer
56 {
57
58 // Instance Variables --------------------------------------------------
59
60 /**
61 * The target to call when the hook is executed.
62 */
63 private String target;
64
65 /**
66 * Ordered list of the contained tasks that should be invoked when the
67 * hook is executed.
68 */
69 private List tasks = new ArrayList();
70
71 // Public Methods ------------------------------------------------------
72
73 /**
74 * Sets the target to call.
75 *
76 * @param theTarget The name of the target
77 */
78 public void setTarget(String theTarget)
79 {
80 if (!this.tasks.isEmpty())
81 {
82 throw new BuildException("The generic element supports either "
83 + "a [target] attribute or nested tasks, but not both");
84 }
85 this.target = theTarget;
86 }
87
88 /**
89 * {@inheritDoc}
90 * @see org.apache.tools.ant.TaskContainer#addTask
91 */
92 public void addTask(Task theTask) throws BuildException
93 {
94 if (this.target != null)
95 {
96 throw new BuildException("The generic element supports either "
97 + "a [target] attribute or nested tasks, but not both");
98 }
99 this.tasks.add(theTask);
100 }
101
102 /**
103 * Executes the hook by either calling the specified target, or invoking
104 * all nested tasks.
105 *
106 * @throws BuildException If thrown by the called target or one of the
107 * nested tasks
108 */
109 public void execute() throws BuildException
110 {
111 if (this.target != null)
112 {
113 CallTarget antCall = (CallTarget) createAntTask("antcall");
114 antCall.setInheritAll(true);
115 antCall.setInheritRefs(true);
116 antCall.init();
117 //antCall.setOwningTarget(owningTarget);
118 antCall.setTarget(this.target);
119 antCall.execute();
120 }
121 else
122 {
123 for (Iterator i = this.tasks.iterator(); i.hasNext();)
124 {
125 Task task = (Task) i.next();
126 task.perform();
127 }
128 }
129 }
130
131 }
132
133 // Instance Variables ------------------------------------------------------
134
135 /**
136 * Name of the container for logging purposes.
137 */
138 private String name = "Unknown Container";
139
140 /**
141 * The hook that is called when the container should be started.
142 */
143 private Hook startUpHook;
144
145 /**
146 * The hook that is called when the container should be shut down.
147 */
148 private Hook shutDownHook;
149
150 /**
151 * The port to which the container should be bound.
152 */
153 private int port = 8080;
154
155 // Public Methods ----------------------------------------------------------
156
157 /**
158 * Creates a nested 'startup' element.
159 *
160 * @return The new hook element
161 * @throws BuildException If a startup hook has already been added
162 */
163 public Hook createStartUp() throws BuildException
164 {
165 if (isStartUpSet())
166 {
167 throw new BuildException("The container element supports only one"
168 + "nested [startup] element");
169 }
170 this.startUpHook = new Hook();
171 return this.startUpHook;
172 }
173
174 /**
175 * Creates a nested 'shutdown' element.
176 *
177 * @return The new hook element
178 * @throws BuildException If a shutdown hook has already been added
179 */
180 public Hook createShutDown() throws BuildException
181 {
182 if (isShutDownSet())
183 {
184 throw new BuildException("The container element supports only one"
185 + "nested [shutdown] element");
186 }
187 this.shutDownHook = new Hook();
188 return this.shutDownHook;
189 }
190
191 /**
192 * Returns whether a way to start the container has already been set, either
193 * as a target, or as a nested task container.
194 *
195 * @return <code>true</code> if the shut down procedure has been set
196 */
197 public boolean isShutDownSet()
198 {
199 return (this.shutDownHook != null);
200 }
201
202 /**
203 * Returns whether a way to stop the container has already been set, either
204 * as a target, or as a nested task container.
205 *
206 * @return <code>true</code> if the start up procedure has been set
207 */
208 public boolean isStartUpSet()
209 {
210 return (this.startUpHook != null);
211 }
212
213 /**
214 * Sets the name of the container for logging purposes.
215 *
216 * @param theName The container name
217 */
218 public void setName(String theName)
219 {
220 this.name = theName;
221 }
222
223 /**
224 * Sets the port to which the container should listen.
225 *
226 * @param thePort The port to set
227 */
228 public void setPort(int thePort)
229 {
230 this.port = thePort;
231 }
232
233 /**
234 * Sets the target to call to start the server.
235 *
236 * @param theStartUpTarget the Ant target to call
237 */
238 public void setStartUpTarget(String theStartUpTarget)
239 {
240 if (isStartUpSet())
241 {
242 throw new BuildException("Either specify the [startuptarget] "
243 + "attribute or the nested [startup] element, but not both");
244 }
245 this.startUpHook = new Hook();
246 this.startUpHook.setTarget(theStartUpTarget);
247 }
248
249 /**
250 * Sets the target to call to stop the server.
251 *
252 * @param theShutDownTarget the Ant target to call
253 */
254 public void setShutDownTarget(String theShutDownTarget)
255 {
256 if (isShutDownSet())
257 {
258 throw new BuildException("Either specify the [shutdowntarget] "
259 + "attribute or the nested [shutdown] element, but not both");
260 }
261 this.shutDownHook = new Hook();
262 this.shutDownHook.setTarget(theShutDownTarget);
263 }
264
265 // AbstractContainer Implementation ----------------------------------------
266
267 /**
268 * {@inheritDoc}
269 * @see org.apache.cactus.integration.ant.container.Container#getName
270 */
271 public String getName()
272 {
273 return this.name;
274 }
275
276 /**
277 * Returns the port to which the container should listen.
278 *
279 * @return The port
280 */
281 public int getPort()
282 {
283 return this.port;
284 }
285
286 /**
287 * Starts up the container by delegating to the startup hook.
288 *
289 * @throws BuildException If thrown by the startup hook
290 */
291 public void startUp() throws BuildException
292 {
293 if (this.startUpHook != null)
294 {
295 this.startUpHook.execute();
296 }
297 }
298
299 /**
300 * Shuts down the container by delegating to the shutdown hook.
301 *
302 * @throws BuildException If thrown by the shutdown hook
303 */
304 public void shutDown() throws BuildException
305 {
306 if (this.shutDownHook != null)
307 {
308 this.shutDownHook.execute();
309 }
310 }
311
312 /**
313 * Creates and returns a new instance of the Ant task mapped to the
314 * specified logical name using the
315 * {@link org.apache.cactus.integration.ant.util.AntTaskFactory} set.
316 *
317 * @param theName The logical name of the task to create
318 * @return A new isntance of the task
319 * @see AntTaskFactory#createTask
320 */
321 protected final Task createAntTask(String theName)
322 {
323 return this.antTaskFactory.createTask(theName);
324 }
325
326 /**
327 * {@inheritDoc}
328 * @see Container#setAntTaskFactory
329 */
330 public final void setAntTaskFactory(AntTaskFactory theFactory)
331 {
332 this.antTaskFactory = theFactory;
333 }
334 }