001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: * Copyright (C) 1999-2004 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: * --------------------------------------------------------------------------
022: * $Id: AxisWsClientGenerator.java 6661 2005-04-28 08:43:27Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.generator.axis;
025:
026: import java.io.File;
027:
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030:
031: import org.apache.velocity.VelocityContext;
032:
033: import org.objectweb.jonas_lib.I18n;
034: import org.objectweb.jonas_lib.genbase.GenBaseException;
035: import org.objectweb.jonas_lib.genbase.archive.Archive;
036: import org.objectweb.jonas_lib.genbase.archive.EjbJar;
037: import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
038: import org.objectweb.jonas_lib.genbase.archive.WebApp;
039: import org.objectweb.jonas_lib.genbase.generator.Config;
040: import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
041: import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
042:
043: import org.objectweb.jonas_ws.deployment.api.MappingFile;
044: import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
045: import org.objectweb.jonas_ws.wsgen.WsGenException;
046: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsClientDDModifier;
047: import org.objectweb.jonas_ws.wsgen.generator.WsClientGenerator;
048: import org.objectweb.jonas_ws.wsgen.generator.axis.wsdl2java.JOnASWSDL2Java;
049:
050: import org.objectweb.util.monolog.api.BasicLevel;
051:
052: /**
053: * Generate WebServices client files dedicated to axis. <ul><li>
054: * client-config.wsdd : if needed</li><li>java sources : from WSDL</li>
055: * </ul>
056: *
057: * @author Guillaume sauthier
058: */
059: public class AxisWsClientGenerator extends WsClientGenerator {
060:
061: /** unique JVelocity instance */
062: private static JVelocity jvelocity = null;
063:
064: /**
065: * jonas-init-param name for client configuration file declaration
066: */
067: private static final String CLIENT_CONFIG = "axis.clientConfigFile";
068:
069: /**
070: * WSDD Extension suffix
071: */
072: private static final String WSDD_SUFFIX = ".wsdd";
073:
074: /**
075: * WSDD Extension prefix
076: */
077: private static final String WSDD_PREFIX = "deploy-client-";
078:
079: /** count generated files to assure names unicity */
080: private static int count = 0;
081:
082: /** generated config file */
083: private File generated = null;
084:
085: /**
086: * WEB-INF/ prefix
087: */
088: private static final String WEB_PREFIX = "WEB-INF/";
089:
090: /**
091: * I18n
092: */
093: private static I18n i18n = I18n
094: .getInstance(AxisWsClientGenerator.class);
095:
096: /**
097: * Creates a new AxisWsClientGenerator
098: * @param config Generator Configuration
099: * @param srd WebService Endpoint description
100: * @param ddm Web DD Modifier
101: * @param archive client archive containing WSDL
102: * @throws GenBaseException When instanciation fails
103: * @throws WsGenException When instanciation fails
104: */
105: public AxisWsClientGenerator(Config config, ServiceRefDesc srd,
106: 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(
126: "javax.xml.rpc.Service")) {
127:
128: try {
129: // classpath creation
130: J2EEArchive j2eeArchive = (J2EEArchive) getArchive();
131: AbsModuleClassLoader cl = (AbsModuleClassLoader) j2eeArchive
132: .getModuleClassloader();
133: getConfig().setClasspath(
134: getConfig().getClasspath() + cl.getClasspath());
135:
136: JOnASWSDL2Java jWsdl2Java = new JOnASWSDL2Java();
137: jWsdl2Java.run(this );
138: getLogger()
139: .log(BasicLevel.INFO,
140: "Web Services Classes successfully generated by Axis.");
141: } catch (Exception e) {
142: String err = getI18n().getMessage(
143: "AxisWsClientGenerator.generate.WSDL2Java");
144: e.printStackTrace(System.err);
145: throw new WsGenException(err, e);
146: }
147: }
148:
149: // Client-config.wsdd ??
150: // only if handlers are specified in ServiceRef
151: // but if noConfig set to true, assume that
152: // user configure himself the client. (expert)
153: // Could we create configuration ?
154: if (hasClientConfigFile(getRef())) {
155: // create deploy.wsdd
156: // use velocity template
157: // build a unique file name
158: String filename = WSDD_PREFIX + (count++) + WSDD_SUFFIX;
159:
160: VelocityContext vctx = VContextFactory.getContext(getRef());
161:
162: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
163: getLogger().log(BasicLevel.DEBUG,
164: "Creating '" + filename + "'");
165: }
166: generated = new File(getSources(), filename);
167:
168: jvelocity.generate(generated, vctx);
169:
170: }
171: // End !noConfig
172: }
173:
174: /**
175: * Returns true if given service-ref need a client configuration files
176: * @param ref Service Ref
177: * @return true if given service-ref need a client configuration files
178: */
179: private boolean hasClientConfigFile(ServiceRefDesc ref) {
180: // no configuration asked
181: if (getConfig().isNoConfig()) {
182: return false;
183: }
184:
185: MappingFile mf = ref.getMappingFile();
186:
187: if (mf != null) {
188: // mapping file defined
189: if (mf.getXmlTypeMappings().hasNext()) {
190: // we have 1 mapping at least
191: return true;
192: } else {
193: // no mapping
194: // return true if ref has Handlers
195: return (ref.getHandlerRefs().size() != 0);
196: }
197: } else {
198: // no mapping file
199: // return true if ref has Handlers
200: return (ref.getHandlerRefs().size() != 0);
201: }
202: }
203:
204: /**
205: * Add generated files in given archive
206: * @param archive archive where generated fils will be added.
207: * @throws WsGenException when files cannot be added
208: */
209: public void addFiles(Archive archive) throws WsGenException {
210: if (archive instanceof WebApp) {
211: archive.addDirectoryIn("WEB-INF/classes/", getClasses());
212:
213: if (generated != null) {
214: archive.addFileIn("WEB-INF/", generated);
215:
216: // ensure the optionnal descriptor exists
217: if (!getModifier().hasJonasServiceRef()) {
218: if (!getArchive().getContainedFiles().contains(
219: "WEB-INF/jonas-web.xml")) {
220: // jonas-web.xml doesn't exists
221: createEmptyJonasWeb((J2EEArchive) archive);
222: }
223: Element jsr = getModifier().createJonasServiceRef(
224: getRef().getServiceRefName());
225: // update
226: getModifier().setElement(jsr);
227:
228: }
229:
230: // add init param
231: getModifier().addJonasInitParam(CLIENT_CONFIG,
232: WEB_PREFIX + generated.getName());
233: }
234: } else if (archive instanceof EjbJar) {
235: archive.addDirectory(getClasses());
236:
237: if (generated != null) {
238: archive.addFileIn("META-INF/", generated);
239:
240: // ensure the optionnal descriptor exists
241: if (!getModifier().hasJonasServiceRef()) {
242: Element jsr = getModifier().createJonasServiceRef(
243: getRef().getServiceRefName());
244: // update
245: getModifier().setElement(jsr);
246: }
247:
248: // add init param
249: getModifier().addJonasInitParam(CLIENT_CONFIG,
250: "META-INF/" + generated.getName());
251: }
252: } else {
253: archive.addDirectory(getClasses());
254:
255: if (generated != null) {
256: archive.addFileIn("META-INF/", generated);
257:
258: // ensure the optionnal descriptor exists
259: if (!getModifier().hasJonasServiceRef()) {
260: if (!getArchive().getContainedFiles().contains(
261: "META-INF/jonas-client.xml")) {
262: // jonas-client.xml doesn't exists
263: createEmptyJonasClient((J2EEArchive) archive);
264: }
265: Element jsr = getModifier().createJonasServiceRef(
266: getRef().getServiceRefName());
267: // update
268: getModifier().setElement(jsr);
269:
270: }
271:
272: // add init param
273: getModifier().addJonasInitParam(CLIENT_CONFIG,
274: "META-INF/" + generated.getName());
275: }
276: }
277: }
278:
279: /**
280: * Add an empty jonas-web.xml in given J2EEArchive.
281: * @param archive archive to be updated
282: */
283: private void createEmptyJonasWeb(J2EEArchive archive) {
284: Document doc = XMLUtils.newJonasWeb();
285: archive.getDescriptors().put("WEB-INF/jonas-web.xml", doc);
286: getModifier().setDocument(doc);
287: }
288:
289: /**
290: * Add an empty jonas-client.xml in given J2EEArchive.
291: * @param archive archive to be updated
292: */
293: private void createEmptyJonasClient(J2EEArchive archive) {
294: Document doc = XMLUtils.newJonasClient();
295: archive.getDescriptors().put("META-INF/jonas-client.xml", doc);
296: getModifier().setDocument(doc);
297: }
298:
299: /**
300: * @return Returns the i18n.
301: */
302: public static I18n getI18n() {
303: return i18n;
304: }
305: }
|