KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > toJava > JavaDeployWriter


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.toJava;
56
57 import org.jboss.axis.Constants;
58 import org.jboss.axis.deployment.wsdd.WSDDConstants;
59 import org.jboss.axis.enums.Scope;
60 import org.jboss.axis.enums.Style;
61 import org.jboss.axis.enums.Use;
62 import org.jboss.axis.utils.JavaUtils;
63 import org.jboss.axis.utils.Messages;
64 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
65 import org.jboss.axis.wsdl.symbolTable.CollectionTE;
66 import org.jboss.axis.wsdl.symbolTable.Element;
67 import org.jboss.axis.wsdl.symbolTable.FaultInfo;
68 import org.jboss.axis.wsdl.symbolTable.Parameter;
69 import org.jboss.axis.wsdl.symbolTable.Parameters;
70 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
71 import org.jboss.axis.wsdl.symbolTable.TypeEntry;
72
73 import javax.wsdl.Binding;
74 import javax.wsdl.BindingOperation;
75 import javax.wsdl.Definition;
76 import javax.wsdl.Operation;
77 import javax.wsdl.OperationType;
78 import javax.wsdl.Port;
79 import javax.wsdl.Service;
80 import javax.wsdl.extensions.UnknownExtensibilityElement;
81 import javax.wsdl.extensions.soap.SOAPBinding;
82 import javax.xml.namespace.QName JavaDoc;
83 import java.io.File JavaDoc;
84 import java.io.FileOutputStream JavaDoc;
85 import java.io.IOException JavaDoc;
86 import java.io.OutputStreamWriter JavaDoc;
87 import java.io.PrintWriter JavaDoc;
88 import java.util.ArrayList JavaDoc;
89 import java.util.HashSet JavaDoc;
90 import java.util.Iterator JavaDoc;
91 import java.util.Map JavaDoc;
92 import java.util.Vector JavaDoc;
93
94 /**
95  * This is Wsdl2java's deploy Writer. It writes the deploy.wsdd file.
96  */

97 public class JavaDeployWriter extends JavaWriter
98 {
99    protected Definition definition;
100    protected SymbolTable symbolTable;
101
102    /**
103     * Constructor.
104     */

105    public JavaDeployWriter(Emitter emitter,
106                            Definition definition,
107                            SymbolTable symbolTable)
108    {
109       super(emitter, "deploy");
110       this.definition = definition;
111       this.symbolTable = symbolTable;
112    } // ctor
113

114    /**
115     * Generate deploy.wsdd. Only generate it if the emitter
116     * is generating server-side mappings.
117     */

118    public void generate() throws IOException JavaDoc
119    {
120       if (emitter.isServerSide())
121       {
122          super.generate();
123       }
124    } // generate
125

126    /**
127     * Return the fully-qualified name of the deploy.wsdd file
128     * to be generated.
129     */

130    protected String JavaDoc getFileName()
131    {
132       String JavaDoc dir = emitter.getNamespaces().getAsDir(definition.getTargetNamespace());
133       return dir + "deploy.wsdd";
134    } // getFileName
135

136    /**
137     * Replace the default file header with the deployment doc file header.
138     */

139    protected void writeFileHeader(PrintWriter JavaDoc pw) throws IOException JavaDoc
140    {
141       pw.println(Messages.getMessage("deploy00"));
142       pw.println(Messages.getMessage("deploy02"));
143       pw.println(Messages.getMessage("deploy03"));
144       pw.println(Messages.getMessage("deploy05"));
145       pw.println(Messages.getMessage("deploy06"));
146       pw.println(Messages.getMessage("deploy07"));
147       pw.println(Messages.getMessage("deploy09"));
148       pw.println();
149       pw.println("<deployment");
150       pw.println(" xmlns=\"" + WSDDConstants.URI_WSDD + "\"");
151       pw.println(" xmlns:" + WSDDConstants.NS_PREFIX_WSDD_JAVA + "=\"" +
152               WSDDConstants.URI_WSDD_JAVA + "\">");
153    } // writeFileHeader
154

155    /**
156     * Write the body of the deploy.wsdd file.
157     */

158    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
159    {
160       writeDeployServices(pw);
161       pw.println("</deployment>");
162    } // writeFileBody
163

164    /**
165     * Write out deployment and undeployment instructions for each WSDL service
166     */

167    protected void writeDeployServices(PrintWriter JavaDoc pw) throws IOException JavaDoc
168    {
169       //deploy the ports on each service
170
Map JavaDoc serviceMap = definition.getServices();
171       for (Iterator JavaDoc mapIterator = serviceMap.values().iterator();
172            mapIterator.hasNext();)
173       {
174          Service myService = (Service)mapIterator.next();
175
176          pw.println();
177          pw.println(" <!-- " + Messages.getMessage("wsdlService00", myService.getQName().getLocalPart())
178                  + " -->");
179          pw.println();
180
181          for (Iterator JavaDoc portIterator = myService.getPorts().values().iterator();
182               portIterator.hasNext();)
183          {
184             Port myPort = (Port)portIterator.next();
185             BindingEntry bEntry =
186                     symbolTable.getBindingEntry(myPort.getBinding().getQName());
187
188             // If this isn't an SOAP binding, skip it
189
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP)
190             {
191                continue;
192             }
193             writeDeployPort(pw, myPort, myService, bEntry);
194          }
195       }
196    } //writeDeployServices
197

