|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| GenericContainer.java | 40% | 47.6% | 66.7% | 49.4% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.integration.ant.container;
|
|
| 58 |
|
|
| 59 |
import java.util.ArrayList;
|
|
| 60 |
import java.util.Iterator;
|
|
| 61 |
import java.util.List;
|
|
| 62 |
|
|
| 63 |
import org.apache.tools.ant.BuildException;
|
|
| 64 |
import org.apache.tools.ant.Task;
|
|
| 65 |
import org.apache.tools.ant.TaskContainer;
|
|
| 66 |
import org.apache.tools.ant.taskdefs.CallTarget;
|
|
| 67 |
|
|
| 68 |
/**
|
|
| 69 |
* A generic container that can be nested in the
|
|
| 70 |
* {@link org.apache.cactus.integration.ant.CactusTask} to support complete
|
|
| 71 |
* customization of the container lifecycle from a build file.
|
|
| 72 |
*
|
|
| 73 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
| 74 |
*
|
|
| 75 |
* @version $Id: GenericContainer.java,v 1.6.2.3 2003/10/23 18:20:43 vmassol Exp $
|
|
| 76 |
*/
|
|
| 77 |
public final class GenericContainer extends AbstractContainer |
|
| 78 |
{
|
|
| 79 |
|
|
| 80 |
// Inner Classes -----------------------------------------------------------
|
|
| 81 |
|
|
| 82 |
/**
|
|
| 83 |
* Class that represents the nested 'startup' and 'shutdown' elements. It
|
|
| 84 |
* supports either an Ant target to delegate to, or a list of nested tasks
|
|
| 85 |
* that are to be executed in order to perform the operation.
|
|
| 86 |
*/
|
|
| 87 |
public final class Hook implements TaskContainer |
|
| 88 |
{
|
|
| 89 |
|
|
| 90 |
// Instance Variables --------------------------------------------------
|
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* The target to call when the hook is executed.
|
|
| 94 |
*/
|
|
| 95 |
private String target;
|
|
| 96 |
|
|
| 97 |
/**
|
|
| 98 |
* Ordered list of the contained tasks that should be invoked when the
|
|
| 99 |
* hook is executed.
|
|
| 100 |
*/
|
|
| 101 |
private List tasks = new ArrayList(); |
|
| 102 |
|
|
| 103 |
// Public Methods ------------------------------------------------------
|
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* Sets the target to call.
|
|
| 107 |
*
|
|
| 108 |
* @param theTarget The name of the target
|
|
| 109 |
*/
|
|
| 110 | 0 |
public void setTarget(String theTarget) |
| 111 |
{
|
|
| 112 | 0 |
if (!this.tasks.isEmpty()) |
| 113 |
{
|
|
| 114 | 0 |
throw new BuildException("The generic element supports either " |
| 115 |
+ "a [target] attribute or nested tasks, but not both");
|
|
| 116 |
} |
|
| 117 | 0 |
this.target = theTarget;
|
| 118 |
} |
|
| 119 |
|
|
| 120 |
/**
|
|
| 121 |
* @see org.apache.tools.ant.TaskContainer#addTask
|
|
| 122 |
*/
|
|
| 123 | 2 |
public void addTask(Task theTask) throws BuildException |
| 124 |
{
|
|
| 125 | 2 |
if (this.target != null) |
| 126 |
{
|
|
| 127 | 0 |
throw new BuildException("The generic element supports either " |
| 128 |
+ "a [target] attribute or nested tasks, but not both");
|
|
| 129 |
} |
|
| 130 | 2 |
this.tasks.add(theTask);
|
| 131 |
} |
|
| 132 |
|
|
| 133 |
/**
|
|
| 134 |
* Executes the hook by either calling the specified target, or invoking
|
|
| 135 |
* all nested tasks.
|
|
| 136 |
*
|
|
| 137 |
* @throws BuildException If thrown by the called target or one of the
|
|
| 138 |
* nested tasks
|
|
| 139 |
*/
|
|
| 140 | 2 |
public void execute() throws BuildException |
| 141 |
{
|
|
| 142 | 2 |
if (this.target != null) |
| 143 |
{
|
|
| 144 | 0 |
CallTarget antCall = (CallTarget) createAntTask("antcall");
|
| 145 | 0 |
antCall.setInheritAll(true);
|
| 146 | 0 |
antCall.setInheritRefs(true);
|
| 147 | 0 |
antCall.init(); |
| 148 | 0 |
antCall.setTarget(this.target);
|
| 149 | 0 |
antCall.execute(); |
| 150 |
} |
|
| 151 |
else
|
|
| 152 |
{
|
|
| 153 | 2 |
for (Iterator i = this.tasks.iterator(); i.hasNext();) |
| 154 |
{
|
|
| 155 | 2 |
Task task = (Task) i.next(); |
| 156 | 2 |
task.perform(); |
| 157 |
} |
|
| 158 |
} |
|
| 159 |
} |
|
| 160 |
|
|
| 161 |
} |
|
| 162 |
|
|
| 163 |
// Instance Variables ------------------------------------------------------
|
|
| 164 |
|
|
| 165 |
/**
|
|
| 166 |
* Name of the container for logging purposes.
|
|
| 167 |
*/
|
|
| 168 |
private String name = "Unknown Container"; |
|
| 169 |
|
|
| 170 |
/**
|
|
| 171 |
* The hook that is called when the container should be started.
|
|
| 172 |
*/
|
|
| 173 |
private Hook startUpHook;
|
|
| 174 |
|
|
| 175 |
/**
|
|
| 176 |
* The hook that is called when the container should be shut down.
|
|
| 177 |
*/
|
|
| 178 |
private Hook shutDownHook;
|
|
| 179 |
|
|
| 180 |
/**
|
|
| 181 |
* The port to which the container should be bound.
|
|
| 182 |
*/
|
|
| 183 |
private int port = 8080; |
|
| 184 |
|
|
| 185 |
// Public Methods ----------------------------------------------------------
|
|
| 186 |
|
|
| 187 |
/**
|
|
| 188 |
* Creates a nested 'startup' element.
|
|
| 189 |
*
|
|
| 190 |
* @return The new hook element
|
|
| 191 |
* @throws BuildException If a startup hook has already been added
|
|
| 192 |
*/
|
|
| 193 | 1 |
public Hook createStartUp() throws BuildException |
| 194 |
{
|
|
| 195 | 1 |
if (isStartUpSet())
|
| 196 |
{
|
|
| 197 | 0 |
throw new BuildException("The container element supports only one" |
| 198 |
+ "nested [startup] element");
|
|
| 199 |
} |
|
| 200 | 1 |
this.startUpHook = new Hook(); |
| 201 | 1 |
return this.startUpHook; |
| 202 |
} |
|
| 203 |
|
|
| 204 |
/**
|
|
| 205 |
* Creates a nested 'shutdown' element.
|
|
| 206 |
*
|
|
| 207 |
* @return The new hook element
|
|
| 208 |
* @throws BuildException If a shutdown hook has already been added
|
|
| 209 |
*/
|
|
| 210 | 1 |
public Hook createShutDown() throws BuildException |
| 211 |
{
|
|
| 212 | 1 |
if (isShutDownSet())
|
| 213 |
{
|
|
| 214 | 0 |
throw new BuildException("The container element supports only one" |
| 215 |
+ "nested [shutdown] element");
|
|
| 216 |
} |
|
| 217 | 1 |
this.shutDownHook = new Hook(); |
| 218 | 1 |
return this.shutDownHook; |
| 219 |
} |
|
| 220 |
|
|
| 221 |
/**
|
|
| 222 |
* Returns whether a way to start the container has already been set, either
|
|
| 223 |
* as a target, or as a nested task container.
|
|
| 224 |
*
|
|
| 225 |
* @return <code>true</code> if the shut down procedure has been set
|
|
| 226 |
*/
|
|
| 227 | 1 |
public boolean isShutDownSet() |
| 228 |
{
|
|
| 229 | 1 |
return (this.shutDownHook != null); |
| 230 |
} |
|
| 231 |
|
|
| 232 |
/**
|
|
| 233 |
* Returns whether a way to stop the container has already been set, either
|
|
| 234 |
* as a target, or as a nested task container.
|
|
| 235 |
*
|
|
| 236 |
* @return <code>true</code> if the start up procedure has been set
|
|
| 237 |
*/
|
|
| 238 | 1 |
public boolean isStartUpSet() |
| 239 |
{
|
|
| 240 | 1 |
return (this.startUpHook != null); |
| 241 |
} |
|
| 242 |
|
|
| 243 |
/**
|
|
| 244 |
* Sets the name of the container for logging purposes.
|
|
| 245 |
*
|
|
| 246 |
* @param theName The container name
|
|
| 247 |
*/
|
|
| 248 | 0 |
public void setName(String theName) |
| 249 |
{
|
|
| 250 | 0 |
this.name = theName;
|
| 251 |
} |
|
| 252 |
|
|
| 253 |
/**
|
|
| 254 |
* Sets the port to which the container should listen.
|
|
| 255 |
*
|
|
| 256 |
* @param thePort The port to set
|
|
| 257 |
*/
|
|
| 258 | 3 |
public void setPort(int thePort) |
| 259 |
{
|
|
| 260 | 3 |
this.port = thePort;
|
| 261 |
} |
|
| 262 |
|
|
| 263 |
/**
|
|
| 264 |
* Sets the target to call to start the server.
|
|
| 265 |
*
|
|
| 266 |
* @param theStartUpTarget the Ant target to call
|
|
| 267 |
*/
|
|
| 268 | 0 |
public void setStartUpTarget(String theStartUpTarget) |
| 269 |
{
|
|
| 270 | 0 |
if (isStartUpSet())
|
| 271 |
{
|
|
| 272 | 0 |
throw new BuildException("Either specify the [startuptarget] " |
| 273 |
+ "attribute or the nested [startup] element, but not both");
|
|
| 274 |
} |
|
| 275 | 0 |
this.startUpHook = new Hook(); |
| 276 | 0 |
this.startUpHook.setTarget(theStartUpTarget);
|
| 277 |
} |
|
| 278 |
|
|
| 279 |
/**
|
|
| 280 |
* Sets the target to call to stop the server.
|
|
| 281 |
*
|
|
| 282 |
* @param theShutDownTarget the Ant target to call
|
|
| 283 |
*/
|
|
| 284 | 0 |
public void setShutDownTarget(String theShutDownTarget) |
| 285 |
{
|
|
| 286 | 0 |
if (isShutDownSet())
|
| 287 |
{
|
|
| 288 | 0 |
throw new BuildException("Either specify the [shutdowntarget] " |
| 289 |
+ "attribute or the nested [shutdown] element, but not both");
|
|
| 290 |
} |
|
| 291 | 0 |
this.shutDownHook = new Hook(); |
| 292 | 0 |
this.shutDownHook.setTarget(theShutDownTarget);
|
| 293 |
} |
|
| 294 |
|
|
| 295 |
// AbstractContainer Implementation ----------------------------------------
|
|
| 296 |
|
|
| 297 |
/**
|
|
| 298 |
* @see org.apache.cactus.integration.ant.container.Container#getName
|
|
| 299 |
*/
|
|
| 300 | 0 |
public String getName()
|
| 301 |
{
|
|
| 302 | 0 |
return this.name; |
| 303 |
} |
|
| 304 |
|
|
| 305 |
/**
|
|
| 306 |
* Returns the port to which the container should listen.
|
|
| 307 |
*
|
|
| 308 |
* @return The port
|
|
| 309 |
*/
|
|
| 310 | 1 |
public int getPort() |
| 311 |
{
|
|
| 312 | 1 |
return this.port; |
| 313 |
} |
|
| 314 |
|
|
| 315 |
/**
|
|
| 316 |
* Starts up the container by delegating to the startup hook.
|
|
| 317 |
*
|
|
| 318 |
* @throws BuildException If thrown by the startup hook
|
|
| 319 |
*/
|
|
| 320 | 1 |
public void startUp() throws BuildException |
| 321 |
{
|
|
| 322 | 1 |
if (this.startUpHook != null) |
| 323 |
{
|
|
| 324 | 1 |
this.startUpHook.execute();
|
| 325 |
} |
|
| 326 |
} |
|
| 327 |
|
|
| 328 |
/**
|
|
| 329 |
* Shuts down the container by delegating to the shutdown hook.
|
|
| 330 |
*
|
|
| 331 |
* @throws BuildException If thrown by the shutdown hook
|
|
| 332 |
*/
|
|
| 333 | 1 |
public void shutDown() throws BuildException |
| 334 |
{
|
|
| 335 | 1 |
if (this.shutDownHook != null) |
| 336 |
{
|
|
| 337 | 1 |
this.shutDownHook.execute();
|
| 338 |
} |
|
| 339 |
} |
|
| 340 |
|
|
| 341 |
} |
|
| 342 |
|
|
||||||||||