servicebuilder.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 833 行 · 第 1/3 页

JAVA
833
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.axis2.deployment;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.dataretrieval.DRConstants;
import org.apache.axis2.deployment.util.PhasesInfo;
import org.apache.axis2.deployment.util.Utils;
import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisOperationFactory;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.InOutAxisOperation;
import org.apache.axis2.description.ModuleConfiguration;
import org.apache.axis2.description.ParameterInclude;
import org.apache.axis2.description.PolicyInclude;
import org.apache.axis2.description.WSDL2Constants;
import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
import org.apache.axis2.description.java2wsdl.TypeTable;
import org.apache.axis2.engine.MessageReceiver;
import org.apache.axis2.engine.ObjectSupplier;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.apache.axis2.i18n.Messages;
import org.apache.axis2.util.Loader;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

/**
 * Builds a service description from OM
 */
public class ServiceBuilder extends DescriptionBuilder {
    private static final Log log = LogFactory.getLog(ServiceBuilder.class);
    private AxisService service;
    private HashMap wsdlServiceMap = new HashMap();

    public ServiceBuilder(ConfigurationContext configCtx, AxisService service) {
        this.service = service;
        this.configCtx = configCtx;
        this.axisConfig = this.configCtx.getAxisConfiguration();
    }

    public ServiceBuilder(InputStream serviceInputStream, ConfigurationContext configCtx,
                          AxisService service) {
        super(serviceInputStream, configCtx);
        this.service = service;
    }

