1 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 38 public final class GenericContainer extends AbstractContainer 39 { 40 41 43 48 public final class Hook implements TaskContainer 49 { 50 51 53 56 private String target; 57 58 62 private List tasks = new ArrayList (); 63 64 66 71 public void setTarget(String 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 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 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 i = this.tasks.iterator(); i.hasNext();) 115 { 116 Task task = (Task) i.next(); 117 task.perform(); 118 } 119 } 120 } 121 122 } 123 124 126 129 private String name = "Unknown Container"; 130 131 134 private Hook startUpHook; 135 136 139 private Hook shutDownHook; 140 141 144 private int port = 8080; 145 146 148 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 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 188 public boolean isShutDownSet() 189 { 190 return (this.shutDownHook != null); 191 } 192 193 199 public boolean isStartUpSet() 200 { 201 return (this.startUpHook != null); 202 } 203 204 209 public void setName(String theName) 210 { 211 this.name = theName; 212 } 213 214 219 public void setPort(int thePort) 220 { 221 this.port = thePort; 222 } 223 224 229 public void setStartUpTarget(String 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 245 public void setShutDownTarget(String 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 258 261 public String getName() 262 { 263 return this.name; 264 } 265 266 271 public int getPort() 272 { 273 return this.port; 274 } 275 276 281 public void startUp() throws BuildException 282 { 283 if (this.startUpHook != null) 284 { 285 this.startUpHook.execute(); 286 } 287 } 288 289 294 public void shutDown() throws BuildException 295 { 296 if (this.shutDownHook != null) 297 { 298 this.shutDownHook.execute(); 299 } 300 } 301 302 } 303 | Popular Tags |