KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > axis > extensions > MuleRPCProvider


1 /*
2  * $Id: MuleRPCProvider.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.soap.axis.extensions;
12
13 import org.apache.axis.AxisFault;
14 import org.apache.axis.Constants;
15 import org.apache.axis.MessageContext;
16 import org.apache.axis.constants.Style;
17 import org.apache.axis.description.OperationDesc;
18 import org.apache.axis.description.ParameterDesc;
19 import org.apache.axis.description.ServiceDesc;
20 import org.apache.axis.handlers.soap.SOAPService;
21 import org.apache.axis.message.RPCElement;
22 import org.apache.axis.message.RPCHeaderParam;
23 import org.apache.axis.message.RPCParam;
24 import org.apache.axis.message.SOAPEnvelope;
25 import org.apache.axis.providers.java.RPCProvider;
26 import org.apache.axis.soap.SOAPConstants;
27 import org.apache.axis.utils.JavaUtils;
28 import org.mule.MuleManager;
29 import org.mule.impl.RequestContext;
30 import org.mule.providers.soap.ServiceProxy;
31 import org.mule.providers.soap.axis.AxisConnector;
32 import org.mule.providers.soap.axis.AxisMessageReceiver;
33 import org.mule.providers.soap.axis.AxisServiceProxy;
34 import org.mule.umo.UMOSession;
35
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.rpc.holders.Holder JavaDoc;
38 import java.lang.reflect.Proxy JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 /**
43  * <code>MuleProvider</code> Is an Axis service endpoint that builds services from
44  * Mule managed components
45  *
46  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
47  * @version $Revision: 3798 $
48  */

49 public class MuleRPCProvider extends RPCProvider
50 {
51     /**
52      * Serial version
53      */

54     private static final long serialVersionUID = -4987111047650933518L;
55
56     private AxisConnector connector;
57
58     public MuleRPCProvider(AxisConnector connector)
59     {
60         this.connector = connector;
61     }
62
63     protected Object JavaDoc makeNewServiceObject(MessageContext messageContext, String JavaDoc s) throws Exception JavaDoc
64     {
65         String JavaDoc transUrl = (String JavaDoc)messageContext.getProperty("transport.url");
66         int i = transUrl.indexOf("?");
67         if (i > -1)
68         {
69             transUrl = transUrl.substring(0, i);
70         }
71         AxisMessageReceiver receiver = (AxisMessageReceiver)connector.getReceiver(transUrl);
72         if (receiver == null)
73         {
74             receiver = (AxisMessageReceiver)connector.getReceiver(messageContext.getTargetService());
75         }
76         if (receiver == null)
77         {
78             throw new AxisFault("Could not find Mule registered service: " + s);
79         }
80         Class JavaDoc[] classes = ServiceProxy.getInterfacesForComponent(receiver.getComponent());
81         return AxisServiceProxy.createProxy(receiver, true, classes);
82     }
83
84     protected Class JavaDoc getServiceClass(String JavaDoc s, SOAPService soapService, MessageContext messageContext)
85         throws AxisFault
86     {
87         UMOSession session = MuleManager.getInstance().getModel().getComponentSession(soapService.getName());
88         try
89         {
90             Class JavaDoc[] classes = ServiceProxy.getInterfacesForComponent(session.getComponent());
91             return Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), classes);
92         }
93         catch (Exception JavaDoc e)
94         {
95             throw new AxisFault("Failed to implementation class for component: " + e.getMessage(), e);
96         }
97     }
98
99     public void invoke(MessageContext msgContext) throws AxisFault
100     {
101         super.invoke(msgContext);
102         if (RequestContext.getExceptionPayload() != null)
103         {
104             Throwable JavaDoc t = RequestContext.getExceptionPayload().getException();
105             if (t instanceof Exception JavaDoc)
106             {
107                 AxisFault fault = AxisFault.makeFault((Exception JavaDoc)t);
108                 if (t instanceof RuntimeException JavaDoc)
109                 {
110                     fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION, "true");
111                 }
112                 throw fault;
113             }
114             else
115             {
116                 throw (Error JavaDoc)t;
117             }
118         }
119     }
120
121     protected RPCElement createResponseBody(RPCElement body,
122                                             MessageContext msgContext,
123                                             OperationDesc operation,
124                                             ServiceDesc serviceDesc,
125                                             Object JavaDoc objRes,
126                                             SOAPEnvelope resEnv,
127                                             ArrayList JavaDoc outs) throws Exception JavaDoc
128     {
129         String JavaDoc methodName = operation.getName();
130
131         /* Now put the result in the result SOAPEnvelope */
132         RPCElement resBody = new RPCElement(methodName + "Response");
133         resBody.setPrefix(body.getPrefix());
134         resBody.setNamespaceURI(body.getNamespaceURI());
135         resBody.setEncodingStyle(msgContext.getEncodingStyle());
136         try
137         {
138             // Return first
139
if (operation.getMethod().getReturnType() != Void.TYPE)
140             {
141                 QName JavaDoc returnQName = operation.getReturnQName();
142                 if (returnQName == null)
143                 {
144                     String JavaDoc nsp = body.getNamespaceURI();
145                     if (nsp == null || nsp.length() == 0)
146                     {
147                         nsp = serviceDesc.getDefaultNamespace();
148                     }
149                     returnQName = new QName JavaDoc(msgContext.isEncoded() ? "" : nsp, methodName + "Return");
150                 }
151
152                 RPCParam param = new RPCParam(returnQName, objRes);
153                 param.setParamDesc(operation.getReturnParamDesc());
154
155                 if (!operation.isReturnHeader())
156                 {
157                     // For SOAP 1.2 rpc style, add a result
158
if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS
159                         && (serviceDesc.getStyle().equals(Style.RPC)))
160                     {
161                         RPCParam resultParam = new RPCParam(Constants.QNAME_RPC_RESULT, returnQName);
162                         resultParam.setXSITypeGeneration(Boolean.FALSE);
163                         resBody.addParam(resultParam);
164                     }
165                     resBody.addParam(param);
166                 }
167                 else
168                 {
169                     resEnv.addHeader(new RPCHeaderParam(param));
170                 }
171
172             }
173
174             // Then any other out params
175
if (!outs.isEmpty())
176             {
177                 for (Iterator JavaDoc i = outs.iterator(); i.hasNext();)
178                 {
179                     // We know this has a holder, so just unwrap the value
180
RPCParam param = (RPCParam)i.next();
181                     Holder JavaDoc holder = (Holder JavaDoc)param.getObjectValue();
182                     Object JavaDoc value = JavaUtils.getHolderValue(holder);
183                     ParameterDesc paramDesc = param.getParamDesc();
184
185                     param.setObjectValue(value);
186                     if (paramDesc != null && paramDesc.isOutHeader())
187                     {
188                         resEnv.addHeader(new RPCHeaderParam(param));
189                     }
190                     else
191                     {
192                         resBody.addParam(param);
193                     }
194                 }
195             }
196         }
197         catch (Exception JavaDoc e)
198         {
199             throw e;
200         }
201         return resBody;
202     }
203 }
204
Popular Tags