summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/builder/ExternalWebServiceBuilder.java
blob: 9b428f8be85b8ff15739f0727f90ab7ad0d81c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.tuscany.binding.axis2.builder;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.wsdl.Definition;
import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContextConstants;
import org.apache.axis2.description.AxisService;
import org.apache.tuscany.binding.axis2.assembly.WebServiceBinding;
import org.apache.tuscany.binding.axis2.config.WSExternalServiceContextFactory;
import org.apache.tuscany.binding.axis2.externalservice.Axis2OperationInvoker;
import org.apache.tuscany.binding.axis2.externalservice.Axis2ServiceInvoker;
import org.apache.tuscany.binding.axis2.util.SDODataBinding;
import org.apache.tuscany.binding.axis2.util.TuscanyAxisConfigurator;
import org.apache.tuscany.binding.axis2.util.WebServiceOperationMetaData;
import org.apache.tuscany.binding.axis2.util.WebServicePortMetaData;
import org.apache.tuscany.core.builder.BuilderConfigException;
import org.apache.tuscany.core.extension.ExternalServiceBuilderSupport;
import org.apache.tuscany.core.extension.ExternalServiceContextFactory;
import org.apache.tuscany.core.injection.SingletonObjectFactory;
import org.apache.tuscany.model.assembly.ExternalService;
import org.osoa.sca.annotations.Scope;

import commonj.sdo.helper.TypeHelper;

/**
 * Creates a <code>ContextFactory</code> for an external service configured with the {@link WebServiceBinding}
 */
@Scope("MODULE")
public class ExternalWebServiceBuilder extends ExternalServiceBuilderSupport<WebServiceBinding> {

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.tuscany.core.extension.ExternalServiceBuilderSupport#createExternalServiceContextFactory(org.apache.tuscany.model.assembly.ExternalService)
     */
    @Override
    protected ExternalServiceContextFactory createExternalServiceContextFactory(ExternalService externalService) {

        WebServiceBinding wsBinding = (WebServiceBinding) externalService.getBindings().get(0);
        Definition wsdlDefinition = wsBinding.getWSDLDefinition();
        WebServicePortMetaData wsPortMetaData = new WebServicePortMetaData(wsdlDefinition, wsBinding.getWSDLPort(), wsBinding.getURI(), false);

        ServiceClient serviceClient = createServiceClient(externalService.getName(), wsdlDefinition, wsPortMetaData);

        TypeHelper typeHelper = wsBinding.getTypeHelper();
        ClassLoader cl = wsBinding.getResourceLoader().getClassLoader();
        Class serviceInterface = externalService.getConfiguredService().getPort().getServiceContract().getInterface();
        Map<String, Axis2OperationInvoker> invokers = createOperationInvokers(serviceInterface, typeHelper, cl, wsPortMetaData);

        Axis2ServiceInvoker axis2Client = new Axis2ServiceInvoker(serviceClient, invokers);

        return new WSExternalServiceContextFactory(externalService.getName(), new SingletonObjectFactory<Axis2ServiceInvoker>(axis2Client));

    }

    /**
     * Create an Axis2 ServiceClient configured for the externalService
     */
    protected ServiceClient createServiceClient(String externalServiceName, Definition wsdlDefinition, WebServicePortMetaData wsPortMetaData) {

        TuscanyAxisConfigurator tuscanyAxisConfigurator = new TuscanyAxisConfigurator(null, null);
        ConfigurationContext configurationContext = tuscanyAxisConfigurator.getConfigurationContext();

        QName serviceQName = wsPortMetaData.getServiceName();
        String portName = wsPortMetaData.getPortName().getLocalPart();

        ServiceClient serviceClient;
        try {

            AxisService axisService = AxisService.createClientSideAxisService(wsdlDefinition, serviceQName, portName, new Options());
            serviceClient = new ServiceClient(configurationContext, axisService);

        } catch (AxisFault e) {
            BuilderConfigException bce = new BuilderConfigException("AxisFault creating external service", e);
            bce.addContextName(externalServiceName);
            throw bce;
        }

        return serviceClient;
    }

    /**
     * Create and configure an Axis2OperationInvoker for each operation in the externalService
     */
    protected Map<String, Axis2OperationInvoker> createOperationInvokers(Class sc, TypeHelper typeHelper, ClassLoader cl, WebServicePortMetaData wsPortMetaData) {
        SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
        String portTypeNS = wsPortMetaData.getPortTypeName().getNamespaceURI();
        Map<String, Axis2OperationInvoker> invokers = new HashMap<String, Axis2OperationInvoker>();

        for (Method m : sc.getMethods()) {
            String methodName = m.getName();

            WebServiceOperationMetaData operationMetaData = wsPortMetaData.getOperationMetaData(methodName);
            boolean isWrapped = operationMetaData.isDocLitWrapped();
            List<?> sig = operationMetaData.getOperationSignature();
            SDODataBinding dataBinding = new SDODataBinding(cl, typeHelper, sig.size() > 0 ? (QName) sig.get(0) : null, isWrapped);

            Options options = new Options();
            options.setTo(new EndpointReference(wsPortMetaData.getEndpoint()));
            options.setProperty(MessageContextConstants.CHUNKED, Boolean.FALSE);

            String wsdlOperationName = operationMetaData.getBindingOperation().getOperation().getName();

            String soapAction = wsPortMetaData.getOperationMetaData(wsdlOperationName).getSOAPAction();
            if (soapAction != null && soapAction.length() > 1) {
                options.setAction(soapAction);
            }

            QName wsdlOperationQName = new QName(portTypeNS, wsdlOperationName);
            Axis2OperationInvoker invoker = new Axis2OperationInvoker(wsdlOperationQName, options, dataBinding, soapFactory);

            invokers.put(methodName, invoker);
        }

        return invokers;
    }

}