|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| GenericContainer.java | 40% | 47.6% | 66.7% | 49.4% |
|
||||||||||||||
| 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;
|
|
| 23 |
import java.util.Iterator;
|
|
| 24 |
import java.util.List;
|
|
| 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 238811 2004-02-29 10:10:42Z vmassol $
|
|
| 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 target;
|
|
| 57 |
|
|
| 58 |
/**
|
|
| 59 |
* Ordered list of the contained tasks that should be invoked when the
|
|
| 60 |
* hook is executed.
|
|
| 61 |
*/
|
|
| 62 |
private List tasks = new ArrayList(); |
|
| 63 |
|
|
| 64 |
// Public Methods ------------------------------------------------------
|
|
| 65 |
|
|
| 66 |
/**
|
|
| 67 |
* Sets the target to call.
|
|
| 68 |
*
|
|
| 69 |
* @param theTarget The name of the target
|
|
| 70 |
*/
|
|
| 71 | 0 |
public void setTarget(String theTarget) |
| 72 |
{
|
|
| 73 | 0 |
if (!this.tasks.isEmpty()) |
| 74 |
{
|
|
| 75 | 0 |
throw new BuildException("The generic element supports either " |
| 76 |
+ "a [target] attribute or nested tasks, but not both");
|
|
| 77 |
} |
|
| 78 | 0 |
this.target = theTarget;
|
| 79 |
} |
|
| 80 |
|
|
| 81 |
/**
|
|
| 82 |
* @see org.apache.tools.ant.TaskContainer#addTask
|
|
| 83 |
*/
|
|
| 84 | 2 |
public void addTask(Task theTask) throws BuildException |
| 85 |
{
|
|
| 86 | 2 |
if (this.target != null) |
| 87 |
{
|
|
| 88 | 0 |
throw new BuildException("The generic element supports either " |
| 89 |
+ "a [target] attribute or nested tasks, but not both");
|
|
| 90 |
} |
|
| 91 | 2 |
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 | 2 |
public void execute() throws BuildException |
| 102 |
{
|
|
| 103 | 2 |
if (this.target != null) |
| 104 |
{
|
|
| 105 | 0 |
CallTarget antCall = (CallTarget) createAntTask("antcall");
|
| 106 | 0 |
antCall.setInheritAll(true);
|
| 107 | 0 |
antCall.setInheritRefs(true);
|
| 108 | 0 |
antCall.init(); |
| 109 | 0 |
antCall.setTarget(this.target);
|
| 110 | 0 |
antCall.execute(); |
| 111 |
} |
|
| 112 |
else
|
|
| 113 |
{
|
|
| 114 | 2 |
for (Iterator i = this.tasks.iterator(); i.hasNext();) |
| 115 |
{
|
|
| 116 | 2 |
Task task = (Task) i.next(); |
| 117 | 2 |
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 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 | 1 |
public Hook createStartUp() throws BuildException |
| 155 |
{
|
|
| 156 | 1 |
if (isStartUpSet())
|
| 157 |
{
|
|
| 158 | 0 |
throw new BuildException("The container element supports only one" |
| 159 |
+ "nested [startup] element");
|
|
| 160 |
} |
|
| 161 | 1 |
this.startUpHook = new Hook(); |
| 162 | 1 |
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 | 1 |
public Hook createShutDown() throws BuildException |
| 172 |
{
|
|
| 173 | 1 |
if (isShutDownSet())
|
| 174 |
{
|
|
| 175 | 0 |
throw new BuildException("The container element supports only one" |
| 176 |
+ "nested [shutdown] element");
|
|
| 177 |
} |
|
| 178 | 1 |
this.shutDownHook = new Hook(); |
| 179 | 1 |
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 | 1 |
public boolean isShutDownSet() |
| 189 |
{
|
|
| 190 | 1 |
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 | 1 |
public boolean isStartUpSet() |
| 200 |
{
|
|
| 201 | 1 |
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 | 0 |
public void setName(String theName) |
| 210 |
{
|
|
| 211 | 0 |
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 | 3 |
public void setPort(int thePort) |
| 220 |
{
|
|
| 221 | 3 |
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 | 0 |
public void setStartUpTarget(String theStartUpTarget) |
| 230 |
{
|
|
| 231 | 0 |
if (isStartUpSet())
|
| 232 |
{
|
|
| 233 | 0 |
throw new BuildException("Either specify the [startuptarget] " |
| 234 |
+ "attribute or the nested [startup] element, but not both");
|
|
| 235 |
} |
|
| 236 | 0 |
this.startUpHook = new Hook(); |
| 237 | 0 |
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 | 0 |
public void setShutDownTarget(String theShutDownTarget) |
| 246 |
{
|
|
| 247 | 0 |
if (isShutDownSet())
|
| 248 |
{
|
|
| 249 | 0 |
throw new BuildException("Either specify the [shutdowntarget] " |
| 250 |
+ "attribute or the nested [shutdown] element, but not both");
|
|
| 251 |
} |
|
| 252 | 0 |
this.shutDownHook = new Hook(); |
| 253 | 0 |
this.shutDownHook.setTarget(theShutDownTarget);
|
| 254 |
} |
|
| 255 |
|
|
| 256 |
// AbstractContainer Implementation ----------------------------------------
|
|
| 257 |
|
|
| 258 |
/**
|
|
| 259 |
* @see org.apache.cactus.integration.ant.container.Container#getName
|
|
| 260 |
*/
|
|
| 261 | 0 |
public String getName()
|
| 262 |
{
|
|
| 263 | 0 |
return this.name; |
| 264 |
} |
|
| 265 |
|
|
| 266 |
/**
|
|
| 267 |
* Returns the port to which the container should listen.
|
|
| 268 |
*
|
|
| 269 |
* @return The port
|
|
| 270 |
*/
|
|
| 271 | 1 |
public int getPort() |
| 272 |
{
|
|
| 273 | 1 |
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 | 1 |
public void startUp() throws BuildException |
| 282 |
{
|
|
| 283 | 1 |
if (this.startUpHook != null) |
| 284 |
{
|
|
| 285 | 1 |
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 | 1 |
public void shutDown() throws BuildException |
| 295 |
{
|
|
| 296 | 1 |
if (this.shutDownHook != null) |
| 297 |
{
|
|
| 298 | 1 |
this.shutDownHook.execute();
|
| 299 |
} |
|
| 300 |
} |
|
| 301 |
|
|
| 302 |
} |
|
| 303 |
|
|
||||||||||