KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > axis > AxisWsClientGenerator


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  * Copyright (C) 1999-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: AxisWsClientGenerator.java,v 1.12 2005/04/28 08:43:27 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.wsgen.generator.axis;
27
28 import java.io.File JavaDoc;
29
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33 import org.apache.velocity.VelocityContext;
34
35 import org.objectweb.jonas_lib.I18n;
36 import org.objectweb.jonas_lib.genbase.GenBaseException;
37 import org.objectweb.jonas_lib.genbase.archive.Archive;
38 import org.objectweb.jonas_lib.genbase.archive.EjbJar;
39 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
40 import org.objectweb.jonas_lib.genbase.archive.WebApp;
41 import org.objectweb.jonas_lib.genbase.generator.Config;
42 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
43 import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
44
45 import org.objectweb.jonas_ws.deployment.api.MappingFile;
46 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
47 import org.objectweb.jonas_ws.wsgen.WsGenException;
48 import org.objectweb.jonas_ws.wsgen.ddmodifier.WsClientDDModifier;
49 import org.objectweb.jonas_ws.wsgen.generator.WsClientGenerator;
50 import org.objectweb.jonas_ws.wsgen.generator.axis.wsdl2java.JOnASWSDL2Java;
51
52 import org.objectweb.util.monolog.api.BasicLevel;
53
54 /**
55  * Generate WebServices client files dedicated to axis. <ul><li>
56  * client-config.wsdd : if needed</li><li>java sources : from WSDL</li>
57  * </ul>
58  *
59  * @author Guillaume sauthier
60  */

61 public class AxisWsClientGenerator extends WsClientGenerator {
62
63     /** unique JVelocity instance */
64     private static JVelocity jvelocity = null;
65
66     /**
67      * jonas-init-param name for client configuration file declaration
68      */

69     private static final String JavaDoc CLIENT_CONFIG = "axis.clientConfigFile";
70
71     /**
72      * WSDD Extension suffix
73      */

74     private static final String JavaDoc WSDD_SUFFIX = ".wsdd";
75
76     /**
77      * WSDD Extension prefix
78      */

79     private static final String JavaDoc WSDD_PREFIX = "deploy-client-";
80
81     /** count generated files to assure names unicity */
82     private static int count = 0;
83
84     /** generated config file */
85     private File JavaDoc generated = null;
86
87     /**
88      * WEB-INF/ prefix
89      */

90     private static final String JavaDoc WEB_PREFIX = "WEB-INF/";
91
92     /**
93      * I18n
94      */

95     private static I18n i18n = I18n.getInstance(AxisWsClientGenerator.class);
96
97     /**
98      * Creates a new AxisWsClientGenerator
99      * @param config Generator Configuration
100      * @param srd WebService Endpoint description
101      * @param ddm Web DD Modifier
102      * @param archive client archive containing WSDL
103      * @throws GenBaseException When instanciation fails
104      * @throws WsGenException When instanciation fails
105      */

106     public AxisWsClientGenerator(Config config, ServiceRefDesc srd, WsClientDDModifier ddm, Archive archive)
107             throws GenBaseException, WsGenException {
108         super(config, srd, ddm, archive);
109
110         // init velocity
111
if (jvelocity == null) {
112             jvelocity = new JVelocity("deploy_client.vm");
113         }
114     }
115
116     /**
117      * generate axis specific files
118      * @throws WsGenException if WSDL cannot be found in archive
119      */

120     public void generate() throws WsGenException {
121
122         // the source generation is possible only when a
123
// WSDL Definition is provided
124
if ((getRef().getWsdlFileName() != null)
125                 && !getRef().getServiceInterface().getName().equals("javax.xml.rpc.Service")) {
126
127             try {
128                 // classpath creation
129
J2EEArchive j2eeArchive = (J2EEArchive) getArchive();
130                 AbsModuleClassLoader cl = (AbsModuleClassLoader) j2eeArchive.getModuleClassloader();
131                 getConfig().setClasspath(getConfig().getClasspath() + cl.getClasspath());
132
133
134                 JOnASWSDL2Java jWsdl2Java = new JOnASWSDL2Java();
135                 jWsdl2Java.run(this);
136                 getLogger().log(BasicLevel.INFO, "Web Services Classes successfully generated by Axis.");
137             } catch (Exception JavaDoc e) {
138                 String JavaDoc err = getI18n().getMessage("AxisWsClientGenerator.generate.WSDL2Java");
139                 e.printStackTrace(System.err);
140                 throw new WsGenException(err, e);
141             }
142         }
143
144         // Client-config.wsdd ??
145
// only if handlers are specified in ServiceRef
146
// but if noConfig set to true, assume that
147
// user configure himself the client. (expert)
148
// Could we create configuration ?
149
if (hasClientConfigFile(getRef())) {
150             // create deploy.wsdd
151
// use velocity template
152
// build a unique file name
153
String JavaDoc filename = WSDD_PREFIX + (count++) + WSDD_SUFFIX;
154
155             VelocityContext vctx = VContextFactory.getContext(getRef());
156
157             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
158                 getLogger().log(BasicLevel.DEBUG, "Creating '" + filename + "'");
159             }
160             generated = new File JavaDoc(getSources(), filename);
161
162             jvelocity.generate(generated, vctx);
163
164         }
165         // End !noConfig
166
}
167
168     /**
169      * Returns true if given service-ref need a client configuration files
170      * @param ref Service Ref
171      * @return true if given service-ref need a client configuration files
172      */

