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