001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.configuration;
019
020 import java.util.Iterator;
021
022 /**
023 * A configuration based on the system properties.
024 *
025 * @author Emmanuel Bourg
026 * @version $Revision: 727834 $, $Date: 2008-12-18 23:16:32 +0100 (Do, 18 Dez 2008) $
027 * @since 1.1
028 */
029 public class SystemConfiguration extends MapConfiguration
030 {
031 /**
032 * Create a Configuration based on the system properties.
033 *
034 * @see System#getProperties
035 */
036 public SystemConfiguration()
037 {
038 super(System.getProperties());
039 }
040
041 /**
042 * The method allows system properties to be set from a property file.
043 * @param fileName The name of the property file.
044 * @throws Exception if an error occurs.
045 */
046 public static void setSystemProperties(String fileName) throws Exception
047 {
048 PropertiesConfiguration config = fileName.endsWith(".xml")
049 ? new XMLPropertiesConfiguration(fileName) : new PropertiesConfiguration(fileName);
050 setSystemProperties(config);
051 }
052
053 /**
054 * Set System properties from a configuration file.
055 * @param systemConfig The configuration containing the properties to be set.
056 */
057 public static void setSystemProperties(PropertiesConfiguration systemConfig)
058 {
059 Iterator iter = systemConfig.getKeys();
060 while (iter.hasNext())
061 {
062 String key = (String) iter.next();
063 String value = (String) systemConfig.getProperty(key);
064 System.setProperty(key, value);
065 }
066 }
067 }