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     * Sets System properties from a configuration object.
038     *
039     * @param systemConfig The configuration containing the properties to be set.
040     * @since 1.6
041     */
042    public static void setSystemProperties(final Configuration systemConfig) {
043        final Iterator<String> iter = systemConfig.getKeys();
044        while (iter.hasNext()) {
045            final String key = iter.next();
046            final String value = (String) systemConfig.getProperty(key);
047            if (LOG.isDebugEnabled()) {
048                LOG.debug("Setting system property " + key + " to " + value);
049            }
050            System.setProperty(key, value);
051        }
052    }
053
054    /**
055     * Sets system properties from a file specified by its file name. This is just a short cut for
056     * {@code setSystemProperties(null, fileName)}.
057     *
058     * @param fileName The name of the property file.
059     * @throws ConfigurationException if an error occurs.
060     * @since 1.6
061     */
062    public static void setSystemProperties(final String fileName) throws ConfigurationException {
063        setSystemProperties(null, fileName);
064    }
065
066    /**
067     * Sets system properties from a file specified using its base path and file name. The file can either be a properties
068     * file or an XML properties file. It is loaded, and all properties it contains are added to system properties.
069     *
070     * @param basePath The base path to look for the property file.
071     * @param fileName The name of the property file.
072     * @throws ConfigurationException if an error occurs.
073     * @since 1.6
074     */
075    public static void setSystemProperties(final String basePath, final String fileName) throws ConfigurationException {
076        final FileBasedConfiguration config = fileName.endsWith(".xml") ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
077
078        final FileHandler handler = new FileHandler(config);
079        handler.setBasePath(basePath);
080        handler.setFileName(fileName);
081        handler.load();
082        setSystemProperties(config);
083    }
084
085    /**
086     * Create a Configuration based on the system properties.
087     *
088     * @see System#getProperties
089     */
090    public SystemConfiguration() {
091        super(System.getProperties());
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}