1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import java.util.Iterator;
21
22 /***
23 * A configuration based on the system properties.
24 *
25 * @author Emmanuel Bourg
26 * @version $Revision: 727834 $, $Date: 2008-12-18 23:16:32 +0100 (Do, 18 Dez 2008) $
27 * @since 1.1
28 */
29 public class SystemConfiguration extends MapConfiguration
30 {
31 /***
32 * Create a Configuration based on the system properties.
33 *
34 * @see System#getProperties
35 */
36 public SystemConfiguration()
37 {
38 super(System.getProperties());
39 }
40
41 /***
42 * The method allows system properties to be set from a property file.
43 * @param fileName The name of the property file.
44 * @throws Exception if an error occurs.
45 */
46 public static void setSystemProperties(String fileName) throws Exception
47 {
48 PropertiesConfiguration config = fileName.endsWith(".xml")
49 ? new XMLPropertiesConfiguration(fileName) : new PropertiesConfiguration(fileName);
50 setSystemProperties(config);
51 }
52
53 /***
54 * Set System properties from a configuration file.
55 * @param systemConfig The configuration containing the properties to be set.
56 */
57 public static void setSystemProperties(PropertiesConfiguration systemConfig)
58 {
59 Iterator iter = systemConfig.getKeys();
60 while (iter.hasNext())
61 {
62 String key = (String) iter.next();
63 String value = (String) systemConfig.getProperty(key);
64 System.setProperty(key, value);
65 }
66 }
67 }