From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/binding/axis2/Axis2TargetInvoker.java | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 branches/sca-java-M2/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2TargetInvoker.java (limited to 'branches/sca-java-M2/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2TargetInvoker.java') diff --git a/branches/sca-java-M2/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2TargetInvoker.java b/branches/sca-java-M2/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2TargetInvoker.java new file mode 100755 index 0000000000..175f995c76 --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2TargetInvoker.java @@ -0,0 +1,136 @@ +/* + * 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.binding.axis2; + +import java.lang.reflect.InvocationTargetException; + +import javax.xml.namespace.QName; + +import org.apache.axiom.om.OMElement; +import org.apache.axiom.soap.SOAPBody; +import org.apache.axiom.soap.SOAPEnvelope; +import org.apache.axiom.soap.SOAPFactory; +import org.apache.axis2.AxisFault; +import org.apache.axis2.client.OperationClient; +import org.apache.axis2.client.Options; +import org.apache.axis2.client.ServiceClient; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.wsdl.WSDLConstants; +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * Axis2TargetInvoker uses an Axis2 OperationClient to invoke a remote web service + */ +public class Axis2TargetInvoker implements TargetInvoker { + + private QName wsdlOperationName; + + private Options options; + + private SOAPFactory soapFactory; + + private ServiceClient serviceClient; + + public Axis2TargetInvoker(ServiceClient serviceClient, QName wsdlOperationName, Options options, + SOAPFactory soapFactory) { + this.wsdlOperationName = wsdlOperationName; + this.options = options; + this.soapFactory = soapFactory; + this.serviceClient = serviceClient; + } + + /** + * Invoke a WS operation + * + * @param payload + * @return + * @throws InvocationTargetException + */ + public Object invokeTarget(final Object payload) throws InvocationTargetException { + try { + Object[] args = (Object[]) payload; + OperationClient operationClient = createOperationClient(args); + + operationClient.execute(true); + + MessageContext responseMC = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); + return responseMC.getEnvelope().getBody().getFirstElement(); + + } catch (AxisFault e) { + throw new InvocationTargetException(e); + } + + } + + protected OperationClient createOperationClient(Object[] args) throws AxisFault { + SOAPEnvelope env = soapFactory.getDefaultEnvelope(); + if (args != null && args.length > 0) { + SOAPBody body = env.getBody(); + for (Object bc : args) { + if (bc instanceof OMElement) { + body.addChild((OMElement) bc); + } else { + throw new IllegalArgumentException( + "Can't handle mixed payloads betweem OMElements and other types."); + } + } + } + MessageContext requestMC = new MessageContext(); + requestMC.setEnvelope(env); + // Axis2 operationClients can not be shared so create a new one for each request + OperationClient operationClient = serviceClient.createClient(wsdlOperationName); + operationClient.setOptions(options); + operationClient.addMessageContext(requestMC); + return operationClient; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + public Axis2TargetInvoker clone() throws CloneNotSupportedException { + try { + return (Axis2TargetInvoker) super.clone(); + } catch (CloneNotSupportedException e) { + // will not happen + return null; + } + } + + public boolean isCacheable() { + return true; + } + + public void setCacheable(boolean cacheable) { + + } + + public boolean isOptimizable() { + return false; + } + +} -- cgit v1.2.3