1
2
3
4
5
6
7
8
9
10 package org.mule.config.endpoint;
11
12 import org.mule.api.EndpointAnnotationParser;
13 import org.mule.api.MuleContext;
14 import org.mule.api.MuleException;
15 import org.mule.api.annotations.meta.Channel;
16 import org.mule.api.config.ConfigurationException;
17 import org.mule.api.context.MuleContextAware;
18 import org.mule.api.endpoint.InboundEndpoint;
19 import org.mule.api.endpoint.OutboundEndpoint;
20 import org.mule.config.i18n.CoreMessages;
21 import org.mule.util.StringUtils;
22
23 import java.lang.annotation.Annotation;
24 import java.lang.reflect.Member;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.StringTokenizer;
28
29
30
31
32 public abstract class AbstractEndpointAnnotationParser implements EndpointAnnotationParser, MuleContextAware
33 {
34 public static final String ENDPOINT_BUILDER_POSTFIX = ".builder";
35
36 protected MuleContext muleContext;
37
38 public void setMuleContext(MuleContext context)
39 {
40 this.muleContext = context;
41 }
42
43 protected AnnotatedEndpointHelper getEndpointHelper() throws MuleException
44 {
45 AnnotatedEndpointHelper helper = muleContext.getRegistry().lookupObject(AnnotatedEndpointHelper.class);
46 if (helper == null)
47 {
48 helper = new AnnotatedEndpointHelper(muleContext);
49 }
50 return helper;
51 }
52
53 public OutboundEndpoint parseOutboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
54 {
55 return (OutboundEndpoint) getEndpointHelper().processEndpoint(createEndpointData(annotation));
56 }
57
58 public InboundEndpoint parseInboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
59 {
60 return (InboundEndpoint) getEndpointHelper().processEndpoint(createEndpointData(annotation));
61 }
62
63 public boolean supports(Annotation annotation, Class clazz, Member member)
64 {
65 Channel channel = annotation.annotationType().getAnnotation(Channel.class);
66 return channel != null && channel.identifer().equals(getIdentifier());
67 }
68
69 protected Properties convertProperties(String[] properties)
70 {
71 if(properties==null || properties.length==0)
72 {
73 return null;
74 }
75
76 Properties props = new Properties();
77 for (String property : properties)
78 {
79 StringTokenizer st = new StringTokenizer(property, "=");
80 if(st.hasMoreTokens())
81 {
82 props.setProperty(st.nextToken().trim(), st.nextToken().trim());
83 }
84 }
85 return props;
86 }
87
88 protected <T> T lookupConfig(String location, Class<T> type) throws ConfigurationException
89 {
90 if (StringUtils.isEmpty(location))
91 {
92 return null;
93 }
94 Object o = muleContext.getRegistry().lookupObject(location + ENDPOINT_BUILDER_POSTFIX);
95 if (o == null)
96 {
97 o = muleContext.getRegistry().lookupObject(location);
98 if (o == null)
99 {
100 return null;
101
102 }
103 }
104 if (type.isInstance(o))
105 {
106 return (T) o;
107 }
108 else
109 {
110 throw new ConfigurationException(CoreMessages.objectNotOfCorrectType(o.getClass(), type));
111 }
112 }
113
114 protected abstract String getIdentifier();
115
116 protected abstract AnnotatedEndpointData createEndpointData(Annotation annotation) throws MuleException;
117 }