1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.processors; |
8 | |
|
9 | |
import org.mule.api.AnnotationException; |
10 | |
import org.mule.api.EndpointAnnotationParser; |
11 | |
import org.mule.api.MessageProcessorAnnotationParser; |
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleRuntimeException; |
15 | |
import org.mule.api.annotations.meta.Channel; |
16 | |
import org.mule.api.annotations.meta.ChannelType; |
17 | |
import org.mule.api.annotations.meta.Router; |
18 | |
import org.mule.api.annotations.meta.RouterType; |
19 | |
import org.mule.api.config.MuleProperties; |
20 | |
import org.mule.api.context.MuleContextAware; |
21 | |
import org.mule.api.endpoint.InboundEndpoint; |
22 | |
import org.mule.api.endpoint.OutboundEndpoint; |
23 | |
import org.mule.api.lifecycle.Initialisable; |
24 | |
import org.mule.api.processor.MessageProcessor; |
25 | |
import org.mule.api.registry.PreInitProcessor; |
26 | |
import org.mule.api.routing.OutboundRouter; |
27 | |
import org.mule.api.routing.OutboundRouterCollection; |
28 | |
import org.mule.api.service.Service; |
29 | |
import org.mule.api.source.CompositeMessageSource; |
30 | |
import org.mule.component.AbstractJavaComponent; |
31 | |
import org.mule.config.AnnotationsParserFactory; |
32 | |
import org.mule.config.endpoint.AnnotatedEndpointHelper; |
33 | |
import org.mule.config.i18n.AnnotationsMessages; |
34 | |
import org.mule.config.i18n.CoreMessages; |
35 | |
import org.mule.registry.RegistryMap; |
36 | |
import org.mule.routing.outbound.OutboundPassThroughRouter; |
37 | |
import org.mule.service.ServiceCompositeMessageSource; |
38 | |
import org.mule.util.TemplateParser; |
39 | |
import org.mule.util.annotation.AnnotationMetaData; |
40 | |
import org.mule.util.annotation.AnnotationUtils; |
41 | |
|
42 | |
import java.lang.annotation.Annotation; |
43 | |
import java.lang.annotation.ElementType; |
44 | |
import java.util.Collection; |
45 | |
import java.util.Collections; |
46 | |
import java.util.List; |
47 | |
|
48 | |
import org.apache.commons.logging.Log; |
49 | |
import org.apache.commons.logging.LogFactory; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public class DecoratingAnnotatedServiceProcessor implements PreInitProcessor, MuleContextAware |
59 | |
{ |
60 | |
|
61 | |
|
62 | |
|
63 | 0 | protected transient final Log logger = LogFactory.getLog(DecoratingAnnotatedServiceProcessor.class); |
64 | |
|
65 | |
protected MuleContext context; |
66 | 0 | private final TemplateParser parser = TemplateParser.createAntStyleParser(); |
67 | |
protected RegistryMap regProps; |
68 | |
protected AnnotatedEndpointHelper helper; |
69 | |
protected AnnotationsParserFactory parserFactory; |
70 | |
|
71 | |
public DecoratingAnnotatedServiceProcessor() |
72 | 0 | { |
73 | 0 | } |
74 | |
|
75 | |
public DecoratingAnnotatedServiceProcessor(MuleContext context) |
76 | 0 | { |
77 | 0 | setMuleContext(context); |
78 | 0 | } |
79 | |
|
80 | |
public void setMuleContext(MuleContext context) |
81 | |
{ |
82 | |
try |
83 | |
{ |
84 | 0 | this.context = context; |
85 | 0 | this.regProps = new RegistryMap(context.getRegistry()); |
86 | 0 | this.helper = new AnnotatedEndpointHelper(context); |
87 | 0 | this.parserFactory = context.getRegistry().lookupObject(AnnotationsParserFactory.class); |
88 | 0 | if(parserFactory==null) |
89 | |
{ |
90 | 0 | logger.info(AnnotationsParserFactory.class.getName() +" implementation not found in registry, annotations not enabled"); |
91 | |
} |
92 | |
} |
93 | 0 | catch (MuleException e) |
94 | |
{ |
95 | 0 | throw new MuleRuntimeException(CoreMessages.failedToCreate(getClass().getName()), e); |
96 | 0 | } |
97 | 0 | } |
98 | |
|
99 | |
public Object process(Object object) |
100 | |
{ |
101 | 0 | if (object == null || parserFactory == null) |
102 | |
{ |
103 | 0 | return object; |
104 | |
} |
105 | |
|
106 | 0 | if (object instanceof Service) |
107 | |
{ |
108 | 0 | Service service = (Service) object; |
109 | |
|
110 | 0 | if (service.getComponent() instanceof AbstractJavaComponent) |
111 | |
{ |
112 | |
try |
113 | |
{ |
114 | 0 | AbstractJavaComponent component = (AbstractJavaComponent) service.getComponent(); |
115 | 0 | if(AnnotationUtils.getMethodMetaAnnotations(component.getObjectType(), Channel.class).size()==0) |
116 | |
{ |
117 | 0 | return object; |
118 | |
} |
119 | |
|
120 | 0 | processInbound(component.getObjectType(), service); |
121 | 0 | processOutbound(component.getObjectType(), service); |
122 | |
|
123 | |
|
124 | 0 | processReply(component.getObjectType(), service); |
125 | |
} |
126 | 0 | catch (MuleException e) |
127 | |
{ |
128 | 0 | e.printStackTrace(); |
129 | 0 | } |
130 | |
} |
131 | |
|
132 | |
} |
133 | 0 | return object; |
134 | |
} |
135 | |
|
136 | |
protected void processInbound(Class componentFactoryClass, org.mule.api.service.Service service) throws MuleException |
137 | |
{ |
138 | |
|
139 | |
InboundEndpoint inboundEndpoint; |
140 | 0 | List<AnnotationMetaData> annotations = AnnotationUtils.getClassAndMethodAnnotations(componentFactoryClass); |
141 | 0 | for (AnnotationMetaData annotation : annotations) |
142 | |
{ |
143 | 0 | inboundEndpoint = tryInboundEndpointAnnotation(annotation, ChannelType.Inbound); |
144 | 0 | if (inboundEndpoint != null) |
145 | |
{ |
146 | 0 | if (annotation.getType() == ElementType.METHOD) |
147 | |
{ |
148 | 0 | inboundEndpoint.getProperties().put(MuleProperties.MULE_METHOD_PROPERTY, annotation.getElementName()); |
149 | |
} |
150 | 0 | ((CompositeMessageSource) service.getMessageSource()).addSource(inboundEndpoint); |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
|
155 | 0 | processInboundRouters(componentFactoryClass, service); |
156 | 0 | } |
157 | |
|
158 | |
protected void processInboundRouters(Class componentFactoryClass, org.mule.api.service.Service service) throws MuleException |
159 | |
{ |
160 | 0 | for (int i = 0; i < componentFactoryClass.getAnnotations().length; i++) |
161 | |
{ |
162 | 0 | Annotation annotation = componentFactoryClass.getAnnotations()[i]; |
163 | 0 | Router routerAnnotation = annotation.annotationType().getAnnotation(Router.class); |
164 | 0 | if (routerAnnotation != null && routerAnnotation.type() == RouterType.Inbound) |
165 | |
{ |
166 | 0 | MessageProcessorAnnotationParser parser = parserFactory.getRouterParser(annotation, componentFactoryClass, null); |
167 | 0 | if (parser != null) |
168 | |
{ |
169 | 0 | ((ServiceCompositeMessageSource) service.getMessageSource()).addMessageProcessor(parser.parseMessageProcessor(annotation)); |
170 | |
} |
171 | |
else |
172 | |
{ |
173 | |
|
174 | 0 | throw new IllegalStateException("Cannot find parser for router annotation: " + annotation.toString()); |
175 | |
} |
176 | |
} |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
protected void processReplyRouters(Class componentFactoryClass, org.mule.api.service.Service service) throws MuleException |
181 | |
{ |
182 | 0 | List<AnnotationMetaData> annotations = AnnotationUtils.getClassAndMethodAnnotations(componentFactoryClass); |
183 | 0 | for (AnnotationMetaData metaData : annotations) |
184 | |
{ |
185 | 0 | Router routerAnnotation = metaData.getAnnotation().annotationType().getAnnotation(Router.class); |
186 | 0 | if (routerAnnotation != null && routerAnnotation.type() == RouterType.ReplyTo) |
187 | |
{ |
188 | |
|
189 | |
|
190 | 0 | MessageProcessorAnnotationParser parser = parserFactory.getRouterParser(metaData.getAnnotation(), metaData.getClazz(), metaData.getMember()); |
191 | 0 | if (parser != null) |
192 | |
{ |
193 | 0 | MessageProcessor router = parser.parseMessageProcessor(metaData.getAnnotation()); |
194 | |
|
195 | 0 | if (router instanceof MuleContextAware) |
196 | |
{ |
197 | 0 | ((MuleContextAware) router).setMuleContext(context); |
198 | |
} |
199 | 0 | if (router instanceof Initialisable) |
200 | |
{ |
201 | 0 | ((Initialisable) router).initialise(); |
202 | |
} |
203 | |
|
204 | |
break; |
205 | |
} |
206 | |
else |
207 | |
{ |
208 | |
|
209 | 0 | throw new IllegalStateException("Cannot find parser for router annotation: " + metaData.getAnnotation().toString()); |
210 | |
} |
211 | |
} |
212 | 0 | } |
213 | 0 | } |
214 | |
|
215 | |
protected OutboundRouter processOutboundRouter(Class componentFactoryClass) throws MuleException |
216 | |
{ |
217 | 0 | Collection routerParsers = context.getRegistry().lookupObjects(MessageProcessorAnnotationParser.class); |
218 | 0 | OutboundRouter router = null; |
219 | |
|
220 | 0 | List<AnnotationMetaData> annotations = AnnotationUtils.getClassAndMethodAnnotations(componentFactoryClass); |
221 | 0 | for (AnnotationMetaData metaData : annotations) |
222 | |
{ |
223 | 0 | Router routerAnnotation = metaData.getAnnotation().annotationType().getAnnotation(Router.class); |
224 | 0 | if (routerAnnotation != null && routerAnnotation.type() == RouterType.Outbound) |
225 | |
{ |
226 | 0 | if (router != null) |
227 | |
{ |
228 | |
|
229 | 0 | throw new IllegalStateException("You can only configure one outbound router on a service"); |
230 | |
} |
231 | 0 | MessageProcessorAnnotationParser parser = parserFactory.getRouterParser(metaData.getAnnotation(), metaData.getClazz(), metaData.getMember()); |
232 | 0 | if (parser != null) |
233 | |
{ |
234 | 0 | router = (OutboundRouter) parser.parseMessageProcessor(metaData.getAnnotation()); |
235 | |
} |
236 | |
else |
237 | |
{ |
238 | |
|
239 | 0 | throw new IllegalStateException("Cannot find parser for router annotation: " + metaData.getAnnotation().toString()); |
240 | |
} |
241 | |
} |
242 | 0 | } |
243 | 0 | if (router == null) |
244 | |
{ |
245 | 0 | router = new OutboundPassThroughRouter(); |
246 | |
} |
247 | |
|
248 | 0 | if (router instanceof MuleContextAware) |
249 | |
{ |
250 | 0 | ((MuleContextAware) router).setMuleContext(context); |
251 | |
} |
252 | 0 | router.initialise(); |
253 | 0 | return router; |
254 | |
} |
255 | |
|
256 | |
protected void processOutbound(Class componentFactoryClass, org.mule.api.service.Service service) throws MuleException |
257 | |
{ |
258 | 0 | OutboundRouter router = processOutboundRouter(componentFactoryClass); |
259 | |
|
260 | |
OutboundEndpoint outboundEndpoint; |
261 | 0 | List<AnnotationMetaData> annotations = AnnotationUtils.getClassAndMethodAnnotations(componentFactoryClass); |
262 | 0 | for (AnnotationMetaData annotation : annotations) |
263 | |
{ |
264 | 0 | outboundEndpoint = tryOutboundEndpointAnnotation(annotation, ChannelType.Outbound); |
265 | 0 | if (outboundEndpoint != null) |
266 | |
{ |
267 | 0 | router.addRoute(outboundEndpoint); |
268 | |
} |
269 | |
} |
270 | |
|
271 | 0 | if (router instanceof MuleContextAware) |
272 | |
{ |
273 | 0 | ((MuleContextAware) router).setMuleContext(context); |
274 | |
} |
275 | 0 | router.initialise(); |
276 | 0 | ((OutboundRouterCollection) service.getOutboundMessageProcessor()).addRoute(router); |
277 | 0 | } |
278 | |
|
279 | |
protected InboundEndpoint tryInboundEndpointAnnotation(AnnotationMetaData metaData, ChannelType channelType) throws MuleException |
280 | |
{ |
281 | 0 | Channel channelAnno = metaData.getAnnotation().annotationType().getAnnotation(Channel.class); |
282 | 0 | if (channelAnno != null && channelAnno.type() == channelType) |
283 | |
{ |
284 | 0 | EndpointAnnotationParser parser = parserFactory.getEndpointParser(metaData.getAnnotation(), metaData.getClazz(), metaData.getMember()); |
285 | 0 | if (parser == null) |
286 | |
{ |
287 | |
|
288 | 0 | throw new AnnotationException(AnnotationsMessages.createStaticMessage("No parser found for annotation: " + metaData)); |
289 | |
} |
290 | |
else |
291 | |
{ |
292 | 0 | return parser.parseInboundEndpoint(metaData.getAnnotation(), Collections.EMPTY_MAP); |
293 | |
} |
294 | |
} |
295 | 0 | return null; |
296 | |
} |
297 | |
|
298 | |
protected OutboundEndpoint tryOutboundEndpointAnnotation(AnnotationMetaData metaData, ChannelType channelType) throws MuleException |
299 | |
{ |
300 | 0 | Channel channelAnno = metaData.getAnnotation().annotationType().getAnnotation(Channel.class); |
301 | 0 | if (channelAnno != null && channelAnno.type() == channelType) |
302 | |
{ |
303 | 0 | EndpointAnnotationParser parser = parserFactory.getEndpointParser(metaData.getAnnotation(), metaData.getClazz(), metaData.getMember()); |
304 | 0 | if (parser == null) |
305 | |
{ |
306 | |
|
307 | 0 | throw new AnnotationException(AnnotationsMessages.createStaticMessage("No parser found for annotation: " + metaData)); |
308 | |
} |
309 | |
else |
310 | |
{ |
311 | 0 | return parser.parseOutboundEndpoint(metaData.getAnnotation(), Collections.EMPTY_MAP); |
312 | |
} |
313 | |
} |
314 | 0 | return null; |
315 | |
} |
316 | |
|
317 | |
|
318 | |
protected void processReply(Class componentFactoryClass, org.mule.api.service.Service service) throws MuleException |
319 | |
{ |
320 | |
|
321 | |
InboundEndpoint inboundEndpoint; |
322 | 0 | for (int i = 0; i < componentFactoryClass.getAnnotations().length; i++) |
323 | |
{ |
324 | 0 | Annotation annotation = componentFactoryClass.getAnnotations()[i]; |
325 | 0 | inboundEndpoint = tryInboundEndpointAnnotation( |
326 | |
new AnnotationMetaData(componentFactoryClass, null, ElementType.TYPE, annotation), ChannelType.Reply); |
327 | 0 | if (inboundEndpoint != null) |
328 | |
{ |
329 | 0 | service.getAsyncReplyMessageSource().addSource(inboundEndpoint); |
330 | |
} |
331 | |
} |
332 | |
|
333 | |
|
334 | 0 | processReplyRouters(componentFactoryClass, service); |
335 | 0 | } |
336 | |
|
337 | |
protected String getValue(String key) |
338 | |
{ |
339 | 0 | return parser.parse(regProps, key); |
340 | |
} |
341 | |
|
342 | |
} |