001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004-2007 Bull S.A.S.
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: * --------------------------------------------------------------------------
022: * $Id: Generator.java 10094 2007-03-23 16:15:39Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.genclientstub.generator;
025:
026: import java.io.File;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029:
030: import org.objectweb.carol.util.configuration.ConfigurationRepository;
031: import org.objectweb.common.Cmd;
032: import org.objectweb.fastrmic.RMIC;
033: import org.objectweb.jonas_lib.I18n;
034: import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
035: import org.objectweb.jonas_lib.genbase.GenBaseException;
036: import org.objectweb.jonas_lib.genbase.archive.Archive;
037: import org.objectweb.jonas_lib.genbase.archive.Client;
038: import org.objectweb.jonas_lib.genbase.archive.EjbJar;
039: import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
040: import org.objectweb.jonas_lib.genbase.archive.WebApp;
041: import org.objectweb.jonas_lib.genbase.generator.AbsGenerator;
042: import org.objectweb.jonas_lib.genbase.generator.Config;
043: import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;
044: import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
045: import org.objectweb.util.monolog.api.BasicLevel;
046:
047: /**
048: * Generator used to generate Stubs for clients
049: * @author Florent Benoit
050: */
051: public class Generator extends AbsGenerator {
052:
053: /**
054: * i18n
055: */
056: private static I18n i18n = I18n.getInstance(Generator.class);
057:
058: /**
059: * EjbRef used for this generator
060: */
061: private EjbRefDesc ejbRef = null;
062:
063: /**
064: * Class used if no ejbRef is given
065: */
066: private String intfStubClassName = null;
067:
068: /**
069: * Archive used by the modifier
070: */
071: private Archive archive = null;
072:
073: /**
074: * Creates a new Generator with the given Config.
075: * @param config internal configuration object.
076: * @param ejbRef reference to the ejb on which we want create the stub
077: * @param intfStubClassName name of the interface
078: * @param archive given ejbjar, webapp, ...
079: * @throws GenBaseException When sources and target temporary directory
080: * cannot be created
081: */
082: public Generator(Config config, EjbRefDesc ejbRef,
083: String intfStubClassName, Archive archive)
084: throws GenBaseException {
085: super (config);
086: this .ejbRef = ejbRef;
087: this .intfStubClassName = intfStubClassName;
088: this .archive = archive;
089: }
090:
091: /**
092: * Generate stub files.
093: * @throws ClientStubGenException When generation fails.
094: */
095: public void generate() throws ClientStubGenException {
096:
097: try {
098: //URL[] urls = null;
099: String archiveClasspath = null;
100:
101: J2EEArchive arch = (J2EEArchive) this .archive;
102: AbsModuleClassLoader loader = (AbsModuleClassLoader) arch
103: .getModuleClassloader();
104:
105: archiveClasspath = loader.getClasspath();
106:
107: // itf class
108: String itfClass = null;
109: if (ejbRef != null) {
110: itfClass = ejbRef.getHome();
111: } else {
112: itfClass = intfStubClassName;
113: }
114:
115: // TODO Now try to load the class and check if stub is here
116:
117: // Add to the rmic Classpath all the lib and classes of the archive
118: String classpath = getConfig().getClasspath()
119: + File.separator + archiveClasspath;
120:
121: // Get current protocol
122: String rmiName = ConfigurationRepository
123: .getCurrentConfiguration().getProtocol().getName();
124:
125: if (rmiName.equalsIgnoreCase("iiop")) {
126: Cmd iiopCmd = new Cmd(getConfig().getJavaHomeBin()
127: + getConfig().getNameRmic());
128: iiopCmd.addArgument("-classpath");
129: iiopCmd.addArgument(classpath);
130: iiopCmd.addArgument("-iiop");
131: iiopCmd.addArgument("-poa");
132: iiopCmd.addArgument("-always");
133: iiopCmd.addArgument("-keepgenerated");
134: iiopCmd.addArgument("-g");
135:
136: iiopCmd.addArgument("-d");
137: iiopCmd.addArgument(getClasses().getCanonicalPath());
138: iiopCmd.addArguments(getConfig().getJavacOpts());
139:
140: // add itf class as arg name
141: iiopCmd.addArgument(itfClass);
142:
143: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
144: getLogger().log(BasicLevel.DEBUG,
145: "Running '" + iiopCmd.toString() + "'");
146: }
147: if (iiopCmd.run()) {
148: getLogger()
149: .log(
150: BasicLevel.INFO,
151: "Client stubs for classname '"
152: + itfClass
153: + "' successfully generated for protocol 'iiop'.");
154: } else {
155: String err = i18n
156: .getMessage("Generator.generate.error");
157: getLogger().log(BasicLevel.ERROR, err);
158: throw new ClientStubGenException(err);
159: }
160:
161: } else if ("jrmp".equalsIgnoreCase(rmiName)
162: || "irmi".equalsIgnoreCase(rmiName)) {
163: Cmd jrmpCmd = new Cmd(getConfig().getJavaHomeBin()
164: + getConfig().getNameRmic());
165: jrmpCmd.addArgument("-classpath");
166: jrmpCmd.addArgument(classpath);
167: jrmpCmd.addArgument("-keepgenerated");
168: jrmpCmd.addArgument("-g");
169: jrmpCmd.addArgument("-d");
170: jrmpCmd.addArgument(getClasses().getCanonicalPath());
171: jrmpCmd.addArguments(getConfig().getJavacOpts());
172:
173: // add itf class as arg name
174: jrmpCmd.addArgument(itfClass);
175:
176: ArrayList args = new ArrayList();
177: boolean skip = true;
178: for (Iterator it = jrmpCmd.getCommandLine(); it
179: .hasNext();) {
180: Object o = it.next();
181: if (skip) {
182: skip = false;
183: continue;
184: }
185: args.add(o);
186: }
187:
188: // Build the stub using Fast RMIC
189: String[] a = (String[]) args.toArray(new String[] {});
190: RMIC rmic = new RMIC(a);
191:
192: if (rmic.run()) {
193: getLogger()
194: .log(
195: BasicLevel.INFO,
196: "Client stubs for classname '"
197: + itfClass
198: + "' successfully generated for protocol '"
199: + rmiName + "'.");
200: } else {
201: String err = i18n
202: .getMessage("Generator.generate.error");
203: getLogger().log(BasicLevel.ERROR, err);
204: throw new ClientStubGenException(err, rmic
205: .getException());
206: }
207:
208: }
209:
210: } catch (Exception e) {
211: String err = i18n.getMessage("Generator.generate.error");
212: getLogger().log(BasicLevel.ERROR, err);
213: throw new ClientStubGenException(err, e);
214: }
215:
216: }
217:
218: /**
219: * Compile generated java files into classes directory. Do nothing in case
220: * of clientstub generator as all classes are generated
221: * @throws ClientStubGenException When compilation fails
222: */
223: public void compile() throws ClientStubGenException {
224: }
225:
226: /**
227: * Add generated files into an Archive
228: * @param archive the archive destination of generated files.
229: * @throws ClientStubGenException When files cannot be added in the given
230: * Archive.
231: */
232: public void addFiles(Archive archive) throws ClientStubGenException {
233: if (archive instanceof WebApp) {
234: archive.addDirectoryIn("WEB-INF/classes/", getClasses());
235: } else if (archive instanceof EjbJar) {
236: archive.addDirectory(getClasses());
237: } else if (archive instanceof Client) {
238: archive.addDirectory(getClasses());
239: }
240: }
241:
242: }
|