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