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 | 0 | super(muleContext); |
44 | 0 | overrides = (Properties) muleContext.getRegistry().lookupObject(CHANNEL_OVERRIDES); |
45 | 0 | if (overrides == null) |
46 | |
{ |
47 | 0 | overrides = loadOverrides(); |
48 | 0 | muleContext.getRegistry().registerObject(CHANNEL_OVERRIDES, overrides); |
49 | |
} |
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
public Connector createConnector(EndpointURI endpointURI) throws TransportFactoryException |
54 | |
{ |
55 | |
Connector c; |
56 | 0 | String scheme = endpointURI.getScheme(); |
57 | 0 | Map temp = new Properties(); |
58 | 0 | PropertiesUtils.getPropertiesWithPrefix(overrides, scheme, temp); |
59 | 0 | temp = PropertiesUtils.removeNamespaces(temp); |
60 | 0 | String singleton = (String) temp.remove(SINGLETON_PROPERTY); |
61 | 0 | if (TRUE.equalsIgnoreCase(singleton)) |
62 | |
{ |
63 | 0 | c = this.getConnectorByProtocol(scheme); |
64 | 0 | if (c != null) |
65 | |
{ |
66 | 0 | return c; |
67 | |
} |
68 | |
} |
69 | 0 | c = super.createConnector(endpointURI); |
70 | 0 | BeanUtils.populateWithoutFail(c, temp, true); |
71 | 0 | return c; |
72 | |
} |
73 | |
|
74 | |
protected Properties loadOverrides() throws MuleException |
75 | |
{ |
76 | 0 | Properties props = new Properties(); |
77 | 0 | Enumeration e = ClassUtils.getResources(CHANNEL_OVERRIDES_FILE, getClass()); |
78 | 0 | while (e.hasMoreElements()) |
79 | |
{ |
80 | 0 | URL url = (URL) e.nextElement(); |
81 | |
try |
82 | |
{ |
83 | 0 | props.load(url.openStream()); |
84 | |
} |
85 | 0 | catch (IOException e1) |
86 | |
{ |
87 | 0 | throw new DefaultMuleException("failed to read channel overrides from URL: " + url.toExternalForm()); |
88 | 0 | } |
89 | 0 | } |
90 | 0 | return props; |
91 | |
} |
92 | |
} |