From 75e7d7c9999fbb11021cfbe27a802cd9fe4ad9ba Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 1 Apr 2009 00:19:41 +0000 Subject: Bring up the reference binding with the WeatherForecast web service git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@760744 13f79535-47bb-0310-9956-ffa450edef68 --- .../modules/binding-ws-jaxws/META-INF/MANIFEST.MF | 1 - java/sca/modules/binding-ws-jaxws/pom.xml | 157 +++++++++++++++++++-- .../sca/binding/ws/jaxws/JAXWSBindingInvoker.java | 73 ++++++++-- .../ws/jaxws/JAXWSReferenceBindingProvider.java | 13 +- .../test/java/weather/WeatherForecastClient.java | 67 +++++++++ .../src/test/java/weather/WeatherForecastImpl.java | 43 ++++++ 6 files changed, 322 insertions(+), 32 deletions(-) create mode 100644 java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastClient.java create mode 100644 java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastImpl.java (limited to 'java') diff --git a/java/sca/modules/binding-ws-jaxws/META-INF/MANIFEST.MF b/java/sca/modules/binding-ws-jaxws/META-INF/MANIFEST.MF index 31e002e6ec..fabf1652a2 100644 --- a/java/sca/modules/binding-ws-jaxws/META-INF/MANIFEST.MF +++ b/java/sca/modules/binding-ws-jaxws/META-INF/MANIFEST.MF @@ -42,7 +42,6 @@ Import-Package: javax.security.auth.callback;resolution:=optional, org.apache.tuscany.sca.policy.authentication.basic;version="2.0.0", org.apache.tuscany.sca.policy.security;version="2.0.0", org.apache.tuscany.sca.policy.util;version="2.0.0", - org.apache.tuscany.sca.policy.xml.ws;version="2.0.0";resolution:=optional, org.apache.tuscany.sca.provider;version="2.0.0", org.apache.tuscany.sca.runtime;version="2.0.0", org.apache.tuscany.sca.xsd;version="2.0.0", diff --git a/java/sca/modules/binding-ws-jaxws/pom.xml b/java/sca/modules/binding-ws-jaxws/pom.xml index dbe8662bb0..a878f65b3b 100644 --- a/java/sca/modules/binding-ws-jaxws/pom.xml +++ b/java/sca/modules/binding-ws-jaxws/pom.xml @@ -34,6 +34,7 @@ org.apache.tuscany.sca tuscany-core 2.0-SNAPSHOT + runtime @@ -53,6 +54,7 @@ org.apache.tuscany.sca tuscany-assembly-xml 2.0-SNAPSHOT + runtime @@ -84,8 +86,9 @@ org.apache.tuscany.sca - tuscany-databinding-axiom + tuscany-databinding-jaxb 2.0-SNAPSHOT + runtime @@ -99,18 +102,14 @@ org.apache.tuscany.sca tuscany-host-http 2.0-SNAPSHOT + runtime org.apache.tuscany.sca tuscany-interface-java-jaxws 2.0-SNAPSHOT - - - - org.apache.tuscany.sca - tuscany-policy-xml-ws - 2.0-SNAPSHOT + runtime @@ -125,7 +124,149 @@ 2.5 provided - + + org.apache.tuscany.sca + tuscany-node-impl + 2.0-SNAPSHOT + test + + + org.apache.tuscany.sca + tuscany-implementation-java-runtime + 2.0-SNAPSHOT + test + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 1.1 + + + + install-wsdl + validate + + run + + + + + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + -Djava.endorsed.dirs=target/endorsed + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy + generate-sources + + copy + + + + + javax.xml.ws + jaxws-api + 2.1 + jar + + + javax.xml.bind + jaxb-api + 2.1 + jar + + + ${project.build.directory}/endorsed + false + true + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.0 + + + add-source + generate-sources + + add-test-source + + + + target/jaxws-source + + + + + + + org.codehaus.mojo + jaxws-maven-plugin + 1.11 + + + + javax.jws + jsr181-api + 1.0-MR1 + + + javax.annotation + jsr250-api + 1.0 + + + + + wsimport + process-resources + + wsimport + + + com.example.weather + ${project.build.directory}/jaxws-source/wsdl + + WeatherForecast.wsdl + + + + + + 2.1 + ${project.build.directory}/jaxws-source + false + true + + + + + + diff --git a/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSBindingInvoker.java b/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSBindingInvoker.java index 7b9f108e88..ecb3b4f87b 100644 --- a/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSBindingInvoker.java +++ b/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSBindingInvoker.java @@ -18,9 +18,16 @@ */ package org.apache.tuscany.sca.binding.ws.jaxws; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Iterator; +import javax.wsdl.Binding; +import javax.wsdl.BindingOperation; +import javax.wsdl.extensions.soap.SOAPOperation; +import javax.wsdl.extensions.soap12.SOAP12Operation; import javax.xml.namespace.QName; +import javax.xml.soap.Detail; import javax.xml.soap.DetailEntry; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; @@ -29,11 +36,11 @@ import javax.xml.soap.SOAPFault; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.ws.Dispatch; +import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceFeature; import javax.xml.ws.soap.SOAPFaultException; -import org.apache.axis2.addressing.AddressingConstants; import org.apache.tuscany.sca.binding.ws.WebServiceBinding; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.DataExchangeSemantics; @@ -49,9 +56,6 @@ import org.w3c.dom.Node; public class JAXWSBindingInvoker implements Invoker, DataExchangeSemantics { private final static String SCA11_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.1"; - public static final QName QNAME_WSA_FROM = - new QName(AddressingConstants.Final.WSA_NAMESPACE, AddressingConstants.WSA_FROM, - AddressingConstants.WSA_DEFAULT_PREFIX); public static final String TUSCANY_PREFIX = "tuscany"; public static final QName CALLBACK_ID_REFPARM_QN = new QName(SCA11_TUSCANY_NS, "CallbackID", TUSCANY_PREFIX); public static final QName CONVERSATION_ID_REFPARM_QN = @@ -62,16 +66,33 @@ public class JAXWSBindingInvoker implements Invoker, DataExchangeSemantics { private Operation operation; private WebServiceBinding wsBinding; - public JAXWSBindingInvoker(Dispatch dispatch, - Operation operation, + public JAXWSBindingInvoker(Operation operation, WebServiceFeature[] features, MessageFactory messageFactory, WebServiceBinding wsBinding) { - this.dispatch = dispatch; this.messageFactory = messageFactory; this.operation = operation; this.wsBinding = wsBinding; + this.dispatch = createDispatch(wsBinding); + } + private Dispatch createDispatch(WebServiceBinding wsBinding) { + // FIXME: What should we do if the WSDL is generated in memory? + URL wsdlLocation = null; + try { + wsdlLocation = new URL(wsBinding.getWSDLDocument().getDocumentBaseURI()); + } catch (MalformedURLException e) { + try { + wsdlLocation = wsBinding.getWSDLDefinition().getLocation().toURL(); + } catch (MalformedURLException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + Service service = Service.create(wsdlLocation, wsBinding.getServiceName()); + return service.createDispatch(new QName(wsBinding.getServiceName().getNamespaceURI(), wsBinding.getPortName()), + SOAPMessage.class, + Service.Mode.MESSAGE); } public Message invoke(Message msg) { @@ -88,8 +109,8 @@ public class JAXWSBindingInvoker implements Invoker, DataExchangeSemantics { } } - msg.setBody(resp); } catch (SOAPFaultException e) { + e.printStackTrace(); setFault(msg, e.getFault()); } catch (WebServiceException e) { msg.setFaultBody(e); @@ -103,23 +124,53 @@ public class JAXWSBindingInvoker implements Invoker, DataExchangeSemantics { } private void setFault(Message msg, SOAPFault fault) { - for (Iterator i = fault.getDetail().getDetailEntries(); i.hasNext();) { + Detail detail = fault.getDetail(); + if (detail == null) { + return; + } + for (Iterator i = detail.getDetailEntries(); i.hasNext();) { DetailEntry entry = (DetailEntry)i.next(); msg.setFaultBody(entry); } } + protected String getSOAPAction(String operationName) { + Binding binding = wsBinding.getBinding(); + if (binding != null) { + for (Object o : binding.getBindingOperations()) { + BindingOperation bop = (BindingOperation)o; + if (bop.getName().equalsIgnoreCase(operationName)) { + for (Object o2 : bop.getExtensibilityElements()) { + if (o2 instanceof SOAPOperation) { + return ((SOAPOperation)o2).getSoapActionURI(); + } else if (o2 instanceof SOAP12Operation) { + return ((SOAP12Operation)o2).getSoapActionURI(); + } + } + } + } + } + return null; + } + protected SOAPMessage invokeTarget(Message msg) throws SOAPException { SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); javax.xml.soap.SOAPEnvelope envelope = soapPart.getEnvelope(); - javax.xml.soap.SOAPBody body = envelope.addBody(); - body.addDocument(((Node)msg.getBody()).getOwnerDocument()); + javax.xml.soap.SOAPBody body = envelope.getBody(); + Object[] args = (Object[])msg.getBody(); + body.addDocument(((Node)args[0]).getOwnerDocument()); soapMessage.saveChanges(); if (operation.isNonBlocking()) { dispatch.invokeOneWay(soapMessage); return null; } + + // FIXME: We need to find out the soapAction + String action = getSOAPAction(operation.getName()); + if (action != null) { + dispatch.getRequestContext().put(Dispatch.SOAPACTION_URI_PROPERTY, action); + } SOAPMessage response = dispatch.invoke(soapMessage); return response; } diff --git a/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSReferenceBindingProvider.java b/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSReferenceBindingProvider.java index a57c5a726f..13f38044af 100644 --- a/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSReferenceBindingProvider.java +++ b/java/sca/modules/binding-ws-jaxws/src/main/java/org/apache/tuscany/sca/binding/ws/jaxws/JAXWSReferenceBindingProvider.java @@ -18,12 +18,6 @@ */ package org.apache.tuscany.sca.binding.ws.jaxws; -import javax.xml.bind.JAXBContext; -import javax.xml.namespace.QName; -import javax.xml.soap.SOAPMessage; -import javax.xml.ws.Dispatch; -import javax.xml.ws.Service; - import org.apache.tuscany.sca.binding.ws.WebServiceBinding; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; @@ -40,7 +34,6 @@ public class JAXWSReferenceBindingProvider implements ReferenceBindingProvider { private javax.xml.soap.MessageFactory messageFactory; private WebServiceBinding wsBinding; - private Dispatch dispatch; public JAXWSReferenceBindingProvider(RuntimeComponent component, RuntimeComponentReference reference, @@ -61,10 +54,6 @@ public class JAXWSReferenceBindingProvider implements ReferenceBindingProvider { if (contract.getInterface() != null) { contract.getInterface().resetDataBinding(Node.class.getName()); } - - Service service = Service.create(wsBinding.getServiceName()); - JAXBContext context = null; - Dispatch dispath = service.createDispatch(new QName(wsBinding.getServiceName().getNamespaceURI(), wsBinding.getPortName()), context,Service.Mode.MESSAGE); } public void start() { @@ -82,7 +71,7 @@ public class JAXWSReferenceBindingProvider implements ReferenceBindingProvider { } public Invoker createInvoker(Operation operation) { - return new JAXWSBindingInvoker(dispatch, null, null, messageFactory, wsBinding); + return new JAXWSBindingInvoker(operation, null, messageFactory, wsBinding); } } diff --git a/java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastClient.java b/java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastClient.java new file mode 100644 index 0000000000..40b0b8f6dc --- /dev/null +++ b/java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastClient.java @@ -0,0 +1,67 @@ +/* + * 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 weather; + +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; + +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.ContributionLocationHelper; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; + +import com.example.weather.GetWeatherByZipCodeResponse; +import com.example.weather.WeatherForecastSoap; +import com.example.weather.WeatherForecasts; + +/** + * This client program to invoke the Mortgage LoanApproval service + */ +public class WeatherForecastClient { + + public static void main(String[] args) throws Exception { + + String location = ContributionLocationHelper.getContributionLocation("WeatherForecast.composite"); + Node node = NodeFactory.newInstance().createNode("WeatherForecast.composite", new Contribution("c1", location)); + node.start(); + WeatherForecastSoap weatherService = node.getService(WeatherForecastSoap.class, "WeatherForecastService"); + + WeatherForecasts result = weatherService.getWeatherByZipCode("94555"); + + // Dump the result as XML + + // Wrap the result so that it can be marshaled + GetWeatherByZipCodeResponse response = new GetWeatherByZipCodeResponse(); + response.setGetWeatherByZipCodeResult(result); + + // Marshal the JAXB object into XML + JAXBContext context = JAXBContext.newInstance(GetWeatherByZipCodeResponse.class); + StringWriter writer = new StringWriter(); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.marshal(response, writer); + + String xml = writer.toString(); + System.out.println(xml); + + node.stop(); + } +} diff --git a/java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastImpl.java b/java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastImpl.java new file mode 100644 index 0000000000..d3b44450a7 --- /dev/null +++ b/java/sca/modules/binding-ws-jaxws/src/test/java/weather/WeatherForecastImpl.java @@ -0,0 +1,43 @@ +/* + * 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 weather; + +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Service; + +import com.example.weather.WeatherForecastSoap; +import com.example.weather.WeatherForecasts; + +/** + * @version $Rev$ $Date$ + */ +@Service(WeatherForecastSoap.class) +public class WeatherForecastImpl implements WeatherForecastSoap{ + @Reference + protected WeatherForecastSoap weatherForecast; + + public WeatherForecasts getWeatherByPlaceName(String placeName) { + return weatherForecast.getWeatherByPlaceName(placeName); + } + + public WeatherForecasts getWeatherByZipCode(String zipCode) { + return weatherForecast.getWeatherByZipCode(zipCode); + } +} -- cgit v1.2.3