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