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