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