SystemConfiguration.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.configuration2;

  18. import java.util.Iterator;

  19. import org.apache.commons.configuration2.ex.ConfigurationException;
  20. import org.apache.commons.configuration2.io.FileHandler;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;

  23. /**
  24.  * A configuration based on the system properties.
  25.  *
  26.  * @since 1.1
  27.  */
  28. public class SystemConfiguration extends MapConfiguration {
  29.     /** The logger. */
  30.     private static final Log LOG = LogFactory.getLog(SystemConfiguration.class);

  31.     /**
  32.      * Sets System properties from a configuration object.
  33.      *
  34.      * @param systemConfig The configuration containing the properties to be set.
  35.      * @since 1.6
  36.      */
  37.     public static void setSystemProperties(final Configuration systemConfig) {
  38.         final Iterator<String> iter = systemConfig.getKeys();
  39.         while (iter.hasNext()) {
  40.             final String key = iter.next();
  41.             final String value = (String) systemConfig.getProperty(key);
  42.             if (LOG.isDebugEnabled()) {
  43.                 LOG.debug("Setting system property " + key + " to " + value);
  44.             }
  45.             System.setProperty(key, value);
  46.         }
  47.     }

  48.     /**
  49.      * Sets system properties from a file specified by its file name. This is just a short cut for
  50.      * {@code setSystemProperties(null, fileName)}.
  51.      *
  52.      * @param fileName The name of the property file.
  53.      * @throws ConfigurationException if an error occurs.
  54.      * @since 1.6
  55.      */
  56.     public static void setSystemProperties(final String fileName) throws ConfigurationException {
  57.         setSystemProperties(null, fileName);
  58.     }

  59.     /**
  60.      * Sets system properties from a file specified using its base path and file name. The file can either be a properties
  61.      * file or an XML properties file. It is loaded, and all properties it contains are added to system properties.
  62.      *
  63.      * @param basePath The base path to look for the property file.
  64.      * @param fileName The name of the property file.
  65.      * @throws ConfigurationException if an error occurs.
  66.      * @since 1.6
  67.      */
  68.     public static void setSystemProperties(final String basePath, final String fileName) throws ConfigurationException {
  69.         final FileBasedConfiguration config = fileName.endsWith(".xml") ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();

  70.         final FileHandler handler = new FileHandler(config);
  71.         handler.setBasePath(basePath);
  72.         handler.setFileName(fileName);
  73.         handler.load();
  74.         setSystemProperties(config);
  75.     }

  76.     /**
  77.      * Create a Configuration based on the system properties.
  78.      *
  79.      * @see System#getProperties
  80.      */
  81.     public SystemConfiguration() {
  82.         super(System.getProperties());
  83.     }

  84.     /**
  85.      * {@inheritDoc} This implementation returns a snapshot of the keys in the system properties. If another thread modifies
  86.      * system properties concurrently, these changes are not reflected by the iterator returned by this method.
  87.      */
  88.     @Override
  89.     protected Iterator<String> getKeysInternal() {
  90.         return System.getProperties().stringPropertyNames().iterator();
  91.     }
  92. }