001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: * Copyright (C) 2004-2005 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: EWSWsClientGenerator.java 7560 2005-10-21 13:50:04Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.generator.ews;
025:
026: import java.io.File;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032:
033: import org.apache.axis.constants.Scope;
034: import org.apache.axis.wsdl.toJava.GeneratedFileInfo;
035: import org.apache.ws.ews.context.webservices.client.ServiceReferenceContext;
036:
037: import org.objectweb.jonas_lib.I18n;
038: import org.objectweb.jonas_lib.deployment.api.HandlerDesc;
039: import org.objectweb.jonas_lib.genbase.GenBaseException;
040: import org.objectweb.jonas_lib.genbase.archive.Archive;
041: import org.objectweb.jonas_lib.genbase.archive.Client;
042: import org.objectweb.jonas_lib.genbase.archive.EjbJar;
043: import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
044: import org.objectweb.jonas_lib.genbase.archive.WebApp;
045: import org.objectweb.jonas_lib.genbase.generator.Config;
046: import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
047: import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
048:
049: import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
050: import org.objectweb.jonas_ws.wsgen.WsGenException;
051: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsClientDDModifier;
052: import org.objectweb.jonas_ws.wsgen.generator.WsClientGenerator;
053: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.JOnASJ2EEWebServicesContext;
054: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.ServiceReferenceContextImpl;
055: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.emitter.FullEmitter;
056: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.factory.JOnASClientGeneratorFactory;
057: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.wscf.JOnASWSCFHandler;
058:
059: import org.objectweb.jonas.common.Log;
060:
061: import org.objectweb.util.monolog.api.BasicLevel;
062: import org.objectweb.util.monolog.api.Logger;
063:
064: /**
065: * Generate WebServices client files dedicated to axis. <ul><li>
066: * client-config.wsdd : if needed</li><li>java sources : from WSDL</li>
067: * </ul>
068: * @author Guillaume sauthier
069: */
070: public class EWSWsClientGenerator extends WsClientGenerator {
071:
072: /**
073: * logger
074: */
075: private static Logger logger = Log
076: .getLogger(Log.JONAS_WSGEN_EWS_PREFIX);
077:
078: /**
079: * jonas-init-param name for client configuration file declaration
080: */
081: private static final String CLIENT_CONFIG = "axis.clientConfigFile";
082:
083: /** generated config file */
084: private File generated = null;
085:
086: /**
087: * WEB-INF/ prefix
088: */
089: private static final String WEB_PREFIX = "WEB-INF/";
090:
091: /**
092: * I18n
093: */
094: private static I18n i18n = I18n
095: .getInstance(EWSWsClientGenerator.class);
096:
097: /**
098: * Creates a new AxisWsClientGenerator
099: * @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: */
105: public EWSWsClientGenerator(Config config, ServiceRefDesc srd,
106: WsClientDDModifier ddm, Archive archive)
107: throws GenBaseException {
108: super (config, srd, ddm, archive);
109: }
110:
111: /**
112: * generate axis specific files
113: * @throws WsGenException if WSDL cannot be found in archive
114: */
115: public void generate() throws WsGenException {
116:
117: // the source generation is possible only when a
118: // WSDL Definition is provided
119: // we need :
120: // - a: wsdl-file
121: // - b: jaxrpc-mapping-file
122: // - c: service-interface != javax.xml.rpc.Service
123: // - d: some port-component-ref
124: // = ((a && b) && (c || d))
125:
126: boolean hasWSDL = getRef().getWsdlFileName() != null;
127:
128: if (hasWSDL) {
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: try {
137: // WSDL + mapping file
138: FullEmitter wsEmitter = new FullEmitter();
139: wsEmitter.setMappingFileInputStream(getRef()
140: .getMappingFileURL().openStream());
141: JOnASClientGeneratorFactory factory = new JOnASClientGeneratorFactory();
142: factory.setEmitter(wsEmitter);
143:
144: wsEmitter.setTypeMappingVersion("1.3");
145: wsEmitter.setScope(Scope.REQUEST);
146: wsEmitter.setWrapArrays(true);
147:
148: // Classloader to use to check if classes are present before
149: // generating java classes
150: wsEmitter.setClassLoader(cl);
151:
152: wsEmitter.setFactory(factory);
153: //wsEmitter.setNowrap(true);
154:
155: wsEmitter
156: .setJOnASWsContext(prepareJ2EEWebServicesContext(getRef()));
157:
158: wsEmitter.setOutputDir(this .getSources()
159: .getCanonicalPath());
160: wsEmitter.setAllowInvalidURL(true);
161:
162: wsEmitter.run(getRef().getLocalWSDLURL()
163: .toExternalForm());
164:
165: generated = new File(findClientConfigFile(wsEmitter
166: .getGeneratedFileInfo()));
167:
168: getLogger()
169: .log(BasicLevel.INFO,
170: "Web Services Classes successfully generated by EWS.");
171: getLogger().log(
172: BasicLevel.INFO,
173: "Webservice client WSDD file '" + generated
174: + "' sucessfully generated by EWS.");
175: } catch (Exception e) {
176: String err = getI18n().getMessage(
177: "EWSWsClientGenerator.generate.WSDL2Java",
178: e.getMessage());
179: logger.log(BasicLevel.ERROR, err, e);
180: throw new WsGenException(err, e);
181: }
182:
183: }
184:
185: }
186:
187: /**
188: * @param generatedFileInfo files generated by Axis
189: * @return Returns the first filename that matches "deploy-client-*.wsdd"
190: */
191: private static String findClientConfigFile(
192: GeneratedFileInfo generatedFileInfo) {
193: List generated = generatedFileInfo.getFileNames();
194: boolean found = false;
195: String filename = null;
196: for (Iterator i = generated.iterator(); i.hasNext() && !found;) {
197: String entry = (String) i.next();
198: // */deploy-client-*.wsdd
199: if (entry.matches(".*deploy-client-\\d+\\.wsdd$")) {
200: found = true;
201: filename = entry;
202: }
203: }
204: return filename;
205: }
206:
207: /**
208: * @param ref the ServiceRefDesc to be used in J2EE WS Context
209: * @return Returns a configured J2EEWebServiceContext
210: */
211: private JOnASJ2EEWebServicesContext prepareJ2EEWebServicesContext(
212: ServiceRefDesc ref) {
213:
214: JOnASJ2EEWebServicesContext wsCtx = new JOnASJ2EEWebServicesContext();
215: ServiceReferenceContext sRefCtx = new ServiceReferenceContextImpl();
216: sRefCtx.setServiceQName(ref.getServiceQName());
217:
218: for (Iterator i = ref.getHandlerRefs().iterator(); i.hasNext();) {
219: HandlerDesc handler = (HandlerDesc) i.next();
220: sRefCtx.addHandler(new JOnASWSCFHandler(handler));
221: }
222: wsCtx.addServiceReferenceContext(sRefCtx);
223: return wsCtx;
224: }
225:
226: /**
227: * Add generated files in given archive
228: * @param archive archive where generated fils will be added.
229: * @throws WsGenException when files cannot be added
230: */
231: public void addFiles(Archive archive) throws WsGenException {
232: if (archive instanceof WebApp) {
233: WebApp web = (WebApp) archive;
234: archive.addDirectoryIn("WEB-INF/classes/", getClasses());
235: archive.addDirectoryIn("WEB-INF/sources/", getSources());
236:
237: if (generated != null) {
238: archive.addFileIn("WEB-INF/", generated);
239:
240: // ensure the optionnal descriptor exists
241: if (!getModifier().hasJonasServiceRef()) {
242: boolean jwaFileExists = getArchive()
243: .getContainedFiles().contains(
244: "WEB-INF/jonas-web.xml");
245: boolean jwaDocumentExists = web.getDescriptors()
246: .containsKey("WEB-INF/jonas-web.xml");
247: if (!(jwaFileExists || jwaDocumentExists)) {
248: // jonas-web.xml doesn't exists
249: createEmptyJonasWeb((J2EEArchive) archive);
250: }
251: Element jsr = getModifier().createJonasServiceRef(
252: getRef().getServiceRefName());
253: // update
254: getModifier().setElement(jsr);
255:
256: }
257:
258: // add init param
259: getModifier().addJonasInitParam(CLIENT_CONFIG,
260: WEB_PREFIX + generated.getName());
261: }
262: } else if (archive instanceof EjbJar) {
263: archive.addDirectory(getClasses());
264:
265: if (generated != null) {
266: archive.addFileIn("META-INF/", generated);
267:
268: // ensure the optionnal descriptor exists
269: if (!getModifier().hasJonasServiceRef()) {
270: Element jsr = getModifier().createJonasServiceRef(
271: getRef().getServiceRefName());
272: // update
273: getModifier().setElement(jsr);
274: }
275:
276: // add init param
277: getModifier().addJonasInitParam(CLIENT_CONFIG,
278: "META-INF/" + generated.getName());
279: }
280: } else {
281: // We have a Client archive
282: Client client = (Client) archive;
283: archive.addDirectory(getClasses());
284:
285: if (generated != null) {
286: archive.addFileIn("META-INF/", generated);
287:
288: // ensure the optionnal descriptor exists
289: if (!getModifier().hasJonasServiceRef()) {
290: boolean jcFileExists = getArchive()
291: .getContainedFiles().contains(
292: "META-INF/jonas-client.xml");
293: boolean jcDocumentExists = client.getDescriptors()
294: .containsKey("META-INF/jonas-client.xml");
295: if (!(jcFileExists || jcDocumentExists)) {
296: // jonas-client.xml doesn't exists
297: createEmptyJonasClient((J2EEArchive) archive);
298: }
299: Element jsr = getModifier().createJonasServiceRef(
300: getRef().getServiceRefName());
301: // update
302: getModifier().setElement(jsr);
303:
304: }
305:
306: // add init param
307: getModifier().addJonasInitParam(CLIENT_CONFIG,
308: "META-INF/" + generated.getName());
309: }
310: }
311: }
312:
313: /**
314: * Add an empty jonas-web.xml in given J2EEArchive.
315: * @param archive archive to be updated
316: */
317: private void createEmptyJonasWeb(J2EEArchive archive) {
318: Document doc = XMLUtils.newJonasWeb();
319: archive.getDescriptors().put("WEB-INF/jonas-web.xml", doc);
320: getModifier().setDocument(doc);
321: }
322:
323: /**
324: * Add an empty jonas-client.xml in given J2EEArchive.
325: * @param archive archive to be updated
326: */
327: private void createEmptyJonasClient(J2EEArchive archive) {
328: Document doc = XMLUtils.newJonasClient();
329: archive.getDescriptors().put("META-INF/jonas-client.xml", doc);
330: getModifier().setDocument(doc);
331: }
332:
333: /**
334: * @return Returns the i18n.
335: */
336: public static I18n getI18n() {
337: return i18n;
338: }
339: }
|