    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
    018package org.apache.commons.configuration2;
    019
    020import java.util.Iterator;
    021
    022import org.apache.commons.configuration2.ex.ConfigurationException;
    023import org.apache.commons.configuration2.io.FileHandler;
    024import org.apache.commons.logging.Log;
    025import org.apache.commons.logging.LogFactory;
    026
    027/**
    028 * A configuration based on the system properties.
    029 *
    030 * @author Emmanuel Bourg
    031 * @version $Id: SystemConfiguration.java 1842194 2018-09-27 22:24:23Z ggregory $
    032 * @since 1.1
    033 */
    034public class SystemConfiguration extends MapConfiguration
    035{
    036    /** The logger. */
    037    private static Log log = LogFactory.getLog(SystemConfiguration.class);
    038
    039    /**
    040     * Create a Configuration based on the system properties.
    041     *
    042     * @see System#getProperties
    043     */
    044    public SystemConfiguration()
    045    {
    046        super(System.getProperties());
    047    }
    048
    049    /**
    050     * Sets system properties from a file specified by its file name. This is
    051     * just a short cut for {@code setSystemProperties(null, fileName)}.
    052     *
    053     * @param fileName The name of the property file.
    054     * @throws ConfigurationException if an error occurs.
    055     * @since 1.6
    056     */
    057    public static void setSystemProperties(final String fileName)
    058            throws ConfigurationException
    059    {
    060        setSystemProperties(null, fileName);
    061    }
    062
    063    /**
    064     * Sets system properties from a file specified using its base path and
    065     * file name. The file can either be a properties file or an XML properties
    066     * file. It is loaded, and all properties it contains are added to system
    067     * properties.
    068     *
    069     * @param basePath The base path to look for the property file.
    070     * @param fileName The name of the property file.
    071     * @throws ConfigurationException if an error occurs.
    072     * @since 1.6
    073     */
    074    public static void setSystemProperties(final String basePath, final String fileName)
    075            throws ConfigurationException
    076    {
    077        final FileBasedConfiguration config =
    078                fileName.endsWith(".xml") ? new XMLPropertiesConfiguration()
    079                        : new PropertiesConfiguration();
    080
    081        final FileHandler handler = new FileHandler(config);
    082        handler.setBasePath(basePath);
    083        handler.setFileName(fileName);
    084        handler.load();
    085        setSystemProperties(config);
    086    }
    087
    088    /**
    089     * Set System properties from a configuration object.
    090     * @param systemConfig The configuration containing the properties to be set.
    091     * @since 1.6
    092     */
    093    public static void setSystemProperties(final Configuration systemConfig)
    094    {
    095        final Iterator<String> iter = systemConfig.getKeys();
    096        while (iter.hasNext())
    097        {
    098            final String key = iter.next();
    099            final String value = (String) systemConfig.getProperty(key);
    100            if (log.isDebugEnabled())
    101            {
    102                log.debug("Setting system property " + key + " to " + value);
    103            }
    104            System.setProperty(key, value);
    105        }
    106    }
    107
    108    /**
    109     * {@inheritDoc} This implementation returns a snapshot of the keys in the
    110     * system properties. If another thread modifies system properties concurrently,
    111     * these changes are not reflected by the iterator returned by this method.
    112     */
    113    @Override
    114    protected Iterator<String> getKeysInternal()
    115    {
    116        return System.getProperties().stringPropertyNames().iterator();
    117    }
    118}