View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.ibeans.spi;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleMessage;
12  import org.mule.registry.RegistryMap;
13  
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.LinkedList;
17  import java.util.List;
18  import java.util.Map;
19  
20  import javax.activation.DataHandler;
21  import javax.activation.DataSource;
22  import javax.activation.MimeTypeParseException;
23  
24  import org.ibeans.api.CallInterceptor;
25  import org.ibeans.api.IBeanInvocationData;
26  import org.ibeans.api.IBeanInvoker;
27  import org.ibeans.api.IBeansException;
28  import org.ibeans.api.channel.CHANNEL;
29  import org.ibeans.impl.DefaultIBeanInvoker;
30  import org.ibeans.impl.InvokeAnnotationHandler;
31  import org.ibeans.impl.TemplateAnnotationHandler;
32  import org.ibeans.impl.support.util.Utils;
33  import org.ibeans.spi.ErrorFilterFactory;
34  import org.ibeans.spi.ExpressionParser;
35  import org.ibeans.spi.IBeansPlugin;
36  
37  /**
38   * The entry-point for Mule to integrate with IBeans
39   */
40  public class MuleIBeansPlugin implements IBeansPlugin<MuleRequestMessage, MuleResponseMessage>
41  {
42      private MuleContext muleContext;
43  
44      private Map<String, Object> properties;
45      private MuleExpressionParser expressionParser;
46      private MuleCallAnnotationHandler callAnnotationHandler;
47      private TemplateAnnotationHandler templateAnnotationHandler;
48      private InvokeAnnotationHandler invokeAnnotationHandler;
49      private MuleResponseTransformInterceptor responseTransformInterceptor;
50      private List<ErrorFilterFactory> errorFilterFactories;
51  
52      public MuleIBeansPlugin(MuleContext muleContext)
53      {
54          this.muleContext = muleContext;
55          callAnnotationHandler = new MuleCallAnnotationHandler(muleContext);
56          expressionParser = new MuleExpressionParser(muleContext);
57          properties = new RegistryMap(muleContext.getRegistry());
58          templateAnnotationHandler = new TemplateAnnotationHandler(this);
59          invokeAnnotationHandler = new InvokeAnnotationHandler(this);
60          responseTransformInterceptor = new MuleResponseTransformInterceptor(muleContext, expressionParser);
61  
62          errorFilterFactories = new ArrayList<ErrorFilterFactory>();
63          errorFilterFactories.add(new ExpressionErrorFilterFactory(muleContext));
64      }
65  
66      public CallInterceptor getResponseTransformInterceptor() throws IBeansException
67      {
68          return responseTransformInterceptor;
69      }
70  
71      public IBeanInvoker<MuleCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler> getIBeanInvoker() throws IBeansException
72      {
73          return new DefaultIBeanInvoker<MuleCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler>(callAnnotationHandler, templateAnnotationHandler, invokeAnnotationHandler);
74      }
75  
76      public IBeanInvoker<MuleMockCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler> getMockIBeanInvoker(Object mock) throws IBeansException
77      {
78          return new DefaultIBeanInvoker<MuleMockCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler>(new MuleMockCallAnnotationHandler(muleContext, mock, this), templateAnnotationHandler, invokeAnnotationHandler);
79      }
80  
81      public List<ErrorFilterFactory> getErrorFilterFactories()
82      {
83          return errorFilterFactories;
84      }
85  
86      public Map getProperties()
87      {
88          return properties;
89      }
90  
91      public ExpressionParser getExpressionParser()
92      {
93          return expressionParser;
94      }
95  
96      public void addInterceptors(LinkedList<CallInterceptor> interceptors)
97      {
98          //nothing to do
99      }
100 
101     public MuleRequestMessage createRequest(IBeanInvocationData data) throws IBeansException
102     {
103         MuleRequestMessage request;
104         Object payload = (data.getPayloads().size() == 1 ? data.getPayloads().get(0) : data.getPayloads());
105 
106         MuleMessage message = new DefaultMuleMessage(payload, muleContext);
107 
108         //We need to scrub any null header values since Mule does not allow Null headers
109         for (Map.Entry<String, Object> entry : data.getHeaderParams().entrySet())
110         {
111             if (entry.getValue() != null)
112             {
113                 message.setOutboundProperty(entry.getKey(), entry.getValue());
114             }
115         }
116 
117         //TODO (requires API change)
118         // message.setInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, XXX);
119 
120         //Set the URI params so the correct URI can be constructed for this invocation
121         message.setOutboundProperty(CHANNEL.URI_PARAM_PROPERTIES, data.getUriParams());
122 
123         //Add any attachments
124         for (DataSource dataSource : data.getAttachments())
125         {
126             try
127             {
128                 message.addOutboundAttachment(dataSource.getName(), new DataHandler(dataSource));
129             }
130             catch (Exception e)
131             {
132                 throw new IBeansException(e);
133             }
134         }
135 
136         //Add the properties to the invocation scope
137         for (String key : data.getPropertyParams().keySet())
138         {
139             message.setInvocationProperty(key, data.getPropertyParams().get(key));
140         }
141 
142         request = new MuleRequestMessage(data, message);
143 
144         //TODO It may be useful to set the Method invoked on the request, In Mule,
145         //Some transports such as Axis, RMI and EJB can use the method information
146         //Not doing this for now since the scope is HTTP only
147 
148         //Set the request timeout, the default -1 means it will not timeout
149         request.setTimeout(Utils.getInt(data.getPropertyParams().get(CHANNEL.TIMEOUT), -1));
150         return request;
151     }
152 
153     public MuleResponseMessage createResponse(Object payload, Map<String, Object> headers, Map<String, DataHandler> attachments) throws IBeansException
154     {
155         MuleMessage message = new DefaultMuleMessage(payload, headers, new HashMap<String, Object>(), attachments, muleContext);
156         try
157         {
158             return new MuleResponseMessage(message);
159         }
160         catch (MimeTypeParseException e)
161         {
162             throw new IBeansException(e);
163         }
164     }
165 }