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