KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MuleMsgProvider.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.description.OperationDesc;
17 import org.apache.axis.handlers.soap.SOAPService;
18 import org.apache.axis.providers.java.MsgProvider;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.mule.MuleManager;
22 import org.mule.impl.RequestContext;
23 import org.mule.providers.soap.ServiceProxy;
24 import org.mule.providers.soap.axis.AxisConnector;
25 import org.mule.providers.soap.axis.AxisMessageReceiver;
26 import org.mule.providers.soap.axis.AxisServiceProxy;
27 import org.mule.umo.UMOSession;
28
29 import java.lang.reflect.Proxy JavaDoc;
30
31 /**
32  * <code>MuleMsgProvider</code> Is an Axis service endpoint that builds services
33  * from Mule managed components
34  *
35  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
36  * @version $Revision: 3798 $
37  */

38 public class MuleMsgProvider extends MsgProvider
39 {
40     /**
41      * Serial version
42      */

43     private static final long serialVersionUID = -4399291846942449361L;
44
45     private AxisConnector connector;
46
47     private static Log logger = LogFactory.getLog(MuleMsgProvider.class);
48
49     private String JavaDoc METHOD_BODYARRAY = "soapbodyelement";
50     private String JavaDoc METHOD_ELEMENTARRAY = "element";
51     private String JavaDoc METHOD_DOCUMENT = "document";
52
53     public MuleMsgProvider(AxisConnector connector)
54     {
55         this.connector = connector;
56     }
57
58     protected Object JavaDoc makeNewServiceObject(MessageContext messageContext, String JavaDoc s) throws Exception JavaDoc
59     {
60         String JavaDoc transUrl = (String JavaDoc)messageContext.getProperty("transport.url");
61         int i = transUrl.indexOf("?");
62         if (i > -1)
63         {
64             transUrl = transUrl.substring(0, i);
65         }
66         AxisMessageReceiver receiver = (AxisMessageReceiver)connector.getReceiver(transUrl);
67         if (receiver == null)
68         {
69             receiver = (AxisMessageReceiver)connector.getReceiver(messageContext.getTargetService());
70         }
71         if (receiver == null)
72         {
73             throw new AxisFault("Could not find Mule registered service: " + s);
74         }
75         Class JavaDoc[] classes = ServiceProxy.getInterfacesForComponent(receiver.getComponent());
76         return AxisServiceProxy.createProxy(receiver, true, classes);
77     }
78
79     protected Class JavaDoc getServiceClass(String JavaDoc s, SOAPService soapService, MessageContext messageContext)
80         throws AxisFault
81     {
82         UMOSession session = MuleManager.getInstance().getModel().getComponentSession(soapService.getName());
83         try
84         {
85             Class JavaDoc[] classes = ServiceProxy.getInterfacesForComponent(session.getComponent());
86             return Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), classes);
87         }
88         catch (Exception JavaDoc e)
89         {
90             throw new AxisFault("Failed to implementation class for component: " + e.getMessage(), e);
91         }
92     }
93
94     /**
95      * @param msgContext
96      * @deprecated I dont think this is necessary, but leaving it here for a while
97      */

98     protected void setOperationStyle(MessageContext msgContext)
99     {
100         /*
101          * Axis requires that the OperationDesc.operationStyle be set to match the
102          * method signature This does not appear to be an automated process so
103          * determine from the 4 allowed forms public Element [] method(Element []
104          * bodies); public SOAPBodyElement [] method (SOAPBodyElement [] bodies);
105          * public Document method(Document body); public void method(SOAPEnvelope
106          * req, SOAPEnvelope resp);
107          */

108         int methodType = msgContext.getOperation().getMessageOperationStyle();
109         if (methodType > -1)
110         {
111             // Already set, nothing more to do
112
return;
113         }
114         OperationDesc operation = msgContext.getOperation();
115         String JavaDoc methodSignature = operation.getMethod().toString().toLowerCase();
116         if (methodSignature.indexOf(METHOD_BODYARRAY) != -1)
117         {
118             methodType = OperationDesc.MSG_METHOD_BODYARRAY;
119         }
120         else if (methodSignature.indexOf(METHOD_ELEMENTARRAY) != -1)
121         {
122             methodType = OperationDesc.MSG_METHOD_ELEMENTARRAY;
123         }
124         else if (methodSignature.indexOf(METHOD_DOCUMENT) != -1)
125         {
126             methodType = OperationDesc.MSG_METHOD_DOCUMENT;
127         }
128         else
129         {
130             methodType = OperationDesc.MSG_METHOD_SOAPENVELOPE;
131         }
132         operation.setMessageOperationStyle(methodType);
133         logger.debug("Now Invoking service (Method Format) " + operation.getMethod().toString());
134         logger.debug("Now Invoking service (MethodType) "
135                      + String.valueOf(operation.getMessageOperationStyle()));
136     }
137
138     public void invoke(MessageContext msgContext) throws AxisFault
139     {
140         // Make sure that the method style is correctly set (This does not appear to
141
// be handled by default)
142
// setOperationStyle(msgContext);
143
super.invoke(msgContext);
144         if (RequestContext.getExceptionPayload() != null)
145         {
146             Throwable JavaDoc t = RequestContext.getExceptionPayload().getException();
147             if (t instanceof Exception JavaDoc)
148             {
149                 AxisFault fault = AxisFault.makeFault((Exception JavaDoc)t);
150                 if (t instanceof RuntimeException JavaDoc)
151                 {
152                     fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION, "true");
153                 }
154                 throw fault;
155             }
156             else
157             {
158                 throw (Error JavaDoc)t;
159             }
160         }
161     }
162
163 }
164
Popular Tags