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.HashMap; 021 022/** 023 * <p>A Configuration implementation that reads the platform specific 024 * environment variables using the map returned by {@link System#getenv()}.</p> 025 * 026 * <p>This configuration implementation is read-only. It allows read access to the 027 * defined OS environment variables, but their values cannot be changed. Any 028 * attempts to add or remove a property will throw an 029 * {@link UnsupportedOperationException}</p> 030 * 031 * <p>Usage of this class is easy: After an instance has been created the get 032 * methods provided by the {@code Configuration} interface can be used 033 * for querying environment variables, e.g.:</p> 034 * 035 * <pre> 036 * Configuration envConfig = new EnvironmentConfiguration(); 037 * System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME"); 038 * </pre> 039 * 040 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a> 041 * @since 1.5 042 */ 043public class EnvironmentConfiguration extends MapConfiguration 044{ 045 /** 046 * Create a Configuration based on the environment variables. 047 * 048 * @see System#getenv() 049 */ 050 public EnvironmentConfiguration() 051 { 052 super(new HashMap<String, Object>(System.getenv())); 053 } 054 055 /** 056 * Adds a property to this configuration. Because this configuration is 057 * read-only, this operation is not allowed and will cause an exception. 058 * 059 * @param key the key of the property to be added 060 * @param value the property value 061 */ 062 @Override 063 protected void addPropertyDirect(final String key, final Object value) 064 { 065 throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); 066 } 067 068 /** 069 * Removes a property from this configuration. Because this configuration is 070 * read-only, this operation is not allowed and will cause an exception. 071 * 072 * @param key the key of the property to be removed 073 */ 074 @Override 075 protected void clearPropertyDirect(final String key) 076 { 077 throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); 078 } 079 080 /** 081 * Removes all properties from this configuration. Because this 082 * configuration is read-only, this operation is not allowed and will cause 083 * an exception. 084 */ 085 @Override 086 protected void clearInternal() 087 { 088 throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); 089 } 090}