View Javadoc

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