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