KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > ews > wsdltoj2ee > writer > JOnASDeployWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /**
17  * JOnAS : Java(TM) OpenSource Application Server
18  * Copyright (C) 2004 Bull S.A.
19  * Contact: jonas-team@objectweb.org
20  *
21  * This library is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Lesser General Public
23  * License as published by the Free Software Foundation; either
24  * version 2.1 of the License, or any later version.
25  *
26  * This library is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29  * Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public
32  * License along with this library; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34  * USA
35  *
36  * --------------------------------------------------------------------------
37  * $Id: JOnASDeployWriter.java,v 1.4 2005/05/27 15:01:22 sauthieg Exp $
38  * --------------------------------------------------------------------------
39  */

40 package org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.writer;
41
42 import java.io.File JavaDoc;
43 import java.io.FileOutputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.OutputStreamWriter JavaDoc;
46 import java.io.PrintWriter JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.Collection JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 import javax.wsdl.Binding;
56 import javax.wsdl.Definition;
57 import javax.wsdl.OperationType;
58 import javax.xml.namespace.QName JavaDoc;
59
60 import org.apache.axis.deployment.wsdd.WSDDConstants;
61 import org.apache.axis.constants.Use;
62 import org.apache.axis.utils.Messages;
63 import org.apache.axis.wsdl.symbolTable.FaultInfo;
64 import org.apache.axis.wsdl.symbolTable.Parameter;
65 import org.apache.axis.wsdl.symbolTable.Parameters;
66 import org.apache.axis.wsdl.symbolTable.SchemaUtils;
67 import org.apache.axis.wsdl.symbolTable.SymbolTable;
68 import org.apache.axis.wsdl.symbolTable.TypeEntry;
69 import org.apache.axis.wsdl.toJava.Emitter;
70 import org.apache.axis.wsdl.toJava.JavaWriter;
71 import org.apache.axis.wsdl.toJava.Utils;
72
73 import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.JOnASJ2EEWebServicesContext;
74 import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.emitter.JOnASWSEmitter;
75
76
77 /**
78  * JOnAS Base DeployWriter. It's a placeholder for commonly used methods and fields.
79  * @author Guillaume Sauthier
80  * Based on J2eeDeployWriter from Ias
81  * (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)
82  */

