KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genclientstub > generator > Generator


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 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  * --------------------------------------------------------------------------
22  * $Id: Generator.java,v 1.7 2005/04/28 16:52:59 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genclientstub.generator;
27
28 import java.io.File JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.objectweb.carol.util.configuration.ConfigurationRepository;
34
35 import org.objectweb.common.Cmd;
36
37 import org.objectweb.jonas_lib.I18n;
38 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
39 import org.objectweb.jonas_lib.genbase.GenBaseException;
40 import org.objectweb.jonas_lib.genbase.archive.Archive;
41 import org.objectweb.jonas_lib.genbase.archive.Client;
42 import org.objectweb.jonas_lib.genbase.archive.EjbJar;
43 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
44 import org.objectweb.jonas_lib.genbase.archive.WebApp;
45 import org.objectweb.jonas_lib.genbase.generator.AbsGenerator;
46 import org.objectweb.jonas_lib.genbase.generator.Config;
47 import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;
48 import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
49
50 import org.objectweb.util.monolog.api.BasicLevel;
51
52 /**
53  * Generator used to generate Stubs for clients
54  * @author Florent Benoit
55  */

56 public class Generator extends AbsGenerator {
57
58     /**
59      * i18n
60      */

61     private static I18n i18n = I18n.getInstance(Generator.class);
62
63     /**
64      * EjbRef used for this generator
65      */

66     private EjbRefDesc ejbRef = null;
67
68     /**
69      * Class used if no ejbRef is given
70      */

71     private String JavaDoc intfStubClassName = null;
72
73     /**
74      * Archive used by the modifier
75      */

76     private Archive archive = null;
77
78     /**
79      * Creates a new Generator with the given Config.
80      * @param config internal configuration object.
81      * @param ejbRef reference to the ejb on which we want create the stub
82      * @param intfStubClassName name of the interface
83      * @param archive given ejbjar, webapp, ...
84      * @throws GenBaseException When sources and target temporary directory
85      * cannot be created
86      */

87     public Generator(Config config, EjbRefDesc ejbRef, String JavaDoc intfStubClassName, Archive archive) throws GenBaseException {
88         super(config);
89         this.ejbRef = ejbRef;
90         this.intfStubClassName = intfStubClassName;
91         this.archive = archive;
92     }
93
94     /**
95      * Generate stub files.
96      * @throws ClientStubGenException When generation fails.
97      */

98     public void generate() throws ClientStubGenException {
99
100         try {
101             //URL[] urls = null;
102
String JavaDoc archiveClasspath = null;
103
104             J2EEArchive arch = (J2EEArchive) this.archive;
105             AbsModuleClassLoader loader = (AbsModuleClassLoader) arch.getModuleClassloader();
106
107             archiveClasspath = loader.getClasspath();
108
109             // itf class
110
String JavaDoc itfClass = null;
111             if (ejbRef != null) {
112                 itfClass = ejbRef.getHome();
113             } else {
114                 itfClass = intfStubClassName;
115             }
116
117             // TODO Now try to load the class and check if stub is here
118

119             // Add to the rmic Classpath all the lib and classes of the archive
120
String JavaDoc classpath = getConfig().getClasspath() + File.separator + archiveClasspath;
121
122             // Build stub
123

124             // Array of commands
125
Map JavaDoc commands = new HashMap JavaDoc();
126
127             // Get current protocol
128
String JavaDoc rmiName = ConfigurationRepository.getCurrentConfiguration().getProtocol().getName();
129
130             // Build rmic command for jrmp
131
//Cmd jrmpCmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameRmic());
132
//jrmpCmd.addArgument("-classpath");
133
//jrmpCmd.addArgument(classpath);
134

135 // Cmd jeremieCmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameJava());
136
// jeremieCmd.addArgument("-classpath");
137
// jeremieCmd.addArgument(classpath);
138
// jeremieCmd.addArgument("org.objectweb.jeremie.tools.jrmic.JRMICompiler");
139
// jeremieCmd.addArgument("-opt");
140
// jeremieCmd.addArgument("-owext");
141
// commands.put("jeremie", jeremieCmd);
142

143             if (rmiName.equalsIgnoreCase("iiop")) {
144                 Cmd iiopCmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameRmic());
145                 iiopCmd.addArgument("-classpath");
146                 iiopCmd.addArgument(classpath);
147                 iiopCmd.addArgument("-iiop");
148                 iiopCmd.addArgument("-poa");
149                 iiopCmd.addArgument("-always");
150                 iiopCmd.addArgument("-keepgenerated");
151                 iiopCmd.addArgument("-g");
152                 commands.put("iiop", iiopCmd);
153             }
154
155
156             for (Iterator JavaDoc itCmd = commands.keySet().iterator(); itCmd.hasNext();) {
157                 String JavaDoc protocol = (String JavaDoc) itCmd.next();
158                 Cmd cmd = (Cmd) commands.get(protocol);
159                 cmd.addArgument("-d");
160                 cmd.addArgument(getClasses().getCanonicalPath());
161                 cmd.addArguments(getConfig().getJavacOpts());
162
163                 // add itf class as arg name
164
cmd.addArgument(itfClass);
165
166                 if (getLogger().isLoggable(BasicLevel.DEBUG)) {
167                     getLogger().log(BasicLevel.DEBUG, "Running '" + cmd.toString() + "'");
168                 }
169                 if (cmd.run()) {
170                     getLogger().log(BasicLevel.INFO,
171                             "Client stubs for classname '" + itfClass + "' successfully generated for protocol '" + protocol + "'.");
172                 } else {
173                     String JavaDoc err = i18n.getMessage("Generator.generate.error");
174                     getLogger().log(BasicLevel.ERROR, err);
175                     throw new ClientStubGenException(err);
176                 }
177             }
178
179         } catch (Exception JavaDoc e) {
180             String JavaDoc err = i18n.getMessage("Generator.generate.error");
181             getLogger().log(BasicLevel.ERROR, err);
182             throw new ClientStubGenException(err, e);
183         }
184
185     }
186
187     /**
188      * Compile generated java files into classes directory. Do nothing in case
189      * of clientstub generator as all classes are generated
190      * @throws ClientStubGenException When compilation fails
191      */

192     public void compile() throws ClientStubGenException {
193     }
194
195     /**
196      * Add generated files into an Archive
197      * @param archive the archive destination of generated files.
198      * @throws ClientStubGenException When files cannot be added in the given
199      * Archive.
200      */

201     public void addFiles(Archive archive) throws ClientStubGenException {
202         if (archive instanceof WebApp) {
203             archive.addDirectoryIn("WEB-INF/classes/", getClasses());
204         } else if (archive instanceof EjbJar) {
205             archive.addDirectory(getClasses());
206         } else if (archive instanceof Client) {
207             archive.addDirectory(getClasses());
208         }
209     }
210
211 }
Popular Tags