198    /**
199     * Write out bean mappings for each type
200     */

201    protected void writeDeployTypes(PrintWriter JavaDoc pw,
202                                    Binding binding,
203                                    boolean hasLiteral,
204                                    boolean hasMIME,
205                                    Use use) throws IOException JavaDoc
206    {
207       Vector JavaDoc types = symbolTable.getTypes();
208
209       pw.println();
210
211       if (hasMIME)
212       {
213          QName JavaDoc bQName = binding.getQName();
214          writeTypeMapping(pw, bQName.getNamespaceURI(), "DataHandler",
215                  "javax.activation.DataHandler",
216                  "org.jboss.axis.encoding.ser.JAFDataHandlerSerializerFactory",
217                  "org.jboss.axis.encoding.ser.JAFDataHandlerDeserializerFactory",
218                  use.getEncoding());
219       }
220
221       for (int i = 0; i < types.size(); ++i)
222       {
223          TypeEntry type = (TypeEntry)types.elementAt(i);
224
225          // Note this same check is repeated in JavaStubWriter.
226
boolean process = true;
227
228          // 1) Don't register types that are base (primitive) types.
229
// If the baseType != null && getRefType() != null this
230
// is a simpleType that must be registered.
231
// 2) Don't register the special types for collections
232
// (indexed properties) or element types
233
// 3) Don't register types that are not referenced
234
// or only referenced in a literal context.
235
if ((type.getBaseType() != null && type.getRefType() == null) ||
236                  type instanceof CollectionTE ||
237                  type instanceof Element ||
238                  !type.isReferenced() ||
239                  type.isOnlyLiteralReferenced())
240          {
241             process = false;
242          }
243
244          if (process)
245          {
246             String JavaDoc namespaceURI = type.getQName().getNamespaceURI();
247             String JavaDoc localPart = type.getQName().getLocalPart();
248             String JavaDoc javaType = type.getName();
249             String JavaDoc serializerFactory;
250             String JavaDoc deserializerFactory;
251             String JavaDoc encodingStyle = "";
252             if (!hasLiteral)
253             {
254                encodingStyle = use.getEncoding();
255             }
256
257             if (javaType.endsWith("[]"))
258             {
259                serializerFactory = "org.jboss.axis.encoding.ser.ArraySerializerFactory";
260                deserializerFactory = "org.jboss.axis.encoding.ser.ArrayDeserializerFactory";
261             }
262             else if (type.getNode() != null &&
263                     Utils.getEnumerationBaseAndValues(type.getNode(), symbolTable) != null)
264             {
265                serializerFactory = "org.jboss.axis.encoding.ser.EnumSerializerFactory";
266                deserializerFactory = "org.jboss.axis.encoding.ser.EnumDeserializerFactory";
267             }
268             else if (type.isSimpleType())
269             {
270                serializerFactory = "org.jboss.axis.encoding.ser.SimpleSerializerFactory";
271                deserializerFactory = "org.jboss.axis.encoding.ser.SimpleDeserializerFactory";
272             }
273             else if (type.getBaseType() != null)
274             {
275                serializerFactory = "org.jboss.axis.encoding.ser.SimpleSerializerFactory";
276                deserializerFactory = "org.jboss.axis.encoding.ser.SimpleDeserializerFactory";
277             }
278             else
279             {
280                serializerFactory = "org.jboss.axis.encoding.ser.BeanSerializerFactory";
281                deserializerFactory = "org.jboss.axis.encoding.ser.BeanDeserializerFactory";
282             }
283             writeTypeMapping(pw, namespaceURI, localPart, javaType, serializerFactory,
284                     deserializerFactory, encodingStyle);
285          }
286       }
287    } //writeDeployTypes
288

