View Javadoc

1   /*
2    * $Id: MuleMsgProvider.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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.mule.impl.RequestContext;
14  import org.mule.impl.model.ModelHelper;
15  import org.mule.providers.soap.ServiceProxy;
16  import org.mule.providers.soap.axis.AxisConnector;
17  import org.mule.providers.soap.axis.AxisMessageReceiver;
18  import org.mule.providers.soap.axis.AxisServiceProxy;
19  import org.mule.umo.UMOComponent;
20  
21  import java.lang.reflect.Proxy;
22  
23  import org.apache.axis.AxisFault;
24  import org.apache.axis.Constants;
25  import org.apache.axis.MessageContext;
26  import org.apache.axis.description.OperationDesc;
27  import org.apache.axis.handlers.soap.SOAPService;
28  import org.apache.axis.providers.java.MsgProvider;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * <code>MuleMsgProvider</code> Is an Axis service endpoint that builds services
34   * from Mule managed components
35   * 
36   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
37   * @version $Revision: 7976 $
38   */
39  public class MuleMsgProvider extends MsgProvider
40  {
41      /**
42       * Serial version
43       */
44      private static final long serialVersionUID = -4399291846942449361L;
45  
46      private AxisConnector connector;
47  
48      private static Log logger = LogFactory.getLog(MuleMsgProvider.class);
49  
50      private String METHOD_BODYARRAY = "soapbodyelement";
51      private String METHOD_ELEMENTARRAY = "element";
52      private String METHOD_DOCUMENT = "document";
53  
54      public MuleMsgProvider(AxisConnector connector)
55      {
56          this.connector = connector;
57      }
58  
59      protected Object makeNewServiceObject(MessageContext messageContext, String s) throws Exception
60      {
61          String transUrl = (String)messageContext.getProperty("transport.url");
62          int i = transUrl.indexOf('?');
63          if (i > -1)
64          {
65              transUrl = transUrl.substring(0, i);
66          }
67          AxisMessageReceiver receiver = (AxisMessageReceiver)connector.lookupReceiver(transUrl);
68          if (receiver == null)
69          {
70              receiver = (AxisMessageReceiver)connector.lookupReceiver(messageContext.getTargetService());
71          }
72          if (receiver == null)
73          {
74              throw new AxisFault("Could not find Mule registered service: " + s);
75          }
76          Class[] classes = ServiceProxy.getInterfacesForComponent(receiver.getComponent());
77          return AxisServiceProxy.createProxy(receiver, true, classes);
78      }
79  
80      protected Class getServiceClass(String s, SOAPService soapService, MessageContext messageContext)
81          throws AxisFault
82      {
83          UMOComponent component = ModelHelper.getComponent(soapService.getName());
84          try
85          {
86              Class[] classes = ServiceProxy.getInterfacesForComponent(component);
87              return Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), classes);
88          }
89          catch (Exception e)
90          {
91              throw new AxisFault("Failed to implementation class for component: " + e.getMessage(), e);
92          }
93      }
94  
95      /**
96       * @param msgContext
97       * @deprecated I dont think this is necessary, but leaving it here for a while
98       */
99      protected void setOperationStyle(MessageContext msgContext)
100     {
101         /*
102          * Axis requires that the OperationDesc.operationStyle be set to match the
103          * method signature This does not appear to be an automated process so
104          * determine from the 4 allowed forms public Element [] method(Element []
105          * bodies); public SOAPBodyElement [] method (SOAPBodyElement [] bodies);
106          * public Document method(Document body); public void method(SOAPEnvelope
107          * req, SOAPEnvelope resp);
108          */
109         int methodType = msgContext.getOperation().getMessageOperationStyle();
110         if (methodType > -1)
111         {
112             // Already set, nothing more to do
113             return;
114         }
115         OperationDesc operation = msgContext.getOperation();
116         String methodSignature = operation.getMethod().toString().toLowerCase();
117         if (methodSignature.indexOf(METHOD_BODYARRAY) != -1)
118         {
119             methodType = OperationDesc.MSG_METHOD_BODYARRAY;
120         }
121         else if (methodSignature.indexOf(METHOD_ELEMENTARRAY) != -1)
122         {
123             methodType = OperationDesc.MSG_METHOD_ELEMENTARRAY;
124         }
125         else if (methodSignature.indexOf(METHOD_DOCUMENT) != -1)
126         {
127             methodType = OperationDesc.MSG_METHOD_DOCUMENT;
128         }
129         else
130         {
131             methodType = OperationDesc.MSG_METHOD_SOAPENVELOPE;
132         }
133         operation.setMessageOperationStyle(methodType);
134         logger.debug("Now Invoking service (Method Format) " + operation.getMethod().toString());
135         logger.debug("Now Invoking service (MethodType) "
136                      + String.valueOf(operation.getMessageOperationStyle()));
137     }
138 
139     public void invoke(MessageContext msgContext) throws AxisFault
140     {
141         // Make sure that the method style is correctly set (This does not appear to
142         // be handled by default)
143         // setOperationStyle(msgContext);
144         super.invoke(msgContext);
145         if (RequestContext.getExceptionPayload() != null)
146         {
147             Throwable t = RequestContext.getExceptionPayload().getException();
148             if (t instanceof Exception)
149             {
150                 AxisFault fault = AxisFault.makeFault((Exception)t);
151                 if (t instanceof RuntimeException)
152                 {
153                     fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION, "true");
154                 }
155                 throw fault;
156             }
157             else
158             {
159                 throw (Error)t;
160             }
161         }
162     }
163 
164 }