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