289    /**
290     * Raw routine that writes out the typeMapping.
291     */

292    protected void writeTypeMapping(PrintWriter JavaDoc pw, String JavaDoc namespaceURI, String JavaDoc localPart, String JavaDoc javaType,
293                                    String JavaDoc serializerFactory, String JavaDoc deserializerFactory,
294                                    String JavaDoc encodingStyle) throws IOException JavaDoc
295    {
296       pw.println(" <typeMapping");
297       pw.println(" xmlns:ns=\"" + namespaceURI + "\"");
298       pw.println(" qname=\"ns:" + localPart + '"');
299       pw.println(" type=\"java:" + javaType + '"');
300       pw.println(" serializer=\"" + serializerFactory + "\"");
301       pw.println(" deserializer=\"" + deserializerFactory + "\"");
302       pw.println(" encodingStyle=\"" + encodingStyle + "\"");
303       pw.println(" />");
304    }
305
306    /**
307     * Write out deployment and undeployment instructions for given WSDL port
308     */

309    protected void writeDeployPort(PrintWriter JavaDoc pw,
310                                   Port port,
311                                   Service service,
312                                   BindingEntry bEntry) throws IOException JavaDoc
313    {
314       String JavaDoc serviceName = port.getName();
315
316       boolean hasLiteral = bEntry.hasLiteral();
317       boolean hasMIME = Utils.hasMIME(bEntry);
318
319       String JavaDoc prefix = WSDDConstants.NS_PREFIX_WSDD_JAVA;
320       String JavaDoc styleStr = "";
321       Use use = Use.DEFAULT;
322
323
324       Iterator JavaDoc iterator = bEntry.getBinding().getExtensibilityElements().iterator();
325       while (iterator.hasNext())
326       {
327          Object JavaDoc obj = iterator.next();
328          if (obj instanceof SOAPBinding)
329          {
330             use = Use.ENCODED;
331          }
332          else if (obj instanceof UnknownExtensibilityElement)
333          {
334             //TODO: After WSDL4J supports soap12, change this code
335
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
336             QName JavaDoc name = unkElement.getElementType();
337             if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
338                     name.getLocalPart().equals("binding"))
339             {
340                use = Use.ENCODED;
341             }
342          }
343       }
344
345       if (symbolTable.isWrapped())
346       {
347          styleStr = " style=\"" + Style.WRAPPED + "\"";
348          use = Use.LITERAL;
349       }
350       else
351       {
352          styleStr = " style=\"" +
353                  bEntry.getBindingStyle().getName() + "\"";
354          if (hasLiteral)
355          {
356             use = Use.LITERAL;
357          }
358       }
359
360       String JavaDoc useStr = " use=\"" + use + "\"";
361
362       pw.println(" <service name=\"" + serviceName
363               + "\" provider=\"" + prefix + ":RPC"
364               + "\"" + styleStr + useStr + ">");
365
366       pw.println(" <parameter name=\"wsdlTargetNamespace\" value=\""
367               + service.getQName().getNamespaceURI() + "\"/>");
368       pw.println(" <parameter name=\"wsdlServiceElement\" value=\""
369               + service.getQName().getLocalPart() + "\"/>");
370       pw.println(" <parameter name=\"wsdlServicePort\" value=\""
371               + serviceName + "\"/>");
372
373       // MIME attachments don't work with multiref, so turn it off.
374
if (hasMIME)
375       {
376          pw.println(" <parameter name=\"sendMultiRefs\" value=\"false\"/>");
377       }
378
379       writeDeployBinding(pw, bEntry);
380       writeDeployTypes(pw, bEntry.getBinding(), hasLiteral, hasMIME, use);
381
382       pw.println(" </service>");
383    } //writeDeployPort
384

