View Javadoc

1   /*
2    * $Id: TransportFactory.java 10787 2008-02-12 18:51:50Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.RegistryContext;
14  import org.mule.api.MuleContext;
15  import org.mule.api.endpoint.EndpointURI;
16  import org.mule.api.endpoint.ImmutableEndpoint;
17  import org.mule.api.registry.ServiceDescriptorFactory;
18  import org.mule.api.registry.ServiceException;
19  import org.mule.api.transport.Connector;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.transport.AbstractConnector;
22  import org.mule.util.BeanUtils;
23  import org.mule.util.ObjectNameHelper;
24  
25  import java.util.Collection;
26  import java.util.Iterator;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * <code>TransportFactory</code> can be used for generically creating endpoints
33   * from an url. Note that for some endpoints, the url alone is not enough to create
34   * the endpoint if a connector for the endpoint has not already been configured with
35   * the Mule Manager.
36   *
37   */
38  public class TransportFactory
39  {
40      /**
41       * logger used by this class
42       */
43      protected static final Log logger = LogFactory.getLog(TransportFactory.class);
44  
45      /**
46       * Creates an uninitialied connector from the provided MuleEndpointURI. The
47       * scheme is used to determine what kind of connector to create. Any params set
48       * on the uri can be used to initialise bean properties on the created connector.
49       * <p/> Note that the initalise method will need to be called on the connector
50       * returned. This is so that developers can control when the connector
51       * initialisation takes place as this is likely to initialse all connecotr
52       * resources.
53       *
54       * @param url the MuleEndpointURI url to create the connector with
55       * @return a new Connector
56       * @throws TransportFactoryException
57       */
58      public static Connector createConnector(EndpointURI url, MuleContext muleContext) throws TransportFactoryException
59      {
60  
61          try
62          {
63              Connector connector;
64              String scheme = url.getSchemeMetaInfo();
65  
66              TransportServiceDescriptor sd = (TransportServiceDescriptor)
67                  RegistryContext.getRegistry().lookupServiceDescriptor(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, scheme, null);
68              if (sd == null)
69              {
70                  throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
71              }
72  
73              connector = sd.createConnector();
74              if (connector != null)
75              {
76                  if (connector instanceof AbstractConnector)
77                  {
78                      ((AbstractConnector)connector).initialiseFromUrl(url);
79                  }
80              }
81              else
82              {
83                  throw new TransportFactoryException(
84                      CoreMessages.objectNotSetInService("Connector", scheme));
85              }
86  
87              connector.setName(ObjectNameHelper.getConnectorName(connector));
88  
89              // TODO Do we still need to support this for 2.x?
90              // set any manager default properties for the connector
91              // these are set on the Manager with a protocol i.e.
92              // jms.specification=1.1
93  //            Map props = new HashMap();
94  //            PropertiesUtils.getPropertiesWithPrefix(RegistryContext.getRegistry().lookupProperties(),
95  //                connector.getProtocol().toLowerCase(), props);
96  //            if (props.size() > 0)
97  //            {
98  //                props = PropertiesUtils.removeNamespaces(props);
99  //                BeanUtils.populateWithoutFail(connector, props, true);
100 //            }
101 
102             return connector;
103         }
104         catch (Exception e)
105         {
106             throw new TransportFactoryException(
107                 CoreMessages.failedToCreateObjectWith("Endpoint", url), e);
108         }
109     }
110 
111     public static Connector getOrCreateConnectorByProtocol(ImmutableEndpoint endpoint, MuleContext muleContext)
112         throws TransportFactoryException
113     {
114         return getOrCreateConnectorByProtocol(endpoint.getEndpointURI(), muleContext);
115     }
116 
117     /**
118      * Returns an initialized connector.
119      */
120     public static Connector getOrCreateConnectorByProtocol(EndpointURI uri, MuleContext muleContext)
121         throws TransportFactoryException
122     {
123         String connectorName = uri.getConnectorName();
124         if (null != connectorName)
125         {
126             // TODO this lookup fails currently on Mule 2.x! MuleAdminAgentTestCase
127             Connector connector = RegistryContext.getRegistry().lookupConnector(connectorName);
128             if (connector != null)
129             {
130                 return connector;
131             }
132         }
133 
134         Connector connector = getConnectorByProtocol(uri.getFullScheme());
135         if (connector == null)
136         {
137             connector = createConnector(uri, muleContext);
138             try
139             {
140                 BeanUtils.populate(connector, uri.getParams());
141                 connector.setMuleContext(muleContext);
142                 muleContext.getRegistry().registerConnector(connector);
143             }
144             catch (Exception e)
145             {
146                 throw new TransportFactoryException(e);
147             }
148         }
149         return connector;
150     }
151 
152     public static Connector getConnectorByProtocol(String protocol)
153     {
154         Connector connector;
155         Connector resultConnector = null;
156         Collection connectors = RegistryContext.getRegistry().lookupObjects(Connector.class);
157         for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
158         {
159             connector = (Connector)iterator.next();
160             if (connector.supportsProtocol(protocol))
161             {
162                 if(resultConnector==null)
163                 {
164                     resultConnector = connector;
165                 }
166                 else
167                 {
168                     throw new IllegalStateException(
169                         CoreMessages.moreThanOneConnectorWithProtocol(protocol).getMessage());
170                 }
171             }
172         }
173         return resultConnector;
174     }
175 }