summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/modules/binding-ws-runtime-jaxws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/axis2/JAXWSServiceBindingProvider.java
blob: 2aa2cc5db2edda82c72db0284ebdf50f1725b555 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * 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.tuscany.sca.binding.ws.jaxws.axis2;

import java.net.URI;
import java.util.HashMap;
import java.util.List;

import javax.wsdl.Port;
import javax.xml.ws.Binding;
import javax.xml.ws.Endpoint;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.jaxws.binding.BindingUtils;
import org.apache.axis2.jaxws.description.DescriptionFactory;
import org.apache.axis2.jaxws.description.EndpointDescription;
import org.apache.axis2.jaxws.description.ServiceDescription;
import org.apache.axis2.jaxws.description.builder.DescriptionBuilderComposite;
import org.apache.axis2.jaxws.description.builder.WebServiceProviderAnnot;
import org.apache.axis2.jaxws.description.builder.converter.JavaClassToDBCConverter;
import org.apache.axis2.jaxws.description.impl.DescriptionFactoryImpl;
import org.apache.tuscany.sca.binding.ws.WebServiceBinding;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint;
import org.apache.tuscany.sca.host.http.SecurityContext;
import org.apache.tuscany.sca.host.http.ServletHost;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.invocation.MessageFactory;
import org.apache.tuscany.sca.provider.ServiceBindingProvider;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.runtime.RuntimeComponentService;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
import org.apache.tuscany.sca.runtime.RuntimeProperties;
import org.oasisopen.sca.ServiceRuntimeException;

public class JAXWSServiceBindingProvider implements ServiceBindingProvider {
    
    // Tuscany extensions
    private ExtensionPointRegistry extensionPoints;
    private ServletHost servletHost;
    
    // Tuscany WS service data
    private RuntimeComponent component;
    private RuntimeComponentService service;
    private RuntimeEndpoint endpoint;
    private InterfaceContract contract;
    private WebServiceBinding wsBinding;
    private Port wsdlPort;
    private String endpointURI;
    private String deployedURI; 
    protected SecurityContext httpSecurityContext;
    
    // JAXWS fields
    private JAXWSBindingProvider jaxwsBindingProvider;
    private Endpoint wsEndpoint;
   
    // Axis2 fields
    private ConfigurationContext configContext;
    
    public JAXWSServiceBindingProvider(ExtensionPointRegistry extensionPoints,
                                       RuntimeEndpoint endpoint,
                                       ServletHost servletHost) {
    
        this.extensionPoints = extensionPoints;
        this.endpoint = endpoint;
        this.wsBinding = (WebServiceBinding)endpoint.getBinding();
        this.component = (RuntimeComponent)endpoint.getComponent();
        this.contract = wsBinding.getBindingInterfaceContract();
        this.service = (RuntimeComponentService)endpoint.getService();
        this.servletHost = servletHost;
        
        // TODO
        this.httpSecurityContext = new SecurityContext();
        
        // Set to use the Axiom data binding
        contract.getInterface().resetDataBinding(OMElement.class.getName());
        
        // A WSDL document should always be present in the binding
        if (wsBinding.getWSDLDocument() == null) {
            throw new ServiceRuntimeException("No WSDL document for " + component.getName() + "/" + service.getName());
        }
        
        // Update port addresses with runtime information
        // We can safely assume there is only one port here because you configure
        // a binding in the following ways: 
        // 1/ default             - one port generated = host domain : host port / structural path 
        // 2/ uri="absolute addr" - one port generated = host domain : uri port  / uri path
        // 3/ uri="relative addr" - one port generated = host domain : host port / structural path / relative path
        // 4/ wsdl.binding        - one port generated = host domain : host port / structural path 
        // 5/ wsdl.port           - one port generated = host domain : port port / port path
        // 6/ wsa:Address         - one port generated = host domain : address port / address path
        // 7/ 4 + 6               - as 6
        wsdlPort = (Port)wsBinding.getService().getPorts().values().iterator().next();
        
        if (wsdlPort == null){
            throw new ServiceRuntimeException("No WSDL port for ws binding of " + component.getName() + "/" + service.getName());
        }
        
        endpointURI = Axis2EngineIntegration.getPortAddress(wsdlPort);
        
        if (endpointURI.startsWith("jms:")) {
            deployedURI = endpointURI;
            //isJMSRequired = true;
        } else {
            if (servletHost == null) {
                throw new ServiceRuntimeException("No Servlet host is avaible for HTTP web services");
            }
            deployedURI = servletHost.getURLMapping(endpointURI, httpSecurityContext).toString();
        }  
        
        wsBinding.setURI(deployedURI);
        
        FactoryExtensionPoint modelFactories =  extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
        DataBindingExtensionPoint dataBindings = extensionPoints.getExtensionPoint(DataBindingExtensionPoint.class);
        
        // TODO ===
        String defaultPort = "8085";
        RuntimeProperties ps = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class).getUtility(RuntimeProperties.class);
        String pp = ps.getProperties().getProperty(this.getClass().getName() + ".defaultPort");
        if (pp != null) {
            defaultPort = ps.getProperties().getProperty(this.getClass().getName() + ".defaultPort");
        }
        // ========
/*        
        jaxwsBindingProvider = new JAXWSBindingProvider(endpoint, 
                                                        servletHost,
                                                        modelFactories,
                                                        dataBindings,
                                                        defaultPort);
*/                                                        
        jaxwsBindingProvider = new JAXWSBindingProvider();
                