385    /**
386     * Write out deployment instructions for given WSDL binding
387     */

388    protected void writeDeployBinding(PrintWriter JavaDoc pw,
389                                      BindingEntry bEntry) throws IOException JavaDoc
390    {
391       Binding binding = bEntry.getBinding();
392       String JavaDoc className = bEntry.getName();
393       if (emitter.isSkeletonWanted())
394          className += "Skeleton";
395       else
396          className += "Impl";
397
398       pw.println(" <parameter name=\"className\" value=\""
399               + className + "\"/>");
400
401       pw.println(" <parameter name=\"wsdlPortType\" value=\""
402               + binding.getPortType().getQName().getLocalPart() + "\"/>");
403
404
405       HashSet JavaDoc allowedMethods = new HashSet JavaDoc();
406       if (!emitter.isSkeletonWanted())
407       {
408          Iterator JavaDoc operationsIterator = binding.getBindingOperations().iterator();
409          for (; operationsIterator.hasNext();)
410          {
411             BindingOperation bindingOper = (BindingOperation)operationsIterator.next();
412             Operation operation = bindingOper.getOperation();
413             OperationType type = operation.getStyle();
414             String JavaDoc javaOperName = JavaUtils.xmlNameToJava(operation.getName());
415
416             // These operation types are not supported. The signature
417
// will be a string stating that fact.
418
if (type == OperationType.NOTIFICATION
419                     || type == OperationType.SOLICIT_RESPONSE)
420             {
421                continue;
422             }
423
424             allowedMethods.add(javaOperName);
425
426             // We pass "" as the namespace argument because we're just
427
// interested in the return type for now.
428
Parameters params =
429                     symbolTable.getOperationParameters(operation, "", bEntry);
430             if (params != null)
431             {
432
433                // Get the operation QName
434
QName JavaDoc elementQName =
435                        Utils.getOperationQName(bindingOper, bEntry, symbolTable);
436
437                // Get the operation's return QName and type
438
QName JavaDoc returnQName = null;
439                QName JavaDoc returnType = null;
440                if (params.returnParam != null)
441                {
442                   returnQName = params.returnParam.getQName();
443                   returnType = Utils.getXSIType(params.returnParam);
444                }
445
446                // Get the operations faults
447
Map JavaDoc faultMap = bEntry.getFaults();
448                ArrayList JavaDoc faults = null;
449                if (faultMap != null)
450                {
451                   faults = (ArrayList JavaDoc)faultMap.get(bindingOper);
452                }
453                // Write the operation metadata
454
writeOperation(pw, javaOperName, elementQName,
455                        returnQName, returnType,
456                        params, binding.getQName(), faults);
457             }
458          }
459       }
460
461       pw.print(" <parameter name=\"allowedMethods\" value=\"");
462       if (allowedMethods.isEmpty())
463       {
464          pw.println("*\"/>");
465       }
466       else
467       {
468          boolean first = true;
469          for (Iterator JavaDoc i = allowedMethods.iterator(); i.hasNext();)
470          {
471             String JavaDoc method = (String JavaDoc)i.next();
472             if (first)
473             {
474                pw.print(method);
475                first = false;
476             }
477             else
478             {
479                pw.print(" " + method);
480             }
481          }
482          pw.println("\"/>");
483       }
484
485       Scope scope = emitter.getScope();
486       if (scope != null)
487          pw.println(" <parameter name=\"scope\" value=\"" + scope.getName() + "\"/>");
488    } //writeDeployBinding
489

490    /**
491     * Raw routine that writes out the operation and parameters.
492     */

