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