    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 * @since 1.5
    041 */
    042public class EnvironmentConfiguration extends MapConfiguration
    043{
    044    /**
    045     * Create a Configuration based on the environment variables.
    046     *
    047     * @see System#getenv()
    048     */
    049    public EnvironmentConfiguration()
    050    {
    051        super(new HashMap<String, Object>(System.getenv()));
    052    }
    053
    054    /**
    055     * Adds a property to this configuration. Because this configuration is
    056     * read-only, this operation is not allowed and will cause an exception.
    057     *
    058     * @param key the key of the property to be added
    059     * @param value the property value
    060     */
    061    @Override
    062    protected void addPropertyDirect(final String key, final Object value)
    063    {
    064        throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
    065    }
    066
    067    /**
    068     * Removes a property from this configuration. Because this configuration is
    069     * read-only, this operation is not allowed and will cause an exception.
    070     *
    071     * @param key the key of the property to be removed
    072     */
    073    @Override
    074    protected void clearPropertyDirect(final String key)
    075    {
    076        throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
    077    }
    078
    079    /**
    080     * Removes all properties from this configuration. Because this
    081     * configuration is read-only, this operation is not allowed and will cause
    082     * an exception.
    083     */
    084    @Override
    085    protected void clearInternal()
    086    {
    087        throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
    088    }
    089}