    /**
     * Populates service from corresponding OM.
     *
     * @param service_element an OMElement for the <service> tag
     * @return a filled-in AxisService, configured from the passed XML
     * @throws DeploymentException if there is a problem
     */
    public AxisService populateService(OMElement service_element) throws DeploymentException {
        try {
            // Determine whether service should be activated.
            String serviceActivate = service_element.getAttributeValue(new QName(ATTRIBUTE_ACTIVATE));
            if (serviceActivate != null) {
                if ("true".equals(serviceActivate)) {
                    service.setActive(true);
                } else if ("false".equals(serviceActivate)) {
                    service.setActive(false);
                }
            }
            
            // Processing service level parameters
             OMAttribute serviceNameatt = service_element.getAttribute(new QName(ATTRIBUTE_NAME));

            // If the service name is explicitly specified in the services.xml
            // then use that as the service name
            if (serviceNameatt != null) {
                if (!"".equals(serviceNameatt.getAttributeValue().trim())) {
                    AxisService wsdlService = (AxisService)wsdlServiceMap.get(
                            serviceNameatt.getAttributeValue());
                    if (wsdlService != null) {
                        wsdlService.setClassLoader(service.getClassLoader());
                        wsdlService.setParent(service.getAxisServiceGroup());
                        service = wsdlService;
                        service.setWsdlFound(true);
                        service.setCustomWsdl(true);
                    }
                    service.setName(serviceNameatt.getAttributeValue());
                    //To be on the safe side
                    if (service.getDocumentation() == null) {
                        service.setDocumentation(serviceNameatt.getAttributeValue());
                    }
                }
            }


            Iterator itr = service_element.getChildrenWithName(new QName(TAG_PARAMETER));
            processParameters(itr, service, service.getParent());

            // process service description
            OMElement descriptionElement =
                    service_element.getFirstChildWithName(new QName(TAG_DESCRIPTION));
            if (descriptionElement != null) {
                OMElement descriptionValue = descriptionElement.getFirstElement();
                if (descriptionValue != null) {
                    StringWriter writer = new StringWriter();
                    descriptionValue.build();
                    descriptionValue.serialize(writer);
                    writer.flush();
                    service.setDocumentation(writer.toString());
                } else {
                    service.setDocumentation(descriptionElement.getText());
                }
            } else {
                serviceNameatt =
                        service_element.getAttribute(new QName(ATTRIBUTE_NAME));

                if (serviceNameatt != null) {
                    if (!"".equals(serviceNameatt.getAttributeValue().trim()) && service.getDocumentation() == null) {
                        service.setDocumentation(serviceNameatt.getAttributeValue());
                    }
                }
            }

            if (service.getParameter("ServiceClass") == null) {
                log.debug("The Service " + service.getName() + " does not specify a Service Class");
            }

            // Process WS-Addressing flag attribute
            OMAttribute addressingRequiredatt =
                    service_element.getAttribute(new QName(ATTRIBUTE_WSADDRESSING));
            if (addressingRequiredatt != null) {
                String addressingRequiredString = addressingRequiredatt.getAttributeValue();
                service.setWSAddressingFlag(addressingRequiredString);
            }

            //Setting service target namespace if any
            OMAttribute targetNameSpace = service_element.
                    getAttribute(new QName(TARGET_NAME_SPACE));
            if (targetNameSpace != null) {
                String nameSpeceVale = targetNameSpace.getAttributeValue();
                if (nameSpeceVale != null && !"".equals(nameSpeceVale)) {
                    service.setTargetNamespace(nameSpeceVale);
                }
            } else {
                if (service.getTargetNamespace() == null ||
                        "".equals(service.getTargetNamespace())) {
                    service.setTargetNamespace(Java2WSDLConstants.DEFAULT_TARGET_NAMESPACE);
                }
            }

            //Processing service lifecycle attribute
            OMAttribute serviceLifeCycleClass = service_element.
                    getAttribute(new QName(TAG_CLASS_NAME));
            if (serviceLifeCycleClass != null) {
                String className = serviceLifeCycleClass.getAttributeValue();
                loadServiceLifeCycleClass(className);
            }
            //Setting schema namespece if any
            OMElement schemaElement = service_element.getFirstChildWithName(new QName(SCHEMA));
            if (schemaElement != null) {
                OMAttribute schemaNameSpace = schemaElement.
                        getAttribute(new QName(SCHEMA_NAME_SPACE));
                if (schemaNameSpace != null) {
                    String nameSpeceVale = schemaNameSpace.getAttributeValue();
                    if (nameSpeceVale != null && !"".equals(nameSpeceVale)) {
                        service.setSchemaTargetNamespace(nameSpeceVale);
                    }
                }
                OMAttribute elementFormDefault = schemaElement.
                        getAttribute(new QName(SCHEMA_ELEMENT_QUALIFIED));
                if (elementFormDefault != null) {
                    String value = elementFormDefault.getAttributeValue();
                    if ("true".equals(value)) {
                        service.setElementFormDefault(true);
                    } else if ("false".equals(value)) {
                        service.setElementFormDefault(false);
                    }
                }

                //package to namespace mapping. This will be an element that maps pkg names to a namespace
                //when this is doing AxisService.getSchematargetNamespace will be overridden
                //This will be <mapping/>  with @namespace and @package
                Iterator mappingIterator = schemaElement.getChildrenWithName(new QName(MAPPING));
                if (mappingIterator != null) {
                    Map pkg2nsMap = new Hashtable();
                    while (mappingIterator.hasNext()) {
                        OMElement mappingElement = (OMElement) mappingIterator.next();
                        OMAttribute namespaceAttribute =
                                mappingElement.getAttribute(new QName(ATTRIBUTE_NAMESPACE));
                        OMAttribute packageAttribute =
                                mappingElement.getAttribute(new QName(ATTRIBUTE_PACKAGE));
                        if (namespaceAttribute != null && packageAttribute != null) {
                            String namespaceAttributeValue = namespaceAttribute.getAttributeValue();
                            String packageAttributeValue = packageAttribute.getAttributeValue();
                            if (namespaceAttributeValue != null && packageAttributeValue != null) {
                                pkg2nsMap.put(packageAttributeValue.trim(),
                                              namespaceAttributeValue.trim());
                            } else {
                                log.warn(
                                        "Either value of @namespce or @packagename not available. Thus, generated will be selected.");
                            }
                        } else {
                            log.warn(
                                    "Either @namespce or @packagename not available. Thus, generated will be selected.");
                        }
                    }
                    service.setP2nMap(pkg2nsMap);

                }

            }

            //processing Default Message receivers
            OMElement messageReceiver = service_element.getFirstChildWithName(
                    new QName(TAG_MESSAGE_RECEIVERS));
            if (messageReceiver != null) {
                HashMap mrs = processMessageReceivers(service.getClassLoader(), messageReceiver);
                Iterator keys = mrs.keySet().iterator();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    service.addMessageReceiver(key, (MessageReceiver) mrs.get(key));
                }
            }

            //Removing exclude operations
            OMElement excludeOperations = service_element.getFirstChildWithName(
                    new QName(TAG_EXCLUDE_OPERATIONS));
            ArrayList excludeops = null;
            if (excludeOperations != null) {
                excludeops = processExcludeOperations(excludeOperations);
            }
            if (excludeops == null) {
                excludeops = new ArrayList();
            }
            Utils.addExcludeMethods(excludeops);

            //<schema targetNamespace="http://x.y.z"/>
            // setting the PolicyInclude
            // processing <wsp:Policy> .. </..> elements
            Iterator policyElements = service_element.getChildrenWithName(
                    new QName(POLICY_NS_URI, TAG_POLICY));

            if (policyElements != null && policyElements.hasNext()) {
                processPolicyElements(
                        PolicyInclude.AXIS_SERVICE_POLICY, policyElements,
                        service.getPolicyInclude());
            }

            // processing <wsp:PolicyReference> .. </..> elements
            Iterator policyRefElements = service_element.getChildrenWithName(

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?