Coverage Report - org.mule.transport.service.TransportFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TransportFactory
45%
18/40
39%
7/18
5.5
 
 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  0
 public class TransportFactory
 39  
 {
 40  
     /**
 41  
      * logger used by this class
 42  
      */
 43  2
     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  34
             String scheme = url.getSchemeMetaInfo();
 65  
 
 66  34
             TransportServiceDescriptor sd = (TransportServiceDescriptor)
 67  
                 RegistryContext.getRegistry().lookupServiceDescriptor(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, scheme, null);
 68  34
             if (sd == null)
 69  
             {
 70  0
                 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
 71  
             }
 72  
 
 73  34
             connector = sd.createConnector();
 74  34
             if (connector != null)
 75  
             {
 76  34
                 if (connector instanceof AbstractConnector)
 77  
                 {
 78  34
                     ((AbstractConnector)connector).initialiseFromUrl(url);
 79  
                 }
 80  
             }
 81  
             else
 82  
             {
 83  0
                 throw new TransportFactoryException(
 84  
                     CoreMessages.objectNotSetInService("Connector", scheme));
 85  
             }
 86  
 
 87  34
             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  34
             return connector;
 103  
         }
 104  0
         catch (Exception e)
 105  
         {
 106  0
             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  0
         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  0
         String connectorName = uri.getConnectorName();
 124  0
         if (null != connectorName)
 125  
         {
 126  
             // TODO this lookup fails currently on Mule 2.x! MuleAdminAgentTestCase
 127  0
             Connector connector = RegistryContext.getRegistry().lookupConnector(connectorName);
 128  0
             if (connector != null)
 129  
             {
 130  0
                 return connector;
 131  
             }
 132  
         }
 133  
 
 134  0
         Connector connector = getConnectorByProtocol(uri.getFullScheme());
 135  0
         if (connector == null)
 136  
         {
 137  0
             connector = createConnector(uri, muleContext);
 138  
             try
 139  
             {
 140  0
                 BeanUtils.populate(connector, uri.getParams());
 141  0
                 connector.setMuleContext(muleContext);
 142  0
                 muleContext.getRegistry().registerConnector(connector);
 143  
             }
 144  0
             catch (Exception e)
 145  
             {
 146  0
                 throw new TransportFactoryException(e);
 147  0
             }
 148  
         }
 149  0
         return connector;
 150  
     }
 151  
 
 152  
     public static Connector getConnectorByProtocol(String protocol)
 153  
     {
 154  
         Connector connector;
 155  44
         Connector resultConnector = null;
 156  44
         Collection connectors = RegistryContext.getRegistry().lookupObjects(Connector.class);
 157  44
         for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
 158  
         {
 159  10
             connector = (Connector)iterator.next();
 160  10
             if (connector.supportsProtocol(protocol))
 161  
             {
 162  10
                 if(resultConnector==null)
 163  
                 {
 164  10
                     resultConnector = connector;
 165  
                 }
 166  
                 else
 167  
                 {
 168  0
                     throw new IllegalStateException(
 169  
                         CoreMessages.moreThanOneConnectorWithProtocol(protocol).getMessage());
 170  
                 }
 171  
             }
 172  
         }
 173  44
         return resultConnector;
 174  
     }
 175  
 }