1
2
3
4
5
6
7
8
9
10 package org.mule.config.endpoint;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.MuleException;
14 import org.mule.api.annotations.meta.ChannelType;
15 import org.mule.api.endpoint.EndpointBuilder;
16 import org.mule.api.endpoint.ImmutableEndpoint;
17 import org.mule.api.expression.PropertyConverter;
18 import org.mule.api.routing.filter.Filter;
19 import org.mule.api.transformer.Transformer;
20 import org.mule.config.i18n.AnnotationsMessages;
21 import org.mule.endpoint.MuleEndpointURI;
22 import org.mule.registry.RegistryMap;
23 import org.mule.routing.MessageFilter;
24 import org.mule.transport.AbstractConnector;
25 import org.mule.transport.service.TransportFactory;
26 import org.mule.util.TemplateParser;
27
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.List;
31
32
33
34
35
36
37
38 public class AnnotatedEndpointHelper
39 {
40 protected TemplateParser parser = TemplateParser.createAntStyleParser();
41
42 protected RegistryMap regMap;
43 protected MuleContext muleContext;
44 protected TransportFactory transportFactory;
45
46 public AnnotatedEndpointHelper(MuleContext muleContext) throws MuleException
47 {
48 this.muleContext = muleContext;
49 this.transportFactory = new ConfigurableTransportFactory(muleContext);
50 regMap = new RegistryMap(muleContext.getRegistry());
51 }
52
53 protected String parsePlaceholderValues(String key)
54 {
55 return parser.parse(regMap, key);
56 }
57
58 protected EndpointBuilder getEndpointBuilder(AnnotatedEndpointData epData) throws MuleException
59 {
60 String uri = parsePlaceholderValues(epData.getAddress());
61
62 EndpointBuilder endpointBuilder = muleContext.getEndpointFactory().getEndpointBuilder(uri);
63 endpointBuilder.setMuleContext(muleContext);
64
65 return endpointBuilder;
66 }
67
68 public ImmutableEndpoint processEndpoint(AnnotatedEndpointData epData) throws MuleException
69 {
70 preprocessEndpointData(epData);
71
72 ImmutableEndpoint endpoint;
73 EndpointBuilder endpointBuilder = getEndpointBuilder(epData);
74
75 if (epData.getProperties() != null && epData.getProperties().size() > 0)
76 {
77 endpointBuilder.setProperties(epData.getProperties());
78 }
79
80 if (epData.getTransformers() != null)
81 {
82 List<Transformer> transformers = (List) convertProperty(List.class, epData.getTransformers());
83 endpointBuilder.setTransformers(transformers);
84 }
85
86 if (epData.getFilter() != null)
87 {
88 Filter filter = (Filter) convertProperty(Filter.class, epData.getFilter());
89 endpointBuilder.addMessageProcessor(new MessageFilter(filter));
90
91 }
92
93 if (epData.getEncoding() != null)
94 {
95 endpointBuilder.setEncoding(parsePlaceholderValues(epData.getEncoding()));
96 }
97
98 AbstractConnector connector;
99 if (epData.getConnectorName() != null)
100 {
101 connector = (AbstractConnector) muleContext.getRegistry().lookupConnector(parsePlaceholderValues(epData.getConnectorName()));
102 }
103 else if (epData.getConnector() != null)
104 {
105 connector = (AbstractConnector) epData.getConnector();
106 }
107 else
108 {
109
110 MuleEndpointURI uri = new MuleEndpointURI(parsePlaceholderValues(epData.getAddress()), muleContext);
111
112 connector = (AbstractConnector) transportFactory.createConnector(uri);
113
114 if (muleContext.getRegistry().lookupConnector(connector.getName()) == null)
115 {
116 muleContext.getRegistry().registerConnector(connector);
117 }
118 }
119 endpointBuilder.setConnector(connector);
120
121
122
123 String threadsString = (String) epData.getProperties().get("threads");
124 if (threadsString != null)
125 {
126 int threads = Integer.valueOf(threadsString);
127 connector.setMaxDispatchersActive(threads);
128 connector.setMaxRequestersActive(threads);
129 connector.getReceiverThreadingProfile().setMaxThreadsActive(threads);
130 connector.getReceiverThreadingProfile().setMaxThreadsIdle(threads);
131 }
132
133 if (epData.getName() != null)
134 {
135 endpointBuilder.setName(parsePlaceholderValues(epData.getName()));
136 }
137
138 endpointBuilder.setExchangePattern(epData.getMep());
139
140 if (epData.getType() == ChannelType.Inbound)
141 {
142 endpoint = endpointBuilder.buildInboundEndpoint();
143 }
144 else if (epData.getType() == ChannelType.Outbound)
145 {
146 endpoint = endpointBuilder.buildOutboundEndpoint();
147 }
148 else
149 {
150 throw new IllegalArgumentException("Channel type not recognised: " + epData.getType());
151 }
152
153 if (epData.getName() != null)
154 {
155 muleContext.getRegistry().registerEndpointBuilder(epData.getName(), endpointBuilder);
156 }
157 return endpoint;
158 }
159
160
161
162
163
164
165
166 protected void preprocessEndpointData(AnnotatedEndpointData data)
167 {
168
169 }
170
171 public Object convertProperty(Class type, String property)
172 {
173 String prop = parsePlaceholderValues(property);
174 Collection c = muleContext.getRegistry().lookupObjects(PropertyConverter.class);
175 for (Iterator iterator = c.iterator(); iterator.hasNext();)
176 {
177 PropertyConverter converter = (PropertyConverter) iterator.next();
178 if (converter.getType().equals(type))
179 {
180 return converter.convert(prop, muleContext);
181 }
182 }
183 throw new IllegalArgumentException(AnnotationsMessages.noPropertyConverterForType(type).getMessage());
184 }
185
186 }