    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
    018    package org.apache.commons.configuration;
    019
    020    import 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     * @version $Id: EnvironmentConfiguration.java 1210171 2011-12-04 18:32:07Z oheger $
    042     * @since 1.5
    043     */
    044    public class EnvironmentConfiguration extends MapConfiguration
    045    {
    046        /**
    047         * Create a Configuration based on the environment variables.
    048         *
    049         * @see System#getenv()
    050         */
    051        public EnvironmentConfiguration()
    052        {
    053            super(new HashMap<String, Object>(System.getenv()));
    054        }
    055
    056        /**
    057         * Adds a property to this configuration. Because this configuration is
    058         * read-only, this operation is not allowed and 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(String key, Object value)
    065        {
    066            throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
    067        }
    068
    069        /**
    070         * Removes a property from this configuration. Because this configuration is
    071         * read-only, this operation is not allowed and will cause an exception.
    072         *
    073         * @param key the key of the property to be removed
    074         */
    075        @Override
    076        public void clearProperty(String key)
    077        {
    078            throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
    079        }
    080
    081        /**
    082         * Removes all properties from this configuration. Because this
    083         * configuration is read-only, this operation is not allowed and will cause
    084         * an exception.
    085         */
    086        @Override
    087        public void clear()
    088        {
    089            throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
    090        }
    091    }