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 18 package org.apache.commons.configuration2; 19 20 import java.util.Iterator; 21 22 import org.apache.commons.configuration2.ex.ConfigurationException; 23 import org.apache.commons.configuration2.io.FileHandler; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 /** 28 * A configuration based on the system properties. 29 * 30 * @since 1.1 31 */ 32 public class SystemConfiguration extends MapConfiguration { 33 /** The logger. */ 34 private static final Log LOG = LogFactory.getLog(SystemConfiguration.class); 35 36 /** 37 * Sets System properties from a configuration object. 38 * 39 * @param systemConfig The configuration containing the properties to be set. 40 * @since 1.6 41 */ 42 public static void setSystemProperties(final Configuration systemConfig) { 43 final Iterator<String> iter = systemConfig.getKeys(); 44 while (iter.hasNext()) { 45 final String key = iter.next(); 46 final String value = (String) systemConfig.getProperty(key); 47 if (LOG.isDebugEnabled()) { 48 LOG.debug("Setting system property " + key + " to " + value); 49 } 50 System.setProperty(key, value); 51 } 52 } 53 54 /** 55 * Sets system properties from a file specified by its file name. This is just a short cut for 56 * {@code setSystemProperties(null, fileName)}. 57 * 58 * @param fileName The name of the property file. 59 * @throws ConfigurationException if an error occurs. 60 * @since 1.6 61 */ 62 public static void setSystemProperties(final String fileName) throws ConfigurationException { 63 setSystemProperties(null, fileName); 64 } 65 66 /** 67 * Sets system properties from a file specified using its base path and file name. The file can either be a properties 68 * file or an XML properties file. It is loaded, and all properties it contains are added to system properties. 69 * 70 * @param basePath The base path to look for the property file. 71 * @param fileName The name of the property file. 72 * @throws ConfigurationException if an error occurs. 73 * @since 1.6 74 */ 75 public static void setSystemProperties(final String basePath, final String fileName) throws ConfigurationException { 76 final FileBasedConfiguration config = fileName.endsWith(".xml") ? new XMLPropertiesConfiguration() : new PropertiesConfiguration(); 77 78 final FileHandler handler = new FileHandler(config); 79 handler.setBasePath(basePath); 80 handler.setFileName(fileName); 81 handler.load(); 82 setSystemProperties(config); 83 } 84 85 /** 86 * Create a Configuration based on the system properties. 87 * 88 * @see System#getProperties 89 */ 90 public SystemConfiguration() { 91 super(System.getProperties()); 92 } 93 94 /** 95 * {@inheritDoc} This implementation returns a snapshot of the keys in the system properties. If another thread modifies 96 * system properties concurrently, these changes are not reflected by the iterator returned by this method. 97 */ 98 @Override 99 protected Iterator<String> getKeysInternal() { 100 return System.getProperties().stringPropertyNames().iterator(); 101 } 102 }