83 public abstract class JOnASDeployWriter extends JavaWriter {
84
85     /**
86      * JOnASJ2EEWebServicesContext storing JOnAS specific info.
87      */

88     private JOnASJ2EEWebServicesContext jonasWSContext;
89
90     /**
91      * WSDD Extension suffix
92      */

93     protected static final String JavaDoc WSDD_SUFFIX = ".wsdd";
94
95     /**
96      * count generated files
97      */

98     private static int count = 0;
99
100     /**
101      * Definition base used to write deploy file
102      */

103     private Definition definition = null;
104
105     /**
106      * Symbol Table to use
107      */

108     private SymbolTable symbolTable = null;
109
110     /** Field use */
111     protected Use use = Use.DEFAULT;
112
113     /**
114      * Constructor.
115      * @param emitter J2EE Emitter
116      * @param definition Current Definition
117      * @param symbolTable SymbolTable
118      */

119     public JOnASDeployWriter(Emitter emitter, Definition definition, SymbolTable symbolTable) {
120
121         super(emitter, "deploy");
122         this.definition = definition;
123         this.symbolTable = symbolTable;
124         if (emitter instanceof JOnASWSEmitter) {
125             this.jonasWSContext = ((JOnASWSEmitter) emitter).getJOnASWsContext();
126         }
127         if (jonasWSContext == null) {
128             throw new RuntimeException JavaDoc("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 JavaDoc getFileName() {
136         // put directly in "output" directory
137
String JavaDoc 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 JavaDoc pw) throws IOException JavaDoc {
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 JavaDoc pw) throws IOException JavaDoc {
170         writeDeployServices(pw);
171         pw.println("</deployment>");
172     }
173
174     /**
175      * @return Returns the filename prefix.
176      */

177     protected abstract String JavaDoc 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 JavaDoc pw) throws IOException JavaDoc;
185
186     /**
187      * Raw routine that writes out the typeMapping.
188      * @param pw PrintWriter
189      * @param namespaceURI xml type namespace
190      * @param localPart xml type localpart
191      * @param javaType java classname
192      * @param serializerFactory java serializer factory classname
193      * @param deserializerFactory java deserializer factory classname
194      * @param encodingStyle encoding style
195      */

196     protected void writeTypeMapping(PrintWriter JavaDoc pw, String JavaDoc namespaceURI, String JavaDoc localPart, String JavaDoc javaType,
197             String JavaDoc serializerFactory, String JavaDoc deserializerFactory, String JavaDoc encodingStyle) {
198
199         pw.println(" <typeMapping");
200         pw.println(" xmlns:ns=\"" + namespaceURI + "\"");
201         pw.println(" qname=\"ns:" + localPart + '"');
202         pw.println(" type=\"java:" + javaType + '"');
203         pw.println(" serializer=\"" + serializerFactory + "\"");
204         pw.println(" deserializer=\"" + deserializerFactory + "\"");
205         pw.println(" encodingStyle=\"" + encodingStyle + "\"");
206         pw.println(" />");
207     }
208
209     /**
210      * Raw routine that writes out the operation and parameters.
211      *
212      * @param pw PrintWriter
213      * @param javaOperName java method name
214      * @param elementQName wsdl operation qname
215      * @param returnQName wsdl return type qname
216      * @param returnType java return type classname ?
217      * @param params list of params used by this operation
218      * @param faults list of faults thrown by this operation
219      * @param soapAction soapAction value
220      */

221     protected void writeOperation(PrintWriter JavaDoc pw, String JavaDoc javaOperName,
222                                   QName JavaDoc elementQName, QName JavaDoc returnQName,
223                                   QName JavaDoc returnType, Parameters params,
224                                   ArrayList JavaDoc faults, String JavaDoc soapAction) {
225
226         pw.print(" <operation name=\"" + javaOperName + "\"");
227
228         if (elementQName != null) {
229             pw.print(" qname=\""
230                     + Utils.genQNameAttributeString(elementQName, "operNS")
231                     + "\"");
232         }
233
234         if (returnQName != null) {
235             pw.print(" returnQName=\""
236                     + Utils.genQNameAttributeStringWithLastLocalPart(returnQName, "retNS")
237                     + "\"");
238         }
239
240         if (returnType != null) {
241             pw.print(" returnType=\""
242                     + Utils.genQNameAttributeString(returnType, "rtns")
243                     + "\"");
244         }
245
246         Parameter retParam = params.returnParam;
247         if (retParam != null) {
248             TypeEntry type = retParam.getType();
249             QName JavaDoc returnItemQName = Utils.getItemQName(type);
250             if (returnItemQName != null) {
251                 pw.print(" returnItemQName=\"");
252                 pw.print(Utils.genQNameAttributeString(returnItemQName, "tns3"));
253                 pw.print("\"");
254             }
255             QName JavaDoc returnItemType = Utils.getItemType(type);
256             if (returnItemType != null && use == Use.ENCODED) {
257                     pw.print(" returnItemType=\"");
258                     pw.print(Utils.genQNameAttributeString(returnItemType, "tns2"));
259                     pw.print("\"");
260             }
261         }
262
263         if (soapAction != null) {
264             pw.print(" soapAction=\"" + soapAction + "\"");
265         }
266
267         if (!OperationType.REQUEST_RESPONSE.equals(params.mep)) {
268             String JavaDoc mepString = getMepString(params.mep);
269             if (mepString != null) {
270                 pw.print(" mep=\"" + mepString + "\"");
271             }
272         }
273
274         if ((params.returnParam != null) && params.returnParam.isOutHeader()) {
275             pw.print(" returnHeader=\"true\"");
276         }
277
278         pw.println(" >");
279
280         Vector JavaDoc paramList = params.list;
281
282         for (int i = 0; i < paramList.size(); i++) {
283             Parameter param = (Parameter) paramList.elementAt(i);
284
285             // Get the parameter name QName and type QName
286
QName JavaDoc paramQName = param.getQName();
287             QName JavaDoc paramType = Utils.getXSIType(param);
288
289             pw.print(" <parameter");
290
291             if (paramQName == null) {
292                 pw.print(" name=\"" + param.getName() + "\"");
293             } else {
294                 pw.print(" qname=\""
295                         + Utils.genQNameAttributeStringWithLastLocalPart(paramQName, "pns")
296                         + "\"");
297             }
298
299             pw.print(" type=\""
300                     + Utils.genQNameAttributeString(paramType, "ptns") + "\"");
301
302             // Get the parameter mode
303
if (param.getMode() != Parameter.IN) {
304                 pw.print(" mode=\"" + getModeString(param.getMode()) + "\"");
305             }
306
307             // Is this a header?
308
if (param.isInHeader()) {
309                 pw.print(" inHeader=\"true\"");
310             }
311
312             if (param.isOutHeader()) {
313                 pw.print(" outHeader=\"true\"");
314             }
315
316             QName JavaDoc itemQName = Utils.getItemQName(param.getType());
317             if (itemQName != null) {
318                 pw.print(" itemQName=\"");
319                 pw.print(Utils.genQNameAttributeString(itemQName, "pitns"));
320                 pw.print("\"");
321             }
322
323             pw.println("/>");
324         }
325
326         if (faults != null) {
327             for (Iterator JavaDoc iterator = faults.iterator(); iterator.hasNext();) {
328                 FaultInfo faultInfo = (FaultInfo) iterator.next();
329                 QName JavaDoc faultQName = faultInfo.getQName();
330
331                 if (faultQName != null) {
332                     String JavaDoc className =
333                             Utils.getFullExceptionName(faultInfo.getMessage(),
334                                     symbolTable);
335
336                     pw.print(" <fault");
337                     pw.print(" name=\"" + faultInfo.getName() + "\"");
338                     pw.print(" qname=\""
339                             + Utils.genQNameAttributeString(faultQName, "fns")
340                             + "\"");
341                     pw.print(" class=\"" + className + "\"");
342                     pw.print(
343                             " type=\""
344                             + Utils.genQNameAttributeString(
345                                     faultInfo.getXMLType(), "ftns") + "\"");
346                     pw.println("/>");
347                 }
348             }
349         }
350
351         pw.println(" </operation>");
352     }
353
354     /**
355      * Write out bean mappings for each type
356      * @param pw PrintWriter
357      * @param binding wsdl:binding
358      * @param hasLiteral has a literal type ?
359      * @param hasMIME has MIME type ?
360      * @param use Use
361      */

362     protected void writeDeployTypes(PrintWriter JavaDoc pw, Binding binding, boolean hasLiteral, boolean hasMIME, Use use) {
363
364         pw.println();
365
366         if (hasMIME) {
367             QName JavaDoc bQName = binding.getQName();
368
369             writeTypeMapping(pw, bQName.getNamespaceURI(), "DataHandler", "javax.activation.DataHandler",
370                     "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory",
371                     "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory", use.getEncoding());
372         }
373
374         boolean useJAF = false;
375
376         Map JavaDoc types = getSymbolTable().getTypeIndex();
377         Collection JavaDoc typeCollection = types.values();
378         for (Iterator JavaDoc i = typeCollection.iterator(); i.hasNext();) {
379             TypeEntry type = (TypeEntry) i.next();
380
381             // Note this same check is repeated in JavaStubWriter.
382
boolean process = true;
383
384             // 1) Don't register types we shouldn't
385
if (!Utils.shouldEmit(type)) {
386                 process = false;
387             }
388
389             if (process) {
390                 String JavaDoc namespaceURI = type.getQName().getNamespaceURI();
391                 String JavaDoc localPart = type.getQName().getLocalPart();
392                 String JavaDoc javaType = type.getName();
393                 String JavaDoc serializerFactory;
394                 String JavaDoc deserializerFactory;
395                 String JavaDoc encodingStyle = "";
396                 QName JavaDoc innerType = null;
397
398                 if (!hasLiteral) {
399                     encodingStyle = use.getEncoding();
400                 }
401
402                 if (javaType.endsWith("[]")) {
403                     if (SchemaUtils.isListWithItemType(type.getNode())) {
404                         serializerFactory = "org.apache.axis.encoding.ser.SimpleListSerializerFactory";
405                         deserializerFactory = "org.apache.axis.encoding.ser.SimpleListDeserializerFactory";
406                     } else {
407                         serializerFactory = "org.apache.axis.encoding.ser.ArraySerializerFactory";
408                         deserializerFactory = "org.apache.axis.encoding.ser.ArrayDeserializerFactory";
409                         innerType = type.getComponentType();
410                     }
411                 } else if ((type.getNode() != null)
412                         && (Utils.getEnumerationBaseAndValues(type.getNode(), getSymbolTable()) != null)) {
413                     serializerFactory = "org.apache.axis.encoding.ser.EnumSerializerFactory";
414                     deserializerFactory = "org.apache.axis.encoding.ser.EnumDeserializerFactory";
415                 } else if (type.isSimpleType()) {
416                     serializerFactory = "org.apache.axis.encoding.ser.SimpleSerializerFactory";
417                     deserializerFactory = "org.apache.axis.encoding.ser.SimpleDeserializerFactory";
418                 } else if (type.getBaseType() != null) {
419                     serializerFactory = "org.apache.axis.encoding.ser.SimpleSerializerFactory";
420                     deserializerFactory = "org.apache.axis.encoding.ser.SimpleDeserializerFactory";
421                 } else {
422                     serializerFactory = "org.apache.axis.encoding.ser.BeanSerializerFactory";
423                     deserializerFactory = "org.apache.axis.encoding.ser.BeanDeserializerFactory";
424                 }
425
426                 List JavaDoc jafType = new Vector JavaDoc();
427                 jafType.add("java.awt.Image");
428                 jafType.add("javax.xml.transform.Source");
429                 jafType.add("javax.mail.internet.MimeMultipart");
430                 if (jafType.contains(javaType)) {
431                     serializerFactory = "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory";
432                     deserializerFactory = "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory";
433                     useJAF = true;
434                 }
435
436                 if (innerType == null) {
437                     // no arrays
438
writeTypeMapping(pw, namespaceURI, localPart, javaType,
439                         serializerFactory, deserializerFactory,
440                         encodingStyle);
441                 } else {
442                     // arrays
443
writeArrayTypeMapping(pw, namespaceURI, localPart, javaType,
444                             encodingStyle, innerType);
445                 }
446             }
447         }
448
449         if (useJAF) {
450             // mime don't work with ref
451
pw.println(" <parameter name=\"sendMultiRefs\" value=\"false\"/>");
452         }
453     }
454
455
456     /**
457      * Raw routine that writes out the arrayMapping.
458      * @param pw PrintWriter
459      * @param namespaceURI xml type namespace
460      * @param localPart xml type localpart
461      * @param javaType java classname
462      * @param encodingStyle encoding style
463      */

464     protected void writeArrayTypeMapping(
465             PrintWriter JavaDoc pw, String JavaDoc namespaceURI, String JavaDoc localPart, String JavaDoc javaType, String JavaDoc encodingStyle, QName JavaDoc innerType) {
466
467         pw.println(" <arrayMapping");
468         pw.println(" xmlns:ns=\"" + namespaceURI + "\"");
469         pw.println(" qname=\"ns:" + localPart + '"');
470         pw.println(" type=\"java:" + javaType + '"');
471         pw.println(" innerType=\"" + Utils.genQNameAttributeString(innerType, "cmp-ns") + '"');
472         pw.println(" encodingStyle=\"" + encodingStyle + "\"");
473         pw.println(" />");
474     }
475
476     /**
477      * Method getModeString
478      *
479      * @param mode Parameter mode (IN, INOUT, OUT)
480      * @return Parameter Mode String representation
481      */

482     public String JavaDoc getModeString(byte mode) {
483
484         if (mode == Parameter.IN) {
485             return "IN";
486         } else if (mode == Parameter.INOUT) {
487             return "INOUT";
488         } else {
489             return "OUT";
490         }
491     }
492
493     /**
494      * Method getPrintWriter
495      * @param filename file to open
496      * @return Returns the printWriter for the file
497      * @throws IOException When File cannot be open/written
498      */

499     protected PrintWriter JavaDoc getPrintWriter(String JavaDoc filename) throws IOException JavaDoc {
500
501         File JavaDoc file = new File JavaDoc(filename);
502         File JavaDoc parent = new File JavaDoc(file.getParent());
503
504         parent.mkdirs();
505
506         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
507         OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(out, "UTF-8");
508
509         return new PrintWriter JavaDoc(writer);
510     }
511
512     /**
513      * @return Returns the count.
514      */

515     public static int getCount() {
516         return count++;
517     }
518
519     /**
520      * @return Returns the definition.
521      */

522     public Definition getDefinition() {
523         return definition;
524     }
525
526     /**
527      * @return Returns the jonasWSContext.
528      */

529     public JOnASJ2EEWebServicesContext getJonasWSContext() {
530         return jonasWSContext;
531     }
532
533     /**
534      * @return Returns the symbolTable.
535      */

536     public SymbolTable getSymbolTable() {
537         return symbolTable;
538     }
539
540     /**
541      * Store operation type String representation
542      */

543     private static Map JavaDoc mepStrings = new HashMap JavaDoc();
544     static {
545         mepStrings.put(OperationType.REQUEST_RESPONSE, "request-response");
546         mepStrings.put(OperationType.ONE_WAY, "oneway");
547     }
548
549     /**
550      * @param mep OperationType
551      * @return Returns the String representation of this type
552      */

553     private String JavaDoc getMepString(OperationType mep) {
554         return (String JavaDoc) mepStrings.get(mep);
555     }
556 }
557
Popular Tags