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