001: /*
002: * $Id: DefaultEndpointFactory.java 11278 2008-03-07 20:26:34Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.endpoint;
012:
013: import org.mule.RegistryContext;
014: import org.mule.api.MuleContext;
015: import org.mule.api.MuleException;
016: import org.mule.api.endpoint.EndpointBuilder;
017: import org.mule.api.endpoint.EndpointException;
018: import org.mule.api.endpoint.EndpointFactory;
019: import org.mule.api.endpoint.EndpointURI;
020: import org.mule.api.endpoint.ImmutableEndpoint;
021: import org.mule.api.endpoint.InboundEndpoint;
022: import org.mule.api.endpoint.OutboundEndpoint;
023: import org.mule.api.registry.RegistrationException;
024: import org.mule.config.i18n.CoreMessages;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: public class DefaultEndpointFactory implements EndpointFactory {
030: /** logger used by this class */
031: protected static final Log logger = LogFactory
032: .getLog(DefaultEndpointFactory.class);
033:
034: public static final String ENDPOINT_REGISTRY_PREFIX = "endpoint:";
035:
036: protected MuleContext muleContext;
037:
038: public InboundEndpoint getInboundEndpoint(String uri)
039: throws MuleException {
040: logger
041: .debug("DefaultEndpointFactory request for inbound endpoint for uri: "
042: + uri);
043: EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
044: if (endpointBuilder == null) {
045: logger
046: .debug("Named EndpointBuilder not found, creating endpoint from uri");
047: endpointBuilder = new EndpointURIEndpointBuilder(uri,
048: muleContext);
049: }
050: return getInboundEndpoint(endpointBuilder);
051: }
052:
053: public OutboundEndpoint getOutboundEndpoint(String uri)
054: throws MuleException {
055: logger
056: .debug("DefaultEndpointFactory request for outbound endpoint for uri: "
057: + uri);
058: EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
059: if (endpointBuilder == null) {
060: logger
061: .debug("Named EndpointBuilder not found, creating endpoint from uri");
062: endpointBuilder = new EndpointURIEndpointBuilder(uri,
063: muleContext);
064:
065: }
066: return getOutboundEndpoint(endpointBuilder);
067: }
068:
069: protected EndpointBuilder lookupEndpointBuilder(String endpointName) {
070: logger.debug("Looking up EndpointBuilder with name:"
071: + endpointName + " in registry");
072: // TODO DF: Do some simple parsing of endpointName to not lookup endpoint builder if endpointName is
073: // obviously a uri and not a substituted name ??
074: EndpointBuilder endpointBuilder = RegistryContext.getRegistry()
075: .lookupEndpointBuilder(endpointName);
076: if (endpointBuilder != null) {
077: logger.debug("EndpointBuilder with name:" + endpointName
078: + " FOUND");
079: }
080: return endpointBuilder;
081: }
082:
083: public InboundEndpoint getInboundEndpoint(EndpointBuilder builder)
084: throws MuleException {
085: InboundEndpoint endpoint = builder.buildInboundEndpoint();
086: return (InboundEndpoint) registerEndpoint(endpoint);
087: }
088:
089: public OutboundEndpoint getOutboundEndpoint(EndpointBuilder builder)
090: throws MuleException {
091: OutboundEndpoint endpoint = builder.buildOutboundEndpoint();
092: return (OutboundEndpoint) registerEndpoint(endpoint);
093: }
094:
095: /**
096: *
097: * @param endpoint
098: * @return
099: * @throws RegistrationException
100: */
101: protected ImmutableEndpoint registerEndpoint(
102: ImmutableEndpoint endpoint) throws RegistrationException {
103: ImmutableEndpoint registryEndpoint = (ImmutableEndpoint) muleContext
104: .getRegistry().lookupObject(
105: ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode());
106: if (registryEndpoint == null) {
107: muleContext.getRegistry().registerObject(
108: ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode(),
109: endpoint);
110: registryEndpoint = endpoint;
111: }
112: return registryEndpoint;
113: }
114:
115: public EndpointBuilder getEndpointBuilder(String uri)
116: throws MuleException {
117: logger
118: .debug("DefaultEndpointFactory request for endpoint builder for uri: "
119: + uri);
120: EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
121: if (endpointBuilder != null) {
122: try {
123: endpointBuilder = (EndpointBuilder) endpointBuilder
124: .clone();
125: } catch (Exception e) {
126: throw new EndpointException(
127: CoreMessages
128: .failedToClone("global endpoint EndpointBuilder"),
129: e);
130: }
131: } else {
132: logger
133: .debug("Named EndpointBuilder not found, creating endpoint builder for uri");
134: endpointBuilder = new EndpointURIEndpointBuilder(uri,
135: muleContext);
136: }
137: return endpointBuilder;
138: }
139:
140: public void setMuleContext(MuleContext context) {
141: this .muleContext = context;
142: }
143:
144: public org.mule.api.endpoint.InboundEndpoint getInboundEndpoint(
145: EndpointURI uri) throws MuleException {
146: return (InboundEndpoint) getEndpoint(uri, new EndpointSource() {
147: public ImmutableEndpoint getEndpoint(EndpointBuilder builder)
148: throws MuleException {
149: return getInboundEndpoint(builder);
150: }
151: });
152: }
153:
154: public OutboundEndpoint getOutboundEndpoint(EndpointURI uri)
155: throws MuleException {
156: return (OutboundEndpoint) getEndpoint(uri,
157: new EndpointSource() {
158: public ImmutableEndpoint getEndpoint(
159: EndpointBuilder builder)
160: throws MuleException {
161: return getOutboundEndpoint(builder);
162: }
163: });
164: }
165:
166: protected ImmutableEndpoint getEndpoint(EndpointURI uri,
167: EndpointSource source) throws MuleException {
168: logger
169: .debug("DefaultEndpointFactory request for endpoint for: "
170: + uri);
171: EndpointBuilder endpointBuilder = null;
172: if (uri.getEndpointName() != null) {
173: endpointBuilder = lookupEndpointBuilder(uri
174: .getEndpointName());
175: if (endpointBuilder == null) {
176: throw new IllegalArgumentException(
177: "The endpoint with name: "
178: + uri.getEndpointName()
179: + "was not found.");
180: }
181: } else {
182: logger
183: .debug("Named EndpointBuilder not found, creating endpoint from uri");
184: endpointBuilder = new EndpointURIEndpointBuilder(uri,
185: muleContext);
186: }
187: return source.getEndpoint(endpointBuilder);
188: }
189:
190: private interface EndpointSource {
191: ImmutableEndpoint getEndpoint(EndpointBuilder endpointBuilder)
192: throws MuleException;
193: }
194:
195: }
|