Generator.java

00001 
00026 package org.objectweb.jonas_lib.genclientstub.generator;
00027 
00028 import java.io.File;
00029 import java.util.HashMap;
00030 import java.util.Iterator;
00031 import java.util.Map;
00032 
00033 import org.objectweb.carol.util.configuration.CarolCurrentConfiguration;
00034 
00035 import org.objectweb.common.Cmd;
00036 
00037 import org.objectweb.jonas_lib.I18n;
00038 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
00039 import org.objectweb.jonas_lib.genbase.GenBaseException;
00040 import org.objectweb.jonas_lib.genbase.archive.Archive;
00041 import org.objectweb.jonas_lib.genbase.archive.Client;
00042 import org.objectweb.jonas_lib.genbase.archive.EjbJar;
00043 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
00044 import org.objectweb.jonas_lib.genbase.archive.WebApp;
00045 import org.objectweb.jonas_lib.genbase.generator.AbsGenerator;
00046 import org.objectweb.jonas_lib.genbase.generator.Config;
00047 import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;
00048 import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
00049 
00050 import org.objectweb.util.monolog.api.BasicLevel;
00051 
00056 public class Generator extends AbsGenerator {
00057 
00061     private static I18n i18n = I18n.getInstance(Generator.class);
00062 
00066     private EjbRefDesc ejbRef = null;
00067 
00071     private String intfStubClassName = null;
00072 
00076     private Archive archive = null;
00077 
00087     public Generator(Config config, EjbRefDesc ejbRef, String intfStubClassName, Archive archive) throws GenBaseException {
00088         super(config);
00089         this.ejbRef = ejbRef;
00090         this.intfStubClassName = intfStubClassName;
00091         this.archive = archive;
00092     }
00093 
00098     public void generate() throws ClientStubGenException {
00099 
00100         try {
00101             //URL[] urls = null;
00102             String archiveClasspath = null;
00103 
00104             J2EEArchive arch = (J2EEArchive) this.archive;
00105             AbsModuleClassLoader loader = (AbsModuleClassLoader) arch.getModuleClassloader();
00106 
00107             archiveClasspath = loader.getClasspath();
00108 
00109             // itf class
00110             String itfClass = null;
00111             if (ejbRef != null) {
00112                 itfClass = ejbRef.getHome();
00113             } else {
00114                 itfClass = intfStubClassName;
00115             }
00116 
00117             // TODO Now try to load the class and check if stub is here
00118 
00119             // Add to the rmic Classpath all the lib and classes of the archive
00120             String classpath = getConfig().getClasspath() + File.separator + archiveClasspath;
00121 
00122             // Build stub
00123 
00124             // Array of commands
00125             Map commands = new HashMap();
00126 
00127             // Get current protocol
00128             String rmiName = CarolCurrentConfiguration.getCurrent().getCurrentRMIName();
00129 
00130             // Build rmic command for jrmp
00131             //Cmd jrmpCmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameRmic());
00132             //jrmpCmd.addArgument("-classpath");
00133             //jrmpCmd.addArgument(classpath);
00134 
00135 //            Cmd jeremieCmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameJava());
00136 //            jeremieCmd.addArgument("-classpath");
00137 //            jeremieCmd.addArgument(classpath);
00138 //            jeremieCmd.addArgument("org.objectweb.jeremie.tools.jrmic.JRMICompiler");
00139 //            jeremieCmd.addArgument("-opt");
00140 //            jeremieCmd.addArgument("-owext");
00141 //            commands.put("jeremie", jeremieCmd);
00142 
00143             if (rmiName.equalsIgnoreCase("iiop")) {
00144                 Cmd iiopCmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameRmic());
00145                 iiopCmd.addArgument("-classpath");
00146                 iiopCmd.addArgument(classpath);
00147                 iiopCmd.addArgument("-iiop");
00148                 iiopCmd.addArgument("-poa");
00149                 iiopCmd.addArgument("-always");
00150                 iiopCmd.addArgument("-keepgenerated");
00151                 iiopCmd.addArgument("-g");
00152                 commands.put("iiop", iiopCmd);
00153             }
00154 
00155 
00156             for (Iterator itCmd = commands.keySet().iterator(); itCmd.hasNext();) {
00157                 String protocol = (String) itCmd.next();
00158                 Cmd cmd = (Cmd) commands.get(protocol);
00159                 cmd.addArgument("-d");
00160                 cmd.addArgument(getClasses().getCanonicalPath());
00161                 cmd.addArguments(getConfig().getJavacOpts());
00162 
00163                 // add itf class as arg name
00164                 cmd.addArgument(itfClass);
00165 
00166                 getLogger().log(BasicLevel.DEBUG, "Running '" + cmd.toString() + "'");
00167                 if (cmd.run()) {
00168                     getLogger().log(BasicLevel.INFO,
00169                             "Client stubs for classname '" + itfClass + "' successfully generated for protocol '" + protocol + "'.");
00170                 } else {
00171                     String err = i18n.getMessage("Generator.generate.error");
00172                     getLogger().log(BasicLevel.ERROR, err);
00173                     throw new ClientStubGenException(err);
00174                 }
00175             }
00176 
00177         } catch (Exception e) {
00178             String err = i18n.getMessage("Generator.generate.error");
00179             getLogger().log(BasicLevel.ERROR, err);
00180             throw new ClientStubGenException(err, e);
00181         }
00182 
00183     }
00184 
00190     public void compile() throws ClientStubGenException {
00191     }
00192 
00199     public void addFiles(Archive archive) throws ClientStubGenException {
00200         if (archive instanceof WebApp) {
00201             archive.addDirectoryIn("WEB-INF/classes/", getClasses());
00202         } else if (archive instanceof EjbJar) {
00203             archive.addDirectory(getClasses());
00204         } else if (archive instanceof Client) {
00205             archive.addDirectory(getClasses());
00206         }
00207     }
00208 
00209 }

Generated on Tue Feb 15 15:05:54 2005 for JOnAS by  doxygen 1.3.9.1