KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > genidl > GenIDL


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ______________________________________.
22  * Contributor(s): Helene Joanin (jonas-teams).
23  *
24  * --------------------------------------------------------------------------
25  * $Id: GenIDL.java,v 1.9 2004/01/23 17:15:44 joaninh Exp $
26  * --------------------------------------------------------------------------
27  */

28
29
30
31 package org.objectweb.jonas_ejb.genidl;
32
33 import java.io.File JavaDoc;
34 import java.net.URLClassLoader JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 import org.objectweb.common.Cmd;
42 import org.objectweb.common.Env;
43 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
44 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
45 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc;
46 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
47 import org.objectweb.jonas_ejb.lib.BeanNaming;
48 import org.objectweb.jonas_lib.version.Version;
49 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
50
51
52 /**
53  * This class allows to generate:
54  * <ul>
55  * <li>
56  * the IDLs of the Home and Remote interfaces
57  * </ul>
58  * of all the Enterprise Java Beans defined in the given Deployment Descriptor.
59  */

60
61 public class GenIDL {
62
63     private static final int EXIT_SUCCESS = 0;
64     private static final int EXIT_FAILURE = 2;
65
66     // Is the command is verbose ?
67
private boolean verbose = false;
68     
69     // Name of the directory where to place the generated file
70
private String JavaDoc directoryOutputName = null;
71
72     // A BeanDesc for each bean for which IDL have to be generated
73
private Vector JavaDoc vBeanDD = null;
74
75     // Names of the remote classes (package name included)
76
private Vector JavaDoc remoteClasses = null;
77
78
79     /**
80      * GenIDL allows to generate the IDLs of the Home and Remote interfaces of the
81      * beans described in the given EJB deployment descriptor.
82      * <p>
83      *
84      * Usage: java org.objectweb.jonas_ejb.genidl.GenIDL -help
85      * <br>
86      * to print this help message
87      * <p>
88      *
89      * or java org.objectweb.jonas_ejb.genidl.GenIDL <Options> <Input_File>
90      * <br>
91      * to generate the IDLs of the Home and Remote interfaces of the
92      * beans described in the given EJB deployment descriptor.
93      * <p>
94      * Options include:
95      * <ul>
96      * <li>
97      * -d <output_dir> specify where to place the generated files
98      * <li>
99      * -novalidation parse the XML deployment descriptors without validation
100      * <li>
101      * -rmicopts <opt> specify the options to pass to the rmic compiler
102      * <li>
103      * -verbose
104      * <li>
105      * </ul>
106      * <p>
107      * Input_File file name of the standard deployment descriptor
108      * (.xml ended), or
109      * file name of the EJB-jar (.jar ended).
110      */

111
112     public static void main(String JavaDoc args[]) {
113
114     // First of all, check if the used JDK is at least an JDK 1.3
115
// Indeed, 'rmic -idl' only available from JDK 1.3
116
if (Env.getJavaVersion() < Env.JAVA_1_3) {
117         GenIDL.fatalError("'rmic -idl' not available on a JDK version less than 1.3.");
118     }
119
120     boolean error = false;
121
122     boolean isHelp = false;
123     boolean isVerbose = false;
124     boolean parseWithValidation = true;
125
126     String JavaDoc fileInputName = null;
127     String JavaDoc dirOutputName = null;
128     Vector JavaDoc optionsRmiC = new Vector JavaDoc();
129
130     // Get args
131
for (int argn=0; argn<args.length; argn++) {
132         String JavaDoc arg = args[argn];
133         if (arg.equals("-help") || arg.equals("-?")) {
134         isHelp = true;
135         continue;
136         }
137         if (arg.equals("-novalidation")) {
138         parseWithValidation = false;
139         continue;
140         }
141         if (arg.equals("-verbose")) {
142         isVerbose = true;
143         continue;
144         }
145         if (arg.equals("-rmicopts")) {
146         argn++;
147         if (argn<args.length) {
148             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args[argn]);
149             while (st.hasMoreTokens()) {
150             optionsRmiC.addElement(st.nextToken());
151             }
152         } else {
153             error = true;
154         }
155         continue;
156         }
157         if (arg.equals("-d")) {
158         argn++;
159         if (argn<args.length) {
160             dirOutputName = args[argn];
161         } else {
162             error = true;
163         }
164         continue;
165         }
166         fileInputName = args[argn];
167     }
168
169     // Usage ?
170
if (isHelp) {
171         usage();
172         System.exit(EXIT_SUCCESS);
173     }
174
175     // Check args
176
if (error || (fileInputName == null)) {
177         usage();
178         System.exit(EXIT_FAILURE);
179     }
180     if (dirOutputName == null) {
181         dirOutputName = new String JavaDoc("");
182     }
183
184     // Calculate the classpath for rmic
185
String JavaDoc classpath;
186     if (fileInputName.endsWith(".jar")) {
187         classpath = fileInputName +
188         File.pathSeparator +
189         System.getProperty("java.class.path", "");
190     } else {
191         classpath = System.getProperty("java.class.path", "");
192     }
193     optionsRmiC.addElement("-classpath");
194     optionsRmiC.addElement(classpath);
195
196     // Set the parsing mode (with or without validation)
197
if (!parseWithValidation) {
198         EjbDeploymentDescManager.setParsingWithValidation(false);
199     }
200
201     // Generates the IDL for the set of the beans
202
try {
203         DeploymentDesc ejbJarDD = null;
204         if (fileInputName.endsWith(".jar")) {
205         // ejb-jar file
206
URL JavaDoc url[] = new URL JavaDoc[1];
207         url[0] = (new File JavaDoc(fileInputName)).toURL();
208         URLClassLoader JavaDoc cl = new URLClassLoader JavaDoc(url);
209         ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName, cl);
210         } else {
211         // xml file
212
ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName,
213                                       BeanNaming.getJonasXmlName(fileInputName),
214                                       BeanNaming.getParentName(fileInputName));
215         }
216
217         GenIDL gwc = new GenIDL(ejbJarDD, dirOutputName, isVerbose);
218         gwc.generate(optionsRmiC);
219
220     } catch (MalformedURLException JavaDoc e) {
221         GenIDL.fatalError("Invalid ejb-jar file name (" + e.getMessage() + ")");
222     } catch (GenIDLException e) {
223         GenIDL.fatalError(e.getMessage());
224     } catch (DeploymentDescException e) {
225         GenIDL.fatalError("Cannot read the Deployment Descriptors from "
226                   +fileInputName+": "+e.getMessage());
227     }
228     // End
229
}
230
231     /**
232      * GenIDL Constructor
233      *
234      * @param ejbJarDescr deployment descriptors of the beans
235      * @param dirOutputName path of the directory where to place the generated files
236      * (empty string "" if the output directory is the current directory)
237      * @param isVerbose if true, some traces are print
238      * @exception GenIDLException In error case
239      */

