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 | 0 | protected static final Log logger = LogFactory.getLog(TransportFactory.class); |
46 | |
|
47 | |
protected MuleContext muleContext; |
48 | |
|
49 | |
public TransportFactory(MuleContext muleContext) |
50 | 0 | { |
51 | 0 | this.muleContext = muleContext; |
52 | 0 | } |
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 | 0 | String scheme = url.getFullScheme(); |
74 | |
|
75 | 0 | TransportServiceDescriptor sd = (TransportServiceDescriptor) |
76 | |
muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, scheme, null); |
77 | 0 | if (sd == null) |
78 | |
{ |
79 | 0 | throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme)); |
80 | |
} |
81 | |
|
82 | 0 | connector = sd.createConnector(); |
83 | 0 | if (connector != null) |
84 | |
{ |
85 | 0 | if (connector instanceof AbstractConnector) |
86 | |
{ |
87 | 0 | ((AbstractConnector) connector).initialiseFromUrl(url); |
88 | |
} |
89 | |
} |
90 | |
else |
91 | |
{ |
92 | 0 | throw new TransportFactoryException( |
93 | |
CoreMessages.objectNotSetInService("Connector", scheme)); |
94 | |
} |
95 | |
|
96 | 0 | connector.setName(new ObjectNameHelper(muleContext).getConnectorName(connector)); |
97 | |
|
98 | 0 | return connector; |
99 | |
} |
100 | 0 | catch (Exception e) |
101 | |
{ |
102 | 0 | 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 | 0 | return createConnector(new MuleEndpointURI(uri, muleContext)); |
112 | |
} |
113 | 0 | catch (EndpointException e) |
114 | |
{ |
115 | 0 | throw new TransportFactoryException(e); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
public Connector getOrCreateConnectorByProtocol(ImmutableEndpoint endpoint) |
120 | |
throws TransportFactoryException |
121 | |
{ |
122 | 0 | return getOrCreateConnectorByProtocol(endpoint.getEndpointURI()); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public Connector getOrCreateConnectorByProtocol(EndpointURI uri) |
129 | |
throws TransportFactoryException |
130 | |
{ |
131 | 0 | String connectorName = uri.getConnectorName(); |
132 | 0 | if (null != connectorName) |
133 | |
{ |
134 | |
|
135 | 0 | Connector connector = muleContext.getRegistry().lookupConnector(connectorName); |
136 | 0 | if (connector != null) |
137 | |
{ |
138 | 0 | return connector; |
139 | |
} |
140 | |
} |
141 | |
|
142 | 0 | Connector connector = getConnectorByProtocol(uri.getFullScheme()); |
143 | 0 | if (connector == null) |
144 | |
{ |
145 | 0 | connector = createConnector(uri); |
146 | |
try |
147 | |
{ |
148 | 0 | BeanUtils.populate(connector, uri.getParams()); |
149 | 0 | muleContext.getRegistry().registerConnector(connector); |
150 | |
} |
151 | 0 | catch (Exception e) |
152 | |
{ |
153 | 0 | throw new TransportFactoryException(e); |
154 | 0 | } |
155 | |
} |
156 | 0 | return connector; |
157 | |
} |
158 | |
|
159 | |
public Connector getConnectorByProtocol(String protocol) |
160 | |
{ |
161 | |
Connector connector; |
162 | 0 | List<Connector> results = new ArrayList<Connector>(); |
163 | 0 | Collection connectors = muleContext.getRegistry().lookupObjects(Connector.class); |
164 | 0 | for (Iterator iterator = connectors.iterator(); iterator.hasNext();) |
165 | |
{ |
166 | 0 | connector = (Connector) iterator.next(); |
167 | 0 | if (connector.supportsProtocol(protocol)) |
168 | |
{ |
169 | 0 | results.add(connector); |
170 | |
} |
171 | |
} |
172 | 0 | if (results.size() > 1) |
173 | |
{ |
174 | 0 | StringBuffer buf = new StringBuffer(); |
175 | 0 | for (Connector result : results) |
176 | |
{ |
177 | 0 | buf.append(result.getName()).append(", "); |
178 | |
} |
179 | 0 | throw new IllegalStateException( |
180 | |
CoreMessages.moreThanOneConnectorWithProtocol(protocol, buf.toString()).getMessage()); |
181 | |
} |
182 | 0 | else if (results.size() == 1) |
183 | |
{ |
184 | 0 | return results.get(0); |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 0 | return null; |
189 | |
} |
190 | |
} |
191 | |
} |