00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.jonas_ejb.genidl;
00032
00033 import java.io.File;
00034 import java.net.URLClassLoader;
00035 import java.net.URL;
00036 import java.net.MalformedURLException;
00037 import java.util.Enumeration;
00038 import java.util.StringTokenizer;
00039 import java.util.Vector;
00040
00041 import org.objectweb.common.Cmd;
00042 import org.objectweb.common.Env;
00043 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
00044 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
00045 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc;
00046 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
00047 import org.objectweb.jonas_ejb.lib.BeanNaming;
00048 import org.objectweb.jonas_lib.version.Version;
00049 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00050
00051
00061 public class GenIDL {
00062
00063 private static final int EXIT_SUCCESS = 0;
00064 private static final int EXIT_FAILURE = 2;
00065
00066
00067 private boolean verbose = false;
00068
00069
00070 private String directoryOutputName = null;
00071
00072
00073 private Vector vBeanDD = null;
00074
00075
00076 private Vector remoteClasses = null;
00077
00078
00112 public static void main(String args[]) {
00113
00114
00115
00116 if (Env.getJavaVersion() < Env.JAVA_1_3) {
00117 GenIDL.fatalError("'rmic -idl' not available on a JDK version less than 1.3.");
00118 }
00119
00120 boolean error = false;
00121
00122 boolean isHelp = false;
00123 boolean isVerbose = false;
00124 boolean parseWithValidation = true;
00125
00126 String fileInputName = null;
00127 String dirOutputName = null;
00128 Vector optionsRmiC = new Vector();
00129
00130
00131 for (int argn=0; argn<args.length; argn++) {
00132 String arg = args[argn];
00133 if (arg.equals("-help") || arg.equals("-?")) {
00134 isHelp = true;
00135 continue;
00136 }
00137 if (arg.equals("-novalidation")) {
00138 parseWithValidation = false;
00139 continue;
00140 }
00141 if (arg.equals("-verbose")) {
00142 isVerbose = true;
00143 continue;
00144 }
00145 if (arg.equals("-rmicopts")) {
00146 argn++;
00147 if (argn<args.length) {
00148 StringTokenizer st = new StringTokenizer(args[argn]);
00149 while (st.hasMoreTokens()) {
00150 optionsRmiC.addElement(st.nextToken());
00151 }
00152 } else {
00153 error = true;
00154 }
00155 continue;
00156 }
00157 if (arg.equals("-d")) {
00158 argn++;
00159 if (argn<args.length) {
00160 dirOutputName = args[argn];
00161 } else {
00162 error = true;
00163 }
00164 continue;
00165 }
00166 fileInputName = args[argn];
00167 }
00168
00169
00170 if (isHelp) {
00171 usage();
00172 System.exit(EXIT_SUCCESS);
00173 }
00174
00175
00176 if (error || (fileInputName == null)) {
00177 usage();
00178 System.exit(EXIT_FAILURE);
00179 }
00180 if (dirOutputName == null) {
00181 dirOutputName = new String("");
00182 }
00183
00184
00185 String classpath;
00186 if (fileInputName.endsWith(".jar")) {
00187 classpath = fileInputName +
00188 File.pathSeparator +
00189 System.getProperty("java.class.path", "");
00190 } else {
00191 classpath = System.getProperty("java.class.path", "");
00192 }
00193 optionsRmiC.addElement("-classpath");
00194 optionsRmiC.addElement(classpath);
00195
00196
00197 if (!parseWithValidation) {
00198 EjbDeploymentDescManager.setParsingWithValidation(false);
00199 }
00200
00201
00202 try {
00203 DeploymentDesc ejbJarDD = null;
00204 if (fileInputName.endsWith(".jar")) {
00205
00206 URL url[] = new URL[1];
00207 url[0] = (new File(fileInputName)).toURL();
00208 URLClassLoader cl = new URLClassLoader(url);
00209 ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName, cl);
00210 } else {
00211
00212 ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName,
00213 BeanNaming.getJonasXmlName(fileInputName),
00214 BeanNaming.getParentName(fileInputName));
00215 }
00216
00217 GenIDL gwc = new GenIDL(ejbJarDD, dirOutputName, isVerbose);
00218 gwc.generate(optionsRmiC);
00219
00220 } catch (MalformedURLException e) {
00221 GenIDL.fatalError("Invalid ejb-jar file name (" + e.getMessage() + ")");
00222 } catch (GenIDLException e) {
00223 GenIDL.fatalError(e.getMessage());
00224 } catch (DeploymentDescException e) {
00225 GenIDL.fatalError("Cannot read the Deployment Descriptors from "
00226 +fileInputName+": "+e.getMessage());
00227 }
00228
00229 }
00230
00240 public GenIDL(DeploymentDesc ejbJarDesc, String dirOutputName, boolean isVerbose)
00241 throws GenIDLException {
00242
00243 verbose = isVerbose;
00244 directoryOutputName = dirOutputName;
00245 remoteClasses = new Vector();
00246
00247 vBeanDD = new Vector();
00248 BeanDesc[] beansDD = ejbJarDesc.getBeanDesc();
00249 for (int i=0; i<beansDD.length; i++) {
00250 if (!(beansDD[i] instanceof MessageDrivenDesc)) {
00251
00252 vBeanDD.addElement(beansDD[i]);
00253 }
00254 }
00255
00256
00257 StringBuffer message = new StringBuffer();
00258 message.append("GenIDL for JOnAS "+Version.NUMBER+": ");
00259 if (vBeanDD.size()!=0) {
00260 message.append("IDL generation for beans ");
00261 for (int i=0; i < vBeanDD.size(); i++) {
00262 BeanDesc dd = (BeanDesc)vBeanDD.elementAt(i);
00263 message.append((i==0?"":", ") + "'" + dd.getEjbName() + "'");
00264 }
00265 message.append(" ...");
00266 } else {
00267 message.append("No generation to do (only message driven beans)");
00268 }
00269 GenIDL.info(message.toString());
00270
00271
00272 for (Enumeration e = vBeanDD.elements() ; e.hasMoreElements() ;) {
00273 BeanDesc dd = (BeanDesc)e.nextElement();
00274 if (dd.getHomeClass() != null) {
00275 remoteClasses.addElement(dd.getHomeClass().getName());
00276 }
00277 if (dd.getRemoteClass() != null) {
00278 remoteClasses.addElement(dd.getRemoteClass().getName());
00279 }
00280 }
00281 }
00282
00289 public void generate(Vector optionsRmiC)
00290 throws GenIDLException {
00291
00292 String optDirOutput;
00293 Cmd cmd;
00294
00295 if (vBeanDD.size()==0) {
00296 return;
00297 }
00298
00299
00300 cmd = new Cmd("rmic");
00301 cmd.addArgument("-idl");
00302 cmd.addArgument("-always");
00303 if (directoryOutputName.length() != 0) {
00304 cmd.addArgument("-d");
00305 cmd.addArgument(directoryOutputName);
00306 }
00307 cmd.addArguments(optionsRmiC);
00308 for (Enumeration e = remoteClasses.elements() ; e.hasMoreElements() ;) {
00309 String className = (String)e.nextElement();
00310 cmd.addArgument(className);
00311 }
00312
00313 trace("Running '" + cmd.toString() + "'");
00314 if (cmd.run()) {
00315 trace("The IDL of the Home and Remote interfaces of the beans"
00316 +" are successfully generated via rmic -idl.");
00317 } else {
00318 throw new GenIDLException("Failed when generating the IDL of the Home and Remote interfaces of the beans via rmic -idl.");
00319 }
00320
00321 }
00322
00326 private static void usage() {
00327 StringBuffer msg = new StringBuffer();
00328 msg.append("Usage: java org.objectweb.jonas_ejb.genidl.GenIDL -help \n");
00329 msg.append(" to print this help message \n");
00330 msg.append(" or java org.objectweb.jonas_ejb.genidl.GenIDL <Options> <Input_File> \n");
00331 msg.append(" to generate the IDLs for given EJB(s). \n");
00332 msg.append(" \n");
00333 msg.append("Options include: \n");
00334 msg.append(" -d <output_dir> specify where to place the generated files \n");
00335 msg.append(" -novalidation parse the XML deployment descriptors without \n");
00336 msg.append(" validation \n");
00337 msg.append(" -rmicopts <opt> specify the options to pass to the rmi compiler \n");
00338 msg.append(" -verbose \n");
00339 msg.append(" \n");
00340 msg.append("Input_File standard deployment descriptor file's name or \n");
00341 msg.append(" ejb-jar file's name \n");
00342 GenIDL.info(msg.toString());
00343 }
00344
00349 void trace(String msg) {
00350 if (verbose) {
00351 info("-- "+msg);
00352 }
00353 }
00354
00359 static void info(String msg) {
00360 System.out.println(msg);
00361 }
00362
00367 static void error(String errMsg) {
00368 System.err.println("GenIDL error: " + errMsg);
00369 }
00370
00371
00377 static void fatalError(String errMsg) {
00378 System.err.println("GenIDL fatal error: " + errMsg);
00379 System.exit(EXIT_FAILURE);
00380 }
00381 }
00382