    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    import org.apache.commons.logging.Log;
    023    import org.apache.commons.logging.LogFactory;
    024
    025    /**
    026     * A configuration based on the system properties.
    027     *
    028     * @author Emmanuel Bourg
    029     * @version $Id: SystemConfiguration.java 1158888 2011-08-17 20:01:31Z oheger $
    030     * @since 1.1
    031     */
    032    public class SystemConfiguration extends MapConfiguration
    033    {
    034        /** The logger. */
    035        private static Log log = LogFactory.getLog(SystemConfiguration.class);
    036
    037        /**
    038         * Create a Configuration based on the system properties.
    039         *
    040         * @see System#getProperties
    041         */
    042        public SystemConfiguration()
    043        {
    044            super(System.getProperties());
    045        }
    046
    047        /**
    048         * The method allows system properties to be set from a property file.
    049         * @param fileName The name of the property file.
    050         * @throws Exception if an error occurs.
    051         * @since 1.6
    052         */
    053        public static void setSystemProperties(String fileName) throws Exception
    054        {
    055            setSystemProperties(null, fileName);
    056        }
    057
    058        /**
    059         * The method allows system properties to be set from a property file.
    060         * @param basePath The base path to look for the property file.
    061         * @param fileName The name of the property file.
    062         * @throws Exception if an error occurs.
    063         * @since 1.6
    064         */
    065        public static void setSystemProperties(String basePath, String fileName) throws Exception
    066        {
    067            PropertiesConfiguration config = fileName.endsWith(".xml")
    068                ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
    069            if (basePath != null)
    070            {
    071                config.setBasePath(basePath);
    072            }
    073            config.setFileName(fileName);
    074            config.load();
    075            setSystemProperties(config);
    076        }
    077
    078        /**
    079         * Set System properties from a configuration file.
    080         * @param systemConfig The configuration containing the properties to be set.
    081         * @since 1.6
    082         */
    083        public static void setSystemProperties(PropertiesConfiguration systemConfig)
    084        {
    085            Iterator iter = systemConfig.getKeys();
    086            while (iter.hasNext())
    087            {
    088                String key = (String) iter.next();
    089                String value = (String) systemConfig.getProperty(key);
    090                if (log.isDebugEnabled())
    091                {
    092                    log.debug("Setting system property " + key + " to " + value);
    093                }
    094                System.setProperty(key, value);
    095            }
    096        }
    097    }