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