Coverage Report - org.mule.transport.service.TransportFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TransportFactory
0%
0/49
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: TransportFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
 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  0
     protected static final Log logger = LogFactory.getLog(TransportFactory.class);
 46  
 
 47  
     protected MuleContext muleContext;
 48  
 
 49  
     public TransportFactory(MuleContext muleContext)
 50  0
     {
 51  0
         this.muleContext = muleContext;
 52  0
     }
 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  0
             String scheme = url.getFullScheme();
 74  
 
 75  0
             TransportServiceDescriptor sd = (TransportServiceDescriptor)
 76  
                     muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, scheme, null);
 77  0
             if (sd == null)
 78  
             {
 79  0
                 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
 80  
             }
 81  
 
 82  0
             connector = sd.createConnector();
 83  0
             if (connector != null)
 84  
             {
 85  0
                 if (connector instanceof AbstractConnector)
 86  
                 {
 87  0
                     ((AbstractConnector) connector).initialiseFromUrl(url);
 88  
                 }
 89  
             }
 90  
             else
 91  
             {
 92  0
                 throw new TransportFactoryException(
 93  
                         CoreMessages.objectNotSetInService("Connector", scheme));
 94  
             }
 95  
 
 96  0
             connector.setName(new ObjectNameHelper(muleContext).getConnectorName(connector));
 97  
 
 98  0
             return connector;
 99  
         }
 100  0
         catch (Exception e)
 101  
         {
 102  0
             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  0
             return createConnector(new MuleEndpointURI(uri, muleContext));
 112  
         }
 113  0
         catch (EndpointException e)
 114  
         {
 115  0
             throw new TransportFactoryException(e);
 116  
         }
 117  
     }
 118  
 
 119  
     public Connector getOrCreateConnectorByProtocol(ImmutableEndpoint endpoint)
 120  
             throws TransportFactoryException
 121  
     {
 122  0
         return getOrCreateConnectorByProtocol(endpoint.getEndpointURI());
 123  
     }
 124  
 
 125  
     /**
 126  
      * Returns an initialized connector.
 127  
      */
 128  
     public Connector getOrCreateConnectorByProtocol(EndpointURI uri)
 129  
             throws TransportFactoryException
 130  
     {
 131  0
         String connectorName = uri.getConnectorName();
 132  0
         if (null != connectorName)
 133  
         {
 134  
             // TODO this lookup fails currently on Mule 2.x! MuleAdminAgentTestCase
 135  0
             Connector connector = muleContext.getRegistry().lookupConnector(connectorName);
 136  0
             if (connector != null)
 137  
             {
 138  0
                 return connector;
 139  
             }
 140  
         }
 141  
 
 142  0
         Connector connector = getConnectorByProtocol(uri.getFullScheme());
 143  0
         if (connector == null)
 144  
         {
 145  0
             connector = createConnector(uri);
 146  
             try
 147  
             {
 148  0
                 BeanUtils.populate(connector, uri.getParams());
 149  0
                 muleContext.getRegistry().registerConnector(connector);
 150  
             }
 151  0
             catch (Exception e)
 152  
             {
 153  0
                 throw new TransportFactoryException(e);
 154  0
             }
 155  
         }
 156  0
         return connector;
 157  
     }
 158  
 
 159  
     public Connector getConnectorByProtocol(String protocol)
 160  
     {
 161  
         Connector connector;
 162  0
         List<Connector> results = new ArrayList<Connector>();
 163  0
         Collection connectors = muleContext.getRegistry().lookupObjects(Connector.class);
 164  0
         for (Iterator iterator = connectors.iterator(); iterator.hasNext();)
 165  
         {
 166  0
             connector = (Connector) iterator.next();
 167  0
             if (connector.supportsProtocol(protocol))
 168  
             {
 169  0
                 results.add(connector);
 170  
             }
 171  
         }
 172  0
         if (results.size() > 1)
 173  
         {
 174  0
             StringBuffer buf = new StringBuffer();
 175  0
             for (Connector result : results)
 176  
             {
 177  0
                 buf.append(result.getName()).append(", ");
 178  
             }
 179  0
             throw new IllegalStateException(
 180  
                     CoreMessages.moreThanOneConnectorWithProtocol(protocol, buf.toString()).getMessage());
 181  
         }
 182  0
         else if (results.size() == 1)
 183  
         {
 184  0
             return results.get(0);
 185  
         }
 186  
         else
 187  
         {
 188  0
             return null;
 189  
         }
 190  
     }
 191  
 }