    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 * @since 1.1
    031 */
    032public class SystemConfiguration extends MapConfiguration {
    033    /** The logger. */
    034    private static final Log LOG = LogFactory.getLog(SystemConfiguration.class);
    035
    036    /**
    037     * Create a Configuration based on the system properties.
    038     *
    039     * @see System#getProperties
    040     */
    041    public SystemConfiguration() {
    042        super(System.getProperties());
    043    }
    044
    045    /**
    046     * Sets system properties from a file specified by its file name. This is just a short cut for
    047     * {@code setSystemProperties(null, fileName)}.
    048     *
    049     * @param fileName The name of the property file.
    050     * @throws ConfigurationException if an error occurs.
    051     * @since 1.6
    052     */
    053    public static void setSystemProperties(final String fileName) throws ConfigurationException {
    054        setSystemProperties(null, fileName);
    055    }
    056
    057    /**
    058     * Sets system properties from a file specified using its base path and file name. The file can either be a properties
    059     * file or an XML properties file. It is loaded, and all properties it contains are added to system properties.
    060     *
    061     * @param basePath The base path to look for the property file.
    062     * @param fileName The name of the property file.
    063     * @throws ConfigurationException if an error occurs.
    064     * @since 1.6
    065     */
    066    public static void setSystemProperties(final String basePath, final String fileName) throws ConfigurationException {
    067        final FileBasedConfiguration config = fileName.endsWith(".xml") ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
    068
    069        final FileHandler handler = new FileHandler(config);
    070        handler.setBasePath(basePath);
    071        handler.setFileName(fileName);
    072        handler.load();
    073        setSystemProperties(config);
    074    }
    075
    076    /**
    077     * Set System properties from a configuration object.
    078     *
    079     * @param systemConfig The configuration containing the properties to be set.
    080     * @since 1.6
    081     */
    082    public static void setSystemProperties(final Configuration systemConfig) {
    083        final Iterator<String> iter = systemConfig.getKeys();
    084        while (iter.hasNext()) {
    085            final String key = iter.next();
    086            final String value = (String) systemConfig.getProperty(key);
    087            if (LOG.isDebugEnabled()) {
    088                LOG.debug("Setting system property " + key + " to " + value);
    089            }
    090            System.setProperty(key, value);
    091        }
    092    }
    093
    094    /**
    095     * {@inheritDoc} This implementation returns a snapshot of the keys in the system properties. If another thread modifies
    096     * system properties concurrently, these changes are not reflected by the iterator returned by this method.
    097     */
    098    @Override
    099    protected Iterator<String> getKeysInternal() {
    100        return System.getProperties().stringPropertyNames().iterator();
    101    }
    102}