493    protected void writeOperation(PrintWriter JavaDoc pw,
494                                  String JavaDoc javaOperName,
495                                  QName JavaDoc elementQName,
496                                  QName JavaDoc returnQName,
497                                  QName JavaDoc returnType,
498                                  Parameters params,
499                                  QName JavaDoc bindingQName,
500                                  ArrayList JavaDoc faults)
501    {
502       pw.print(" <operation name=\"" + javaOperName + "\"");
503       if (elementQName != null)
504       {
505          pw.print(" qname=\"" +
506                  Utils.genQNameAttributeString(elementQName, "operNS") +
507                  "\"");
508       }
509       if (returnQName != null)
510       {
511          pw.print(" returnQName=\"" +
512                  Utils.genQNameAttributeString(returnQName, "retNS") +
513                  "\"");
514       }
515       if (returnType != null)
516       {
517          pw.print(" returnType=\"" +
518                  Utils.genQNameAttributeString(returnType, "rtns") +
519                  "\"");
520       }
521       if (params.returnParam != null && params.returnParam.isOutHeader())
522       {
523          pw.print(" returnHeader=\"true\"");
524       }
525       pw.println(" >");
526
527       Vector JavaDoc paramList = params.list;
528       for (int i = 0; i < paramList.size(); i++)
529       {
530          Parameter param = (Parameter)paramList.elementAt(i);
531
532          // Get the parameter name QName and type QName
533
QName JavaDoc paramQName = param.getQName();
534          QName JavaDoc paramType = Utils.getXSIType(param);
535
536          pw.print(" <parameter");
537          if (paramQName == null || "".equals(paramQName.getNamespaceURI()))
538          {
539             pw.print(" name=\"" + param.getName() + "\"");
540          }
541          else
542          {
543             pw.print(" qname=\"" +
544                     Utils.genQNameAttributeString(paramQName,
545                             "pns") + "\"");
546          }
547
548          pw.print(" type=\"" +
549                  Utils.genQNameAttributeString(paramType,
550                          "tns") + "\"");
551          // Get the parameter mode
552
if (param.getMode() != Parameter.IN)
553          {
554             pw.print(" mode=\"" + getModeString(param.getMode()) + "\"");
555          }
556
557          // Is this a header?
558
if (param.isInHeader())
559          {
560             pw.print(" inHeader=\"true\"");
561          }
562          if (param.isOutHeader())
563          {
564             pw.print(" outHeader=\"true\"");
565          }
566
567          pw.println("/>");
568       }
569       if (faults != null)
570       {
571          for (Iterator JavaDoc iterator = faults.iterator(); iterator.hasNext();)
572          {
573             FaultInfo faultInfo = (FaultInfo)iterator.next();
574             QName JavaDoc faultQName = faultInfo.getQName();
575             if (faultQName != null)
576             {
577                String JavaDoc className = Utils.getFullExceptionName(faultInfo.getMessage(), symbolTable);
578                pw.print(" <fault");
579                pw.print(" name=\"" + faultInfo.getName() + "\"");
580                pw.print(" qname=\"" +
581                        Utils.genQNameAttributeString(faultQName, "fns") + "\"");
582                pw.print(" class=\"" + className + "\"");
583                pw.print(" type=\"" +
584                        Utils.genQNameAttributeString(faultInfo.getXMLType(), "tns") + "\"");
585                pw.println("/>");
586             }
587          }
588       }
589
590       pw.println(" </operation>");
591    }
592
593    public String JavaDoc getModeString(byte mode)
594    {
595       if (mode == Parameter.IN)
596       {
597          return "IN";
598       }
599       else if (mode == Parameter.INOUT)
600       {
601          return "INOUT";
602       }
603       else
604       {
605          return "OUT";
606       }
607    }
608
609    protected PrintWriter JavaDoc getPrintWriter(String JavaDoc filename) throws IOException JavaDoc
610    {
611       File JavaDoc file = new File JavaDoc(filename);
612       File JavaDoc parent = new File JavaDoc(file.getParent());
613       parent.mkdirs();
614       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
615       OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(out, "UTF-8");
616       return new PrintWriter JavaDoc(writer);
617    }
618 } // class JavaDeployWriter
619
Popular Tags