1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.service; |
12 | |
|
13 | |
import org.mule.RegistryContext; |
14 | |
import org.mule.api.MuleContext; |
15 | |
import org.mule.api.endpoint.EndpointURI; |
16 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
17 | |
import org.mule.api.registry.ServiceDescriptorFactory; |
18 | |
import org.mule.api.registry.ServiceException; |
19 | |
import org.mule.api.transport.Connector; |
20 | |
import org.mule.config.i18n.CoreMessages; |
21 | |
import org.mule.transport.AbstractConnector; |
22 | |
import org.mule.util.BeanUtils; |
23 | |
import org.mule.util.ObjectNameHelper; |
24 | |
|
25 | |
import java.util.Collection; |
26 | |
import java.util.Iterator; |
27 | |
|
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class TransportFactory |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | 2 | protected static final Log logger = LogFactory.getLog(TransportFactory.class); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static Connector createConnector(EndpointURI url, MuleContext muleContext) throws TransportFactoryException |
59 | |
{ |
60 | |
|
61 | |
try |
62 | |
{ |
63 | |
Connector connector; |
64 | 34 | String scheme = url.getSchemeMetaInfo(); |
65 | |
|
66 | 34 | TransportServiceDescriptor sd = (TransportServiceDescriptor) |
67 | |
RegistryContext.getRegistry().lookupServiceDescriptor(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, scheme, null); |
68 | 34 | if (sd == null) |
69 | |
{ |
70 | 0 | throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme)); |
71 | |
} |
72 | |
|
73 | 34 | connector = sd.createConnector(); |
74 | 34 | if (connector != null) |
75 | |
{ |
76 | 34 | if (connector instanceof AbstractConnector) |
77 | |
{ |
78 | 34 | ((AbstractConnector)connector).initialiseFromUrl(url); |
79 | |
} |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | throw new TransportFactoryException( |
84 | |
CoreMessages.objectNotSetInService("Connector", scheme)); |
85 | |
} |
86 | |
|
87 | 34 | connector.setName(ObjectNameHelper.getConnectorName(connector)); |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | 34 | return connector; |
103 | |
} |
104 | 0 | catch (Exception e) |
105 | |
{ |
106 | 0 | throw new TransportFactoryException( |
107 | |
CoreMessages.failedToCreateObjectWith("Endpoint", url), e); |
108 | |
} |
109 | |
} |
110 | |
|
111 | |
public static Connector getOrCreateConnectorByProtocol(ImmutableEndpoint endpoint, MuleContext muleContext) |
112 | |
throws TransportFactoryException |
113 | |
{ |
114 | 0 | return getOrCreateConnectorByProtocol(endpoint.getEndpointURI(), muleContext); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public static Connector getOrCreateConnectorByProtocol(EndpointURI uri, MuleContext muleContext) |
121 | |
throws TransportFactoryException |
122 | |
{ |
123 | 0 | String connectorName = uri.getConnectorName(); |
124 | 0 | if (null != connectorName) |
125 | |
{ |
126 | |
|
127 | 0 | Connector connector = RegistryContext.getRegistry().lookupConnector(connectorName); |
128 | 0 | if (connector != null) |
129 | |
{ |
130 | 0 | return connector; |
131 | |
} |
132 | |
} |
133 | |
|
134 | 0 | Connector connector = getConnectorByProtocol(uri.getFullScheme()); |
135 | 0 | if (connector == null) |
136 | |
{ |
137 | 0 | connector = createConnector(uri, muleContext); |
138 | |
try |
139 | |
{ |
140 | 0 | BeanUtils.populate(connector, uri.getParams()); |
141 | 0 | connector.setMuleContext(muleContext); |
142 | 0 | muleContext.getRegistry().registerConnector(connector); |
143 | |
} |
144 | 0 | catch (Exception e) |
145 | |
{ |
146 | 0 | throw new TransportFactoryException(e); |
147 | 0 | } |
148 | |
} |
149 | 0 | return connector; |
150 | |
} |
151 | |
|
152 | |
public static Connector getConnectorByProtocol(String protocol) |
153 | |
{ |
154 | |
Connector connector; |
155 | 44 | Connector resultConnector = null; |
156 | 44 | Collection connectors = RegistryContext.getRegistry().lookupObjects(Connector.class); |
157 | 44 | for (Iterator iterator = connectors.iterator(); iterator.hasNext();) |
158 | |
{ |
159 | 10 | connector = (Connector)iterator.next(); |
160 | 10 | if (connector.supportsProtocol(protocol)) |
161 | |
{ |
162 | 10 | if(resultConnector==null) |
163 | |
{ |
164 | 10 | resultConnector = connector; |
165 | |
} |
166 | |
else |
167 | |
{ |
168 | 0 | throw new IllegalStateException( |
169 | |
CoreMessages.moreThanOneConnectorWithProtocol(protocol).getMessage()); |
170 | |
} |
171 | |
} |
172 | |
} |
173 | 44 | return resultConnector; |
174 | |
} |
175 | |
} |