001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /**
017: * JOnAS : Java(TM) OpenSource Application Server
018: * Copyright (C) 2004 Bull S.A.
019: * Contact: jonas-team@objectweb.org
020: *
021: * This library is free software; you can redistribute it and/or
022: * modify it under the terms of the GNU Lesser General Public
023: * License as published by the Free Software Foundation; either
024: * version 2.1 of the License, or any later version.
025: *
026: * This library is distributed in the hope that it will be useful,
027: * but WITHOUT ANY WARRANTY; without even the implied warranty of
028: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029: * Lesser General Public License for more details.
030: *
031: * You should have received a copy of the GNU Lesser General Public
032: * License along with this library; if not, write to the Free Software
033: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
034: * USA
035: *
036: * --------------------------------------------------------------------------
037: * $Id: JOnASDeployWriter.java 7560 2005-10-21 13:50:04Z sauthieg $
038: * --------------------------------------------------------------------------
039: */package org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.writer;
040:
041: import java.io.File;
042: import java.io.FileOutputStream;
043: import java.io.IOException;
044: import java.io.OutputStreamWriter;
045: import java.io.PrintWriter;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.Vector;
053:
054: import javax.wsdl.Binding;
055: import javax.wsdl.Definition;
056: import javax.wsdl.OperationType;
057: import javax.xml.namespace.QName;
058:
059: import org.apache.axis.deployment.wsdd.WSDDConstants;
060: import org.apache.axis.constants.Use;
061: import org.apache.axis.utils.Messages;
062: import org.apache.axis.wsdl.symbolTable.FaultInfo;
063: import org.apache.axis.wsdl.symbolTable.Parameter;
064: import org.apache.axis.wsdl.symbolTable.Parameters;
065: import org.apache.axis.wsdl.symbolTable.SchemaUtils;
066: import org.apache.axis.wsdl.symbolTable.SymbolTable;
067: import org.apache.axis.wsdl.symbolTable.TypeEntry;
068: import org.apache.axis.wsdl.toJava.Emitter;
069: import org.apache.axis.wsdl.toJava.JavaWriter;
070: import org.apache.axis.wsdl.toJava.Utils;
071:
072: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.JOnASJ2EEWebServicesContext;
073: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.emitter.JOnASWSEmitter;
074:
075: /**
076: * JOnAS Base DeployWriter. It's a placeholder for commonly used methods and fields.
077: * @author Guillaume Sauthier
078: * Based on J2eeDeployWriter from Ias
079: * (http://cvs.apache.org/viewcvs.cgi/ws-axis/contrib/ews/src/org/apache/geronimo/ews/ws4j2ee/toWs/ws/J2eeDeployWriter.java?rev=1.13&view=markup)
080: */
081: public abstract class JOnASDeployWriter extends JavaWriter {
082:
083: /**
084: * JOnASJ2EEWebServicesContext storing JOnAS specific info.
085: */
086: private JOnASJ2EEWebServicesContext jonasWSContext;
087:
088: /**
089: * WSDD Extension suffix
090: */
091: protected static final String WSDD_SUFFIX = ".wsdd";
092:
093: /**
094: * count generated files
095: */
096: private static int count = 0;
097:
098: /**
099: * Definition base used to write deploy file
100: */
101: private Definition definition = null;
102:
103: /**
104: * Symbol Table to use
105: */
106: private SymbolTable symbolTable = null;
107:
108: /** Field use */
109: protected Use use = Use.DEFAULT;
110:
111: /**
112: * Constructor.
113: * @param emitter J2EE Emitter
114: * @param definition Current Definition
115: * @param symbolTable SymbolTable
116: */
117: public JOnASDeployWriter(Emitter emitter, Definition definition,
118: SymbolTable symbolTable) {
119:
120: super (emitter, "deploy");
121: this .definition = definition;
122: this .symbolTable = symbolTable;
123: if (emitter instanceof JOnASWSEmitter) {
124: this .jonasWSContext = ((JOnASWSEmitter) emitter)
125: .getJOnASWsContext();
126: }
127: if (jonasWSContext == null) {
128: throw new RuntimeException("jonasWSContext can not be null");
129: }
130: }
131:
132: /**
133: * @return Returns the fully-qualified name of the deploy.wsdd file to be generated.
134: */
135: protected String getFileName() {
136: // put directly in "output" directory
137: String dir = emitter.getNamespaces().getAsDir("");
138: return dir + getPrefix() + getCount() + WSDD_SUFFIX;
139: }
140:
141: /**
142: * Replace the default file header with the deployment doc file header.
143: *
144: * @param pw PrintWriter where descriptor has to be written
145: * @throws IOException not thrown
146: */
147: protected void writeFileHeader(PrintWriter pw) throws IOException {
148:
149: pw.println(Messages.getMessage("deploy00"));
150: pw.println(Messages.getMessage("deploy02"));
151: pw.println(Messages.getMessage("deploy03"));
152: pw.println(Messages.getMessage("deploy05"));
153: pw.println(Messages.getMessage("deploy06"));
154: pw.println(Messages.getMessage("deploy07"));
155: pw.println(Messages.getMessage("deploy09"));
156: pw.println();
157: pw.println("<deployment");
158: pw.println(" xmlns=\"" + WSDDConstants.URI_WSDD + "\"");
159: pw.println(" xmlns:" + WSDDConstants.NS_PREFIX_WSDD_JAVA
160: + "=\"" + WSDDConstants.URI_WSDD_JAVA + "\">");
161: }
162:
163: /**
164: * Write the body of the deploy.wsdd file.
165: *
166: * @param pw PrintWriter
167: * @throws IOException thrown by writeDeployServices
168: */
169: protected void writeFileBody(PrintWriter pw) throws IOException {
170: writeDeployServices(pw);
171: pw.println("</deployment>");
172: }
173:
174: /**
175: * @return Returns the filename prefix.
176: */
177: protected abstract String getPrefix();
178:
179: /**
180: * Writes te list of wsdd:service
181: * @param pw PrintWriter
182: * @throws IOException implementation may throw IOException
183: */
184: protected abstract void writeDeployServices(PrintWriter pw)
185: throws IOException;
186:
187: /**
188: * Raw routine that writes out the typeMapping.
189: * @param pw PrintWriter
190: * @param namespaceURI xml type namespace
191: * @param localPart xml type localpart
192: * @param javaType java classname
193: * @param serializerFactory java serializer factory classname
194: * @param deserializerFactory java deserializer factory classname
195: * @param encodingStyle encoding style
196: */
197: protected void writeTypeMapping(PrintWriter pw,
198: String namespaceURI, String localPart, String javaType,
199: String serializerFactory, String deserializerFactory,
200: String encodingStyle) {
201:
202: pw.println(" <typeMapping");
203: pw.println(" xmlns:ns=\"" + namespaceURI + "\"");
204: pw.println(" qname=\"ns:" + localPart + '"');
205: pw.println(" type=\"java:" + javaType + '"');
206: pw.println(" serializer=\"" + serializerFactory + "\"");
207: pw.println(" deserializer=\"" + deserializerFactory
208: + "\"");
209: pw.println(" encodingStyle=\"" + encodingStyle + "\"");
210: pw.println(" />");
211: }
212:
213: /**
214: * Raw routine that writes out the operation and parameters.
215: *
216: * @param pw PrintWriter
217: * @param javaOperName java method name
218: * @param elementQName wsdl operation qname
219: * @param returnQName wsdl return type qname
220: * @param returnType java return type classname ?
221: * @param params list of params used by this operation
222: * @param faults list of faults thrown by this operation
223: * @param soapAction soapAction value
224: */
225: protected void writeOperation(PrintWriter pw, String javaOperName,
226: QName elementQName, QName returnQName, QName returnType,
227: Parameters params, ArrayList faults, String soapAction) {
228:
229: pw.print(" <operation name=\"" + javaOperName + "\"");
230:
231: if (elementQName != null) {
232: pw.print(" qname=\""
233: + Utils.genQNameAttributeString(elementQName,
234: "operNS") + "\"");
235: }
236:
237: if (returnQName != null) {
238: pw.print(" returnQName=\""
239: + Utils.genQNameAttributeStringWithLastLocalPart(
240: returnQName, "retNS") + "\"");
241: }
242:
243: if (returnType != null) {
244: pw.print(" returnType=\""
245: + Utils.genQNameAttributeString(returnType, "rtns")
246: + "\"");
247: }
248:
249: Parameter retParam = params.returnParam;
250: if (retParam != null) {
251: TypeEntry type = retParam.getType();
252: QName returnItemQName = Utils.getItemQName(type);
253: if (returnItemQName != null) {
254: pw.print(" returnItemQName=\"");
255: pw.print(Utils.genQNameAttributeString(returnItemQName,
256: "tns3"));
257: pw.print("\"");
258: }
259: QName returnItemType = Utils.getItemType(type);
260: if (returnItemType != null && use == Use.ENCODED) {
261: pw.print(" returnItemType=\"");
262: pw.print(Utils.genQNameAttributeString(returnItemType,
263: "tns2"));
264: pw.print("\"");
265: }
266: }
267:
268: if (soapAction != null) {
269: pw.print(" soapAction=\"" + soapAction + "\"");
270: }
271:
272: if (!OperationType.REQUEST_RESPONSE.equals(params.mep)) {
273: String mepString = getMepString(params.mep);
274: if (mepString != null) {
275: pw.print(" mep=\"" + mepString + "\"");
276: }
277: }
278:
279: if ((params.returnParam != null)
280: && params.returnParam.isOutHeader()) {
281: pw.print(" returnHeader=\"true\"");
282: }
283:
284: pw.println(" >");
285:
286: Vector paramList = params.list;
287:
288: for (int i = 0; i < paramList.size(); i++) {
289: Parameter param = (Parameter) paramList.elementAt(i);
290:
291: // Get the parameter name QName and type QName
292: QName paramQName = param.getQName();
293: QName paramType = Utils.getXSIType(param);
294:
295: pw.print(" <parameter");
296:
297: if (paramQName == null) {
298: pw.print(" name=\"" + param.getName() + "\"");
299: } else {
300: pw
301: .print(" qname=\""
302: + Utils
303: .genQNameAttributeStringWithLastLocalPart(
304: paramQName, "pns")
305: + "\"");
306: }
307:
308: pw.print(" type=\""
309: + Utils.genQNameAttributeString(paramType, "ptns")
310: + "\"");
311:
312: // Get the parameter mode
313: if (param.getMode() != Parameter.IN) {
314: pw.print(" mode=\"" + getModeString(param.getMode())
315: + "\"");
316: }
317:
318: // Is this a header?
319: if (param.isInHeader()) {
320: pw.print(" inHeader=\"true\"");
321: }
322:
323: if (param.isOutHeader()) {
324: pw.print(" outHeader=\"true\"");
325: }
326:
327: QName itemQName = Utils.getItemQName(param.getType());
328: if (itemQName != null) {
329: pw.print(" itemQName=\"");
330: pw.print(Utils.genQNameAttributeString(itemQName,
331: "pitns"));
332: pw.print("\"");
333: }
334:
335: pw.println("/>");
336: }
337:
338: if (faults != null) {
339: for (Iterator iterator = faults.iterator(); iterator
340: .hasNext();) {
341: FaultInfo faultInfo = (FaultInfo) iterator.next();
342: QName faultQName = faultInfo.getQName();
343:
344: if (faultQName != null) {
345: String className = Utils.getFullExceptionName(
346: faultInfo.getMessage(), symbolTable);
347:
348: pw.print(" <fault");
349: pw.print(" name=\"" + faultInfo.getName() + "\"");
350: pw.print(" qname=\""
351: + Utils.genQNameAttributeString(faultQName,
352: "fns") + "\"");
353: pw.print(" class=\"" + className + "\"");
354: pw.print(" type=\""
355: + Utils.genQNameAttributeString(faultInfo
356: .getXMLType(), "ftns") + "\"");
357: pw.println("/>");
358: }
359: }
360: }
361:
362: pw.println(" </operation>");
363: }
364:
365: /**
366: * Write out bean mappings for each type
367: * @param pw PrintWriter
368: * @param binding wsdl:binding
369: * @param hasLiteral has a literal type ?
370: * @param hasMIME has MIME type ?
371: * @param use Use
372: */
373: protected void writeDeployTypes(PrintWriter pw, Binding binding,
374: boolean hasLiteral, boolean hasMIME, Use use) {
375:
376: pw.println();
377:
378: if (hasMIME) {
379: QName bQName = binding.getQName();
380:
381: writeTypeMapping(
382: pw,
383: bQName.getNamespaceURI(),
384: "DataHandler",
385: "javax.activation.DataHandler",
386: "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory",
387: "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory",
388: use.getEncoding());
389: }
390:
391: boolean useJAF = false;
392:
393: Map types = getSymbolTable().getTypeIndex();
394: Collection typeCollection = types.values();
395: for (Iterator i = typeCollection.iterator(); i.hasNext();) {
396: TypeEntry type = (TypeEntry) i.next();
397:
398: // Note this same check is repeated in JavaStubWriter.
399: boolean process = true;
400:
401: // 1) Don't register types we shouldn't
402: if (!Utils.shouldEmit(type)) {
403: process = false;
404: }
405:
406: if (process) {
407: String namespaceURI = type.getQName().getNamespaceURI();
408: String localPart = type.getQName().getLocalPart();
409: String javaType = type.getName();
410: String serializerFactory;
411: String deserializerFactory;
412: String encodingStyle = "";
413: QName innerType = null;
414:
415: if (!hasLiteral) {
416: encodingStyle = use.getEncoding();
417: }
418:
419: if (javaType.endsWith("[]")) {
420: if (SchemaUtils.isListWithItemType(type.getNode())) {
421: serializerFactory = "org.apache.axis.encoding.ser.SimpleListSerializerFactory";
422: deserializerFactory = "org.apache.axis.encoding.ser.SimpleListDeserializerFactory";
423: } else {
424: serializerFactory = "org.apache.axis.encoding.ser.ArraySerializerFactory";
425: deserializerFactory = "org.apache.axis.encoding.ser.ArrayDeserializerFactory";
426: innerType = type.getComponentType();
427: }
428: } else if ((type.getNode() != null)
429: && (Utils.getEnumerationBaseAndValues(type
430: .getNode(), getSymbolTable()) != null)) {
431: serializerFactory = "org.apache.axis.encoding.ser.EnumSerializerFactory";
432: deserializerFactory = "org.apache.axis.encoding.ser.EnumDeserializerFactory";
433: } else if (type.isSimpleType()) {
434: serializerFactory = "org.apache.axis.encoding.ser.SimpleSerializerFactory";
435: deserializerFactory = "org.apache.axis.encoding.ser.SimpleDeserializerFactory";
436: } else if (type.getBaseType() != null) {
437: serializerFactory = "org.apache.axis.encoding.ser.SimpleSerializerFactory";
438: deserializerFactory = "org.apache.axis.encoding.ser.SimpleDeserializerFactory";
439: } else {
440: serializerFactory = "org.apache.axis.encoding.ser.BeanSerializerFactory";
441: deserializerFactory = "org.apache.axis.encoding.ser.BeanDeserializerFactory";
442: }
443:
444: List jafType = new Vector();
445: jafType.add("java.awt.Image");
446: jafType.add("javax.xml.transform.Source");
447: jafType.add("javax.mail.internet.MimeMultipart");
448: if (jafType.contains(javaType)) {
449: serializerFactory = "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory";
450: deserializerFactory = "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory";
451: useJAF = true;
452: }
453:
454: if (innerType == null) {
455: // no arrays
456: writeTypeMapping(pw, namespaceURI, localPart,
457: javaType, serializerFactory,
458: deserializerFactory, encodingStyle);
459: } else {
460: // arrays
461: writeArrayTypeMapping(pw, namespaceURI, localPart,
462: javaType, encodingStyle, innerType);
463: }
464: }
465: }
466:
467: if (useJAF) {
468: // mime don't work with ref
469: pw
470: .println(" <parameter name=\"sendMultiRefs\" value=\"false\"/>");
471: }
472: }
473:
474: /**
475: * Raw routine that writes out the arrayMapping.
476: * @param pw PrintWriter
477: * @param namespaceURI xml type namespace
478: * @param localPart xml type localpart
479: * @param javaType java classname
480: * @param encodingStyle encoding style
481: * @param innerType array component type QName
482: */
483: protected void writeArrayTypeMapping(PrintWriter pw,
484: String namespaceURI, String localPart, String javaType,
485: String encodingStyle, QName innerType) {
486:
487: pw.println(" <arrayMapping");
488: pw.println(" xmlns:ns=\"" + namespaceURI + "\"");
489: pw.println(" qname=\"ns:" + localPart + '"');
490: pw.println(" type=\"java:" + javaType + '"');
491: pw.println(" innerType=\""
492: + Utils.genQNameAttributeString(innerType, "cmp-ns")
493: + '"');
494: pw.println(" encodingStyle=\"" + encodingStyle + "\"");
495: pw.println(" />");
496: }
497:
498: /**
499: * Method getModeString
500: *
501: * @param mode Parameter mode (IN, INOUT, OUT)
502: * @return Parameter Mode String representation
503: */
504: public String getModeString(byte mode) {
505:
506: if (mode == Parameter.IN) {
507: return "IN";
508: } else if (mode == Parameter.INOUT) {
509: return "INOUT";
510: } else {
511: return "OUT";
512: }
513: }
514:
515: /**
516: * Method getPrintWriter
517: * @param filename file to open
518: * @return Returns the printWriter for the file
519: * @throws IOException When File cannot be open/written
520: */
521: protected PrintWriter getPrintWriter(String filename)
522: throws IOException {
523:
524: File file = new File(filename);
525: File parent = new File(file.getParent());
526:
527: parent.mkdirs();
528:
529: FileOutputStream out = new FileOutputStream(file);
530: OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
531:
532: return new PrintWriter(writer);
533: }
534:
535: /**
536: * @return Returns the count.
537: */
538: public static int getCount() {
539: return count++;
540: }
541:
542: /**
543: * @return Returns the definition.
544: */
545: public Definition getDefinition() {
546: return definition;
547: }
548:
549: /**
550: * @return Returns the jonasWSContext.
551: */
552: public JOnASJ2EEWebServicesContext getJonasWSContext() {
553: return jonasWSContext;
554: }
555:
556: /**
557: * @return Returns the symbolTable.
558: */
559: public SymbolTable getSymbolTable() {
560: return symbolTable;
561: }
562:
563: /**
564: * Store operation type String representation
565: */
566: private static Map mepStrings = new HashMap();
567: static {
568: mepStrings.put(OperationType.REQUEST_RESPONSE,
569: "request-response");
570: mepStrings.put(OperationType.ONE_WAY, "oneway");
571: }
572:
573: /**
574: * @param mep OperationType
575: * @return Returns the String representation of this type
576: */
577: private String getMepString(OperationType mep) {
578: return (String) mepStrings.get(mep);
579: }
580: }
|