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 public class TransportFactory
39 {
40
41
42
43 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 String scheme = url.getSchemeMetaInfo();
65
66 TransportServiceDescriptor sd = (TransportServiceDescriptor)
67 RegistryContext.getRegistry().lookupServiceDescriptor(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, scheme, null);
68 if (sd == null)
69 {
70 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
71 }
72
73 connector = sd.createConnector();
74 if (connector != null)
75 {
76 if (connector instanceof AbstractConnector)
77 {
78 ((AbstractConnector)connector).initialiseFromUrl(url);
79 }
80 }
81 else
82 {
83 throw new TransportFactoryException(
84 CoreMessages.objectNotSetInService("Connector", scheme));
85 }
86
87 connector.setName(ObjectNameHelper.getConnectorName(connector));
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 return connector;
103 }
104 catch (Exception e)
105 {
106 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 return getOrCreateConnectorByProtocol(endpoint.getEndpointURI(), muleContext);
115 }
116
117
118
119
120 public static Connector getOrCreateConnectorByProtocol(EndpointURI uri, MuleContext muleContext)
121 throws TransportFactoryException
122 {
123 String connectorName = uri.getConnectorName();
124 if (null != connectorName)
125 {
126
127 Connector connector = RegistryContext.getRegistry().lookupConnector(connectorName);
128 if (connector != null)
129 {
130 return connector;
131 }
132 }
133
134 Connector connector = getConnectorByProtocol(uri.getFullScheme());
135 if (connector == null)
136 {
137 connector = createConnector(uri, muleContext);
138 try
139 {
140 BeanUtils.populate(connector, uri.getParams());
141 connector.setMuleContext(muleContext);
142 muleContext.getRegistry().registerConnector(connector);
143 }
144 catch (Exception e)
145 {
146 throw new TransportFactoryException(e);
147 }
148 }
149 return connector;
150 }
151
152 public static Connector getConnectorByProtocol(String protocol)
153 {
154 Connector connector;
155 Connector resultConnector = null;
156 Collection connectors = RegistryContext.getRegistry().lookupObjects(Connector.class);
157 for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
158 {
159 connector = (Connector)iterator.next();
160 if (connector.supportsProtocol(protocol))
161 {
162 if(resultConnector==null)
163 {
164 resultConnector = connector;
165 }
166 else
167 {
168 throw new IllegalStateException(
169 CoreMessages.moreThanOneConnectorWithProtocol(protocol).getMessage());
170 }
171 }
172 }
173 return resultConnector;
174 }
175 }