1 54 55 package org.objectweb.jonas.ant; 56 57 import java.io.File ; 59 import java.util.ArrayList ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import javax.xml.parsers.ParserConfigurationException ; 63 import javax.xml.parsers.SAXParser ; 64 import javax.xml.parsers.SAXParserFactory ; 65 import org.apache.tools.ant.BuildException; 66 import org.apache.tools.ant.DirectoryScanner; 67 import org.apache.tools.ant.Project; 68 import org.apache.tools.ant.taskdefs.MatchingTask; 69 import org.apache.tools.ant.types.EnumeratedAttribute; 70 import org.apache.tools.ant.types.FileSet; 71 import org.apache.tools.ant.types.Path; 72 import org.xml.sax.SAXException ; 73 74 96 public class EjbJar extends MatchingTask { 97 98 101 public static class DTDLocation 102 extends org.apache.tools.ant.types.DTDLocation { 103 } 104 105 109 static class Config { 110 114 public File srcDir; 115 116 120 public File descriptorDir; 121 122 123 public String baseNameTerminator = "-"; 124 125 126 public String baseJarName; 127 128 132 public boolean flatDestDir = false; 133 134 137 public Path classpath; 138 139 142 public List supportFileSets = new ArrayList (); 143 144 147 public ArrayList dtdLocations = new ArrayList (); 148 149 153 public NamingScheme namingScheme; 154 155 158 public File manifest; 159 160 163 public String analyzer; 164 } 165 166 170 public static class NamingScheme extends EnumeratedAttribute { 171 175 public static final String EJB_NAME = "ejb-name"; 176 177 181 public static final String DIRECTORY = "directory"; 182 183 187 public static final String DESCRIPTOR = "descriptor"; 188 189 193 public static final String BASEJARNAME = "basejarname"; 194 195 200 public String [] getValues() { 201 return new String [] {EJB_NAME, DIRECTORY, DESCRIPTOR, BASEJARNAME}; 202 } 203 } 204 205 210 public static class CMPVersion extends EnumeratedAttribute { 211 public static final String CMP1_0 = "1.0"; 212 public static final String CMP2_0 = "2.0"; 213 public String [] getValues() { 214 return new String []{ 215 CMP1_0, 216 CMP2_0, 217 }; 218 } 219 } 220 224 private Config config = new Config(); 225 226 227 233 private File destDir; 234 235 236 private String genericJarSuffix = "-generic.jar"; 237 238 239 private String cmpVersion = CMPVersion.CMP1_0; 240 241 242 private ArrayList deploymentTools = new ArrayList (); 243 244 251 protected void addDeploymentTool(EJBDeploymentTool deploymentTool) { 252 deploymentTool.setTask(this); 253 deploymentTools.add(deploymentTool); 254 } 255 256 261 public JonasDeploymentTool createJonas() { 262 log("JOnAS deployment tools", Project.MSG_VERBOSE); 263 264 JonasDeploymentTool tool = new JonasDeploymentTool(); 265 addDeploymentTool(tool); 266 return tool; 267 } 268 269 275 public Path createClasspath() { 276 if (config.classpath == null) { 277 config.classpath = new Path(getProject()); 278 } 279 return config.classpath.createPath(); 280 } 281 282 289 public DTDLocation createDTD() { 290 DTDLocation dtdLocation = new DTDLocation(); 291 config.dtdLocations.add(dtdLocation); 292 293 return dtdLocation; 294 } 295 296 301 public FileSet createSupport() { 302 FileSet supportFileSet = new FileSet(); 303 config.supportFileSets.add(supportFileSet); 304 return supportFileSet; 305 } 306 307 308 319 public void setManifest(File manifest) { 320 config.manifest = manifest; 321 } 322 323 330 public void setSrcdir(File inDir) { 331 config.srcDir = inDir; 332 } 333 334 343 public void setDescriptordir(File inDir) { 344 config.descriptorDir = inDir; 345 } 346 347 352 public void setDependency(String analyzer) { 353 config.analyzer = analyzer; 354 } 355 356 363 public void setBasejarname(String inValue) { 364 config.baseJarName = inValue; 365 if (config.namingScheme == null) { 366 config.namingScheme = new NamingScheme(); 367 config.namingScheme.setValue(NamingScheme.BASEJARNAME); 368 } else if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)) { 369 throw new BuildException("The basejarname attribute is not " 370 + "compatible with the " 371 + config.namingScheme.getValue() + " naming scheme"); 372 } 373 } 374 375 381 public void setNaming(NamingScheme namingScheme) { 382 config.namingScheme = namingScheme; 383 if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME) 384 && config.baseJarName != null) { 385 throw new BuildException("The basejarname attribute is not " 386 + "compatible with the " 387 + config.namingScheme.getValue() + " naming scheme"); 388 } 389 } 390 391 397 public File getDestdir() { 398 return this.destDir; 399 } 400 401 411 public void setDestdir(File inDir) { 412 this.destDir = inDir; 413 } 414 415 421 public String getCmpversion() { 422 return this.cmpVersion; 423 } 424 425 434 public void setCmpversion(CMPVersion version) { 435 this.cmpVersion = version.getValue(); 436 } 437 438 443 public void setClasspath(Path classpath) { 444 config.classpath = classpath; 445 } 446 447 458 public void setFlatdestdir(boolean inValue) { 459 config.flatDestDir = inValue; 460 } 461 462 471 public void setGenericjarsuffix(String inString) { 472 this.genericJarSuffix = inString; 473 } 474 475 484 public void setBasenameterminator(String inValue) { 485 config.baseNameTerminator = inValue; 486 } 487 488 493 private void validateConfig() throws BuildException { 494 if (config.srcDir == null) { 495 throw new BuildException("The srcDir attribute must be specified"); 496 } 497 498 if (config.descriptorDir == null) { 499 config.descriptorDir = config.srcDir; 500 } 501 502 if (config.namingScheme == null) { 503 config.namingScheme = new NamingScheme(); 504 config.namingScheme.setValue(NamingScheme.DESCRIPTOR); 505 } else if (config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME) 506 && config.baseJarName == null) { 507 throw new BuildException("The basejarname attribute must " 508 + "be specified with the basejarname naming scheme"); 509 } 510 } 511 512 527 public void execute() throws BuildException { 528 validateConfig(); 529 530 if (deploymentTools.size() == 0) { 531 GenericDeploymentTool genericTool = new GenericDeploymentTool(); 532 genericTool.setTask(this); 533 genericTool.setDestdir(destDir); 534 genericTool.setGenericJarSuffix(genericJarSuffix); 535 deploymentTools.add(genericTool); 536 } 537 538 for (Iterator i = deploymentTools.iterator(); i.hasNext();) { 539 EJBDeploymentTool tool = (EJBDeploymentTool) i.next(); 540 tool.configure(config); 541 tool.validateConfigured(); 542 } 543 544 try { 545 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 547 saxParserFactory.setValidating(true); 548 saxParserFactory.setNamespaceAware(true); 549 SAXParser saxParser = saxParserFactory.newSAXParser(); 550 551 552 DirectoryScanner ds = getDirectoryScanner(config.descriptorDir); 553 ds.scan(); 554 String [] files = ds.getIncludedFiles(); 555 556 log(files.length + " deployment descriptors located.", 557 Project.MSG_VERBOSE); 558 559 for (int index = 0; index < files.length; ++index) { 562 for (Iterator i = deploymentTools.iterator(); i.hasNext();) { 564 EJBDeploymentTool tool = (EJBDeploymentTool) i.next(); 565 tool.processDescriptor(files[index], saxParser); 566 } 567 } 568 } catch (SAXException se) { 569 String msg = "SAXException while creating parser." 570 + " Details: " 571 + se.getMessage(); 572 throw new BuildException(msg, se); 573 } catch (ParserConfigurationException pce) { 574 String msg = "ParserConfigurationException while creating parser. " 575 + "Details: " + pce.getMessage(); 576 throw new BuildException(msg, pce); 577 } 578 } 580 } 581 582 583 584 585 586 587 588 | Popular Tags |