View Javadoc

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