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.HashMap; 21 22 /** 23 * <p> 24 * A Configuration implementation that reads the platform specific environment variables using the map returned by 25 * {@link System#getenv()}. 26 * </p> 27 * 28 * <p> 29 * This configuration implementation is read-only. It allows read access to the defined OS environment variables, but 30 * their values cannot be changed. Any attempts to add or remove a property will throw an 31 * {@link UnsupportedOperationException} 32 * </p> 33 * 34 * <p> 35 * Usage of this class is easy: After an instance has been created the get methods provided by the {@code Configuration} 36 * interface can be used for querying environment variables, e.g.: 37 * </p> 38 * 39 * <pre> 40 * Configuration envConfig = new EnvironmentConfiguration(); 41 * System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME"); 42 * </pre> 43 * 44 * @since 1.5 45 */ 46 public class EnvironmentConfiguration extends MapConfiguration { 47 /** 48 * Create a Configuration based on the environment variables. 49 * 50 * @see System#getenv() 51 */ 52 public EnvironmentConfiguration() { 53 super(new HashMap<>(System.getenv())); 54 } 55 56 /** 57 * Adds a property to this configuration. Because this configuration is read-only, this operation is not allowed and 58 * will cause an exception. 59 * 60 * @param key the key of the property to be added 61 * @param value the property value 62 */ 63 @Override 64 protected void addPropertyDirect(final String key, final Object value) { 65 throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); 66 } 67 68 /** 69 * Removes a property from this configuration. Because this configuration is read-only, this operation is not allowed 70 * and will cause an exception. 71 * 72 * @param key the key of the property to be removed 73 */ 74 @Override 75 protected void clearPropertyDirect(final String key) { 76 throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); 77 } 78 79 /** 80 * Removes all properties from this configuration. Because this configuration is read-only, this operation is not 81 * allowed and will cause an exception. 82 */ 83 @Override 84 protected void clearInternal() { 85 throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!"); 86 } 87 }