1 18 package org.apache.tools.ant.taskdefs.optional.ejb; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import javax.xml.parsers.SAXParser ; 25 import org.apache.tools.ant.AntClassLoader; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.taskdefs.Java; 29 import org.apache.tools.ant.types.Path; 30 31 38 public class JonasDeploymentTool extends GenericDeploymentTool { 39 40 41 protected static final String EJB_JAR_1_1_PUBLIC_ID 42 = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"; 43 protected static final String EJB_JAR_2_0_PUBLIC_ID 44 = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"; 45 46 47 protected static final String JONAS_EJB_JAR_2_4_PUBLIC_ID 48 = "-//ObjectWeb//DTD JOnAS 2.4//EN"; 49 protected static final String JONAS_EJB_JAR_2_5_PUBLIC_ID 50 = "-//ObjectWeb//DTD JOnAS 2.5//EN"; 51 52 53 protected static final String RMI_ORB = "RMI"; 54 55 56 protected static final String JEREMIE_ORB = "JEREMIE"; 57 58 59 protected static final String DAVID_ORB = "DAVID"; 60 61 65 protected static final String EJB_JAR_1_1_DTD = "ejb-jar_1_1.dtd"; 66 protected static final String EJB_JAR_2_0_DTD = "ejb-jar_2_0.dtd"; 67 68 72 protected static final String JONAS_EJB_JAR_2_4_DTD 73 = "jonas-ejb-jar_2_4.dtd"; 74 protected static final String JONAS_EJB_JAR_2_5_DTD 75 = "jonas-ejb-jar_2_5.dtd"; 76 77 78 protected static final String JONAS_DD = "jonas-ejb-jar.xml"; 79 80 81 protected static final String GENIC_CLASS = 82 "org.objectweb.jonas_ejb.genic.GenIC"; 83 84 85 protected static final String OLD_GENIC_CLASS_1 = 86 "org.objectweb.jonas_ejb.tools.GenWholeIC"; 87 88 89 protected static final String OLD_GENIC_CLASS_2 = 90 "org.objectweb.jonas_ejb.tools.GenIC"; 91 92 97 private String descriptorName; 98 99 104 private String jonasDescriptorName; 105 106 107 108 109 110 113 private File outputdir; 114 115 119 private boolean keepgenerated = false; 120 121 125 private boolean nocompil = false; 126 127 131 private boolean novalidation = false; 132 133 137 private String javac; 138 139 140 private String javacopts; 141 142 143 private String rmicopts; 144 145 151 private boolean secpropag = false; 152 153 157 private boolean verbose = false; 158 159 160 private String additionalargs; 161 162 163 164 165 166 167 private File jonasroot; 168 169 173 private boolean keepgeneric = false; 174 175 176 private String suffix = ".jar"; 177 178 183 private String orb; 184 185 189 private boolean nogenic = false; 190 191 192 193 194 195 200 public void setKeepgenerated(boolean aBoolean) { 201 keepgenerated = aBoolean; 202 } 203 204 209 public void setAdditionalargs(String aString) { 210 additionalargs = aString; 211 } 212 213 218 public void setNocompil(boolean aBoolean) { 219 nocompil = aBoolean; 220 } 221 222 227 public void setNovalidation(boolean aBoolean) { 228 novalidation = aBoolean; 229 } 230 231 236 public void setJavac(String aString) { 237 javac = aString; 238 } 239 240 245 public void setJavacopts(String aString) { 246 javacopts = aString; 247 } 248 249 254 public void setRmicopts(String aString) { 255 rmicopts = aString; 256 } 257 258 263 public void setSecpropag(boolean aBoolean) { 264 secpropag = aBoolean; 265 } 266 267 272 public void setVerbose(boolean aBoolean) { 273 verbose = aBoolean; 274 } 275 276 277 278 279 280 285 public void setJonasroot(File aFile) { 286 jonasroot = aFile; 287 } 288 289 294 public void setKeepgeneric(boolean aBoolean) { 295 keepgeneric = aBoolean; 296 } 297 298 303 public void setJarsuffix(String aString) { 304 suffix = aString; 305 } 306 307 312 public void setOrb(String aString) { 313 orb = aString; 314 } 315 316 321 public void setNogenic(boolean aBoolean) { 322 nogenic = aBoolean; 323 } 324 325 326 327 328 329 330 public void processDescriptor(String aDescriptorName, SAXParser saxParser) { 331 332 descriptorName = aDescriptorName; 333 334 log("JOnAS Deployment Tool processing: " + descriptorName, 335 Project.MSG_VERBOSE); 336 337 super.processDescriptor(descriptorName, saxParser); 338 339 if (outputdir != null) { 340 log("Deleting temp output directory '" + outputdir + "'.", Project.MSG_VERBOSE); 342 deleteAllFiles(outputdir); 343 } 344 } 345 346 347 protected void writeJar(String baseName, File jarfile, Hashtable ejbFiles, String publicId) 348 throws BuildException { 349 350 File genericJarFile = super.getVendorOutputJarFile(baseName); 352 super.writeJar(baseName, genericJarFile, ejbFiles, publicId); 353 354 addGenICGeneratedFiles(genericJarFile, ejbFiles); 356 357 super.writeJar(baseName, getVendorOutputJarFile(baseName), ejbFiles, publicId); 359 360 if (!keepgeneric) { 361 log("Deleting generic JAR " + genericJarFile.toString(), Project.MSG_VERBOSE); 362 genericJarFile.delete(); 363 } 364 } 365 366 367 protected void addVendorFiles(Hashtable ejbFiles, String ddPrefix) { 368 369 jonasDescriptorName = getJonasDescriptorName(); 371 File jonasDD = new File (getConfig().descriptorDir, jonasDescriptorName); 372 373 if (jonasDD.exists()) { 374 ejbFiles.put(META_DIR + JONAS_DD, jonasDD); 375 } else { 376 log("Unable to locate the JOnAS deployment descriptor. It was expected to be in: " 377 + jonasDD.getPath() + ".", Project.MSG_WARN); 378 } 379 } 380 381 382 protected File getVendorOutputJarFile(String baseName) { 383 return new File (getDestDir(), baseName + suffix); 384 } 385 386 394 private String getJonasDescriptorName() { 395 396 400 String jonasDN; boolean jonasConvention = false; String path; String fileName; String baseName; String remainder; 407 int startOfFileName = descriptorName.lastIndexOf(File.separatorChar); 408 if (startOfFileName != -1) { 409 path = descriptorName.substring(0, startOfFileName + 1); 411 fileName = descriptorName.substring(startOfFileName + 1); 412 } else { 413 path = ""; 415 fileName = descriptorName; 416 } 417 418 if (fileName.startsWith(EJB_DD)) { 419 return path + JONAS_DD; 420 } 421 422 int endOfBaseName = descriptorName.indexOf(getConfig().baseNameTerminator, startOfFileName); 423 424 429 if (endOfBaseName < 0) { 430 endOfBaseName = descriptorName.lastIndexOf('.') - 1; 435 if (endOfBaseName < 0) { 436 endOfBaseName = descriptorName.length() - 1; 438 } 439 440 jonasConvention = true; 441 } 442 443 baseName = descriptorName.substring(startOfFileName + 1, endOfBaseName + 1); 444 remainder = descriptorName.substring(endOfBaseName + 1); 445 446 if (jonasConvention) { 447 jonasDN = path + "jonas-" + baseName + ".xml"; 448 } else { 449 jonasDN = path + baseName + "jonas-" + remainder; 450 } 451 452 log("Standard EJB descriptor name: " + descriptorName, Project.MSG_VERBOSE); 453 log("JOnAS-specific descriptor name: " + jonasDN, Project.MSG_VERBOSE); 454 455 return jonasDN; 456 } 457 458 459 protected String getJarBaseName(String descriptorFileName) { 460 461 String baseName = null; 462 463 if (getConfig().namingScheme.getValue().equals(EjbJar.NamingScheme.DESCRIPTOR)) { 464 465 if (descriptorFileName.indexOf(getConfig().baseNameTerminator) == -1) { 467 468 473 String aCanonicalDescriptor = descriptorFileName.replace('\\', '/'); 474 int lastSeparatorIndex = aCanonicalDescriptor.lastIndexOf('/'); 475 int endOfBaseName; 476 477 if (lastSeparatorIndex != -1) { 478 endOfBaseName = descriptorFileName.indexOf(".xml", lastSeparatorIndex); 479 } else { 480 endOfBaseName = descriptorFileName.indexOf(".xml"); 481 } 482 483 if (endOfBaseName != -1) { 484 baseName = descriptorFileName.substring(0, endOfBaseName); 485 } 486 } 487 } 488 489 if (baseName == null) { 490 baseName = super.getJarBaseName(descriptorFileName); 492 } 493 494 log("JAR base name: " + baseName, Project.MSG_VERBOSE); 495 496 return baseName; 497 } 498 499 500 protected void registerKnownDTDs(DescriptorHandler handler) { 501 handler.registerDTD(EJB_JAR_1_1_PUBLIC_ID, 502 jonasroot + File.separator + "xml" + File.separator + EJB_JAR_1_1_DTD); 503 handler.registerDTD(EJB_JAR_2_0_PUBLIC_ID, 504 jonasroot + File.separator + "xml" + File.separator + EJB_JAR_2_0_DTD); 505 506 handler.registerDTD(JONAS_EJB_JAR_2_4_PUBLIC_ID, 507 jonasroot + File.separator + "xml" + File.separator + JONAS_EJB_JAR_2_4_DTD); 508 handler.registerDTD(JONAS_EJB_JAR_2_5_PUBLIC_ID, 509 jonasroot + File.separator + "xml" + File.separator + JONAS_EJB_JAR_2_5_DTD); 510 } 511 512 518 private void addGenICGeneratedFiles( 519 File genericJarFile, Hashtable ejbFiles) { 520 Java genicTask = null; String genicClass = null; if (nogenic) { 524 return; 525 } 526 527 genicTask = new Java(getTask()); 528 genicTask.setTaskName("genic"); 529 genicTask.setFork(true); 530 531 genicTask.createJvmarg().setValue("-Dinstall.root=" + jonasroot); 533 534 String jonasConfigDir = jonasroot + File.separator + "config"; 536 File javaPolicyFile = new File (jonasConfigDir, "java.policy"); 537 if (javaPolicyFile.exists()) { 538 genicTask.createJvmarg().setValue("-Djava.security.policy=" 539 + javaPolicyFile.toString()); 540 } 541 542 try { 544 outputdir = createTempDir(); 545 } catch (IOException aIOException) { 546 String msg = "Cannot create temp dir: " + aIOException.getMessage(); 547 throw new BuildException(msg, aIOException); 548 } 549 log("Using temporary output directory: " + outputdir, Project.MSG_VERBOSE); 550 551 genicTask.createArg().setValue("-d"); 552 genicTask.createArg().setFile(outputdir); 553 554 String key; 556 File f; 557 Enumeration keys = ejbFiles.keys(); 558 while (keys.hasMoreElements()) { 559 key = (String ) keys.nextElement(); 560 f = new File (outputdir + File.separator + key); 561 f.getParentFile().mkdirs(); 562 } 563 log("Worked around a bug of GenIC 2.5.", Project.MSG_VERBOSE); 564 565 Path classpath = getCombinedClasspath(); 567 if (classpath == null) { 568 classpath = new Path(getTask().getProject()); 569 } 570 classpath.append(new Path(classpath.getProject(), jonasConfigDir)); 571 classpath.append(new Path(classpath.getProject(), outputdir.toString())); 572 573 if (orb != null) { 575 String orbJar = jonasroot + File.separator + "lib" 576 + File.separator + orb + "_jonas.jar"; 577 classpath.append(new Path(classpath.getProject(), orbJar)); 578 } 579 log("Using classpath: " + classpath.toString(), Project.MSG_VERBOSE); 580 genicTask.setClasspath(classpath); 581 582 genicClass = getGenicClassName(classpath); 584 if (genicClass == null) { 585 log("Cannot find GenIC class in classpath.", Project.MSG_ERR); 586 throw new BuildException("GenIC class not found, please check the classpath."); 587 } else { 588 log("Using '" + genicClass + "' GenIC class." , Project.MSG_VERBOSE); 589 genicTask.setClassname(genicClass); 590 } 591 592 if (keepgenerated) { 594 genicTask.createArg().setValue("-keepgenerated"); 595 } 596 597 if (nocompil) { 599 genicTask.createArg().setValue("-nocompil"); 600 } 601 602 if (novalidation) { 604 genicTask.createArg().setValue("-novalidation"); 605 } 606 607 if (javac != null) { 609 genicTask.createArg().setValue("-javac"); 610 genicTask.createArg().setLine(javac); 611 } 612 613 if (javacopts != null && !javacopts.equals("")) { 615 genicTask.createArg().setValue("-javacopts"); 616 genicTask.createArg().setLine(javacopts); 617 } 618 619 if (rmicopts != null && !rmicopts.equals("")) { 621 genicTask.createArg().setValue("-rmicopts"); 622 genicTask.createArg().setLine(rmicopts); 623 } 624 625 if (secpropag) { 627 genicTask.createArg().setValue("-secpropag"); 628 } 629 630 if (verbose) { 632 genicTask.createArg().setValue("-verbose"); 633 } 634 635 if (additionalargs != null) { 637 genicTask.createArg().setValue(additionalargs); 638 } 639 640 genicTask.createArg().setValue("-noaddinjar"); 643 644 genicTask.createArg().setValue(genericJarFile.getPath()); 646 647 log("Calling " + genicClass + " for " + getConfig().descriptorDir 649 + File.separator + descriptorName + ".", Project.MSG_VERBOSE); 650 651 if (genicTask.executeJava() != 0) { 652 653 log("Deleting temp output directory '" + outputdir + "'.", Project.MSG_VERBOSE); 655 deleteAllFiles(outputdir); 656 657 if (!keepgeneric) { 658 log("Deleting generic JAR " + genericJarFile.toString(), 659 Project.MSG_VERBOSE); 660 genericJarFile.delete(); 661 } 662 663 throw new BuildException("GenIC reported an error."); 664 } 665 666 addAllFiles(outputdir, "", ejbFiles); 668 } 669 670 677 String getGenicClassName(Path classpath) { 678 679 log("Looking for GenIC class in classpath: " 680 + classpath.toString(), Project.MSG_VERBOSE); 681 682 AntClassLoader cl = classpath.getProject().createClassLoader(classpath); 683 684 try { 685 cl.loadClass(JonasDeploymentTool.GENIC_CLASS); 686 log("Found GenIC class '" + JonasDeploymentTool.GENIC_CLASS 687 + "' in classpath.", Project.MSG_VERBOSE); 688 return JonasDeploymentTool.GENIC_CLASS; 689 690 } catch (ClassNotFoundException cnf1) { 691 log("GenIC class '" + JonasDeploymentTool.GENIC_CLASS 692 + "' not found in classpath.", 693 Project.MSG_VERBOSE); 694 } 695 696 try { 697 cl.loadClass(JonasDeploymentTool.OLD_GENIC_CLASS_1); 698 log("Found GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_1 699 + "' in classpath.", Project.MSG_VERBOSE); 700 return JonasDeploymentTool.OLD_GENIC_CLASS_1; 701 702 } catch (ClassNotFoundException cnf2) { 703 log("GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_1 704 + "' not found in classpath.", 705 Project.MSG_VERBOSE); 706 } 707 708 try { 709 cl.loadClass(JonasDeploymentTool.OLD_GENIC_CLASS_2); 710 log("Found GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_2 711 + "' in classpath.", Project.MSG_VERBOSE); 712 return JonasDeploymentTool.OLD_GENIC_CLASS_2; 713 714 } catch (ClassNotFoundException cnf3) { 715 log("GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_2 716 + "' not found in classpath.", 717 Project.MSG_VERBOSE); 718 } 719 return null; 720 } 721 722 728 protected void checkConfiguration(String descriptorFileName, 729 SAXParser saxParser) throws BuildException { 730 731 if (jonasroot == null) { 733 throw new BuildException("The jonasroot attribut is not set."); 734 } else if (!jonasroot.isDirectory()) { 735 throw new BuildException("The jonasroot attribut '" + jonasroot 736 + "' is not a valid directory."); 737 } 738 739 if (orb != null && !orb.equals(RMI_ORB) && !orb.equals(JEREMIE_ORB) 741 && !orb.equals(DAVID_ORB)) { 742 throw new BuildException("The orb attribut '" + orb 743 + "' is not valid (must be either " 744 + RMI_ORB + ", " + JEREMIE_ORB + " or " + DAVID_ORB + ")."); 745 } 746 747 if (additionalargs != null && additionalargs.equals("")) { 749 throw new BuildException("Empty additionalargs attribut."); 750 } 751 752 if (javac != null && javac.equals("")) { 754 throw new BuildException("Empty javac attribut."); 755 } 756 } 757 758 759 760 761 762 768 private File createTempDir() throws IOException { 769 File tmpDir = File.createTempFile("genic", null, null); 770 tmpDir.delete(); 771 if (!tmpDir.mkdir()) { 772 throw new IOException ("Cannot create the temporary directory '" + tmpDir + "'."); 773 } 774 return tmpDir; 775 } 776 777 783 private void deleteAllFiles(File aFile) { 784 if (aFile.isDirectory()) { 785 File [] someFiles = aFile.listFiles(); 786 787 for (int i = 0; i < someFiles.length; i++) { 788 deleteAllFiles(someFiles[i]); 789 } 790 } 791 aFile.delete(); 792 } 793 794 802 private void addAllFiles(File file, String rootDir, Hashtable hashtable) { 803 804 if (!file.exists()) { 805 throw new IllegalArgumentException (); 806 } 807 808 String newRootDir; 809 if (file.isDirectory()) { 810 File [] files = file.listFiles(); 811 for (int i = 0; i < files.length; i++) { 812 if (rootDir.length() > 0) { 813 newRootDir = rootDir + File.separator + files[i].getName(); 814 } else { 815 newRootDir = files[i].getName(); 816 } 817 addAllFiles(files[i], newRootDir, hashtable); 818 } 819 } else { 820 hashtable.put(rootDir, file); 821 } 822 } 823 } 824 | Popular Tags |