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