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
00102 String archiveClasspath = null;
00103
00104 J2EEArchive arch = (J2EEArchive) this.archive;
00105 AbsModuleClassLoader loader = (AbsModuleClassLoader) arch.getModuleClassloader();
00106
00107 archiveClasspath = loader.getClasspath();
00108
00109
00110 String itfClass = null;
00111 if (ejbRef != null) {
00112 itfClass = ejbRef.getHome();
00113 } else {
00114 itfClass = intfStubClassName;
00115 }
00116
00117
00118
00119
00120 String classpath = getConfig().getClasspath() + File.separator + archiveClasspath;
00121
00122
00123
00124
00125 Map commands = new HashMap();
00126
00127
00128 String rmiName = CarolCurrentConfiguration.getCurrent().getCurrentRMIName();
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
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
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 }