001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ______________________________________.
022: * Contributor(s): Helene Joanin (jonas-teams).
023: *
024: * --------------------------------------------------------------------------
025: * $Id: GenIDL.java 8083 2006-03-06 10:26:40Z sauthieg $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas_ejb.genidl;
030:
031: import java.io.File;
032: import java.net.URLClassLoader;
033: import java.net.URL;
034: import java.net.MalformedURLException;
035: import java.util.Enumeration;
036: import java.util.StringTokenizer;
037: import java.util.Vector;
038:
039: import org.objectweb.common.Cmd;
040: import org.objectweb.common.Env;
041: import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
042: import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
043: import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc;
044: import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
045: import org.objectweb.jonas_ejb.lib.BeanNaming;
046: import org.objectweb.jonas_lib.version.Version;
047: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
048:
049: /**
050: * This class allows to generate:
051: * <ul>
052: * <li>
053: * the IDLs of the Home and Remote interfaces
054: * </ul>
055: * of all the Enterprise Java Beans defined in the given Deployment Descriptor.
056: */
057:
058: public class GenIDL {
059:
060: private static final int EXIT_SUCCESS = 0;
061: private static final int EXIT_FAILURE = 2;
062:
063: // Is the command is verbose ?
064: private boolean verbose = false;
065:
066: // Name of the directory where to place the generated file
067: private String directoryOutputName = null;
068:
069: // A BeanDesc for each bean for which IDL have to be generated
070: private Vector vBeanDD = null;
071:
072: // Names of the remote classes (package name included)
073: private Vector remoteClasses = null;
074:
075: /**
076: * GenIDL allows to generate the IDLs of the Home and Remote interfaces of the
077: * beans described in the given EJB deployment descriptor.
078: * <p>
079: *
080: * Usage: java org.objectweb.jonas_ejb.genidl.GenIDL -help
081: * <br>
082: * to print this help message
083: * <p>
084: *
085: * or java org.objectweb.jonas_ejb.genidl.GenIDL <Options> <Input_File>
086: * <br>
087: * to generate the IDLs of the Home and Remote interfaces of the
088: * beans described in the given EJB deployment descriptor.
089: * <p>
090: * Options include:
091: * <ul>
092: * <li>
093: * -d <output_dir> specify where to place the generated files
094: * <li>
095: * -novalidation parse the XML deployment descriptors without validation
096: * <li>
097: * -rmicopts <opt> specify the options to pass to the rmic compiler
098: * <li>
099: * -verbose
100: * <li>
101: * </ul>
102: * <p>
103: * Input_File file name of the standard deployment descriptor
104: * (.xml ended), or
105: * file name of the EJB-jar (.jar ended).
106: */
107:
108: public static void main(String args[]) {
109:
110: // First of all, check if the used JDK is at least an JDK 1.3
111: // Indeed, 'rmic -idl' only available from JDK 1.3
112: if (Env.getJavaVersion() < Env.JAVA_1_3) {
113: GenIDL
114: .fatalError("'rmic -idl' not available on a JDK version less than 1.3.");
115: }
116:
117: boolean error = false;
118:
119: boolean isHelp = false;
120: boolean isVerbose = false;
121: boolean parseWithValidation = true;
122:
123: String fileInputName = null;
124: String dirOutputName = null;
125: Vector optionsRmiC = new Vector();
126:
127: // Get args
128: for (int argn = 0; argn < args.length; argn++) {
129: String arg = args[argn];
130: if (arg.equals("-help") || arg.equals("-?")) {
131: isHelp = true;
132: continue;
133: }
134: if (arg.equals("-novalidation")) {
135: parseWithValidation = false;
136: continue;
137: }
138: if (arg.equals("-verbose")) {
139: isVerbose = true;
140: continue;
141: }
142: if (arg.equals("-rmicopts")) {
143: argn++;
144: if (argn < args.length) {
145: StringTokenizer st = new StringTokenizer(args[argn]);
146: while (st.hasMoreTokens()) {
147: optionsRmiC.addElement(st.nextToken());
148: }
149: } else {
150: error = true;
151: }
152: continue;
153: }
154: if (arg.equals("-d")) {
155: argn++;
156: if (argn < args.length) {
157: dirOutputName = args[argn];
158: } else {
159: error = true;
160: }
161: continue;
162: }
163: fileInputName = args[argn];
164: }
165:
166: // Usage ?
167: if (isHelp) {
168: usage();
169: System.exit(EXIT_SUCCESS);
170: }
171:
172: // Check args
173: if (error || (fileInputName == null)) {
174: usage();
175: System.exit(EXIT_FAILURE);
176: }
177: if (dirOutputName == null) {
178: dirOutputName = new String("");
179: }
180:
181: // Calculate the classpath for rmic
182: String classpath;
183: if (fileInputName.endsWith(".jar")) {
184: classpath = fileInputName + File.pathSeparator
185: + System.getProperty("java.class.path", "");
186: } else {
187: classpath = System.getProperty("java.class.path", "");
188: }
189: optionsRmiC.addElement("-classpath");
190: optionsRmiC.addElement(classpath);
191:
192: // Set the parsing mode (with or without validation)
193: if (!parseWithValidation) {
194: EjbDeploymentDescManager.setParsingWithValidation(false);
195: }
196:
197: // Generates the IDL for the set of the beans
198: try {
199: DeploymentDesc ejbJarDD = null;
200: if (fileInputName.endsWith(".jar")) {
201: // ejb-jar file
202: URL url[] = new URL[1];
203: url[0] = (new File(fileInputName)).toURL();
204: URLClassLoader cl = new URLClassLoader(url);
205: ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(
206: fileInputName, cl);
207: } else {
208: // xml file
209: ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(
210: fileInputName, BeanNaming
211: .getJonasXmlName(fileInputName),
212: BeanNaming.getParentName(fileInputName));
213: }
214:
215: GenIDL gwc = new GenIDL(ejbJarDD, dirOutputName, isVerbose);
216: gwc.generate(optionsRmiC);
217:
218: } catch (MalformedURLException e) {
219: GenIDL.fatalError("Invalid ejb-jar file name ("
220: + e.getMessage() + ")");
221: } catch (GenIDLException e) {
222: GenIDL.fatalError(e.getMessage());
223: } catch (DeploymentDescException e) {
224: GenIDL
225: .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 dirOutputName,
241: boolean isVerbose) throws GenIDLException {
242:
243: verbose = isVerbose;
244: directoryOutputName = dirOutputName;
245: remoteClasses = new Vector();
246:
247: vBeanDD = new Vector();
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 message = new StringBuffer();
258: message
259: .append("GenIDL for JOnAS " + Version.getNumber()
260: + ": ");
261: if (vBeanDD.size() != 0) {
262: message.append("IDL generation for beans ");
263: for (int i = 0; i < vBeanDD.size(); i++) {
264: BeanDesc dd = (BeanDesc) vBeanDD.elementAt(i);
265: message.append((i == 0 ? "" : ", ") + "'"
266: + dd.getEjbName() + "'");
267: }
268: message.append(" ...");
269: } else {
270: message
271: .append("No generation to do (only message driven beans)");
272: }
273: GenIDL.info(message.toString());
274:
275: // Init the list of the remote classes
276: for (Enumeration e = vBeanDD.elements(); e.hasMoreElements();) {
277: BeanDesc dd = (BeanDesc) e.nextElement();
278: if (dd.getHomeClass() != null) {
279: remoteClasses.addElement(dd.getHomeClass().getName());
280: }
281: if (dd.getRemoteClass() != null) {
282: remoteClasses.addElement(dd.getRemoteClass().getName());
283: }
284: }
285: }
286:
287: /**
288: * Generate the IDLs for all Beans.
289: *
290: * @param optionsRmiC options for the rmic tool
291: * @exception GenIDLException In error case
292: */
293: public void generate(Vector optionsRmiC) throws GenIDLException {
294:
295: String optDirOutput;
296: Cmd cmd;
297:
298: if (vBeanDD.size() == 0) {
299: return;
300: }
301:
302: // Generate the IDLs by the way of the rmic -idl command
303: cmd = new Cmd("rmic");
304: cmd.addArgument("-idl");
305: cmd.addArgument("-always");
306: if (directoryOutputName.length() != 0) {
307: cmd.addArgument("-d");
308: cmd.addArgument(directoryOutputName);
309: }
310: cmd.addArguments(optionsRmiC);
311: for (Enumeration e = remoteClasses.elements(); e
312: .hasMoreElements();) {
313: String className = (String) e.nextElement();
314: cmd.addArgument(className);
315: }
316:
317: trace("Running '" + cmd.toString() + "'");
318: if (cmd.run()) {
319: trace("The IDL of the Home and Remote interfaces of the beans"
320: + " are successfully generated via rmic -idl.");
321: } else {
322: throw new GenIDLException(
323: "Failed when generating the IDL of the Home and Remote interfaces of the beans via rmic -idl.");
324: }
325:
326: }
327:
328: /**
329: * Display the usage
330: */
331: private static void usage() {
332: StringBuffer msg = new StringBuffer();
333: msg
334: .append("Usage: java org.objectweb.jonas_ejb.genidl.GenIDL -help \n");
335: msg.append(" to print this help message \n");
336: msg
337: .append(" or java org.objectweb.jonas_ejb.genidl.GenIDL <Options> <Input_File> \n");
338: msg
339: .append(" to generate the IDLs for given EJB(s). \n");
340: msg.append(" \n");
341: msg.append("Options include: \n");
342: msg
343: .append(" -d <output_dir> specify where to place the generated files \n");
344: msg
345: .append(" -novalidation parse the XML deployment descriptors without \n");
346: msg.append(" validation \n");
347: msg
348: .append(" -rmicopts <opt> specify the options to pass to the rmi compiler \n");
349: msg.append(" -verbose \n");
350: msg.append(" \n");
351: msg
352: .append("Input_File standard deployment descriptor file's name or \n");
353: msg.append(" ejb-jar file's name \n");
354: GenIDL.info(msg.toString());
355: }
356:
357: /**
358: * Display the specified message only if verbose.
359: * @param msg the message to display
360: */
361: void trace(String msg) {
362: if (verbose) {
363: info("-- " + msg);
364: }
365: }
366:
367: /**
368: * Display the specified message.
369: * @param msg the message to display
370: */
371: static void info(String msg) {
372: System.out.println(msg);
373: }
374:
375: /**
376: * Display the specified error message.
377: * @param errMsg the error message to display
378: */
379: static void error(String errMsg) {
380: System.err.println("GenIDL error: " + errMsg);
381: }
382:
383: /**
384: * Display the specified error message and exits with an
385: * EXIT_FAILURE status.
386: * @param errMsg the error message to display
387: */
388: static void fatalError(String errMsg) {
389: System.err.println("GenIDL fatal error: " + errMsg);
390: System.exit(EXIT_FAILURE);
391: }
392: }
|