240     public GenIDL(DeploymentDesc ejbJarDesc, String JavaDoc dirOutputName, boolean isVerbose)
241     throws GenIDLException {
242     
243     verbose = isVerbose;
244     directoryOutputName = dirOutputName;
245     remoteClasses = new Vector JavaDoc();
246
247     vBeanDD = new Vector JavaDoc();
248     BeanDesc[] beansDD = ejbJarDesc.getBeanDesc();
249     for (int i=0; i<beansDD.length; i++) {
250         if (!(beansDD[i] instanceof MessageDrivenDesc)) {
251         // Nothing to generate in case of MessageDriven Bean
252
vBeanDD.addElement(beansDD[i]);
253         }
254     }
255
256     // Display the bean's names
257
StringBuffer JavaDoc message = new StringBuffer JavaDoc();
258     message.append("GenIDL for JOnAS "+Version.NUMBER+": ");
259     if (vBeanDD.size()!=0) {
260         message.append("IDL generation for beans ");
261         for (int i=0; i < vBeanDD.size(); i++) {
262         BeanDesc dd = (BeanDesc)vBeanDD.elementAt(i);
263         message.append((i==0?"":", ") + "'" + dd.getEjbName() + "'");
264         }
265         message.append(" ...");
266     } else {
267         message.append("No generation to do (only message driven beans)");
268     }
269     GenIDL.info(message.toString());
270
271     // Init the list of the remote classes
272
for (Enumeration JavaDoc e = vBeanDD.elements() ; e.hasMoreElements() ;) {
273         BeanDesc dd = (BeanDesc)e.nextElement();
274         if (dd.getHomeClass() != null) {
275         remoteClasses.addElement(dd.getHomeClass().getName());
276         }
277         if (dd.getRemoteClass() != null) {
278         remoteClasses.addElement(dd.getRemoteClass().getName());
279         }
280     }
281     }
282
283     /**
284      * Generate the IDLs for all Beans.
285      *
286      * @param optionsRmiC options for the rmic tool
287      * @exception GenIDLException In error case
288      */

