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