2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.configuration;
20
import java.util.Iterator;
22
import org.apache.commons.logging.Log;
23
import org.apache.commons.logging.LogFactory;
26
* A configuration based on the system properties.
28
* @author Emmanuel Bourg
29
* @version $Id: SystemConfiguration.java 1210204 2011-12-04 20:38:02Z oheger $
32
public class SystemConfiguration extends MapConfiguration
35
private static Log log = LogFactory.getLog(SystemConfiguration.class);
38
* Create a Configuration based on the system properties.
40
* @see System#getProperties
42
public SystemConfiguration()
44
super(System.getProperties());
48
* The method allows system properties to be set from a property file.
49
* @param fileName The name of the property file.
50
* @throws Exception if an error occurs.
53
public static void setSystemProperties(String fileName) throws Exception
55
setSystemProperties(null, fileName);
59
* The method allows system properties to be set from a property file.
60
* @param basePath The base path to look for the property file.
61
* @param fileName The name of the property file.
62
* @throws Exception if an error occurs.
65
public static void setSystemProperties(String basePath, String fileName) throws Exception
67
PropertiesConfiguration config = fileName.endsWith(".xml")
68
? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
71
config.setBasePath(basePath);
73
config.setFileName(fileName);
75
setSystemProperties(config);
79
* Set System properties from a configuration file.
80
* @param systemConfig The configuration containing the properties to be set.
83
public static void setSystemProperties(PropertiesConfiguration systemConfig)
85
Iterator<String> iter = systemConfig.getKeys();
86
while (iter.hasNext())
88
String key = iter.next();
89
String value = (String) systemConfig.getProperty(key);
90
if (log.isDebugEnabled())
92
log.debug("Setting system property " + key + " to " + value);
94
System.setProperty(key, value);