GenIDL.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): ______________________________________.
00022  * Contributor(s): Helene Joanin (jonas-teams).
00023  *
00024  * --------------------------------------------------------------------------
00025  * $Id: GenIDL.java,v 1.9 2004/01/23 17:15:44 joaninh Exp $
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     // Is the command is verbose ?
00067     private boolean verbose = false;
00068     
00069     // Name of the directory where to place the generated file
00070     private String directoryOutputName = null;
00071 
00072     // A BeanDesc for each bean for which IDL have to be generated
00073     private Vector vBeanDD = null;
00074 
00075     // Names of the remote classes (package name included)
00076     private Vector remoteClasses = null;
00077 
00078 
00112     public static void main(String args[]) {
00113 
00114         // First of all, check if the used JDK is at least an JDK 1.3
00115         // Indeed, 'rmic -idl' only available from JDK 1.3
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         // Get args
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         // Usage ?
00170         if (isHelp) {
00171             usage();
00172             System.exit(EXIT_SUCCESS);
00173         }
00174 
00175         // Check args
00176         if (error || (fileInputName == null)) {
00177             usage();
00178             System.exit(EXIT_FAILURE);
00179         }
00180         if (dirOutputName == null) {
00181             dirOutputName = new String("");
00182         }
00183 
00184         // Calculate the classpath for rmic
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         // Set the parsing mode (with or without validation)
00197         if (!parseWithValidation) {
00198             EjbDeploymentDescManager.setParsingWithValidation(false);
00199         }
00200 
00201         // Generates the IDL for the set of the beans
00202         try {
00203             DeploymentDesc ejbJarDD = null;
00204             if (fileInputName.endsWith(".jar")) {
00205                 // ejb-jar file
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                 // xml file
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         // End
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                 // Nothing to generate in case of MessageDriven Bean
00252                 vBeanDD.addElement(beansDD[i]);
00253             }
00254         }
00255 
00256         // Display the bean's names
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         // Init the list of the remote classes
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         // Generate the IDLs by the way of the rmic -idl command
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 

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