1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config; |
8 | |
|
9 | |
import org.mule.MuleServer; |
10 | |
import org.mule.api.config.MuleConfiguration; |
11 | |
import org.mule.util.BeanUtils; |
12 | |
import org.mule.util.ClassUtils; |
13 | |
import org.mule.util.FilenameUtils; |
14 | |
|
15 | |
import java.io.FileNotFoundException; |
16 | |
import java.io.IOException; |
17 | |
import java.io.InputStream; |
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.net.URL; |
20 | |
import java.util.Map; |
21 | |
import java.util.Map.Entry; |
22 | |
import java.util.Properties; |
23 | |
|
24 | |
import org.apache.commons.io.IOUtils; |
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
public class PropertiesMuleConfigurationFactory |
29 | |
{ |
30 | |
|
31 | 0 | private static Log logger = LogFactory.getLog(PropertiesMuleConfigurationFactory.class); |
32 | |
|
33 | |
private Properties properties; |
34 | |
|
35 | |
public static String getMuleAppConfiguration(String muleConfig) |
36 | |
{ |
37 | 0 | String directory = FilenameUtils.getFullPath(muleConfig); |
38 | 0 | String muleAppConfiguration = directory + MuleServer.DEFAULT_APP_CONFIGURATION; |
39 | 0 | return muleAppConfiguration; |
40 | |
} |
41 | |
|
42 | |
public PropertiesMuleConfigurationFactory(String muleAppConfiguration) |
43 | 0 | { |
44 | 0 | URL muleAppURL = ClassUtils.getResource(muleAppConfiguration, getClass()); |
45 | 0 | if (muleAppURL != null) |
46 | |
{ |
47 | 0 | this.properties = new Properties(); |
48 | 0 | InputStream inputStream = null; |
49 | |
try |
50 | |
{ |
51 | 0 | inputStream = muleAppURL.openStream(); |
52 | 0 | this.properties.load(inputStream); |
53 | |
} |
54 | 0 | catch (FileNotFoundException e) |
55 | |
{ |
56 | 0 | logger.debug(e); |
57 | |
} |
58 | 0 | catch (IOException e) |
59 | |
{ |
60 | 0 | logger.debug(e); |
61 | |
} |
62 | |
finally |
63 | |
{ |
64 | 0 | IOUtils.closeQuietly(inputStream); |
65 | 0 | } |
66 | |
} |
67 | 0 | } |
68 | |
|
69 | |
public DefaultMuleConfiguration createConfiguration() |
70 | |
{ |
71 | 0 | DefaultMuleConfiguration configuration = new DefaultMuleConfiguration(); |
72 | 0 | if (this.properties != null) |
73 | |
{ |
74 | 0 | this.initializeFromProperties(configuration); |
75 | |
} |
76 | 0 | return configuration; |
77 | |
} |
78 | |
|
79 | |
private void initializeFromProperties(MuleConfiguration configuration) |
80 | |
{ |
81 | 0 | initializeFromProperties(configuration, this.properties); |
82 | 0 | } |
83 | |
|
84 | |
public static void initializeFromProperties(MuleConfiguration configuration, Map properties) |
85 | |
{ |
86 | 0 | for (Object entryObject : properties.entrySet()) |
87 | |
{ |
88 | 0 | Entry entry = (Entry) entryObject; |
89 | 0 | String key = (String) entry.getKey(); |
90 | 0 | String value = (String) entry.getValue(); |
91 | |
|
92 | 0 | if (key.startsWith("sys.")) |
93 | |
{ |
94 | 0 | String systemProperty = key.substring(4); |
95 | 0 | System.setProperty(systemProperty, value); |
96 | 0 | } |
97 | 0 | else if (key.startsWith("mule.config.")) |
98 | |
{ |
99 | 0 | String configProperty = key.substring(12); |
100 | |
try |
101 | |
{ |
102 | 0 | BeanUtils.setProperty(configuration, configProperty, value); |
103 | |
} |
104 | 0 | catch (IllegalAccessException e) |
105 | |
{ |
106 | 0 | logger.error(e); |
107 | |
} |
108 | 0 | catch (InvocationTargetException e) |
109 | |
{ |
110 | 0 | logger.error(e); |
111 | 0 | } |
112 | |
} |
113 | 0 | } |
114 | 0 | } |
115 | |
} |