1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.endpoint; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.endpoint.EndpointBuilder; |
16 | |
import org.mule.api.endpoint.EndpointException; |
17 | |
import org.mule.api.endpoint.EndpointFactory; |
18 | |
import org.mule.api.endpoint.EndpointURI; |
19 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
20 | |
import org.mule.api.endpoint.InboundEndpoint; |
21 | |
import org.mule.api.endpoint.OutboundEndpoint; |
22 | |
import org.mule.api.registry.RegistrationException; |
23 | |
import org.mule.api.registry.ServiceType; |
24 | |
import org.mule.config.i18n.CoreMessages; |
25 | |
import org.mule.transport.service.TransportServiceDescriptor; |
26 | |
|
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
|
30 | 0 | public class DefaultEndpointFactory implements EndpointFactory |
31 | |
{ |
32 | |
|
33 | |
|
34 | |
|
35 | 0 | protected static final Log logger = LogFactory.getLog(DefaultEndpointFactory.class); |
36 | |
|
37 | |
public static final String ENDPOINT_REGISTRY_PREFIX = "endpoint:"; |
38 | |
|
39 | |
protected MuleContext muleContext; |
40 | |
|
41 | |
public InboundEndpoint getInboundEndpoint(String uri) |
42 | |
throws MuleException |
43 | |
{ |
44 | 0 | logger.debug("DefaultEndpointFactory request for inbound endpoint for uri: " + uri); |
45 | 0 | EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri); |
46 | 0 | if (endpointBuilder == null) |
47 | |
{ |
48 | 0 | logger.debug("Named EndpointBuilder not found, creating endpoint from uri"); |
49 | 0 | endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext); |
50 | |
} |
51 | 0 | return getInboundEndpoint(endpointBuilder); |
52 | |
} |
53 | |
|
54 | |
public OutboundEndpoint getOutboundEndpoint(String uri) |
55 | |
throws MuleException |
56 | |
{ |
57 | 0 | logger.debug("DefaultEndpointFactory request for outbound endpoint for uri: " + uri); |
58 | 0 | EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri); |
59 | 0 | if (endpointBuilder == null) |
60 | |
{ |
61 | 0 | logger.debug("Named EndpointBuilder not found, creating endpoint from uri"); |
62 | 0 | endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext); |
63 | |
|
64 | |
} |
65 | 0 | return getOutboundEndpoint(endpointBuilder); |
66 | |
} |
67 | |
|
68 | |
protected EndpointBuilder lookupEndpointBuilder(String endpointName) |
69 | |
{ |
70 | 0 | logger.debug("Looking up EndpointBuilder with name:" + endpointName + " in registry"); |
71 | |
|
72 | |
|
73 | 0 | EndpointBuilder endpointBuilder = muleContext.getRegistry().lookupEndpointBuilder(endpointName); |
74 | 0 | if (endpointBuilder != null) |
75 | |
{ |
76 | 0 | logger.debug("EndpointBuilder with name:" + endpointName + " FOUND"); |
77 | |
} |
78 | 0 | return endpointBuilder; |
79 | |
} |
80 | |
|
81 | |
public InboundEndpoint getInboundEndpoint(EndpointBuilder builder) throws MuleException |
82 | |
{ |
83 | 0 | InboundEndpoint endpoint = builder.buildInboundEndpoint(); |
84 | 0 | return (InboundEndpoint) registerEndpoint(endpoint); |
85 | |
} |
86 | |
|
87 | |
public OutboundEndpoint getOutboundEndpoint(EndpointBuilder builder) throws MuleException |
88 | |
{ |
89 | 0 | OutboundEndpoint endpoint = builder.buildOutboundEndpoint(); |
90 | 0 | return (OutboundEndpoint) registerEndpoint(endpoint); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
protected ImmutableEndpoint registerEndpoint(ImmutableEndpoint endpoint) throws RegistrationException |
98 | |
{ |
99 | 0 | ImmutableEndpoint registryEndpoint = (ImmutableEndpoint) muleContext.getRegistry().lookupObject( |
100 | |
ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode()); |
101 | 0 | if (registryEndpoint == null) |
102 | |
{ |
103 | 0 | muleContext.getRegistry().registerObject(ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode(), endpoint); |
104 | 0 | registryEndpoint = endpoint; |
105 | |
} |
106 | 0 | return registryEndpoint; |
107 | |
} |
108 | |
|
109 | |
public EndpointBuilder getEndpointBuilder(String uri) |
110 | |
throws MuleException |
111 | |
{ |
112 | 0 | logger.debug("DefaultEndpointFactory request for endpoint builder for uri: " + uri); |
113 | 0 | EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri); |
114 | 0 | if (endpointBuilder != null) |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | endpointBuilder = (EndpointBuilder) endpointBuilder.clone(); |
119 | |
} |
120 | 0 | catch (Exception e) |
121 | |
{ |
122 | 0 | throw new EndpointException(CoreMessages.failedToClone("global endpoint EndpointBuilder"), e); |
123 | 0 | } |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | logger.debug("Named EndpointBuilder not found, creating endpoint builder for uri"); |
128 | 0 | EndpointURI epURI = new MuleEndpointURI(uri, muleContext); |
129 | 0 | TransportServiceDescriptor tsd = (TransportServiceDescriptor) muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, epURI.getFullScheme(), null); |
130 | 0 | endpointBuilder = tsd.createEndpointBuilder(uri); |
131 | |
} |
132 | 0 | return endpointBuilder; |
133 | |
} |
134 | |
|
135 | |
public void setMuleContext(MuleContext context) |
136 | |
{ |
137 | 0 | this.muleContext = context; |
138 | 0 | } |
139 | |
|
140 | |
public org.mule.api.endpoint.InboundEndpoint getInboundEndpoint(EndpointURI uri) throws MuleException |
141 | |
{ |
142 | 0 | return (InboundEndpoint) getEndpoint(uri, new EndpointSource() |
143 | 0 | { |
144 | |
public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException |
145 | |
{ |
146 | 0 | return getInboundEndpoint(builder); |
147 | |
} |
148 | |
}); |
149 | |
} |
150 | |
|
151 | |
public OutboundEndpoint getOutboundEndpoint(EndpointURI uri) throws MuleException |
152 | |
{ |
153 | 0 | return (OutboundEndpoint) getEndpoint(uri, new EndpointSource() |
154 | 0 | { |
155 | |
public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException |
156 | |
{ |
157 | 0 | return getOutboundEndpoint(builder); |
158 | |
} |
159 | |
}); |
160 | |
} |
161 | |
|
162 | |
protected ImmutableEndpoint getEndpoint(EndpointURI uri, EndpointSource source) throws MuleException |
163 | |
{ |
164 | 0 | logger.debug("DefaultEndpointFactory request for endpoint for: " + uri); |
165 | 0 | EndpointBuilder endpointBuilder = null; |
166 | 0 | if (uri.getEndpointName() != null) |
167 | |
{ |
168 | 0 | endpointBuilder = lookupEndpointBuilder(uri.getEndpointName()); |
169 | 0 | if (endpointBuilder == null) |
170 | |
{ |
171 | 0 | throw new IllegalArgumentException("The endpoint with name: " + uri.getEndpointName() |
172 | |
+ "was not found."); |
173 | |
} |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | logger.debug("Named EndpointBuilder not found, creating endpoint from uri"); |
178 | 0 | endpointBuilder = new EndpointURIEndpointBuilder(uri); |
179 | |
} |
180 | 0 | return source.getEndpoint(endpointBuilder); |
181 | |
} |
182 | |
|
183 | 0 | private interface EndpointSource |
184 | |
{ |
185 | |
ImmutableEndpoint getEndpoint(EndpointBuilder endpointBuilder) throws MuleException; |
186 | |
} |
187 | |
|
188 | |
} |