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.config.endpoint;
8   
9   import org.mule.api.DefaultMuleException;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleException;
12  import org.mule.api.endpoint.EndpointURI;
13  import org.mule.api.transport.Connector;
14  import org.mule.transport.service.TransportFactory;
15  import org.mule.transport.service.TransportFactoryException;
16  import org.mule.util.BeanUtils;
17  import org.mule.util.ClassUtils;
18  import org.mule.util.PropertiesUtils;
19  
20  import java.io.IOException;
21  import java.net.URL;
22  import java.util.Enumeration;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  /**
27   * TODO
28   */
29  public class ConfigurableTransportFactory extends TransportFactory
30  {
31      public static final String CHANNEL_OVERRIDES = "_configuredConnectorOverrides";
32      public static final String CHANNEL_OVERRIDES_FILE = "META-INF/services/org/mule/config/channel-overrides.properties";
33      public static final String SINGLETON_PROPERTY = "singleton";
34      public static final String TRUE = "TRUE";
35  
36      private Properties overrides;
37  
38      public ConfigurableTransportFactory(MuleContext muleContext) throws MuleException
39      {
40          super(muleContext);
41          overrides = (Properties) muleContext.getRegistry().lookupObject(CHANNEL_OVERRIDES);
42          if (overrides == null)
43          {
44              overrides = loadOverrides();
45              muleContext.getRegistry().registerObject(CHANNEL_OVERRIDES, overrides);
46          }
47      }
48  
49      @Override
50      public Connector createConnector(EndpointURI endpointURI) throws TransportFactoryException
51      {
52          Connector c;
53          String scheme = endpointURI.getScheme();
54          Map temp = new Properties();
55          PropertiesUtils.getPropertiesWithPrefix(overrides, scheme, temp);
56          temp = PropertiesUtils.removeNamespaces(temp);
57          String singleton = (String) temp.remove(SINGLETON_PROPERTY);
58          if (TRUE.equalsIgnoreCase(singleton))
59          {
60              c = this.getConnectorByProtocol(scheme);
61              if (c != null)
62              {
63                  return c;
64              }
65          }
66          c = super.createConnector(endpointURI);
67          BeanUtils.populateWithoutFail(c, temp, true);
68          return c;
69      }
70  
71      protected Properties loadOverrides() throws MuleException
72      {
73          Properties props = new Properties();
74          Enumeration e = ClassUtils.getResources(CHANNEL_OVERRIDES_FILE, getClass());
75          while (e.hasMoreElements())
76          {
77              URL url = (URL) e.nextElement();
78              try
79              {
80                  props.load(url.openStream());
81              }
82              catch (IOException e1)
83              {
84                  throw new DefaultMuleException("failed to read channel overrides from URL: " + url.toExternalForm());
85              }
86          }
87          return props;
88      }
89  }