1 18 19 package org.apache.tools.ant.taskdefs.optional.ejb; 20 21 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParser ; 28 import javax.xml.parsers.SAXParserFactory ; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.DirectoryScanner; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.taskdefs.MatchingTask; 33 import org.apache.tools.ant.types.EnumeratedAttribute; 34 import org.apache.tools.ant.types.FileSet; 35 import org.apache.tools.ant.types.Path; 36 import org.xml.sax.SAXException ; 37 38 57 public class EjbJar extends MatchingTask { 58 59 62 public static class DTDLocation 63 extends org.apache.tools.ant.types.DTDLocation { 64 } 65 66 70 static class Config { 71 76 public File srcDir; 77 78 82 public File descriptorDir; 83 84 85 public String baseNameTerminator = "-"; 86 87 88 public String baseJarName; 89 90 94 public boolean flatDestDir = false; 95 96 99 public Path classpath; 100 101 104 public List supportFileSets = new ArrayList (); 105 106 109 public ArrayList dtdLocations = new ArrayList (); 110 111 115 public NamingScheme namingScheme; 116 117 120 public File manifest; 121 122 125 public String analyzer; 126 } 128 129 133 public static class NamingScheme extends EnumeratedAttribute { 134 138 public static final String EJB_NAME = "ejb-name"; 139 140 144 public static final String DIRECTORY = "directory"; 145 146 150 public static final String DESCRIPTOR = "descriptor"; 151 152 156 public static final String BASEJARNAME = "basejarname"; 157 158 163 public String [] getValues() { 164 return new String [] {EJB_NAME, DIRECTORY, DESCRIPTOR, BASEJARNAME}; 165 } 166 } 167 168 173 public static class CMPVersion extends EnumeratedAttribute { 174 175 public static final String CMP1_0 = "1.0"; 176 177 public static final String CMP2_0 = "2.0"; 178 179 public String [] getValues() { 180 return new String []{ 181 CMP1_0, 182 CMP2_0, 183 }; 184 } 185 } 186 190 private Config config = new Config(); 191 192 193 199 private File destDir; 200 201 202 private String genericJarSuffix = "-generic.jar"; 203 204 205 private String cmpVersion = CMPVersion.CMP1_0; 206 207 208 private ArrayList deploymentTools = new ArrayList (); 209 210 217 protected void addDeploymentTool(EJBDeploymentTool deploymentTool) { 218 deploymentTool.setTask(this); 219 deploymentTools.add(deploymentTool); 220 } 221 222 227 public WeblogicDeploymentTool createWeblogic() { 228 WeblogicDeploymentTool tool = new WeblogicDeploymentTool(); 229 addDeploymentTool(tool); 230 return tool; 231 } 232 233 238 public WebsphereDeploymentTool createWebsphere() { 239 WebsphereDeploymentTool tool = new WebsphereDeploymentTool(); 240 addDeploymentTool(tool); 241 return tool; 242 } 243 244 249 public BorlandDeploymentTool createBorland() { 250 log("Borland deployment tools", Project.MSG_VERBOSE); 251 252 BorlandDeploymentTool tool = new BorlandDeploymentTool(); 253 tool.setTask(this); 254 deploymentTools.add(tool); 255 return tool; 256 } 257 258 263 public IPlanetDeploymentTool createIplanet() { 264 log("iPlanet Application Server deployment tools", Project.MSG_VERBOSE); 265 266 IPlanetDeploymentTool tool = new IPlanetDeploymentTool(); 267 addDeploymentTool(tool); 268 return tool; 269 } 270 271 276 public JbossDeploymentTool createJboss() { 277 JbossDeploymentTool tool = new JbossDeploymentTool(); 278 addDeploymentTool(tool); 279 return tool; 280 } 281 282 287 public JonasDeploymentTool createJonas() { 288 log("JOnAS deployment tools", Project.MSG_VERBOSE); 289 290 JonasDeploymentTool tool = new JonasDeploymentTool(); 291 addDeploymentTool(tool); 292 return tool; 293 } 294 295 301 public WeblogicTOPLinkDeploymentTool createWeblogictoplink() { 302 log("The <weblogictoplink> element is no longer required. Please use " 303 + "the <weblogic> element and set newCMP=\"true\"", 304 Project.MSG_INFO); 305 WeblogicTOPLinkDeploymentTool tool 306 = new WeblogicTOPLinkDeploymentTool(); 307 addDeploymentTool(tool); 308 return tool; 309 } 310 311 317 public Path createClasspath() { 318 if (config.classpath == null) { 319 config.classpath = new Path(getProject()); 320 } 321 return config.classpath.createPath(); 322 } 323 324 331 public DTDLocation createDTD() { 332 DTDLocation dtdLocation = new DTDLocation(); 333 config.dtdLocations.add(dtdLocation); 334 335 return dtdLocation; 336 } 337 338 343 public FileSet createSupport() { 344 FileSet supportFileSet = new FileSet(); 345 config.supportFileSets.add(supportFileSet); 346 return supportFileSet; 347 } 348 349 350 361 public void setManifest(File manifest) { 362 config.manifest = manifest; 363 } 364 365 372 public void setSrcdir(File inDir) { 373 config.srcDir = inDir; 374 } 375 376 385 public void setDescriptordir(File inDir) { 386 config.descriptorDir = inDir; 387 } 388 389 394 public void setDependency(String analyzer) { 395 config.analyzer = analyzer; 396 } 397 398 405 public void setBasejarname(String inValue) { 406 config.baseJarName = inValue; 407 if (config.namingScheme == null) { 408 config.namingScheme = new NamingScheme(); 409 config.namingScheme.setValue(NamingScheme.BASEJARNAME); 410 } else if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)) { 411 throw new BuildException("The basejarname attribute is not " 412 + "compatible with the " 413 + config.namingScheme.getValue() + " naming scheme"); 414 } 415 } 416 417 423 public void setNaming(NamingScheme namingScheme) { 424 config.namingScheme = namingScheme; 425 if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME) 426 && config.baseJarName != null) { 427 throw new BuildException("The basejarname attribute is not " 428 + "compatible with the " 429 + config.namingScheme.getValue() + " naming scheme"); 430 } 431 } 432 433 439 public File getDestdir() { 440 return this.destDir; 441 } 442 443 453 public void setDestdir(File inDir) { 454 this.destDir = inDir; 455 } 456 457 463 public String getCmpversion() { 464 return this.cmpVersion; 465 } 466 467 476 public void setCmpversion(CMPVersion version) { 477 this.cmpVersion = version.getValue(); 478 } 479 480 485 public void setClasspath(Path classpath) { 486 config.classpath = classpath; 487 } 488 489 500 public void setFlatdestdir(boolean inValue) { 501 config.flatDestDir = inValue; 502 } 503 504 513 public void setGenericjarsuffix(String inString) { 514 this.genericJarSuffix = inString; 515 } 516 517 526 public void setBasenameterminator(String inValue) { 527 config.baseNameTerminator = inValue; 528 } 529 530 535 private void validateConfig() throws BuildException { 536 if (config.srcDir == null) { 537 throw new BuildException("The srcDir attribute must be specified"); 538 } 539 540 if (config.descriptorDir == null) { 541 config.descriptorDir = config.srcDir; 542 } 543 544 if (config.namingScheme == null) { 545 config.namingScheme = new NamingScheme(); 546 config.namingScheme.setValue(NamingScheme.DESCRIPTOR); 547 } else if (config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME) 548 && config.baseJarName == null) { 549 throw new BuildException("The basejarname attribute must " 550 + "be specified with the basejarname naming scheme"); 551 } 552 } 553 554 569 public void execute() throws BuildException { 570 validateConfig(); 571 572 if (deploymentTools.size() == 0) { 573 GenericDeploymentTool genericTool = new GenericDeploymentTool(); 574 genericTool.setTask(this); 575 genericTool.setDestdir(destDir); 576 genericTool.setGenericJarSuffix(genericJarSuffix); 577 deploymentTools.add(genericTool); 578 } 579 580 for (Iterator i = deploymentTools.iterator(); i.hasNext();) { 581 EJBDeploymentTool tool = (EJBDeploymentTool) i.next(); 582 tool.configure(config); 583 tool.validateConfigured(); 584 } 585 586 try { 587 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 589 saxParserFactory.setValidating(true); 590 SAXParser saxParser = saxParserFactory.newSAXParser(); 591 592 593 DirectoryScanner ds = getDirectoryScanner(config.descriptorDir); 594 ds.scan(); 595 String [] files = ds.getIncludedFiles(); 596 597 log(files.length + " deployment descriptors located.", 598 Project.MSG_VERBOSE); 599 600 for (int index = 0; index < files.length; ++index) { 603 for (Iterator i = deploymentTools.iterator(); i.hasNext();) { 605 EJBDeploymentTool tool = (EJBDeploymentTool) i.next(); 606 tool.processDescriptor(files[index], saxParser); 607 } 608 } 609 } catch (SAXException se) { 610 String msg = "SAXException while creating parser." 611 + " Details: " 612 + se.getMessage(); 613 throw new BuildException(msg, se); 614 } catch (ParserConfigurationException pce) { 615 String msg = "ParserConfigurationException while creating parser. " 616 + "Details: " + pce.getMessage(); 617 throw new BuildException(msg, pce); 618 } 619 } 621 } 622 623 624 625 626 627 628 629 | Popular Tags |