protected void writeFileBody()

in axis-codegen/src/main/java/org/apache/axis/wsdl/toJava/JavaStubWriter.java [155:445]


    protected void writeFileBody(PrintWriter pw) throws IOException {

        PortType portType = binding.getPortType();
        HashSet types = getTypesInPortType(portType);
        boolean hasMIME = Utils.hasMIME(bEntry);

        if ((types.size() > 0) || hasMIME) {
            pw.println(
                    "    private java.util.Vector cachedSerClasses = new java.util.Vector();");
            pw.println(
                    "    private java.util.Vector cachedSerQNames = new java.util.Vector();");
            pw.println(
                    "    private java.util.Vector cachedSerFactories = new java.util.Vector();");
            pw.println(
                    "    private java.util.Vector cachedDeserFactories = new java.util.Vector();");
        }

        pw.println();
        pw.println(
                "    static org.apache.axis.description.OperationDesc [] _operations;");
        pw.println();
        writeOperationMap(pw);
        pw.println();
        pw.println("    public " + className
                + "() throws org.apache.axis.AxisFault {");
        pw.println("         this(null);");
        pw.println("    }");
        pw.println();
        pw.println(
                "    public " + className
                + "(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {");
        pw.println("         this(service);");
        pw.println("         super.cachedEndpoint = endpointURL;");
        pw.println("    }");
        pw.println();
        pw.println(
                "    public " + className
                + "(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {");
        pw.println("        if (service == null) {");
        pw.println(
                "            super.service = new org.apache.axis.client.Service();");
        pw.println("        } else {");
        pw.println("            super.service = service;");
        pw.println("        }");
        pw.println("        ((org.apache.axis.client.Service)super.service).setTypeMappingVersion(\"" + emitter.getTypeMappingVersion() + "\");");

        List deferredBindings = new ArrayList();

        // keep track of how many type mappings we write out
        int typeMappingCount = 0;

        if (types.size() > 0) {
            Iterator it = types.iterator();

            while (it.hasNext()) {
                TypeEntry type = (TypeEntry) it.next();

                if (!Utils.shouldEmit(type)) {
                    continue;
                }

                // Write out serializer declarations
                if (typeMappingCount == 0) {
                    writeSerializationDecls(
                            pw, hasMIME, binding.getQName().getNamespaceURI());
                }

                // write the type mapping for this type
                // writeSerializationInit(pw, type);
                deferredBindings.add(type);

                // increase the number of type mappings count
                typeMappingCount++;
            }
        }

        // Sort the TypeEntry's by their qname.
        Collections.sort(deferredBindings, new Comparator() {
            public int compare(Object a, Object b) {
                TypeEntry type1 = (TypeEntry)a;
                TypeEntry type2 = (TypeEntry)b;
                return type1.getQName().toString().compareToIgnoreCase(type2.getQName().toString());
            }
        });

        // We need to write out the MIME mapping, even if we don't have
        // any type mappings
        if ((typeMappingCount == 0) && hasMIME) {
            writeSerializationDecls(pw, hasMIME,
                    binding.getQName().getNamespaceURI());

            typeMappingCount++;
        }

        // track whether the number of bindings exceeds the threshold
        // that we allow per method.
        boolean needsMultipleBindingMethods = false;

        if (deferredBindings.size() < MAXIMUM_BINDINGS_PER_METHOD) {

            // small number of bindings, just inline them:
            for (Iterator it = deferredBindings.iterator(); it.hasNext();) {
                writeSerializationInit(pw, (TypeEntry) it.next());
            }
        } else {
            needsMultipleBindingMethods = true;

            int methodCount = calculateBindingMethodCount(deferredBindings);

            // invoke each of the soon-to-be generated addBindings methods
            // from the constructor.
            for (int i = 0; i < methodCount; i++) {
                pw.println("        addBindings" + i + "();");
            }
        }

        pw.println("    }");
        pw.println();

        // emit any necessary methods for assembling binding metadata.
        if (needsMultipleBindingMethods) {
            writeBindingMethods(pw, deferredBindings);
            pw.println();
        }

        pw.println(
                "    protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {");
        pw.println("        try {");
        pw.println("            org.apache.axis.client.Call _call = super._createCall();");
        pw.println("            if (super.maintainSessionSet) {");
        pw.println(
                "                _call.setMaintainSession(super.maintainSession);");
        pw.println("            }");
        pw.println("            if (super.cachedUsername != null) {");
        pw.println("                _call.setUsername(super.cachedUsername);");
        pw.println("            }");
        pw.println("            if (super.cachedPassword != null) {");
        pw.println("                _call.setPassword(super.cachedPassword);");
        pw.println("            }");
        pw.println("            if (super.cachedEndpoint != null) {");
        pw.println(
                "                _call.setTargetEndpointAddress(super.cachedEndpoint);");
        pw.println("            }");
        pw.println("            if (super.cachedTimeout != null) {");
        pw.println("                _call.setTimeout(super.cachedTimeout);");
        pw.println("            }");
        pw.println("            if (super.cachedPortName != null) {");
        pw.println("                _call.setPortName(super.cachedPortName);");
        pw.println("            }");
        pw.println(
                "            java.util.Enumeration keys = super.cachedProperties.keys();");
        pw.println("            while (keys.hasMoreElements()) {");
        pw.println(
                "                java.lang.String key = (java.lang.String) keys.nextElement();");
        pw.println(
                "                _call.setProperty(key, super.cachedProperties.get(key));");
        pw.println("            }");

        if (typeMappingCount > 0) {
            pw.println("            // " + Messages.getMessage("typeMap00"));
            pw.println("            // " + Messages.getMessage("typeMap01"));
            pw.println("            // " + Messages.getMessage("typeMap02"));
            pw.println("            // " + Messages.getMessage("typeMap03"));
            pw.println("            // " + Messages.getMessage("typeMap04"));
            pw.println("            synchronized (this) {");
            pw.println("                if (firstCall()) {");

            // Hack alert - we need to establish the encoding style before we register type mappings due
            // to the fact that TypeMappings key off of encoding style
            pw.println("                    // "
                    + Messages.getMessage("mustSetStyle"));

            if (bEntry.hasLiteral()) {
                pw.println("                    _call.setEncodingStyle(null);");
            } else {
                Iterator iterator =
                        bEntry.getBinding().getExtensibilityElements().iterator();

                while (iterator.hasNext()) {
                    Object obj = iterator.next();

                    if (obj instanceof SOAPBinding) {
                        pw.println(
                                "                    _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);");
                        pw.println(
                                "                    _call.setEncodingStyle(org.apache.axis.Constants.URI_SOAP11_ENC);");
                    } else if (obj instanceof SOAP12Binding) {
                        pw.println(
                                "                    _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS);");
                        pw.println(
                                "                    _call.setEncodingStyle(org.apache.axis.Constants.URI_SOAP12_ENC);");
                    }
                }
            }

            pw.println(
                    "                    for (int i = 0; i < cachedSerFactories.size(); ++i) {");
            pw.println(
                    "                        java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i);");
            pw.println(
                    "                        javax.xml.namespace.QName qName =");
            pw.println(
                    "                                (javax.xml.namespace.QName) cachedSerQNames.get(i);");
            pw.println(
                    "                        java.lang.Object x = cachedSerFactories.get(i);");
            pw.println(
                    "                        if (x instanceof Class) {");
            pw.println(
                    "                            java.lang.Class sf = (java.lang.Class)");
            pw.println(
                    "                                 cachedSerFactories.get(i);");
            pw.println(
                    "                            java.lang.Class df = (java.lang.Class)");
            pw.println(
                    "                                 cachedDeserFactories.get(i);");
            pw.println(
                    "                            _call.registerTypeMapping(cls, qName, sf, df, false);");

            pw.println("                        }");
            pw.println(
                    "                        else if (x instanceof javax.xml.rpc.encoding.SerializerFactory) {");
            pw.println(
                    "                            org.apache.axis.encoding.SerializerFactory sf = (org.apache.axis.encoding.SerializerFactory)");
            pw.println(
                    "                                 cachedSerFactories.get(i);");
            pw.println(
                    "                            org.apache.axis.encoding.DeserializerFactory df = (org.apache.axis.encoding.DeserializerFactory)");
            pw.println(
                    "                                 cachedDeserFactories.get(i);");
            pw.println(
                    "                            _call.registerTypeMapping(cls, qName, sf, df, false);");

            pw.println("                        }");
            pw.println("                    }");
            pw.println("                }");
            pw.println("            }");
        }

        pw.println("            return _call;");
        pw.println("        }");
        pw.println("        catch (java.lang.Throwable _t) {");
        pw.println("            throw new org.apache.axis.AxisFault(\""
                + Messages.getMessage("badCall01") + "\", _t);");
        pw.println("        }");
        pw.println("    }");
        pw.println();

        List operations = binding.getBindingOperations();

        for (int i = 0; i < operations.size(); ++i) {
            BindingOperation operation = (BindingOperation) operations.get(i);
            Parameters parameters =
                    bEntry.getParameters(operation.getOperation());

            // Get the soapAction from the <soap:operation>
            String soapAction = "";
            String opStyle = null;
            Iterator operationExtensibilityIterator =
                    operation.getExtensibilityElements().iterator();

            for (; operationExtensibilityIterator.hasNext();) {
                Object obj = operationExtensibilityIterator.next();

                if (obj instanceof SOAPOperation) {
                    soapAction = ((SOAPOperation) obj).getSoapActionURI();
                    opStyle = ((SOAPOperation) obj).getStyle();

                    break;
                } else if (obj instanceof SOAP12Operation) {
                    soapAction = ((SOAP12Operation) obj).getSoapActionURI();
                    opStyle = ((SOAP12Operation) obj).getStyle();

                    break;
                }
            }

            Operation ptOperation = operation.getOperation();
            OperationType type = ptOperation.getStyle();

            // These operation types are not supported.  The signature
            // will be a string stating that fact.
            if ((OperationType.NOTIFICATION.equals(type))
                    || (OperationType.SOLICIT_RESPONSE.equals(type))) {
                pw.println(parameters.signature);
                pw.println();
            } else {
                writeOperation(pw, operation, parameters, soapAction, opStyle,
                        type == OperationType.ONE_WAY, i);
            }
        }
    }    // writeFileBody