1 23 24 77 78 package org.apache.tools.ant.taskdefs.optional.sun.appserv; 79 80 import org.apache.tools.ant.Project; 81 import org.apache.tools.ant.DirectoryScanner; 82 import org.apache.tools.ant.BuildException; 83 import org.apache.tools.ant.types.FileSet; 84 85 import java.io.File ; 86 import java.util.List ; 87 import java.util.ArrayList ; 88 import java.util.Map ; 89 import java.util.HashMap ; 90 import java.util.Iterator ; 91 92 135 public abstract class ComponentAdmin extends AppServerAdmin { 136 137 LocalStringsManager lsm = new LocalStringsManager(); 138 139 143 protected Component component; 144 145 149 protected List components = new ArrayList (); 150 151 154 private List filesets = new ArrayList (); 155 156 protected static final String TYPE_APP = "application"; 157 protected static final String TYPE_EJB = "ejb"; 158 protected static final String TYPE_WEB = "web"; 159 protected static final String TYPE_CONN = "connector"; 160 protected static final String TYPE_CLIENT = "client"; 161 162 168 protected static final java.util.Map TYPE_MAP = new HashMap (4); 169 static { 170 TYPE_MAP.put("ear", TYPE_APP); 171 TYPE_MAP.put("jar", TYPE_EJB); 172 TYPE_MAP.put("war", TYPE_WEB); 173 TYPE_MAP.put("rar", TYPE_CONN); 174 }; 175 176 180 public ComponentAdmin() { 181 super(); 182 component = getNewComponent(); 183 } 184 185 191 protected Component getNewComponent() { 192 return new Component(component); 193 } 194 195 201 public void setFile(File file) { 202 component.setFile(file); } 204 205 211 public void setName(String name) { 212 component.setName(name); } 214 215 221 public void setType(String type) { 222 component.setType(type); } 224 225 230 public void setTarget(String target) { 231 component.setTarget(target); } 233 234 239 public void addFileset(FileSet fileset) { 240 filesets.add(fileset); 241 } 242 243 248 public Component createComponent() { 249 Component newComponent = getNewComponent(); 250 components.add(newComponent); 251 return newComponent; 252 } 253 254 protected void prepareToExecute() throws BuildException { 255 super.prepareToExecute(); 256 processFilesets(); 257 if (components.size() == 0) { 258 components.add(component); 259 } 260 261 } 262 263 protected void execute(Server aServer) throws BuildException { 264 Iterator iterator = components.iterator(); 265 while (iterator.hasNext()) { Component comp = (Component)iterator.next(); 267 String cmdString = getCommandString(aServer, comp); 268 execAdminCommand(cmdString); 269 } 270 } 271 272 276 private void processFilesets() { 277 for (int i = 0; i < filesets.size(); i++) { 278 FileSet fileset = (FileSet) filesets.get(i); 279 DirectoryScanner scanner = fileset.getDirectoryScanner(project); 280 File baseDir = scanner.getBasedir(); 281 282 String [] files = scanner.getIncludedFiles(); 283 for (int j = 0; j < files.length; j++) { 284 Component archive = getNewComponent(); 285 archive.setFile(new File (baseDir, files[j])); 286 components.add(archive); 287 } 288 289 String [] dirs = scanner.getIncludedDirectories(); 290 for (int j = 0; j < dirs.length; j++) { 291 Component expandedArchive = getNewComponent(); 292 expandedArchive.setFile(new File (baseDir, dirs[j])); 293 components.add(expandedArchive); 294 } 295 } 296 } 297 298 protected void checkConfiguration() throws BuildException { 299 super.checkConfiguration(); 300 301 log(components.size() + " components were found.", Project.MSG_DEBUG); 302 303 if (components.size() == 0) { log(lsm.getString("NoComponentsSpecified"), Project.MSG_WARN); 305 } 306 307 } 308 309 316 protected void checkConfiguration(Server aServer) throws BuildException { 317 String hostname = aServer.getHost(); 318 if (hostname == null) { 319 hostname = "localhost"; 320 } 321 log("Checking server config for " + hostname, Project.MSG_DEBUG); 322 323 if (!aServer.hasPassword()) { 324 throw new BuildException(lsm.getString("PasswordMustBeSpecified", 325 new Object [] {getTaskName(), 326 hostname}), getLocation()); 327 } 328 329 Iterator iterator = components.iterator(); 330 while (iterator.hasNext()) { Component comp = (Component)iterator.next(); 332 checkComponentConfig(aServer, comp); 333 } 334 } 335 336 343 protected void checkComponentConfig(Server aServer, Component comp) { 344 log("Checking config for server \"" + aServer + "\" and component \"" 345 + comp + "\"", Project.MSG_DEBUG); 346 347 File theFile = comp.getFile(); 349 log("The file for this component: " + theFile, Project.MSG_DEBUG); 350 if ((theFile != null) && (!theFile.exists())) { 351 throw new BuildException(lsm.getString("FileCouldNotBeFound", 352 new Object [] {theFile}), 353 getLocation()); 354 } 355 356 String theName = comp.getName(); 358 if ((theName == null) || (theName.length() == 0)) { 359 throw new BuildException(lsm.getString("CouldNotDetermineComponentName"), 360 getLocation()); 361 } 362 363 String theType = comp.getType(); 365 if ((theType != null) && (!TYPE_MAP.values().contains(theType))) { 366 throw new BuildException(lsm.getString("TypeNotValid", 367 new Object [] {theType}), 368 getLocation()); 369 } 370 371 } 372 373 380 protected abstract String getCommandString(Server aServer, Component comp); 381 382 388 public class Component { 389 protected Component parent; private File file; private String name; private String type; private String target; 395 398 private static final String DEFAULT_TYPE = TYPE_APP; 399 400 404 public Component() { 405 this(null); 406 } 407 408 414 public Component(Component parent) { 415 this.parent = parent; 416 } 417 418 425 public void setParent(Component parent) { 426 this.parent = parent; 427 } 428 429 435 public void setFile(File file) { 436 this.file = file; 437 } 438 439 445 protected File getFile() { 446 return file; 447 } 448 449 455 public void setName(String name) { 456 this.name = name; 457 } 458 459 466 protected String getName() { 467 if (name == null) { 468 469 String fileName = null; 470 471 if (file != null) { 472 fileName = file.getName(); 473 } else { 474 return null; 475 } 476 477 int index = fileName.indexOf('.'); 478 name = (index < 0) ? fileName : fileName.substring(0, index); 479 } 480 481 return name; 482 } 483 484 490 public void setType(String type) { 491 this.type = type; 492 } 493 494 500 protected String getType() { 501 if (type == null) { 502 return null; 503 } 504 return type; 505 } 506 507 513 public void setTarget(String target) { 514 this.target = target; 515 } 516 517 522 protected String getTarget() { 523 if (target == null) { 524 return null; 525 } 526 return target; 527 } 528 529 public String toString() { 530 return getName(); 531 } 532 }; } 534 | Popular Tags |