1
2
3
4
5
6
7
8
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
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
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
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
121
122
123
124 message.setOutboundProperty(CHANNEL.URI_PARAM_PROPERTIES, data.getUriParams());
125
126
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
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
148
149
150
151
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 }