1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.service;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.endpoint.EndpointException;
15 import org.mule.api.endpoint.EndpointURI;
16 import org.mule.api.endpoint.ImmutableEndpoint;
17 import org.mule.api.registry.ServiceException;
18 import org.mule.api.registry.ServiceType;
19 import org.mule.api.transport.Connector;
20 import org.mule.config.i18n.CoreMessages;
21 import org.mule.endpoint.MuleEndpointURI;
22 import org.mule.transport.AbstractConnector;
23 import org.mule.util.BeanUtils;
24 import org.mule.util.ObjectNameHelper;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35
36
37
38
39
40 public class TransportFactory
41 {
42
43
44
45 protected static final Log logger = LogFactory.getLog(TransportFactory.class);
46
47 protected MuleContext muleContext;
48
49 public TransportFactory(MuleContext muleContext)
50 {
51 this.muleContext = muleContext;
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public Connector createConnector(EndpointURI url) throws TransportFactoryException
68 {
69
70 try
71 {
72 Connector connector;
73 String scheme = url.getFullScheme();
74
75 TransportServiceDescriptor sd = (TransportServiceDescriptor)
76 muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, scheme, null);
77 if (sd == null)
78 {
79 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
80 }
81
82 connector = sd.createConnector();
83 if (connector != null)
84 {
85 if (connector instanceof AbstractConnector)
86 {
87 ((AbstractConnector) connector).initialiseFromUrl(url);
88 }
89 }
90 else
91 {
92 throw new TransportFactoryException(
93 CoreMessages.objectNotSetInService("Connector", scheme));
94 }
95
96 connector.setName(new ObjectNameHelper(muleContext).getConnectorName(connector));
97
98 return connector;
99 }
100 catch (Exception e)
101 {
102 throw new TransportFactoryException(
103 CoreMessages.failedToCreateObjectWith("Endpoint", url), e);
104 }
105 }
106
107 public Connector createConnector(String uri) throws TransportFactoryException
108 {
109 try
110 {
111 return createConnector(new MuleEndpointURI(uri, muleContext));
112 }
113 catch (EndpointException e)
114 {
115 throw new TransportFactoryException(e);
116 }
117 }
118
119 public Connector getOrCreateConnectorByProtocol(ImmutableEndpoint endpoint)
120 throws TransportFactoryException
121 {
122 return getOrCreateConnectorByProtocol(endpoint.getEndpointURI());
123 }
124
125
126
127
128 public Connector getOrCreateConnectorByProtocol(EndpointURI uri)
129 throws TransportFactoryException
130 {
131 String connectorName = uri.getConnectorName();
132 if (null != connectorName)
133 {
134
135 Connector connector = muleContext.getRegistry().lookupConnector(connectorName);
136 if (connector != null)
137 {
138 return connector;
139 }
140 }
141
142 Connector connector = getConnectorByProtocol(uri.getFullScheme());
143 if (connector == null)
144 {
145 connector = createConnector(uri);
146 try
147 {
148 BeanUtils.populate(connector, uri.getParams());
149 muleContext.getRegistry().registerConnector(connector);
150 }
151 catch (Exception e)
152 {
153 throw new TransportFactoryException(e);
154 }
155 }
156 return connector;
157 }
158
159 public Connector getConnectorByProtocol(String protocol)
160 {
161 Connector connector;
162 List<Connector> results = new ArrayList<Connector>();
163 Collection connectors = muleContext.getRegistry().lookupObjects(Connector.class);
164 for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
165 {
166 connector = (Connector) iterator.next();
167 if (connector.supportsProtocol(protocol))
168 {
169 results.add(connector);
170 }
171 }
172 if (results.size() > 1)
173 {
174 StringBuffer buf = new StringBuffer();
175 for (Connector result : results)
176 {
177 buf.append(result.getName()).append(", ");
178 }
179 throw new IllegalStateException(
180 CoreMessages.moreThanOneConnectorWithProtocol(protocol, buf.toString()).getMessage());
181 }
182 else if (results.size() == 1)
183 {
184 return results.get(0);
185 }
186 else
187 {
188 return null;
189 }
190 }
191
192 public Connector getDefaultConnectorByProtocol(String protocol)
193 {
194 Connector connector;
195 List<Connector> results = new ArrayList<Connector>();
196 Collection connectors = muleContext.getRegistry().lookupObjects(Connector.class);
197 for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
198 {
199 connector = (Connector) iterator.next();
200 if (connector.supportsProtocol(protocol) && ObjectNameHelper.isDefaultAutoGeneratedConnector(connector))
201 {
202 results.add(connector);
203 }
204 }
205 if (results.size() > 1)
206 {
207 StringBuffer buf = new StringBuffer();
208 for (Connector result : results)
209 {
210 buf.append(result.getName()).append(", ");
211 }
212 throw new IllegalStateException(
213 CoreMessages.moreThanOneConnectorWithProtocol(protocol, buf.toString()).getMessage());
214 }
215 else if (results.size() == 1)
216 {
217 return results.get(0);
218 }
219 else
220 {
221 return null;
222 }
223 }
224 }