1 54 package org.objectweb.jonas.ant; 55 56 import java.io.BufferedReader ; 57 import java.io.BufferedWriter ; 58 import java.io.File ; 59 import java.io.FileReader ; 60 import java.io.FileWriter ; 61 import java.io.IOException ; 62 import java.util.Hashtable ; 63 64 import javax.xml.parsers.SAXParser ; 65 66 import org.apache.tools.ant.AntClassLoader; 67 import org.apache.tools.ant.BuildException; 68 import org.apache.tools.ant.Project; 69 import org.apache.tools.ant.taskdefs.Java; 70 import org.apache.tools.ant.types.Path; 71 import org.apache.tools.ant.types.Reference; 72 73 84 public class JonasDeploymentTool extends GenericDeploymentTool { 85 86 87 protected static final String EJB_JAR_1_1_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"; 88 protected static final String EJB_JAR_2_0_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"; 89 90 91 protected static final String JONAS_EJB_JAR_2_4_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 2.4//EN"; 92 protected static final String JONAS_EJB_JAR_2_5_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 2.5//EN"; 93 protected static final String JONAS_EJB_JAR_3_0_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.0//EN"; 94 protected static final String JONAS_EJB_JAR_3_2_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.2//EN"; 95 protected static final String JONAS_EJB_JAR_3_3_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.3//EN"; 96 protected static final String JONAS_EJB_JAR_3_3_2_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.3.2//EN"; 97 98 102 protected static final String EJB_JAR_1_1_DTD = "ejb-jar_1_1.dtd"; 103 protected static final String EJB_JAR_2_0_DTD = "ejb-jar_2_0.dtd"; 104 105 109 protected static final String JONAS_EJB_JAR_2_4_DTD = "jonas-ejb-jar_2_4.dtd"; 110 protected static final String JONAS_EJB_JAR_2_5_DTD = "jonas-ejb-jar_2_5.dtd"; 111 protected static final String JONAS_EJB_JAR_3_0_DTD = "jonas-ejb-jar_3_0.dtd"; 112 protected static final String JONAS_EJB_JAR_3_2_DTD = "jonas-ejb-jar_3_2.dtd"; 113 protected static final String JONAS_EJB_JAR_3_3_DTD = "jonas-ejb-jar_3_3.dtd"; 114 protected static final String JONAS_EJB_JAR_3_3_2_DTD = "jonas-ejb-jar_3_3_2.dtd"; 115 116 117 protected static final String JONAS_DD = "jonas-ejb-jar.xml"; 118 119 120 protected static final String BOOTSTRAP_CLASS = "org.objectweb.jonas.server.Bootstrap"; 121 122 123 protected static final String GENIC_CLASS = "org.objectweb.jonas_ejb.genic.GenIC"; 124 125 130 private String descriptorName; 131 132 137 private String jonasDescriptorName; 138 139 140 141 142 143 146 private File outputdir; 147 148 152 private boolean keepgenerated = false; 153 154 158 private boolean nocompil = false; 159 160 164 private boolean invokeCmd = false; 165 166 170 private boolean novalidation = false; 171 172 176 private String javac; 177 178 179 private String javacopts; 180 181 182 private String rmicopts; 183 184 188 private boolean verbose = false; 189 190 191 private String additionalargs; 192 193 194 private String jvmopts = null; 195 196 197 private String protocols = null; 198 199 200 201 202 203 204 private File jonasroot; 205 206 207 private File jonasbase; 208 209 213 private boolean keepgeneric = false; 214 215 216 private String suffix = ".jar"; 217 218 222 private boolean nogenic = false; 223 224 225 226 227 228 232 public void setKeepgenerated(boolean aBoolean) { 233 keepgenerated = aBoolean; 234 } 235 236 240 public void setMappernames(String aString) { 241 log("'mappernames' attribute ignored (deprecated)", Project.MSG_WARN); 242 } 243 244 248 public void setProtocols(String aString) { 249 protocols = aString; 250 } 251 252 256 public void setClasspathref(Reference r) { 257 createClasspath().setRefid(r); 258 } 259 263 public void setAdditionalargs(String aString) { 264 additionalargs = aString; 265 } 266 267 271 public void setNocompil(boolean aBoolean) { 272 nocompil = aBoolean; 273 } 274 275 279 public void setInvokecmd(boolean aBoolean) { 280 invokeCmd = aBoolean; 281 } 282 283 287 public void setNovalidation(boolean aBoolean) { 288 novalidation = aBoolean; 289 } 290 291 295 public void setJavac(String aString) { 296 javac = aString; 297 } 298 299 303 public void setJavacopts(String aString) { 304 javacopts = aString; 305 } 306 307 311 public void setRmicopts(String aString) { 312 rmicopts = aString; 313 } 314 315 319 public void setVerbose(boolean aBoolean) { 320 verbose = aBoolean; 321 } 322 323 324 325 326 327 331 public void setJonasroot(File aFile) { 332 jonasroot = aFile; 333 } 334 335 339 public void setJonasbase(File aFile) { 340 jonasbase = aFile; 341 } 342 343 347 public void setKeepgeneric(boolean aBoolean) { 348 keepgeneric = aBoolean; 349 } 350 351 355 public void setJarsuffix(String aString) { 356 suffix = aString; 357 } 358 359 363 public void setNogenic(boolean aBoolean) { 364 nogenic = aBoolean; 365 } 366 367 371 public void setJvmopts(String aString) { 372 jvmopts = aString; 373 } 374 375 376 377 378 379 public void processDescriptor(String aDescriptorName, SAXParser saxParser) { 380 381 descriptorName = aDescriptorName; 382 383 log("JOnAS Deployment Tool processing for JOnAS: " + descriptorName, Project.MSG_VERBOSE); 384 385 super.processDescriptor(descriptorName, saxParser); 386 387 if (outputdir != null) { 388 log("Deleting temp output directory '" + outputdir + "'.", Project.MSG_VERBOSE); 391 deleteAllFiles(outputdir); 392 } 393 } 394 395 protected void writeJar(String baseName, File jarfile, Hashtable ejbFiles, String publicId, 396 boolean includeInnerClasses) throws BuildException { 397 398 File genericJarFile = super.getVendorOutputJarFile(baseName); 400 401 super.writeJar(baseName, genericJarFile, ejbFiles, publicId, true); 402 403 404 addGenICGeneratedFiles(genericJarFile, ejbFiles); 406 407 if (this.getConfig() != null && this.getConfig().manifest != null 409 && this.getConfig().manifest.exists()) { 410 File filePathGenicManifest = (File ) ejbFiles.get("META-INF/MANIFEST.MF"); 411 if (filePathGenicManifest != null) 412 mergeManifest(this.getConfig().manifest, filePathGenicManifest); 413 else 414 ejbFiles.put("META-INF/MANIFEST.MF", this.getConfig().manifest); 415 } 416 417 super.writeJar(baseName, getVendorOutputJarFile(baseName), ejbFiles, publicId, false); 419 420 425 if (!keepgeneric) { 426 log("Deleting generic JAR " + genericJarFile.toString(), Project.MSG_VERBOSE); 427 genericJarFile.delete(); 428 } 429 } 430 431 436 protected void mergeManifest(File manifestUserFile, File manifestGenicFile) { 437 try { 438 BufferedReader tempManifestUserReader = new BufferedReader (new FileReader (manifestUserFile)); 439 BufferedWriter tempManifestGenicWriter = new BufferedWriter (new FileWriter (manifestGenicFile, true)); 440 String ligneRead; 441 442 while ((ligneRead = tempManifestUserReader.readLine()) != null) { 443 tempManifestGenicWriter.write(ligneRead); 444 tempManifestGenicWriter.newLine(); 445 } 446 tempManifestGenicWriter.close(); 447 tempManifestUserReader.close(); 448 } catch (IOException e) { 449 log("Unable to update Manifest with user manifest :" + e.getMessage(), Project.MSG_WARN); 450 } 451 } 452 453 protected void addVendorFiles(Hashtable ejbFiles, String ddPrefix) { 454 455 jonasDescriptorName = getJonasDescriptorName(); 457 File jonasDD = new File (getConfig().descriptorDir, jonasDescriptorName); 458 459 if (jonasDD.exists()) { 460 ejbFiles.put(META_DIR + JONAS_DD, jonasDD); 461 } else { 462 log("Unable to locate the JOnAS deployment descriptor. It was expected to be in: " + jonasDD.getPath() 463 + ".", Project.MSG_WARN); 464 } 465 } 466 467 protected File getVendorOutputJarFile(String baseName) { 468 return new File (getDestDir(), baseName + suffix); 469 } 470 471 478 private String getJonasDescriptorName() { 479 480 484 String jonasDescriptorName; boolean jonasConvention = false; String path; String fileName; String baseName; String remainder; 492 int startOfFileName = descriptorName.lastIndexOf(File.separatorChar); 493 if (startOfFileName != -1) { 494 path = descriptorName.substring(0, startOfFileName + 1); 496 fileName = descriptorName.substring(startOfFileName + 1); 497 } else { 498 path = ""; 500 fileName = descriptorName; 501 } 502 503 if (fileName.startsWith(EJB_DD)) { 504 return path + JONAS_DD; 505 } 506 507 int endOfBaseName = descriptorName.indexOf(getConfig().baseNameTerminator, startOfFileName); 508 509 514 if (endOfBaseName < 0) { 515 endOfBaseName = descriptorName.lastIndexOf('.') - 1; 520 if (endOfBaseName < 0) { 521 endOfBaseName = descriptorName.length() - 1; 523 } 524 525 jonasConvention = true; 526 } 527 528 baseName = descriptorName.substring(startOfFileName + 1, endOfBaseName + 1); 529 remainder = descriptorName.substring(endOfBaseName + 1); 530 531 if (jonasConvention) { 532 jonasDescriptorName = path + "jonas-" + baseName + ".xml"; 533 } else { 534 jonasDescriptorName = path + baseName + "jonas-" + remainder; 535 } 536 537 log("Standard EJB descriptor name: " + descriptorName, Project.MSG_VERBOSE); 538 log("JOnAS-specific descriptor name: " + jonasDescriptorName, Project.MSG_VERBOSE); 539 540 return jonasDescriptorName; 541 } 542 543 protected String getJarBaseName(String descriptorFileName) { 544 545 String baseName = null; 546 547 if (getConfig().namingScheme.getValue().equals(EjbJar.NamingScheme.DESCRIPTOR)) { 548 549 if (descriptorFileName.indexOf(getConfig().baseNameTerminator) == -1) { 551 552 557 String aCanonicalDescriptor = descriptorFileName.replace('\\', '/'); 558 int lastSeparatorIndex = aCanonicalDescriptor.lastIndexOf('/'); 559 int endOfBaseName; 560 561 if (lastSeparatorIndex != -1) { 562 endOfBaseName = descriptorFileName.indexOf(".xml", lastSeparatorIndex); 563 } else { 564 endOfBaseName = descriptorFileName.indexOf(".xml"); 565 } 566 567 if (endOfBaseName != -1) { 568 baseName = descriptorFileName.substring(0, endOfBaseName); 569 } 570 } 571 } 572 573 if (baseName == null) { 574 baseName = super.getJarBaseName(descriptorFileName); 576 } 577 578 log("JAR base name: " + baseName, Project.MSG_VERBOSE); 579 580 return baseName; 581 } 582 583 protected void registerKnownDTDs(DescriptorHandler handler) { 584 585 handler.registerDTD(EJB_JAR_1_1_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 586 + EJB_JAR_1_1_DTD); 587 handler.registerDTD(EJB_JAR_2_0_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 588 + EJB_JAR_2_0_DTD); 589 590 handler.registerDTD(JONAS_EJB_JAR_2_4_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 591 + JONAS_EJB_JAR_2_4_DTD); 592 handler.registerDTD(JONAS_EJB_JAR_2_5_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 593 + JONAS_EJB_JAR_2_5_DTD); 594 handler.registerDTD(JONAS_EJB_JAR_3_0_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 595 + JONAS_EJB_JAR_3_0_DTD); 596 handler.registerDTD(JONAS_EJB_JAR_3_2_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 597 + JONAS_EJB_JAR_3_2_DTD); 598 handler.registerDTD(JONAS_EJB_JAR_3_3_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 599 + JONAS_EJB_JAR_3_3_DTD); 600 handler.registerDTD(JONAS_EJB_JAR_3_3_2_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator 601 + JONAS_EJB_JAR_3_3_2_DTD); 602 } 603 604 609 private void addGenICGeneratedFiles(File genericJarFile, Hashtable ejbFiles) { 610 611 Java genicTask = null; 613 if (nogenic) { 614 return; 615 } 616 617 genicTask = (Java) getTask().getProject().createTask("java"); 618 genicTask.setTaskName("genic"); 619 genicTask.setFork(true); 620 621 genicTask.createJvmarg().setValue("-Dinstall.root=" + jonasroot); 623 624 genicTask.createJvmarg().setValue("-Djonas.base=" + jonasbase); 626 627 File endorsedDir = new File (new File (jonasroot, "lib"), "endorsed"); 629 genicTask.createJvmarg().setValue("-Djava.endorsed.dirs=" + endorsedDir); 630 631 String jonasConfigDir = jonasroot + File.separator + "conf"; 633 File javaPolicyFile = new File (jonasConfigDir, "java.policy"); 634 if (javaPolicyFile.exists()) { 635 genicTask.createJvmarg().setValue("-Djava.security.policy=" + javaPolicyFile.toString()); 636 } 637 638 genicTask.createArg().setValue(JonasDeploymentTool.GENIC_CLASS); 640 641 try { 643 outputdir = createTempDir(); 644 } catch (IOException aIOException) { 645 throw new BuildException("Cannot create temp dir", aIOException); 646 } 647 log("Using temporary output directory: " + outputdir, Project.MSG_VERBOSE); 648 649 genicTask.createArg().setValue("-d"); 650 genicTask.createArg().setFile(outputdir); 651 652 Path classpath = getCombinedClasspath(); 654 if (classpath == null) { 655 classpath = new Path(getTask().getProject()); 656 } 657 String bootJar = jonasroot + File.separator + "lib" + File.separator + "common" + File.separator 659 + "ow_jonas_bootstrap.jar"; 660 Path bootstrap = new Path(getTask().getProject(), bootJar); 661 663 classpath.append(new Path(classpath.getProject(), outputdir.toString())); 664 665 log("Using classpath: " + bootstrap.toString(), Project.MSG_VERBOSE); 666 genicTask.setClasspath(bootstrap); 667 668 if (!checkBootstrapClassName(bootstrap)) { 669 log("Cannot find bootstrap class in classpath.", Project.MSG_ERR); 670 throw new BuildException("Bootstrap class not found, please check the classpath."); 671 } else { 672 genicTask.setClassname(JonasDeploymentTool.BOOTSTRAP_CLASS); 673 } 674 675 genicTask.createArg().setValue("-classpath"); 677 genicTask.createArg().setPath(classpath); 678 679 if (keepgenerated) { 681 genicTask.createArg().setValue("-keepgenerated"); 682 } 683 684 if (nocompil) { 686 genicTask.createArg().setValue("-nocompil"); 687 } 688 689 if (invokeCmd) { 691 genicTask.createArg().setValue("-invokecmd"); 692 } 693 694 if (novalidation) { 696 genicTask.createArg().setValue("-novalidation"); 697 } 698 699 if (javac != null) { 701 genicTask.createArg().setValue("-javac"); 702 genicTask.createArg().setLine(javac); 703 } 704 705 if (javacopts != null && !javacopts.equals("")) { 707 genicTask.createArg().setValue("-javacopts"); 708 genicTask.createArg().setValue(javacopts); 709 } 710 711 if (rmicopts != null && !rmicopts.equals("")) { 713 genicTask.createArg().setValue("-rmicopts"); 714 genicTask.createArg().setValue(rmicopts); 715 } 716 717 if (verbose) { 719 genicTask.createArg().setValue("-verbose"); 720 } 721 722 if (additionalargs != null) { 724 genicTask.createArg().setLine(additionalargs); 725 } 726 727 if (protocols != null) { 729 genicTask.createArg().setValue("-protocols"); 730 genicTask.createArg().setValue(protocols); 731 } 732 733 genicTask.createArg().setValue("-noaddinjar"); 735 736 if (jvmopts != null && !jvmopts.equals("")) { 738 genicTask.createJvmarg().setLine(jvmopts); 739 } 740 741 genicTask.createArg().setValue(genericJarFile.getPath()); 743 744 log("Calling GenIC task for " + getConfig().descriptorDir + File.separator + descriptorName + ".", 746 Project.MSG_VERBOSE); 747 748 if (genicTask.executeJava() != 0) { 749 751 757 if (!keepgeneric) { 758 log("Deleting generic JAR " + genericJarFile.toString(), Project.MSG_VERBOSE); 759 genericJarFile.delete(); 760 } 761 762 throw new BuildException("GenIC reported an error."); 763 } 764 765 addAllFiles(outputdir, "", ejbFiles); 767 } 768 769 774 private boolean checkBootstrapClassName(Path classpath) { 775 log("Looking for bootstrap class in classpath: " + classpath.toString(), Project.MSG_VERBOSE); 776 AntClassLoader cl = new AntClassLoader(classpath.getProject(), classpath); 777 try { 778 cl.loadClass(JonasDeploymentTool.BOOTSTRAP_CLASS); 779 log("Found Bootstrap class '" + JonasDeploymentTool.BOOTSTRAP_CLASS + "' in classpath.", 780 Project.MSG_VERBOSE); 781 } catch (ClassNotFoundException cnf1) { 782 log("Bootstrap class '" + JonasDeploymentTool.BOOTSTRAP_CLASS + "' not found in classpath.", 783 Project.MSG_VERBOSE); 784 return false; 785 } 786 return true; 787 } 788 789 protected void checkConfiguration(String descriptorFileName, SAXParser saxParser) throws BuildException { 790 if (jonasroot == null) { 792 throw new BuildException("The jonasroot attribute is not set."); 793 } else if (!jonasroot.isDirectory()) { 794 throw new BuildException("The jonasroot attribute '" + jonasroot + "' is not a valid directory."); 795 } 796 if (jonasbase == null) { 798 jonasbase = jonasroot; 799 } 800 801 if (additionalargs != null && additionalargs.equals("")) { 803 throw new BuildException("Empty additionalargs attribute."); 804 } 805 806 if (javac != null && javac.equals("")) { 808 throw new BuildException("Empty javac attribute."); 809 } 810 } 811 812 813 814 815 816 821 private File createTempDir() throws IOException { 822 File tmpDir = File.createTempFile("genic", null, null); 823 tmpDir.delete(); 824 if (!tmpDir.mkdir()) { 825 throw new IOException ("Cannot create the temporary directory '" + tmpDir + "'."); 826 } 827 return tmpDir; 828 } 829 830 835 private void deleteAllFiles(File aFile) { 836 if (aFile.isDirectory()) { 837 File someFiles[] = aFile.listFiles(); 838 for (int i = 0; i < someFiles.length; i++) { 839 deleteAllFiles(someFiles[i]); 840 } 841 } 842 aFile.delete(); 843 } 844 845 852 private void addAllFiles(File file, String rootDir, Hashtable hashtable) { 853 854 if (!file.exists()) { 855 throw new IllegalArgumentException (); 856 } 857 858 String newRootDir; 859 if (file.isDirectory()) { 860 File [] files = file.listFiles(); 861 for (int i = 0; i < files.length; i++) { 862 if (rootDir.length() > 0) { 863 newRootDir = rootDir + File.separator + files[i].getName(); 864 } else { 865 newRootDir = files[i].getName(); 866 } 867 addAllFiles(files[i], newRootDir, hashtable); 868 } 869 } else { 870 hashtable.put(rootDir.replace(File.separatorChar , '/'), file); } 872 } 873 } | Popular Tags |