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 private static Log logger = LogFactory.getLog(PropertiesMuleConfigurationFactory.class);
32
33 private Properties properties;
34
35 public static String getMuleAppConfiguration(String muleConfig)
36 {
37 String directory = FilenameUtils.getFullPath(muleConfig);
38 String muleAppConfiguration = directory + MuleServer.DEFAULT_APP_CONFIGURATION;
39 return muleAppConfiguration;
40 }
41
42 public PropertiesMuleConfigurationFactory(String muleAppConfiguration)
43 {
44 URL muleAppURL = ClassUtils.getResource(muleAppConfiguration, getClass());
45 if (muleAppURL != null)
46 {
47 this.properties = new Properties();
48 InputStream inputStream = null;
49 try
50 {
51 inputStream = muleAppURL.openStream();
52 this.properties.load(inputStream);
53 }
54 catch (FileNotFoundException e)
55 {
56 logger.debug(e);
57 }
58 catch (IOException e)
59 {
60 logger.debug(e);
61 }
62 finally
63 {
64 IOUtils.closeQuietly(inputStream);
65 }
66 }
67 }
68
69 public DefaultMuleConfiguration createConfiguration()
70 {
71 DefaultMuleConfiguration configuration = new DefaultMuleConfiguration();
72 if (this.properties != null)
73 {
74 this.initializeFromProperties(configuration);
75 }
76 return configuration;
77 }
78
79 private void initializeFromProperties(MuleConfiguration configuration)
80 {
81 initializeFromProperties(configuration, this.properties);
82 }
83
84 public static void initializeFromProperties(MuleConfiguration configuration, Map properties)
85 {
86 for (Object entryObject : properties.entrySet())
87 {
88 Entry entry = (Entry) entryObject;
89 String key = (String) entry.getKey();
90 String value = (String) entry.getValue();
91
92 if (key.startsWith("sys."))
93 {
94 String systemProperty = key.substring(4);
95 System.setProperty(systemProperty, value);
96 }
97 else if (key.startsWith("mule.config."))
98 {
99 String configProperty = key.substring(12);
100 try
101 {
102 BeanUtils.setProperty(configuration, configProperty, value);
103 }
104 catch (IllegalAccessException e)
105 {
106 logger.error(e);
107 }
108 catch (InvocationTargetException e)
109 {
110 logger.error(e);
111 }
112 }
113 }
114 }
115 }