289     public void generate(Vector JavaDoc optionsRmiC)
290     throws GenIDLException {
291     
292     String JavaDoc optDirOutput;
293     Cmd cmd;
294
295     if (vBeanDD.size()==0) {
296         return;
297     }
298
299     // Generate the IDLs by the way of the rmic -idl command
300
cmd = new Cmd("rmic");
301     cmd.addArgument("-idl");
302     cmd.addArgument("-always");
303     if (directoryOutputName.length() != 0) {
304         cmd.addArgument("-d");
305         cmd.addArgument(directoryOutputName);
306     }
307     cmd.addArguments(optionsRmiC);
308     for (Enumeration JavaDoc e = remoteClasses.elements() ; e.hasMoreElements() ;) {
309         String JavaDoc className = (String JavaDoc)e.nextElement();
310         cmd.addArgument(className);
311     }
312     
313     trace("Running '" + cmd.toString() + "'");
314     if (cmd.run()) {
315         trace("The IDL of the Home and Remote interfaces of the beans"
316           +" are successfully generated via rmic -idl.");
317     } else {
318         throw new GenIDLException("Failed when generating the IDL of the Home and Remote interfaces of the beans via rmic -idl.");
319     }
320
321     }
322
323     /**
324      * Display the usage
325      */

326     private static void usage() {
327     StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
328     msg.append("Usage: java org.objectweb.jonas_ejb.genidl.GenIDL -help \n");
329     msg.append(" to print this help message \n");
330     msg.append(" or java org.objectweb.jonas_ejb.genidl.GenIDL <Options> <Input_File> \n");
331     msg.append(" to generate the IDLs for given EJB(s). \n");
332     msg.append(" \n");
333     msg.append("Options include: \n");
334         msg.append(" -d <output_dir> specify where to place the generated files \n");
335     msg.append(" -novalidation parse the XML deployment descriptors without \n");
336     msg.append(" validation \n");
337     msg.append(" -rmicopts <opt> specify the options to pass to the rmi compiler \n");
338     msg.append(" -verbose \n");
339     msg.append(" \n");
340     msg.append("Input_File standard deployment descriptor file's name or \n");
341     msg.append(" ejb-jar file's name \n");
342     GenIDL.info(msg.toString());
343     }
344
345     /**
346      * Display the specified message only if verbose.
347      * @param msg the message to display
348      */

349     void trace(String JavaDoc msg) {
350     if (verbose) {
351         info("-- "+msg);
352     }
353     }
354
355     /**
356      * Display the specified message.
357      * @param msg the message to display
358      */

359     static void info(String JavaDoc msg) {
360         System.out.println(msg);
361     }
362
363     /**
364      * Display the specified error message.
365      * @param errMsg the error message to display
366      */

367     static void error(String JavaDoc errMsg) {
368         System.err.println("GenIDL error: " + errMsg);
369     }
370
371
372     /**
373      * Display the specified error message and exits with an
374      * EXIT_FAILURE status.
375      * @param errMsg the error message to display
376      */

377     static void fatalError(String JavaDoc errMsg) {
378     System.err.println("GenIDL fatal error: " + errMsg);
379     System.exit(EXIT_FAILURE);
380     }
381 }
382
383
Popular Tags