JOnASDeployWriter.java

00001 /*
00002  * Copyright 2001-2004 The Apache Software Foundation.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00040 package org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.writer;
00041 
00042 import java.io.File;
00043 import java.io.FileOutputStream;
00044 import java.io.IOException;
00045 import java.io.OutputStreamWriter;
00046 import java.io.PrintWriter;
00047 import java.util.ArrayList;
00048 import java.util.Collection;
00049 import java.util.HashMap;
00050 import java.util.Iterator;
00051 import java.util.List;
00052 import java.util.Map;
00053 import java.util.Vector;
00054 
00055 import javax.wsdl.Binding;
00056 import javax.wsdl.Definition;
00057 import javax.wsdl.OperationType;
00058 import javax.xml.namespace.QName;
00059 
00060 import org.apache.axis.deployment.wsdd.WSDDConstants;
00061 import org.apache.axis.enum.Use;
00062 import org.apache.axis.utils.Messages;
00063 import org.apache.axis.wsdl.symbolTable.CollectionTE;
00064 import org.apache.axis.wsdl.symbolTable.Element;
00065 import org.apache.axis.wsdl.symbolTable.FaultInfo;
00066 import org.apache.axis.wsdl.symbolTable.Parameter;
00067 import org.apache.axis.wsdl.symbolTable.Parameters;
00068 import org.apache.axis.wsdl.symbolTable.SchemaUtils;
00069 import org.apache.axis.wsdl.symbolTable.SymbolTable;
00070 import org.apache.axis.wsdl.symbolTable.TypeEntry;
00071 import org.apache.axis.wsdl.toJava.Emitter;
00072 import org.apache.axis.wsdl.toJava.JavaWriter;
00073 import org.apache.axis.wsdl.toJava.Utils;
00074 
00075 import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.JOnASJ2EEWebServicesContext;
00076 import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.emitter.JOnASWSEmitter;
00077 
00078 
00085 public abstract class JOnASDeployWriter extends JavaWriter {
00086 
00090     private JOnASJ2EEWebServicesContext jonasWSContext;
00091 
00095     protected static final String WSDD_SUFFIX = ".wsdd";
00096 
00100     private static int count = 0;
00101 
00105     private Definition definition = null;
00106 
00110     private SymbolTable symbolTable = null;
00111 
00118     public JOnASDeployWriter(Emitter emitter, Definition definition, SymbolTable symbolTable) {
00119 
00120         super(emitter, "deploy");
00121         this.definition = definition;
00122         this.symbolTable = symbolTable;
00123         if (emitter instanceof JOnASWSEmitter) {
00124             this.jonasWSContext = ((JOnASWSEmitter) emitter).getJOnASWsContext();
00125         }
00126         if (jonasWSContext == null) {
00127             throw new RuntimeException("jonasWSContext can not be null");
00128         }
00129     }
00130 
00134     protected String getFileName() {
00135         // put directly in "output" directory
00136         String dir = emitter.getNamespaces().getAsDir("");
00137         return dir + getPrefix() + getCount() + WSDD_SUFFIX;
00138     }
00139 
00146     protected void writeFileHeader(PrintWriter pw) throws IOException {
00147 
00148         pw.println(Messages.getMessage("deploy00"));
00149         pw.println(Messages.getMessage("deploy02"));
00150         pw.println(Messages.getMessage("deploy03"));
00151         pw.println(Messages.getMessage("deploy05"));
00152         pw.println(Messages.getMessage("deploy06"));
00153         pw.println(Messages.getMessage("deploy07"));
00154         pw.println(Messages.getMessage("deploy09"));
00155         pw.println();
00156         pw.println("<deployment");
00157         pw.println("    xmlns=\"" + WSDDConstants.URI_WSDD + "\"");
00158         pw.println("    xmlns:" + WSDDConstants.NS_PREFIX_WSDD_JAVA + "=\""
00159                 + WSDDConstants.URI_WSDD_JAVA + "\">");
00160     }
00161 
00168     protected void writeFileBody(PrintWriter pw) throws IOException {
00169         writeDeployServices(pw);
00170         pw.println("</deployment>");
00171     }
00172 
00176     protected abstract String getPrefix();
00177 
00183     protected abstract void writeDeployServices(PrintWriter pw) throws IOException;
00184 
00195     protected void writeTypeMapping(PrintWriter pw, String namespaceURI, String localPart, String javaType,
00196             String serializerFactory, String deserializerFactory, String encodingStyle) {
00197 
00198         pw.println("      <typeMapping");
00199         pw.println("        xmlns:ns=\"" + namespaceURI + "\"");
00200         pw.println("        qname=\"ns:" + localPart + '"');
00201         pw.println("        type=\"java:" + javaType + '"');
00202         pw.println("        serializer=\"" + serializerFactory + "\"");
00203         pw.println("        deserializer=\"" + deserializerFactory + "\"");
00204         pw.println("        encodingStyle=\"" + encodingStyle + "\"");
00205         pw.println("      />");
00206     }
00207 
00220     protected void writeOperation(PrintWriter pw, String javaOperName,
00221                                   QName elementQName, QName returnQName,
00222                                   QName returnType, Parameters params,
00223                                   QName bindingQName, ArrayList faults) {
00224 
00225         pw.print("      <operation name=\"" + javaOperName + "\"");
00226 
00227         if (elementQName != null) {
00228             pw.print(" qname=\""
00229                     + Utils.genQNameAttributeString(elementQName, "operNS")
00230                     + "\"");
00231         }
00232 
00233         if (returnQName != null) {
00234             pw.print(" returnQName=\""
00235                     + Utils.genQNameAttributeStringWithLastLocalPart(returnQName, "retNS")
00236                     + "\"");
00237         }
00238 
00239         if (returnType != null) {
00240             pw.print(" returnType=\""
00241                     + Utils.genQNameAttributeString(returnType, "rtns")
00242                     + "\"");
00243         }
00244         
00245         if (params.mep != null && params.mep != OperationType.REQUEST_RESPONSE) {
00246             String mepString = getMepString(params.mep);
00247             if (mepString != null) {
00248                 pw.print(" mep=\"" + mepString + "\"");
00249             }
00250         }
00251 
00252         if ((params.returnParam != null) && params.returnParam.isOutHeader()) {
00253             pw.print(" returnHeader=\"true\"");
00254         }
00255 
00256         pw.println(" >");
00257 
00258         Vector paramList = params.list;
00259 
00260         for (int i = 0; i < paramList.size(); i++) {
00261             Parameter param = (Parameter) paramList.elementAt(i);
00262 
00263             // Get the parameter name QName and type QName
00264             QName paramQName = param.getQName();
00265             QName paramType = Utils.getXSIType(param);
00266 
00267             pw.print("        <parameter");
00268 
00269             if ((paramQName == null)
00270                     || "".equals(paramQName.getNamespaceURI())) {
00271                 pw.print(" name=\"" + param.getName() + "\"");
00272             } else {
00273                 pw.print(" qname=\""
00274                         + Utils.genQNameAttributeStringWithLastLocalPart(paramQName, "pns")
00275                         + "\"");
00276             }
00277 
00278             pw.print(" type=\""
00279                     + Utils.genQNameAttributeString(paramType, "tns") + "\"");
00280 
00281             // Get the parameter mode
00282             if (param.getMode() != Parameter.IN) {
00283                 pw.print(" mode=\"" + getModeString(param.getMode()) + "\"");
00284             }
00285 
00286             // Is this a header?
00287             if (param.isInHeader()) {
00288                 pw.print(" inHeader=\"true\"");
00289             }
00290 
00291             if (param.isOutHeader()) {
00292                 pw.print(" outHeader=\"true\"");
00293             }
00294 
00295             pw.println("/>");
00296         }
00297 
00298         if (faults != null) {
00299             for (Iterator iterator = faults.iterator(); iterator.hasNext();) {
00300                 FaultInfo faultInfo = (FaultInfo) iterator.next();
00301                 QName faultQName = faultInfo.getQName();
00302 
00303                 if (faultQName != null) {
00304                     String className =
00305                             Utils.getFullExceptionName(faultInfo.getMessage(),
00306                                     symbolTable);
00307 
00308                     pw.print("        <fault");
00309                     pw.print(" name=\"" + faultInfo.getName() + "\"");
00310                     pw.print(" qname=\""
00311                             + Utils.genQNameAttributeString(faultQName, "fns")
00312                             + "\"");
00313                     pw.print(" class=\"" + className + "\"");
00314                     pw.print(
00315                             " type=\""
00316                             + Utils.genQNameAttributeString(
00317                                     faultInfo.getXMLType(), "tns") + "\"");
00318                     pw.println("/>");
00319                 }
00320             }
00321         }
00322 
00323         pw.println("      </operation>");
00324     }
00325 
00334     protected void writeDeployTypes(PrintWriter pw, Binding binding, boolean hasLiteral, boolean hasMIME, Use use) {
00335 
00336         pw.println();
00337 
00338         if (hasMIME) {
00339             QName bQName = binding.getQName();
00340 
00341             writeTypeMapping(pw, bQName.getNamespaceURI(), "DataHandler", "javax.activation.DataHandler",
00342                     "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory",
00343                     "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory", use.getEncoding());
00344         }
00345 
00346         boolean useJAF = false;
00347 
00348         Map types = getSymbolTable().getTypeIndex();
00349         Collection typeCollection = types.values();
00350         for (Iterator i = typeCollection.iterator(); i.hasNext();) {
00351             TypeEntry type = (TypeEntry) i.next();
00352 
00353             // Note this same check is repeated in JavaStubWriter.
00354             boolean process = true;
00355 
00356             // 1) Don't register types that are base (primitive) types.
00357             // If the baseType != null && getRefType() != null this
00358             // is a simpleType that must be registered.
00359             // 2) Don't register the special types for collections
00360             // (indexed properties) or element types
00361             // 3) Don't register types that are not referenced
00362             // or only referenced in a literal context.
00363             if (((type.getBaseType() != null) && (type.getRefType() == null)) || (type instanceof CollectionTE)
00364                     || (type instanceof Element) || !type.isReferenced() || type.isOnlyLiteralReferenced()) {
00365                 process = false;
00366             }
00367 
00368             if (process) {
00369                 String namespaceURI = type.getQName().getNamespaceURI();
00370                 String localPart = type.getQName().getLocalPart();
00371                 String javaType = type.getName();
00372                 String serializerFactory;
00373                 String deserializerFactory;
00374                 String encodingStyle = "";
00375 
00376                 if (!hasLiteral) {
00377                     encodingStyle = use.getEncoding();
00378                 }
00379 
00380                 if (javaType.endsWith("[]")) {
00381                     if (SchemaUtils.isListWithItemType(type.getNode())) {
00382                         serializerFactory = "org.apache.axis.encoding.ser.SimpleListSerializerFactory";
00383                         deserializerFactory = "org.apache.axis.encoding.ser.SimpleListDeserializerFactory";
00384                     } else {
00385                         serializerFactory = "org.apache.axis.encoding.ser.ArraySerializerFactory";
00386                         deserializerFactory = "org.apache.axis.encoding.ser.ArrayDeserializerFactory";
00387                     }
00388                 } else if ((type.getNode() != null)
00389                         && (Utils.getEnumerationBaseAndValues(type.getNode(), getSymbolTable()) != null)) {
00390                     serializerFactory = "org.apache.axis.encoding.ser.EnumSerializerFactory";
00391                     deserializerFactory = "org.apache.axis.encoding.ser.EnumDeserializerFactory";
00392                 } else if (type.isSimpleType()) {
00393                     serializerFactory = "org.apache.axis.encoding.ser.SimpleSerializerFactory";
00394                     deserializerFactory = "org.apache.axis.encoding.ser.SimpleDeserializerFactory";
00395                 } else if (type.getBaseType() != null) {
00396                     serializerFactory = "org.apache.axis.encoding.ser.SimpleSerializerFactory";
00397                     deserializerFactory = "org.apache.axis.encoding.ser.SimpleDeserializerFactory";
00398                 } else {
00399                     serializerFactory = "org.apache.axis.encoding.ser.BeanSerializerFactory";
00400                     deserializerFactory = "org.apache.axis.encoding.ser.BeanDeserializerFactory";
00401                 }
00402 
00403                 List jafType = new Vector();
00404                 jafType.add("java.awt.Image");
00405                 //jafType.add("java.lang.String");
00406                 jafType.add("javax.xml.transform.Source");
00407                 jafType.add("javax.mail.internet.MimeMultipart");
00408                 if (jafType.contains(javaType)) {
00409                     serializerFactory = "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory";
00410                     deserializerFactory = "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory";
00411                     useJAF = true;
00412                 }
00413 
00414                 writeTypeMapping(pw, namespaceURI, localPart, javaType, serializerFactory, deserializerFactory,
00415                         encodingStyle);
00416             }
00417         }
00418 
00419         if (useJAF) {
00420             // mime don't work with ref
00421             pw.println("      <parameter name=\"sendMultiRefs\" value=\"false\"/>");
00422         }
00423     }
00424 
00431     public String getModeString(byte mode) {
00432 
00433         if (mode == Parameter.IN) {
00434             return "IN";
00435         } else if (mode == Parameter.INOUT) {
00436             return "INOUT";
00437         } else {
00438             return "OUT";
00439         }
00440     }
00441 
00448     protected PrintWriter getPrintWriter(String filename) throws IOException {
00449 
00450         File file = new File(filename);
00451         File parent = new File(file.getParent());
00452 
00453         parent.mkdirs();
00454 
00455         FileOutputStream out = new FileOutputStream(file);
00456         OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
00457 
00458         return new PrintWriter(writer);
00459     }
00460 
00464     public static int getCount() {
00465         return count++;
00466     }
00467 
00471     public Definition getDefinition() {
00472         return definition;
00473     }
00474 
00478     public JOnASJ2EEWebServicesContext getJonasWSContext() {
00479         return jonasWSContext;
00480     }
00481 
00485     public SymbolTable getSymbolTable() {
00486         return symbolTable;
00487     }
00488 
00489     public static Map mepStrings = new HashMap();
00490     static {
00491         mepStrings.put(OperationType.REQUEST_RESPONSE, "request-response");
00492         mepStrings.put(OperationType.ONE_WAY, "oneway");
00493     }
00494 
00495     String getMepString(OperationType mep) {
00496         return (String)mepStrings.get(mep);
00497     }
00498 }

Generated on Tue Feb 15 15:06:02 2005 for JOnAS by  doxygen 1.3.9.1