1 24 25 package org.objectweb.jonas_ejb.genic; 26 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.InputStreamReader ; 35 import java.io.OutputStreamWriter ; 36 import java.io.PrintStream ; 37 import java.lang.reflect.InvocationTargetException ; 38 import java.lang.reflect.Method ; 39 import java.net.MalformedURLException ; 40 import java.net.URL ; 41 import java.util.ArrayList ; 42 import java.util.Collection ; 43 import java.util.Enumeration ; 44 import java.util.Hashtable ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.Properties ; 48 import java.util.StringTokenizer ; 49 import java.util.TreeSet ; 50 import java.util.jar.JarEntry ; 51 import java.util.jar.JarFile ; 52 import java.util.jar.JarOutputStream ; 53 54 import org.apache.velocity.app.VelocityEngine; 55 import org.apache.velocity.runtime.RuntimeConstants; 56 57 import org.objectweb.common.Cmd; 58 import org.objectweb.common.Env; 59 60 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 61 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc; 62 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb2; 63 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc; 64 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc; 65 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager; 66 import org.objectweb.jonas_ejb.lib.BeanNaming; 67 68 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 69 import org.objectweb.jonas_lib.files.FileUtils; 70 import org.objectweb.jonas_lib.files.FileUtilsException; 71 import org.objectweb.jonas_lib.version.Version; 72 73 import org.objectweb.jonas.common.Log; 74 import org.objectweb.jonas.server.JClassLoader; 75 76 import org.objectweb.jorm.api.PException; 77 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 78 import org.objectweb.jorm.compiler.lib.JormCompiler; 79 import org.objectweb.jorm.metainfo.api.Manager; 80 81 import org.objectweb.medor.api.MedorException; 82 83 import org.objectweb.util.monolog.api.BasicLevel; 84 import org.objectweb.util.monolog.api.Logger; 85 import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger; 86 87 104 public class GenIC { 105 106 109 public static final String RMI_JRMP = "jrmp"; 110 111 114 public static final String RMI_IIOP = "iiop"; 115 116 119 public static final String JEREMIE = "jeremie"; 120 121 124 public static final String CMI_RMI = "cmi"; 125 126 129 private static final String INSTALL_ROOT_PROPERTY = "install.root"; 130 131 134 private static String javaHomeBin = null; 135 136 139 private boolean verbose = false; 140 141 144 private String outputdir = null; 145 146 149 private boolean generatedIC; 150 151 154 private ArrayList filesToDelete = null; 155 156 159 private ArrayList remoteJavas = null; 160 161 164 private ArrayList noRemoteJavas = null; 165 166 169 private ArrayList remoteClasses = null; 170 171 174 private static Logger logger = null; 175 176 179 private static final int BUFFER_SIZE = 1024; 180 181 182 private static final String META_DIR = "META-INF"; 183 184 185 private static final String MANIFEST_JAR_ENTRY = META_DIR + "/MANIFEST.MF"; 186 187 188 private static final String MANIFEST_PATH = META_DIR + File.separator + "MANIFEST.MF"; 189 190 226 public static void main(String [] args) { 227 boolean error = false; 228 229 boolean isHelp = false; 230 boolean isVerbose = false; 231 boolean isKeepGenerated = false; 232 boolean isCompil = true; 233 boolean isAddInJar = true; 234 boolean parseWithValidation = true; 235 boolean invokeCmd = false; 236 237 boolean nofastrmic = false; 239 240 String fileInputName = null; 241 String dirOutputName = null; 242 String nameJavac = null; 243 ArrayList optionsJavaC = new ArrayList (); 244 ArrayList optionsRmiC = new ArrayList (); 245 String protocols = RMI_JRMP; ProtocolNames protocolsNames = null; 247 String classpathParam = null; 248 249 Log.configure("trace"); 251 logger = Log.getLogger(Log.JONAS_GENIC_PREFIX); 252 253 for (int argn = 0; argn < args.length; argn++) { 255 String arg = args[argn]; 256 if (arg.equals("-help") || arg.equals("-?")) { 257 isHelp = true; 258 continue; 259 } 260 if (arg.equals("-verbose")) { 261 isVerbose = true; 262 continue; 263 } 264 if (arg.equals("-debug")) { 265 continue; 267 } 268 if (arg.equals("-mappernames")) { 269 argn++; 270 warning("The -mappernames option is ignored (deprecated)"); 272 } 273 if (arg.equals("-protocols")) { 274 argn++; 275 if (argn < args.length) { 276 protocols = args[argn]; 277 continue; 278 } else { 279 error = true; 280 } 281 } 282 if (arg.equals("-keepgenerated")) { 283 isKeepGenerated = true; 284 optionsRmiC.add(args[argn]); 285 continue; 286 } 287 if (arg.equals("-nocompil")) { 288 isCompil = false; 289 isKeepGenerated = true; 290 continue; 291 } 292 if (arg.equals("-noaddinjar")) { 293 isAddInJar = false; 294 continue; 295 } 296 if (arg.equals("-novalidation")) { 297 parseWithValidation = false; 298 continue; 299 } 300 if (arg.equals("-classpath")) { 301 classpathParam = args[++argn]; 302 continue; 303 } 304 if (arg.equals("-javac")) { 305 argn++; 306 if (argn < args.length) { 307 nameJavac = args[argn]; 308 } else { 309 error = true; 310 } 311 continue; 312 } 313 if (arg.equals("-javacopts")) { 314 argn++; 315 if (argn < args.length) { 316 StringTokenizer st = new StringTokenizer (args[argn]); 317 while (st.hasMoreTokens()) { 318 optionsJavaC.add(st.nextToken()); 319 } 320 } else { 321 error = true; 322 } 323 continue; 324 } 325 if (arg.equals("-rmicopts")) { 326 argn++; 327 if (argn < args.length) { 328 StringTokenizer st = new StringTokenizer (args[argn]); 329 while (st.hasMoreTokens()) { 330 optionsRmiC.add(st.nextToken()); 331 } 332 } else { 333 error = true; 334 } 335 continue; 336 } 337 if (arg.equals("-d")) { 338 argn++; 339 if (argn < args.length) { 340 dirOutputName = args[argn]; 341 } else { 342 error = true; 343 } 344 continue; 345 } 346 if (arg.equals("-invokecmd")) { 347 invokeCmd = true; 348 continue; 349 } 350 if (arg.equals("-fastrmic")) { 351 argn++; 352 warning("The -fastrmic option is ignored as it is the default value. Use -nofastrmic to disable it."); 354 continue; 355 } 356 357 if (arg.equals("-nofastrmic")) { 358 argn++; 359 nofastrmic = true; 360 continue; 361 } 362 363 fileInputName = args[argn]; 364 } 365 366 if (isHelp) { 368 usage(); 369 return; 370 } 371 372 if (error || (fileInputName == null)) { 374 usage(); 375 throw new RuntimeException (); 376 } 377 if ((dirOutputName != null) && isAddInJar && fileInputName.endsWith(".jar")) { 381 warning("The -d '" + dirOutputName + "' option is ignored" + " (deprecated with an ejb-jar as input file)"); 382 } 383 384 if (dirOutputName == null) { 385 dirOutputName = new String (""); 386 } 387 388 protocolsNames = new ProtocolNames(protocols); 390 391 if (fileInputName.endsWith(".jar") && isAddInJar) { 394 try { 395 dirOutputName = GenIC.createTempDir(); 396 } catch (IOException ioe) { 397 GenIC.fatalError(ioe); 398 } 399 } 400 401 JClassLoader pcl = (JClassLoader) Thread.currentThread().getContextClassLoader(); 403 404 String classpath = pcl.getClassPath(); 405 if (fileInputName.endsWith(".jar")) { 406 classpath = fileInputName + File.pathSeparator + classpath; 407 } 408 if (!"".equals(dirOutputName)) { 409 classpath = dirOutputName + File.pathSeparator + classpath; 410 } 411 if (classpathParam != null) { 413 classpath = classpathParam + File.pathSeparator + classpath; 414 } 415 416 if (!parseWithValidation) { 418 EjbDeploymentDescManager.setParsingWithValidation(false); 419 } 420 421 try { 423 DeploymentDesc ejbJarDD = null; 424 if (fileInputName.endsWith(".jar")) { 425 URL [] url = new URL [1]; 427 url[0] = (new File (fileInputName)).toURL(); 428 JClassLoader cl = new JClassLoader("GenIC-" + fileInputName, url, pcl); 429 if (classpathParam != null) { 430 addClasspath(cl, classpathParam); 432 } 433 ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName, cl); 434 } else { 435 ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName, BeanNaming 437 .getJonasXmlName(fileInputName), BeanNaming.getParentName(fileInputName)); 438 } 439 440 GenIC gwc = new GenIC(ejbJarDD, dirOutputName, isVerbose); 441 442 ejbJarDD = null; 449 System.gc(); 450 451 if (isCompil) { 452 gwc.compilClasses(nameJavac, optionsJavaC, optionsRmiC, classpath, protocolsNames, isKeepGenerated, 453 invokeCmd, nofastrmic); 454 if (fileInputName.endsWith(".jar") && isAddInJar) { 455 gwc.addClassesInJar(fileInputName, isKeepGenerated); 456 } 457 if (!isKeepGenerated) { 458 gwc.clean(); 459 } 460 } 461 462 if (fileInputName != null && fileInputName.endsWith(".jar") && isAddInJar) { 464 try { 465 updateAttributInManifest(fileInputName, "Genic-Jonas-Version", Version.NUMBER); 466 updateAttributInManifest(fileInputName, "Genic-Jonas-protocols", protocols); 467 } catch (FileUtilsException e) { 468 if (logger.isLoggable(BasicLevel.WARN)) { 469 logger.log(BasicLevel.WARN, "Can't update the Genic-Jonas-Version of this application :" + fileInputName, e); 470 } 471 } 472 } else if (dirOutputName != null && !isAddInJar) { 473 Hashtable atts = new Hashtable (); 476 atts.put("Manifest-Version", "1.0"); 477 atts.put("Genic-Jonas-Version", Version.NUMBER); 478 atts.put("Genic-Jonas-protocols", protocols); 479 createManifest(dirOutputName, atts); 480 } 481 } catch (MalformedURLException e) { 482 GenIC.fatalError("Invalid ejb-jar file name : ", e); 483 } catch (GenICException e) { 484 GenIC.fatalError(e); 485 } catch (DeploymentDescException e) { 486 GenIC.fatalError("Cannot read the Deployment Descriptors from " + fileInputName + ": ", e); 487 } 488 } 490 491 498 private static void updateAttributInManifest(String jarName, String attributName, String attributValue) throws FileUtilsException { 499 500 boolean jarUpdated = false; 502 JarFile jar = null; 503 File tempJarFile = null; 504 505 File jarFile = new File (jarName); 506 try { 507 jar = new JarFile (jarFile); 508 509 tempJarFile = File.createTempFile("filetoadd" , ".jar"); 512 513 JarOutputStream tempJar = 514 new JarOutputStream (new FileOutputStream (tempJarFile)); 515 516 518 byte[] buffer = new byte[BUFFER_SIZE]; 519 int bytesRead; 520 String ligneRead; 521 522 525 for (Enumeration entries = jar.entries(); entries.hasMoreElements();) { 526 528 JarEntry entry = (JarEntry ) entries.nextElement(); 529 String entryName = entry.getName(); 530 if (!entryName.equalsIgnoreCase(MANIFEST_JAR_ENTRY)) { 533 InputStream entryStream = jar.getInputStream(entry); 535 537 tempJar.putNextEntry(entry); 538 539 while ((bytesRead = entryStream.read(buffer)) != -1) { 540 tempJar.write(buffer, 0, bytesRead); 541 } 542 } else { 543 File tempManifestFile = File.createTempFile("MANIFEST", ".MF"); 544 BufferedWriter tempManifestWriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (tempManifestFile))); 545 InputStream entryStream = jar.getInputStream(entry); 546 InputStreamReader entryStreamReader = new InputStreamReader (entryStream); 547 BufferedReader entryBufferedReader = new BufferedReader (entryStreamReader); 548 549 boolean findJonasVersion = false; 551 String ligneToWrite = attributName + ": " + attributValue; 552 while ((ligneRead = entryBufferedReader.readLine()) != null) { 553 if (ligneRead.equalsIgnoreCase(ligneToWrite)) { 554 tempManifestWriter.write(ligneRead); 555 tempManifestWriter.newLine(); 556 findJonasVersion = true; 557 } else if (ligneRead.startsWith(attributName)) { 558 tempManifestWriter.write(ligneToWrite); 559 tempManifestWriter.newLine(); 560 jarUpdated = true; 561 findJonasVersion = true; 562 } else if (!ligneRead.equals("")) { 563 tempManifestWriter.write(ligneRead); 564 tempManifestWriter.newLine(); 565 } 566 } 567 if (!findJonasVersion) { 568 tempManifestWriter.write(ligneToWrite); 569 tempManifestWriter.newLine(); 570 jarUpdated = true; 571 } 572 tempManifestWriter.close(); 573 574 FileInputStream fileInputManifest = new FileInputStream (tempManifestFile); 576 try { 577 JarEntry newEntry = new JarEntry (MANIFEST_JAR_ENTRY); 579 tempJar.putNextEntry(newEntry); 580 581 583 while ((bytesRead = fileInputManifest.read(buffer)) != -1) { 584 tempJar.write(buffer, 0, bytesRead); 585 } 586 } catch (Exception ex) { 587 logger.log(BasicLevel.WARN, "Can't update manifest", ex); 588 } finally { 589 fileInputManifest.close(); 590 tempManifestFile.delete(); 591 } 592 } 593 } 594 jar.close(); 595 tempJar.close(); 596 } catch (Exception ex) { 597 logger.log(BasicLevel.ERROR, ex); 598 } 599 600 if (jarUpdated) { 602 FileUtils.copyFile(tempJarFile, jarFile); 603 } 604 605 tempJarFile.delete(); 607 } 608 609 615 private static boolean createManifest(String dirOutputName, Hashtable atts) { 616 try { 617 File fileOutput = new File (dirOutputName + File.separator + META_DIR); 618 619 if (!fileOutput.exists()) { 620 fileOutput.mkdirs(); 621 } 622 if (!fileOutput.exists()) { 623 GenIC.warning("Can't create META-INF directory into temp dir : " + dirOutputName + "/META-INF"); 624 return false; 625 } 626 627 fileOutput = new File (dirOutputName + File.separator + MANIFEST_PATH); 628 if (!fileOutput.exists()) { 629 fileOutput.createNewFile(); 630 } 631 if (!fileOutput.exists()) { 632 GenIC.warning("Can't create manifest.mf file into " + dirOutputName + File.separator + "META-INF"); 633 return false; 634 } 635 BufferedWriter tempManifestWriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fileOutput))); 636 Collection names = atts.keySet(); 637 Iterator namesIt = names.iterator(); 638 String name; 639 String value; 640 while (namesIt.hasNext()) { 641 name = (String ) namesIt.next(); 642 value = (String ) atts.get(name); 643 tempManifestWriter.write(name + ": " + value); 644 tempManifestWriter.newLine(); 645 } 646 tempManifestWriter.close(); 647 return true; 648 } catch (Exception e) { 649 GenIC.warning("Problem during creation of META-INF/manifest.mf file into tempdir : " + e.getMessage()); 650 return false; 651 } 652 } 653 654 660 private static void addClasspath(JClassLoader cl, String classpath) throws GenICException { 661 String [] elems = classpath.split(File.pathSeparator); 662 for (int i = 0; i < elems.length; i++) { 663 try { 664 cl.addURL(new File (elems[i]).toURL()); 665 } catch (MalformedURLException e) { 666 throw new GenICException("Cannot create URL from '" + elems[i] + "'", e); 667 } 668 } 669 } 670 671 680 public GenIC(DeploymentDesc ejbJarDesc, String dirOutputName, boolean isVerbose) throws GenICException { 681 682 ArrayList beanList = null; 684 685 verbose = isVerbose; 686 687 if (javaHomeBin == null) { 688 javaHomeBin = System.getProperty("java.home", ""); 689 if (!("".equals(javaHomeBin))) { 690 if (Env.isOsMacOsX()) { 691 javaHomeBin = javaHomeBin + File.separator + "bin" + File.separator; 692 } else { 693 javaHomeBin = javaHomeBin + File.separator + ".." + File.separator + "bin" + File.separator; 694 } 695 } 696 } 697 outputdir = dirOutputName; 698 filesToDelete = new ArrayList (); 699 remoteJavas = new ArrayList (); 700 noRemoteJavas = new ArrayList (); 701 remoteClasses = new ArrayList (); 702 beanList = new ArrayList (); 703 BeanDesc[] beansDD = ejbJarDesc.getBeanDesc(); 704 JormCompiler jormCompiler = null; 705 VelocityEngine ve = allocateVelocityEngine(); 706 StringBuffer message = new StringBuffer (); 708 message.append("GenIC for JOnAS " + Version.NUMBER + ": "); 709 String sep = ""; 710 for (int i = 0; i < beansDD.length; i++) { 711 BeanSources bs = null; 712 if ((beansDD[i] instanceof MessageDrivenDesc)) { 713 continue; 715 } 716 if (beansDD[i] instanceof EntityCmp2Desc) { 717 if (jormCompiler == null) { 718 try { 720 jormCompiler = allocateJormCompiler(((DeploymentDescEjb2) ejbJarDesc).getJormManager()); 721 } catch (DeploymentDescException e) { 722 throw new GenICException("Impossible to load jorm meta information", e); 723 } 724 } 725 bs = new BeanSources(beansDD[i], outputdir, ve, jormCompiler); 726 } else { 727 bs = new BeanSources(beansDD[i], outputdir, ve); 728 } 729 beanList.add(bs); 730 message.append(sep); 732 sep = ", "; 733 message.append("'"); 734 message.append(bs.getEjbName()); 735 message.append("'"); 736 } 737 generatedIC = !beanList.isEmpty(); 738 if (generatedIC) { 739 message.append(" generation ..."); 740 } else { 741 message.append("No generation to do (only message driven beans)"); 742 } 743 GenIC.info(message.toString()); 744 745 for (Iterator it = beanList.iterator(); it.hasNext();) { 749 BeanSources ics = (BeanSources) it.next(); 750 ics.generate(); 751 String ccfn = ics.getWrpHomeClusterFileName(); 752 if (ccfn != null) { 753 filesToDelete.add(ccfn); 754 } 755 trace("Sources classes successfully generated" + " for '" + ics.getEjbName() + "'"); 756 noRemoteJavas.addAll(ics.getNoRemoteJavas()); 757 if (ics.getWrpHomeFileName() != null) { 758 remoteJavas.add(ics.getWrpHomeFileName()); 759 remoteClasses.add(ics.getWrpHomeClassName()); 760 } 761 if (ics.getWrpRemoteFileName() != null) { 762 remoteJavas.add(ics.getWrpRemoteFileName()); 763 remoteClasses.add(ics.getWrpRemoteClassName()); 764 } 765 if (ics.getWrpServiceEndpointFileName() != null) { 766 remoteJavas.add(ics.getWrpServiceEndpointFileName()); 767 remoteClasses.add(ics.getWrpServiceEndpointClassName()); 768 } 769 } 770 jormCompiler = null; 771 772 } 773 774 779 private JormCompiler allocateJormCompiler(Manager miManager) { 780 JormCompiler jormCompiler = new JormCompiler(); 782 JormCompilerConfigurator jcc = jormCompiler.getCompilerConfigurator(); 783 Properties prop = new Properties (); 784 prop.put("jorm.generator", "org.objectweb.jorm.generator.lib.JormGenerator"); 785 prop.put("jorm.mimanager", "org.objectweb.jorm.metainfo.lib.JormManager"); 786 prop.put("jorm.writer", "org.objectweb.jorm.mi2xml.lib.BasicDomWriter"); 787 prop.put("jorm.mapper.list", "rdb"); 788 prop.put("jorm.mapper.mifactory.rdb", "org.objectweb.jorm.mapper.rdb.metainfo.RdbMappingFactory"); 789 prop.put("jorm.mapper.mopfactory.rdb", "org.objectweb.jorm.mapper.rdb.generator.RdbMOPFactory"); 790 prop.put("jorm.mapper.gcmapping.rdb", "org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassMapping"); 791 prop.put("jorm.mapper.schmgr.rdb", "org.objectweb.jorm.mapper.rdb.lib.RdbPMappingStructuresManager"); 792 prop.put("jorm.mapper.writer.rdb", "org.objectweb.jorm.mapper.rdb.mi2xml.RdbDomtreeBuilder"); 793 prop.put("use.context.classloader", "true"); 794 prop.put("jorm.mapper.submappers.rdb", "generic"); 795 jcc.configure(prop); 796 jcc.setLoggerFactory(Log.getLoggerFactory()); 797 jormCompiler.setMIManager(miManager); 798 return jormCompiler; 799 } 800 801 806 private VelocityEngine allocateVelocityEngine() throws GenICException { 807 VelocityEngine ve = new VelocityEngine(); 808 ve.setProperty(RuntimeConstants.VM_LIBRARY, "GenICMacros.vm"); 809 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); 810 811 String jonasRoot = System.getProperty(INSTALL_ROOT_PROPERTY); 812 if (jonasRoot == null) { 813 throw new GenICException("System property '" + INSTALL_ROOT_PROPERTY + "' not set"); 814 } 815 String path2Tmpl = new String (jonasRoot + File.separatorChar + "templates" + File.separatorChar + "genic"); 816 ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path2Tmpl); 817 Logger vLogger = Log.getLogger(Log.JONAS_GENIC_VELOCITY_PREFIX); 819 if (vLogger.isLoggable(BasicLevel.DEBUG)) { 820 ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem"); 823 } else { 824 ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new VelocityLogger(vLogger)); 826 } 827 try { 828 ve.init(); 829 } catch (Exception e) { 830 throw new GenICException("Cannot initialize the Velocity engine", e); 831 } 832 return ve; 833 } 834 835 854 public void compilClasses(String nameJavac, List optionsJavaC, List optionsRmiC, String classpath, 855 ProtocolNames protocols, boolean keepGenerated, boolean invkCmd, 856 boolean nofastrmic) throws GenICException { 857 858 Cmd jrmpCmd; 859 Cmd iiopCmd; 860 Cmd jeremieCmd; 861 Cmd cmd; 862 863 String cmdJava; 864 String cmdJavac; 865 String cmdRmic; 866 867 if (!generatedIC) { 868 return; 869 } 870 871 874 cmdJava = javaHomeBin + "java"; 875 if (nameJavac == null) { 876 cmdJavac = javaHomeBin + "javac"; 877 } else { 878 cmdJavac = nameJavac; 879 } 880 cmdRmic = javaHomeBin + "rmic"; 881 882 885 if (!new File (cmdJavac).exists()) { 886 if (!new File (cmdJavac + ".exe").exists()) { 888 logger.log(BasicLevel.INFO, "No javac command was found at '" + cmdJavac 889 + "'. Check that you are using a JDK and not a JRE."); 890 } 891 } 892 cmd = new Cmd(cmdJavac, invkCmd); 893 cmd.addArgument("-classpath"); 894 cmd.addArgument(classpath); 895 cmd.addArgument("-d"); 896 if ((outputdir.length() == 0)) { 897 cmd.addArgument("."); 898 } else { 899 cmd.addArgument(outputdir); 900 } 901 cmd.addArguments(optionsJavaC); 902 903 for (Iterator it = remoteJavas.iterator(); it.hasNext();) { 904 String srcName = (String ) it.next(); 905 cmd.addArgument(srcName); 906 filesToDelete.add(srcName); 907 } 908 for (Iterator it = noRemoteJavas.iterator(); it.hasNext();) { 909 String srcName = (String ) it.next(); 910 cmd.addArgument(srcName); 911 filesToDelete.add(srcName); 912 } 913 914 trace("Running '" + cmd.toString() + "'"); 915 if (cmd.run()) { 916 trace("Sources classes successfully compiled via java compiler."); 917 } else { 918 throw new GenICException("Failed when compiling the generated classes via java compiler"); 919 } 920 921 if (remoteJavas.size() == 0) { 922 return; 923 } 924 925 928 929 if (!new File (cmdRmic).exists()) { 930 if (!new File (cmdRmic + ".exe").exists()) { 932 logger.log(BasicLevel.INFO, "No rmic command was found at '" + cmdRmic 933 + "'. Check that you are using a JDK and not a JRE."); 934 } 935 } 936 jrmpCmd = new Cmd(cmdRmic); 937 jrmpCmd.addArgument("-classpath"); 938 jrmpCmd.addArgument(classpath); 939 940 iiopCmd = new Cmd(cmdRmic); 941 iiopCmd.addArgument("-classpath"); 942 iiopCmd.addArgument(classpath); 943 iiopCmd.addArgument("-iiop"); 944 iiopCmd.addArgument("-poa"); 945 iiopCmd.addArgument("-always"); 946 947 jeremieCmd = new Cmd(cmdJava); 948 jeremieCmd.addArgument("-classpath"); 949 jeremieCmd.addArgument(classpath); 950 jeremieCmd.addArgument("org.objectweb.jeremie.tools.jrmic.JRMICompiler"); 951 jeremieCmd.addArgument("-opt"); 952 jeremieCmd.addArgument("-owext"); 953 if (nameJavac != null) { 954 jeremieCmd.addArgument("-c"); 955 jeremieCmd.addArgument(nameJavac); 956 } 957 958 if (!"".equals(outputdir)) { 959 jrmpCmd.addArgument("-d"); 960 jrmpCmd.addArgument(outputdir); 961 iiopCmd.addArgument("-d"); 962 iiopCmd.addArgument(outputdir); 963 jeremieCmd.addArgument("-d"); 964 jeremieCmd.addArgument(outputdir); 965 } 966 967 jrmpCmd.addArguments(optionsRmiC); 968 iiopCmd.addArguments(optionsRmiC); 969 jeremieCmd.addArguments(optionsRmiC); 970 971 for (Iterator it = remoteClasses.iterator(); it.hasNext();) { 972 String className = (String ) it.next(); 973 jrmpCmd.addArgument(className); 974 iiopCmd.addArgument(className); 975 jeremieCmd.addArgument(className); 976 } 977 978 if (protocols.isSupported(RMI_JRMP) && !nofastrmic) { 980 trace("Running fastrmic for '" + jrmpCmd.toString() + "'"); 981 ArrayList args = new ArrayList (); 982 boolean skip = true; 983 for (Iterator it = jrmpCmd.getCommandLine(); it.hasNext();) { 984 Object o = it.next(); 985 if (skip) { 986 skip = false; 987 continue; 988 } 989 args.add(o); 990 } 991 992 String [] a = (String []) args.toArray(new String [] {}); 993 Method m = null; 994 try { 995 Class c = Class.forName("org.objectweb.fastrmic.RMIC"); 996 m = c.getMethod("main", new Class [] {String [].class}); 997 } catch (ClassNotFoundException cnfe) { 998 logger.log(BasicLevel.ERROR, 999 "continuing after class not found ", cnfe); 1000 nofastrmic = true; 1001 } catch (NoSuchMethodException nsme) { 1002 logger.log(BasicLevel.ERROR, 1003 "continuing after no such method ", nsme); 1004 nofastrmic = true; 1005 } 1006 1007 if (m != null) { 1008 try { 1009 m.invoke(null, new Object [] {a}); 1010 System.gc(); 1013 } catch (IllegalAccessException iae) { 1014 logger.log(BasicLevel.ERROR, 1015 "continuing after illegal access", iae); 1016 nofastrmic = true; 1017 } catch (InvocationTargetException ite) { 1018 throw new GenICException("error in fastrmic", ite); 1019 } 1020 info("Stubs and Skels successfully generated with fastrmic for rmi/jrmp"); 1021 } 1022 } 1023 1024 if (protocols.isSupported(RMI_JRMP) && nofastrmic) { 1025 trace("Running '" + jrmpCmd.toString() + "'"); 1026 if (jrmpCmd.run()) { 1027 info("Stubs and Skels successfully generated for rmi/jrmp"); 1028 } else { 1029 throw new GenICException("Failed when generating the Stubs and Skels with rmic jrmp"); 1030 } 1031 } 1032 1033 if (protocols.isSupported(RMI_IIOP)) { 1034 trace("Running '" + iiopCmd.toString() + "'"); 1035 if (iiopCmd.run()) { 1036 info("Stubs and Skels successfully generated for rmi/iiop"); 1037 } else { 1038 throw new GenICException("Failed when generating the Stubs and Skels with rmic iiop"); 1039 } 1040 1041 } 1042 1043 if (protocols.isSupported(JEREMIE)) { 1044 trace("Running '" + jeremieCmd.toString() + "'"); 1045 if (jeremieCmd.run()) { 1046 info("Stubs and Skels successfully generated with rmi/jeremie"); 1047 } else { 1048 throw new GenICException("Failed when generating the Stubs and Skels with jeremie jrmic"); 1049 } 1050 } 1051 } 1052 1053 1056 private static Class clusterCompilerClass = null; 1057 1058 1061 private static Method clusterCompilerGenerateMethod = null; 1062 1063 1069 public void addClassesInJar(String jarFileName, boolean keepGenerated) throws GenICException { 1070 1071 if (!generatedIC) { 1072 return; 1073 } 1074 1075 filesToDelete.add(outputdir); 1076 1077 ArrayList lf = new ArrayList (); 1078 getFilesList(outputdir, lf, !keepGenerated); 1079 1080 updateJar(jarFileName, outputdir, lf); 1081 1082 } 1083 1084 1089 protected String convertName(String name) { 1090 return name.replace('\\', '/'); 1091 } 1092 1093 1100 private void updateJar(String jfn, String od, List lfn) 1101 throws GenICException { 1102 1103 File jarFile = null; 1104 File tempJarFile = null; 1105 JarFile jarArchive = null; 1106 JarOutputStream tempJarArchive = null; 1107 1108 try { 1109 jarFile = new File (jfn); 1110 tempJarFile = new File (jfn + ".tmp"); 1111 1112 jarArchive = new JarFile (jarFile); 1114 1115 tempJarArchive = new JarOutputStream ( 1117 new FileOutputStream (tempJarFile)); 1118 1119 byte[] buffer = new byte[BUFFER_SIZE]; 1120 int read = 0; 1121 1122 FileInputStream file = null; 1124 int is = od.length() + 1; 1125 Iterator iterFiles = lfn.iterator(); 1126 while (iterFiles.hasNext()) { 1127 try { 1128 String fileName = (String ) iterFiles.next(); 1129 1130 file = new FileInputStream (fileName); 1131 JarEntry entry = new JarEntry (convertName(fileName.substring(is))); 1132 tempJarArchive.putNextEntry(entry); 1133 while ((read = file.read(buffer)) != -1) { 1135 tempJarArchive.write(buffer, 0, read); 1136 } 1137 } finally { 1138 file.close(); 1139 } 1140 } 1141 1142 Enumeration ents = jarArchive.entries(); 1144 while (ents.hasMoreElements()) { 1145 JarEntry entry = (JarEntry ) ents.nextElement(); 1146 if (!lfn.contains(od + File.separator + entry.getName().replace('/', File.separatorChar))) { 1150 InputStream enStream = jarArchive.getInputStream( 1151 entry); 1152 tempJarArchive.putNextEntry(entry); 1153 while ((read = enStream.read(buffer)) != -1) { 1155 tempJarArchive.write(buffer, 0, read); 1156 } 1157 } 1158 } 1159 1160 } catch (Exception e) { 1161 throw new GenICException("Failed when adding the generated classes " + "in the given ejb-jar file '" + jfn 1162 + "': " + e); 1163 } finally { 1164 try { 1166 jarArchive.close(); 1167 } catch (IOException e1) { 1168 warning("Failed to close jar archive file '" 1169 + jarArchive.getName()); 1170 } 1171 try { 1172 tempJarArchive.close(); 1173 } catch (IOException e1) { 1174 warning("Failed to close temporary jar archive file '" 1175 + jarArchive.getName() + "'"); 1176 } 1177 } 1178 1179 boolean deleted = jarFile.delete(); 1181 boolean rename = tempJarFile.renameTo(jarFile); 1182 if (!deleted || !rename) { 1183 error("Failed to update jar archive file '" 1184 + jarArchive.getName() + "'"); 1185 } 1186 } 1187 1188 1191 public void clean() { 1192 trace("Deleting " + filesToDelete.toString()); 1193 for (Iterator it = filesToDelete.iterator(); it.hasNext();) { 1194 String name = (String ) it.next(); 1195 File f = new File (name); 1196 delete(f); 1197 } 1198 } 1199 1200 1205 private boolean delete(File f) { 1206 if (f.isFile()) { 1207 return f.delete(); 1208 } else { 1209 File [] childs = f.listFiles(); 1210 if (childs == null) { 1211 return f.delete(); 1213 } else { 1214 boolean result = true; 1216 for (int i = 0; i < childs.length; i++) { 1217 result &= delete(childs[i]); 1218 } 1219 return result && f.delete(); 1220 } 1221 } 1222 } 1223 1224 1227 static void usage() { 1228 StringBuffer msg = new StringBuffer (); 1229 msg.append("Usage: java org.objectweb.jonas_ejb.genic.GenIC -help \n"); 1230 msg.append(" to print this help message \n"); 1231 msg.append(" or java org.objectweb.jonas_ejb.genic.GenIC <Options> <Input_File> \n"); 1232 msg.append(" to generate the container-specific classes for given EJB(s). \n"); 1233 msg.append(" \n"); 1234 msg.append("Options include: \n"); 1235 msg.append(" -d <output_dir> specify where to place the generated files \n"); 1236 msg.append(" -noaddinjar do not add the generated classes in the given \n"); 1237 msg.append(" ejb-jar file \n"); 1238 msg.append(" -nocompil do not compile the generated source files via \n"); 1239 msg.append(" the java and rmi compilers \n"); 1240 msg.append(" -novalidation parse the XML deployment descriptors without \n"); 1241 msg.append(" validation \n"); 1242 msg.append(" -classpath <path> define the classpath to be used to compile classes \n"); 1243 msg.append(" -javac <opt> specify the java compiler to use \n"); 1244 msg.append(" -javacopts <opt> specify the options to pass to the java compiler \n"); 1245 msg.append(" -rmicopts <opt> specify the options to pass to the rmi compiler \n"); 1246 msg.append(" -protocols list of protocol, separated by comma \n"); 1247 msg.append(" (default is jrmp) \n"); 1248 msg.append(" -invokecmd invoke, in some case, directly the method of the java class\n"); 1249 msg.append(" corresponding to the command \n"); 1250 msg.append(" -keepgenerated do not delete intermediate generated source files \n"); 1251 msg.append(" -verbose \n"); 1252 msg.append(" -debug deprecated (use the JOnAS trace properties file)\n"); 1253 msg.append(" \n"); 1254 msg.append(" Input_File standard deployment descriptor file's name or \n"); 1255 msg.append(" ejb-jar file's name \n"); 1256 GenIC.info(msg.toString()); 1257 } 1258 1259 1263 void trace(String msg) { 1264 int level = BasicLevel.DEBUG; 1265 if (verbose) { 1266 level = BasicLevel.INFO; 1267 } 1268 logger.log(level, msg); 1269 } 1270 1271 1275 static void info(String msg) { 1276 logger.log(BasicLevel.INFO, msg); 1277 } 1278 1279 1283 static void warning(String msg) { 1284 logger.log(BasicLevel.WARN, msg); 1285 } 1286 1287 1288 1292 static void error(String msg) { 1293 logger.log(BasicLevel.ERROR, msg); 1294 } 1295 1296 1302 static void fatalError(String msg, Exception e) { 1303 error("GenIC fatal error: " + msg + e.getMessage()); 1304 throw new RuntimeException (msg); 1305 } 1306 1307 1312 static void fatalError(Exception e) { 1313 System.err.println("GenIC fatal error: "); 1314 printException(e, System.err); 1315 throw new RuntimeException (e.getMessage()); 1316 } 1317 1318 1324 static void printException(Exception e, PrintStream out) { 1325 Exception inner = null; 1326 if (e instanceof GenICException && (inner = ((GenICException) e).inner) != null) { 1327 out.println(e.getMessage()); 1328 printException(inner, out); 1329 } else if (e instanceof PException && (inner = ((PException) e).getNestedException()) != null) { 1330 out.println(e.getMessage()); 1331 printException(inner, out); 1332 } else if (e instanceof MedorException && (inner = ((MedorException) e).getNestedException()) != null) { 1333 out.println(e.getMessage()); 1334 printException(inner, out); 1335 } else { 1336 e.printStackTrace(); 1337 } 1338 } 1339 1340 1345 static String createTempDir() throws IOException { 1346 File tmpDir = File.createTempFile("genic", null, null); 1347 tmpDir.delete(); 1348 if (!tmpDir.mkdir()) { 1349 throw new IOException ("Cannot create the temporary directory '" + tmpDir + "'."); 1350 } 1351 return tmpDir.getAbsolutePath(); 1352 } 1353 1354 1360 static void getFilesList(String dir, ArrayList list, boolean onlyClass) { 1361 File df = new File (dir); 1362 if (df.exists() && df.isDirectory()) { 1363 File [] ls = df.listFiles(); 1364 for (int i = 0; i < ls.length; i++) { 1365 File f = ls[i]; 1366 String fn = f.getAbsolutePath(); 1367 if (f.isDirectory()) { 1368 getFilesList(fn, list, onlyClass); 1369 } else { 1370 if (!onlyClass || fn.endsWith(".class")) { 1371 if (!fn.endsWith(".save")) { 1372 list.add(fn); 1373 } 1374 } 1375 } 1376 } 1377 } 1378 } 1379} 1380 1381 1384 1385class ProtocolNames { 1386 1387 1390 private TreeSet protocolNames = new TreeSet (); 1391 1392 1396 ProtocolNames(String pns) { 1397 if (pns != null && pns.length() > 0) { 1398 StringTokenizer st = new StringTokenizer (pns, ",", false); 1399 while (st.hasMoreTokens()) { 1400 protocolNames.add(st.nextToken().trim()); 1401 } 1402 } else { 1403 protocolNames.add("jrmp"); 1404 protocolNames.add("jeremie"); 1405 } 1406 if (protocolNames.contains(GenIC.CMI_RMI)) { 1407 protocolNames.add(GenIC.RMI_JRMP); 1408 } 1409 } 1410 1411 1416 boolean isSupported(String pn) { 1417 return protocolNames.contains(pn); 1418 } 1419} 1420 1421 | Popular Tags |