        // get the Axis configuration context
        configContext = Axis2EngineIntegration.getAxisConfigurationContext(extensionPoints.getServiceDiscovery());
        //configContext.setContextRoot(servletHost.getContextPath());
        configContext.setContextRoot("http://localhost:8085");
    }
    
    public void start() {
        // experiment in how to install a JAXWS provider into Axis2 programatically      
        try {
            // create the service description
            JavaClassToDBCConverter converter = new JavaClassToDBCConverter(jaxwsBindingProvider.getClass());
            HashMap<String, DescriptionBuilderComposite> dbcMap = converter.produceDBC();

            // Set the details of the service we are going to expose through this provider
            // TODO - There is something odd in the way that service name is calculated in
            //        some circumstances
            //           sometimes getServiceName() returns null
            //           sometimes getService().getQName returns a QName namespace that doesn't match the WSDL
            //           sometimes getNamespace() returns null
            //        So here we delve directly into the WSDL4J model as the Tuscany model isn't up to date
            String targetNamespace = wsBinding.getWSDLDefinition().getDefinition().getTargetNamespace();
            WebServiceProviderAnnot webServiceProviderAnnot = WebServiceProviderAnnot.createWebServiceAnnotImpl();
            webServiceProviderAnnot.setTargetNamespace(targetNamespace);
            webServiceProviderAnnot.setServiceName(wsBinding.getService().getQName().getLocalPart());
            webServiceProviderAnnot.setPortName(wsBinding.getPort().getName());
            
            // adjust the service description to match the service we're actually exposing
            DescriptionBuilderComposite dbc = dbcMap.values().iterator().next();
            dbc.setWebServiceProviderAnnot(webServiceProviderAnnot);
            dbc.setWsdlDefinition(wsBinding.getService().getQName(), wsBinding.getWSDLDefinition().getDefinition());
            List<ServiceDescription> serviceDescList = DescriptionFactoryImpl.createServiceDescriptionFromDBCMap(dbcMap, configContext);
            ServiceDescription sd = null;
            if (serviceDescList != null && serviceDescList.size() > 0) {
                sd = serviceDescList.get(0);
            }
            
            // get the first endpoint
            EndpointDescription endpointDesc = sd.getEndpointDescriptions_AsCollection().iterator().next();           
            Binding binding = BindingUtils.createBinding(endpointDesc);
            
            // Add the service into the configuration context
            AxisService axisService = endpointDesc.getAxisService();
            String path = URI.create(endpointURI).getPath();
            axisService.setName(path);
            axisService.setServiceDescription("Tuscany configured AxisService for service: " + deployedURI);
            axisService.setClientSide(false);  
            configContext.getAxisConfiguration().addService(axisService);
            
            // fire up the Axis servlet
            Axis2ServiceServlet servlet = new Axis2ServiceServlet();
            servlet.init(configContext);
            servletHost.addServletMapping(deployedURI, servlet);
        
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
/*    
    public void start() {
        // experiment in how to install a JAXWS provider into Axis programatically starting
        // with just using the JAXWS API. This is unlikely to be sufficient as we have to 
        // install policy etc but it's a start
        
        // create the JAXWS endpoint based on the provider
        //wsEndpoint = Endpoint.create(SOAPBinding.SOAP11HTTP_BINDING, jaxwsBindingProvider);
        //wsEndpoint = new EndpointImpl(jaxwsBindingProvider);
        wsEndpoint = Endpoint.create(jaxwsBindingProvider);
       
        // TODO - There is something odd in the way that service name is calculated in
        //        some circumstances
        //           sometimes getServiceName() returns null
        //           sometimes getService().getQName returns a QName namespace that doesn't match the WSDL
        //           sometimes getNamespace() returns null
        //        So here we delve directly into the WSDL4J model as the Tuscany model isn't up to date
        String targetNamespace = wsBinding.getWSDLDefinition().getDefinition().getTargetNamespace();
       
        //set up WSDL for Provider   
        List<Source> metadata = new ArrayList<Source>();
        
        // WSDL DOM seems to be null here so went with writing out
        // string version of WSDL and reading it back in again
        //Node node = wsBinding.getWSDLDefinition().getDefinition().getDocumentationElement();
        //Source source = new DOMSource(node);
        
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        try {
            WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
            writer.writeWSDL(wsBinding.getWSDLDefinition().getDefinition(), outStream);
        } catch (Exception ex){
            ex.printStackTrace();
        }
        
        //System.out.println(outStream.toString());
        ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
        Source source = new StreamSource(inStream);
        source.setSystemId(targetNamespace);
             
        metadata.add(source);
        
        Map<String, Object> properties = new HashMap<String, Object>();

        QName portName =  new QName(targetNamespace,
                                    wsBinding.getPort().getName());
        properties.put(Endpoint.WSDL_PORT, portName);
                      
        QName serviceName = new QName(targetNamespace,
                                      wsBinding.getService().getQName().getLocalPart());
        properties.put(Endpoint.WSDL_SERVICE, serviceName);
        
        wsEndpoint.setMetadata(metadata);
        wsEndpoint.setProperties(properties);               
       
        // Start up the endpoint
        wsEndpoint.publish(wsBinding.getURI());
        
        System.out.println("isPublished: " + wsEndpoint.isPublished());
        System.out.println("getProperties: " + wsEndpoint.getProperties());
        System.out.println("getMetadata: " + wsEndpoint.getMetadata());
        System.out.println("getEndpointReference: " + wsEndpoint.getEndpointReference());
        
        jaxwsBindingProvider.start();  
    }
*/

    public void stop() {
        //jaxwsBindingProvider.stop();
        wsEndpoint.stop();
    }

    public InterfaceContract getBindingInterfaceContract() {
        return wsBinding.getBindingInterfaceContract();
    }

    public boolean supportsOneWayInvocation() {
        return true;
    }
}