1
2
3
4
5
6
7
8
9
10 package org.mule.module.ibeans.config;
11
12 import org.mule.MessageExchangePattern;
13 import org.mule.api.MuleException;
14 import org.mule.api.annotations.meta.Channel;
15 import org.mule.api.annotations.meta.ChannelType;
16 import org.mule.api.endpoint.InboundEndpoint;
17 import org.mule.api.endpoint.OutboundEndpoint;
18 import org.mule.config.endpoint.AbstractEndpointAnnotationParser;
19 import org.mule.config.endpoint.AnnotatedEndpointData;
20 import org.mule.module.ibeans.i18n.IBeansMessages;
21 import org.mule.module.ibeans.spi.support.CallOutboundEndpoint;
22 import org.mule.module.ibeans.spi.support.CallRequestEndpoint;
23
24 import java.beans.ExceptionListener;
25 import java.lang.annotation.Annotation;
26 import java.lang.reflect.Member;
27 import java.lang.reflect.Method;
28 import java.util.Map;
29
30 import org.ibeans.annotation.Call;
31 import org.ibeans.api.CallException;
32 import org.ibeans.api.ExceptionListenerAware;
33
34
35
36
37 public class CallAnnotationParser extends AbstractEndpointAnnotationParser
38 {
39 protected AnnotatedEndpointData createEndpointData(Annotation annotation) throws MuleException
40 {
41 Call call = (Call) annotation;
42 AnnotatedEndpointData epd = new AnnotatedEndpointData(MessageExchangePattern.REQUEST_RESPONSE, ChannelType.Outbound, call);
43 epd.setAddress(call.uri());
44 epd.setProperties(AnnotatedEndpointData.convert(call.properties()));
45 return epd;
46 }
47
48 protected String getIdentifier()
49 {
50 return Call.class.getAnnotation(Channel.class).identifer();
51 }
52
53 @Override
54 public boolean supports(Annotation annotation, Class clazz, Member member)
55 {
56
57 boolean supports = clazz.isInterface();
58 if (supports)
59 {
60 supports = annotation instanceof Call;
61 }
62 if (supports)
63 {
64
65 if (ExceptionListenerAware.class.isAssignableFrom(clazz))
66 {
67 supports = true;
68 }
69 else
70 {
71 Class[] exceptionTypes = ((Method) member).getExceptionTypes();
72 boolean hasValidExceptionType = false;
73 for (int i = 0; i < exceptionTypes.length; i++)
74 {
75 Class exceptionType = exceptionTypes[i];
76 hasValidExceptionType = exceptionType.equals(Exception.class) || exceptionType.isAssignableFrom(CallException.class) || clazz.isAssignableFrom(ExceptionListener.class);
77 }
78 if (!hasValidExceptionType)
79 {
80 throw new IllegalArgumentException(IBeansMessages.illegalCallMethod((Method)member).getMessage());
81 }
82 }
83 }
84 return supports;
85 }
86
87 public OutboundEndpoint parseOutboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
88 {
89 AnnotatedEndpointData data = createEndpointData(annotation);
90 if (data.getConnectorName() == null)
91 {
92 data.setConnectorName((String) metaInfo.get("connectorName"));
93 }
94 return new CallOutboundEndpoint(muleContext, data);
95 }
96
97 public InboundEndpoint parseInboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
98 {
99 AnnotatedEndpointData data = createEndpointData(annotation);
100 if (data.getConnectorName() == null)
101 {
102 data.setConnectorName((String) metaInfo.get("connectorName"));
103 }
104 return new CallRequestEndpoint(muleContext, data);
105 }
106 }