View Javadoc

1   /*
2    * $Id: TransportFactory.java 22550 2011-07-24 22:03:52Z mike.schilling $
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.transport.service;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.endpoint.EndpointException;
15  import org.mule.api.endpoint.EndpointURI;
16  import org.mule.api.endpoint.ImmutableEndpoint;
17  import org.mule.api.registry.ServiceException;
18  import org.mule.api.registry.ServiceType;
19  import org.mule.api.transport.Connector;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.endpoint.MuleEndpointURI;
22  import org.mule.transport.AbstractConnector;
23  import org.mule.util.BeanUtils;
24  import org.mule.util.ObjectNameHelper;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * <code>TransportFactory</code> can be used for generically creating endpoints
36   * from an url. Note that for some endpoints, the url alone is not enough to create
37   * the endpoint if a connector for the endpoint has not already been configured with
38   * the Mule Manager.
39   */
40  public class TransportFactory
41  {
42      /**
43       * logger used by this class
44       */
45      protected static final Log logger = LogFactory.getLog(TransportFactory.class);
46  
47      protected MuleContext muleContext;
48  
49      public TransportFactory(MuleContext muleContext)
50      {
51          this.muleContext = muleContext;
52      }
53  
54      /**
55       * Creates an uninitialied connector from the provided MuleEndpointURI. The
56       * scheme is used to determine what kind of connector to create. Any params set
57       * on the uri can be used to initialise bean properties on the created connector.
58       * <p/> Note that the initalise method will need to be called on the connector
59       * returned. This is so that developers can control when the connector
60       * initialisation takes place as this is likely to initialse all connecotr
61       * resources.
62       *
63       * @param url the MuleEndpointURI url to create the connector with
64       * @return a new Connector
65       * @throws TransportFactoryException
66       */
67      public Connector createConnector(EndpointURI url) throws TransportFactoryException
68      {
69  
70          try
71          {
72              Connector connector;
73              String scheme = url.getFullScheme();
74  
75              TransportServiceDescriptor sd = (TransportServiceDescriptor)
76                      muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, scheme, null);
77              if (sd == null)
78              {
79                  throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
80              }
81  
82              connector = sd.createConnector();
83              if (connector != null)
84              {
85                  if (connector instanceof AbstractConnector)
86                  {
87                      ((AbstractConnector) connector).initialiseFromUrl(url);
88                  }
89              }
90              else
91              {
92                  throw new TransportFactoryException(
93                          CoreMessages.objectNotSetInService("Connector", scheme));
94              }
95  
96              connector.setName(new ObjectNameHelper(muleContext).getConnectorName(connector));
97  
98              return connector;
99          }
100         catch (Exception e)
101         {
102             throw new TransportFactoryException(
103                     CoreMessages.failedToCreateObjectWith("Endpoint", url), e);
104         }
105     }
106 
107     public Connector createConnector(String uri) throws TransportFactoryException
108     {
109         try
110         {
111             return createConnector(new MuleEndpointURI(uri, muleContext));
112         }
113         catch (EndpointException e)
114         {
115             throw new TransportFactoryException(e);
116         }
117     }
118 
119     public Connector getOrCreateConnectorByProtocol(ImmutableEndpoint endpoint)
120             throws TransportFactoryException
121     {
122         return getOrCreateConnectorByProtocol(endpoint.getEndpointURI());
123     }
124 
125     /**
126      * Returns an initialized connector.
127      */
128     public Connector getOrCreateConnectorByProtocol(EndpointURI uri)
129             throws TransportFactoryException
130     {
131         String connectorName = uri.getConnectorName();
132         if (null != connectorName)
133         {
134             // TODO this lookup fails currently on Mule 2.x! MuleAdminAgentTestCase
135             Connector connector = muleContext.getRegistry().lookupConnector(connectorName);
136             if (connector != null)
137             {
138                 return connector;
139             }
140         }
141 
142         Connector connector = getConnectorByProtocol(uri.getFullScheme());
143         if (connector == null)
144         {
145             connector = createConnector(uri);
146             try
147             {
148                 BeanUtils.populate(connector, uri.getParams());
149                 muleContext.getRegistry().registerConnector(connector);
150             }
151             catch (Exception e)
152             {
153                 throw new TransportFactoryException(e);
154             }
155         }
156         return connector;
157     }
158 
159     public Connector getConnectorByProtocol(String protocol)
160     {
161         Connector connector;
162         List<Connector> results = new ArrayList<Connector>();
163         Collection connectors = muleContext.getRegistry().lookupObjects(Connector.class);
164         for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
165         {
166             connector = (Connector) iterator.next();
167             if (connector.supportsProtocol(protocol))
168             {
169                 results.add(connector);
170             }
171         }
172         if (results.size() > 1)
173         {
174             StringBuffer buf = new StringBuffer();
175             for (Connector result : results)
176             {
177                 buf.append(result.getName()).append(", ");
178             }
179             throw new IllegalStateException(
180                     CoreMessages.moreThanOneConnectorWithProtocol(protocol, buf.toString()).getMessage());
181         }
182         else if (results.size() == 1)
183         {
184             return results.get(0);
185         }
186         else
187         {
188             return null;
189         }
190     }
191 
192     public Connector getDefaultConnectorByProtocol(String protocol)
193     {
194         Connector connector;
195         List<Connector> results = new ArrayList<Connector>();
196         Collection connectors = muleContext.getRegistry().lookupObjects(Connector.class);
197         for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
198         {
199             connector = (Connector) iterator.next();
200             if (connector.supportsProtocol(protocol)  && ObjectNameHelper.isDefaultAutoGeneratedConnector(connector))
201             {
202                 results.add(connector);
203             }
204         }
205         if (results.size() > 1)
206         {
207             StringBuffer buf = new StringBuffer();
208             for (Connector result : results)
209             {
210                 buf.append(result.getName()).append(", ");
211             }
212             throw new IllegalStateException(
213                     CoreMessages.moreThanOneConnectorWithProtocol(protocol, buf.toString()).getMessage());
214         }
215         else if (results.size() == 1)
216         {
217             return results.get(0);
218         }
219         else
220         {
221             return null;
222         }
223     }
224 }