1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.builders; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.config.ConfigurationBuilder; |
15 | |
import org.mule.api.config.ConfigurationException; |
16 | |
import org.mule.config.ConfigResource; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
import org.mule.util.StringUtils; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.LinkedHashMap; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
import java.util.Properties; |
27 | |
|
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class AutoConfigurationBuilder extends AbstractResourceConfigurationBuilder |
37 | |
{ |
38 | 2 | protected static final Log logger = LogFactory.getLog(AutoConfigurationBuilder.class); |
39 | |
|
40 | |
public AutoConfigurationBuilder(String resource) throws ConfigurationException |
41 | |
{ |
42 | 4 | super(resource); |
43 | 4 | } |
44 | |
|
45 | |
public AutoConfigurationBuilder(String[] resources) throws ConfigurationException |
46 | |
{ |
47 | 0 | super(resources); |
48 | 0 | } |
49 | |
|
50 | |
public AutoConfigurationBuilder(ConfigResource[] resources) |
51 | |
{ |
52 | 0 | super(resources); |
53 | 0 | } |
54 | |
|
55 | |
protected void doConfigure(MuleContext muleContext) throws ConfigurationException |
56 | |
{ |
57 | 4 | autoConfigure(muleContext, configResources); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected void autoConfigure(MuleContext muleContext, ConfigResource[] configResources) throws ConfigurationException |
67 | |
{ |
68 | |
|
69 | 4 | Map configsMap = new LinkedHashMap(); |
70 | |
|
71 | 8 | for (int i = 0; i < configResources.length; i++) |
72 | |
{ |
73 | 4 | String configExtension = StringUtils.substringAfterLast( |
74 | |
(configResources[i]).getUrl().getFile(), "."); |
75 | 4 | List configs = (List) configsMap.get(configExtension); |
76 | 4 | if (configs == null) |
77 | |
{ |
78 | 4 | configs = new ArrayList(); |
79 | 4 | configsMap.put(configExtension, configs); |
80 | |
} |
81 | 4 | configs.add(configResources[i]); |
82 | |
} |
83 | |
|
84 | |
try |
85 | |
{ |
86 | 4 | Properties props = new Properties(); |
87 | 4 | props.load(ClassUtils.getResource("configuration-builders.properties", this.getClass()).openStream()); |
88 | |
|
89 | 4 | Iterator iterator = configsMap.entrySet().iterator(); |
90 | 4 | while (iterator.hasNext()) |
91 | |
{ |
92 | 4 | Map.Entry e = (Map.Entry) iterator.next(); |
93 | 4 | String extension = (String) e.getKey(); |
94 | 4 | List configs = (List) e.getValue(); |
95 | |
|
96 | 4 | String className = (String) props.get(extension); |
97 | |
|
98 | 4 | if (className == null || !ClassUtils.isClassOnPath(className, this.getClass())) |
99 | |
{ |
100 | 4 | throw new ConfigurationException( |
101 | |
CoreMessages.configurationBuilderNoMatching(createConfigResourcesString())); |
102 | |
} |
103 | |
|
104 | 0 | ConfigResource[] constructorArg = new ConfigResource[configs.size()]; |
105 | 0 | System.arraycopy(configs.toArray(), 0, constructorArg, 0, configs.size()); |
106 | 0 | ConfigurationBuilder cb = (ConfigurationBuilder) ClassUtils.instanciateClass(className, new Object[]{constructorArg}); |
107 | 0 | cb.configure(muleContext); |
108 | 0 | } |
109 | |
} |
110 | 4 | catch (ConfigurationException e) |
111 | |
{ |
112 | 4 | throw e; |
113 | |
} |
114 | 0 | catch (Exception e) |
115 | |
{ |
116 | 0 | throw new ConfigurationException(e); |
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | |
} |