173     private boolean hasClientConfigFile(ServiceRefDesc ref) {
174         // no configuration asked
175
if (getConfig().isNoConfig()) {
176             return false;
177         }
178
179         MappingFile mf = ref.getMappingFile();
180
181         if (mf != null) {
182             // mapping file defined
183
if (mf.getXmlTypeMappings().hasNext()) {
184                 // we have 1 mapping at least
185
return true;
186             } else {
187                 // no mapping
188
// return true if ref has Handlers
189
return (ref.getHandlerRefs().size() != 0);
190             }
191         } else {
192             // no mapping file
193
// return true if ref has Handlers
194
return (ref.getHandlerRefs().size() != 0);
195         }
196     }
197
198     /**
199      * Add generated files in given archive
200      * @param archive archive where generated fils will be added.
201      * @throws WsGenException when files cannot be added
202      */

203     public void addFiles(Archive archive) throws WsGenException {
204         if (archive instanceof WebApp) {
205             archive.addDirectoryIn("WEB-INF/classes/", getClasses());
206
207             if (generated != null) {
208                 archive.addFileIn("WEB-INF/", generated);
209
210                 // ensure the optionnal descriptor exists
211
if (!getModifier().hasJonasServiceRef()) {
212                     if (!getArchive().getContainedFiles().contains("WEB-INF/jonas-web.xml")) {
213                         // jonas-web.xml doesn't exists
214
createEmptyJonasWeb((J2EEArchive) archive);
215                     }
216                     Element JavaDoc jsr = getModifier().createJonasServiceRef(getRef().getServiceRefName());
217                     // update
218
getModifier().setElement(jsr);
219
220                 }
221
222                 // add init param
223
getModifier().addJonasInitParam(CLIENT_CONFIG, WEB_PREFIX + generated.getName());
224             }
225         } else if (archive instanceof EjbJar) {
226             archive.addDirectory(getClasses());
227
228             if (generated != null) {
229                 archive.addFileIn("META-INF/", generated);
230
231                 // ensure the optionnal descriptor exists
232
if (!getModifier().hasJonasServiceRef()) {
233                     Element JavaDoc jsr = getModifier().createJonasServiceRef(getRef().getServiceRefName());
234                     // update
235
getModifier().setElement(jsr);
236                 }
237
238                 // add init param
239
getModifier().addJonasInitParam(CLIENT_CONFIG, "META-INF/" + generated.getName());
240             }
241         } else {
242             archive.addDirectory(getClasses());
243
244             if (generated != null) {
245                 archive.addFileIn("META-INF/", generated);
246
247                 // ensure the optionnal descriptor exists
248
if (!getModifier().hasJonasServiceRef()) {
249                     if (!getArchive().getContainedFiles().contains("META-INF/jonas-client.xml")) {
250                         // jonas-client.xml doesn't exists
251
createEmptyJonasClient((J2EEArchive) archive);
252                     }
253                     Element JavaDoc jsr = getModifier().createJonasServiceRef(getRef().getServiceRefName());
254                     // update
255
getModifier().setElement(jsr);
256
257                 }
258
259                 // add init param
260
getModifier().addJonasInitParam(CLIENT_CONFIG, "META-INF/" + generated.getName());
261             }
262         }
263     }
264
265     /**
266      * Add an empty jonas-web.xml in given J2EEArchive.
267      * @param archive archive to be updated
268      */

269     private void createEmptyJonasWeb(J2EEArchive archive) {
270         Document JavaDoc doc = XMLUtils.newJonasWeb();
271         archive.getDescriptors().put("WEB-INF/jonas-web.xml", doc);
272         getModifier().setDocument(doc);
273     }
274
275     /**
276      * Add an empty jonas-client.xml in given J2EEArchive.
277      * @param archive archive to be updated
278      */

279     private void createEmptyJonasClient(J2EEArchive archive) {
280         Document JavaDoc doc = XMLUtils.newJonasClient();
281         archive.getDescriptors().put("META-INF/jonas-client.xml", doc);
282         getModifier().setDocument(doc);
283     }
284
285     /**
286      * @return Returns the i18n.
287      */

288     public static I18n getI18n() {
289         return i18n;
290     }
291 }
Popular Tags