Source code
Description: source: package: org.apache.commons.configuration2, class: EnvironmentConfiguration

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