View Javadoc

1   /*
2    * $Id: DefaultEndpointFactory.java 19216 2010-08-26 14:16:31Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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  public class DefaultEndpointFactory implements EndpointFactory
31  {
32      /**
33       * logger used by this class
34       */
35      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          logger.debug("DefaultEndpointFactory request for inbound endpoint for uri: " + uri);
45          EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
46          if (endpointBuilder == null)
47          {
48              logger.debug("Named EndpointBuilder not found, creating endpoint from uri");
49              endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
50          }
51          return getInboundEndpoint(endpointBuilder);
52      }
53  
54      public OutboundEndpoint getOutboundEndpoint(String uri)
55              throws MuleException
56      {
57          logger.debug("DefaultEndpointFactory request for outbound endpoint for uri: " + uri);
58          EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
59          if (endpointBuilder == null)
60          {
61              logger.debug("Named EndpointBuilder not found, creating endpoint from uri");
62              endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
63  
64          }
65          return getOutboundEndpoint(endpointBuilder);
66      }
67  
68      protected EndpointBuilder lookupEndpointBuilder(String endpointName)
69      {
70          logger.debug("Looking up EndpointBuilder with name:" + endpointName + " in registry");
71          // TODO DF: Do some simple parsing of endpointName to not lookup endpoint builder if endpointName is
72          // obviously a uri and not a substituted name ??
73          EndpointBuilder endpointBuilder = muleContext.getRegistry().lookupEndpointBuilder(endpointName);
74          if (endpointBuilder != null)
75          {
76              logger.debug("EndpointBuilder with name:" + endpointName + " FOUND");
77          }
78          return endpointBuilder;
79      }
80  
81      public InboundEndpoint getInboundEndpoint(EndpointBuilder builder) throws MuleException
82      {
83          InboundEndpoint endpoint = builder.buildInboundEndpoint();
84          return (InboundEndpoint) registerEndpoint(endpoint);
85      }
86  
87      public OutboundEndpoint getOutboundEndpoint(EndpointBuilder builder) throws MuleException
88      {
89          OutboundEndpoint endpoint = builder.buildOutboundEndpoint();
90          return (OutboundEndpoint) registerEndpoint(endpoint);
91      }
92  
93      /**
94       * @param endpoint
95       * @throws RegistrationException
96       */
97      protected ImmutableEndpoint registerEndpoint(ImmutableEndpoint endpoint) throws RegistrationException
98      {
99          ImmutableEndpoint registryEndpoint = (ImmutableEndpoint) muleContext.getRegistry().lookupObject(
100                 ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode());
101         if (registryEndpoint == null)
102         {
103             muleContext.getRegistry().registerObject(ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode(), endpoint);
104             registryEndpoint = endpoint;
105         }
106         return registryEndpoint;
107     }
108 
109     public EndpointBuilder getEndpointBuilder(String uri)
110             throws MuleException
111     {
112         logger.debug("DefaultEndpointFactory request for endpoint builder for uri: " + uri);
113         EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
114         if (endpointBuilder != null)
115         {
116             try
117             {
118                 endpointBuilder = (EndpointBuilder) endpointBuilder.clone();
119             }
120             catch (Exception e)
121             {
122                 throw new EndpointException(CoreMessages.failedToClone("global endpoint EndpointBuilder"), e);
123             }
124         }
125         else
126         {
127             logger.debug("Named EndpointBuilder not found, creating endpoint builder for uri");
128             EndpointURI epURI = new MuleEndpointURI(uri, muleContext);
129             TransportServiceDescriptor tsd = (TransportServiceDescriptor) muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, epURI.getFullScheme(), null);
130             endpointBuilder = tsd.createEndpointBuilder(uri);
131         }
132         return endpointBuilder;
133     }
134 
135     public void setMuleContext(MuleContext context)
136     {
137         this.muleContext = context;
138     }
139 
140     public org.mule.api.endpoint.InboundEndpoint getInboundEndpoint(EndpointURI uri) throws MuleException
141     {
142         return (InboundEndpoint) getEndpoint(uri, new EndpointSource()
143         {
144             public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
145             {
146                 return getInboundEndpoint(builder);
147             }
148         });
149     }
150 
151     public OutboundEndpoint getOutboundEndpoint(EndpointURI uri) throws MuleException
152     {
153         return (OutboundEndpoint) getEndpoint(uri, new EndpointSource()
154         {
155             public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
156             {
157                 return getOutboundEndpoint(builder);
158             }
159         });
160     }
161 
162     protected ImmutableEndpoint getEndpoint(EndpointURI uri, EndpointSource source) throws MuleException
163     {
164         logger.debug("DefaultEndpointFactory request for endpoint for: " + uri);
165         EndpointBuilder endpointBuilder = null;
166         if (uri.getEndpointName() != null)
167         {
168             endpointBuilder = lookupEndpointBuilder(uri.getEndpointName());
169             if (endpointBuilder == null)
170             {
171                 throw new IllegalArgumentException("The endpoint with name: " + uri.getEndpointName()
172                         + "was not found.");
173             }
174         }
175         else
176         {
177             logger.debug("Named EndpointBuilder not found, creating endpoint from uri");
178             endpointBuilder = new EndpointURIEndpointBuilder(uri);
179         }
180         return source.getEndpoint(endpointBuilder);
181     }
182 
183     private interface EndpointSource
184     {
185         ImmutableEndpoint getEndpoint(EndpointBuilder endpointBuilder) throws MuleException;
186     }
187 
188 }