1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
39 | |
|
40 | 0 | 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 | 0 | { |
54 | 0 | this.muleContext = muleContext; |
55 | 0 | callAnnotationHandler = new MuleCallAnnotationHandler(muleContext); |
56 | 0 | expressionParser = new MuleExpressionParser(muleContext); |
57 | 0 | properties = new RegistryMap(muleContext.getRegistry()); |
58 | 0 | templateAnnotationHandler = new TemplateAnnotationHandler(this); |
59 | 0 | invokeAnnotationHandler = new InvokeAnnotationHandler(this); |
60 | 0 | responseTransformInterceptor = new MuleResponseTransformInterceptor(muleContext, expressionParser); |
61 | |
|
62 | 0 | errorFilterFactories = new ArrayList<ErrorFilterFactory>(); |
63 | 0 | errorFilterFactories.add(new ExpressionErrorFilterFactory(muleContext)); |
64 | 0 | } |
65 | |
|
66 | |
public CallInterceptor getResponseTransformInterceptor() throws IBeansException |
67 | |
{ |
68 | 0 | return responseTransformInterceptor; |
69 | |
} |
70 | |
|
71 | |
public IBeanInvoker<MuleCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler> getIBeanInvoker() throws IBeansException |
72 | |
{ |
73 | 0 | return new DefaultIBeanInvoker<MuleCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler>(callAnnotationHandler, templateAnnotationHandler, invokeAnnotationHandler); |
74 | |
} |
75 | |
|
76 | |
public IBeanInvoker<MuleMockCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler> getMockIBeanInvoker(Object mock) throws IBeansException |
77 | |
{ |
78 | 0 | return new DefaultIBeanInvoker<MuleMockCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler>(new MuleMockCallAnnotationHandler(muleContext, mock, this), templateAnnotationHandler, invokeAnnotationHandler); |
79 | |
} |
80 | |
|
81 | |
public List<ErrorFilterFactory> getErrorFilterFactories() |
82 | |
{ |
83 | 0 | return errorFilterFactories; |
84 | |
} |
85 | |
|
86 | |
public Map getProperties() |
87 | |
{ |
88 | 0 | return properties; |
89 | |
} |
90 | |
|
91 | |
public ExpressionParser getExpressionParser() |
92 | |
{ |
93 | 0 | return expressionParser; |
94 | |
} |
95 | |
|
96 | |
public void addInterceptors(LinkedList<CallInterceptor> interceptors) |
97 | |
{ |
98 | |
|
99 | 0 | } |
100 | |
|
101 | |
public MuleRequestMessage createRequest(IBeanInvocationData data) throws IBeansException |
102 | |
{ |
103 | |
MuleRequestMessage request; |
104 | 0 | Object payload = (data.getPayloads().size() == 1 ? data.getPayloads().get(0) : data.getPayloads()); |
105 | |
|
106 | 0 | MuleMessage message = new DefaultMuleMessage(payload, muleContext); |
107 | |
|
108 | |
|
109 | 0 | for (Map.Entry<String, Object> entry : data.getHeaderParams().entrySet()) |
110 | |
{ |
111 | 0 | if (entry.getValue() != null) |
112 | |
{ |
113 | 0 | message.setOutboundProperty(entry.getKey(), entry.getValue()); |
114 | |
} |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 0 | message.setOutboundProperty(CHANNEL.URI_PARAM_PROPERTIES, data.getUriParams()); |
122 | |
|
123 | |
|
124 | 0 | for (DataSource dataSource : data.getAttachments()) |
125 | |
{ |
126 | |
try |
127 | |
{ |
128 | 0 | message.addOutboundAttachment(dataSource.getName(), new DataHandler(dataSource)); |
129 | |
} |
130 | 0 | catch (Exception e) |
131 | |
{ |
132 | 0 | throw new IBeansException(e); |
133 | 0 | } |
134 | |
} |
135 | |
|
136 | |
|
137 | 0 | for (String key : data.getPropertyParams().keySet()) |
138 | |
{ |
139 | 0 | message.setInvocationProperty(key, data.getPropertyParams().get(key)); |
140 | |
} |
141 | |
|
142 | 0 | request = new MuleRequestMessage(data, message); |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | 0 | request.setTimeout(Utils.getInt(data.getPropertyParams().get(CHANNEL.TIMEOUT), -1)); |
150 | 0 | return request; |
151 | |
} |
152 | |
|
153 | |
public MuleResponseMessage createResponse(Object payload, Map<String, Object> headers, Map<String, DataHandler> attachments) throws IBeansException |
154 | |
{ |
155 | 0 | MuleMessage message = new DefaultMuleMessage(payload, headers, new HashMap<String, Object>(), attachments, muleContext); |
156 | |
try |
157 | |
{ |
158 | 0 | return new MuleResponseMessage(message); |
159 | |
} |
160 | 0 | catch (MimeTypeParseException e) |
161 | |
{ |
162 | 0 | throw new IBeansException(e); |
163 | |
} |
164 | |
} |
165 | |
} |