EnvironmentConfiguration.java

  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. package org.apache.commons.configuration2;

  18. import java.util.HashMap;

  19. /**
  20.  * <p>
  21.  * A Configuration implementation that reads the platform specific environment variables using the map returned by
  22.  * {@link System#getenv()}.
  23.  * </p>
  24.  *
  25.  * <p>
  26.  * This configuration implementation is read-only. It allows read access to the defined OS environment variables, but
  27.  * their values cannot be changed. Any attempts to add or remove a property will throw an
  28.  * {@link UnsupportedOperationException}
  29.  * </p>
  30.  *
  31.  * <p>
  32.  * Usage of this class is easy: After an instance has been created the get methods provided by the {@code Configuration}
  33.  * interface can be used for querying environment variables, for example:
  34.  * </p>
  35.  *
  36.  * <pre>
  37.  * Configuration envConfig = new EnvironmentConfiguration();
  38.  * System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME");
  39.  * </pre>
  40.  *
  41.  * @since 1.5
  42.  */
  43. public class EnvironmentConfiguration extends MapConfiguration {
  44.     /**
  45.      * Create a Configuration based on the environment variables.
  46.      *
  47.      * @see System#getenv()
  48.      */
  49.     public EnvironmentConfiguration() {
  50.         super(new HashMap<>(System.getenv()));
  51.     }

  52.     /**
  53.      * Adds a property to this configuration. Because this configuration is read-only, this operation is not allowed and
  54.      * will cause an exception.
  55.      *
  56.      * @param key the key of the property to be added
  57.      * @param value the property value
  58.      */
  59.     @Override
  60.     protected void addPropertyDirect(final String key, final Object value) {
  61.         throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
  62.     }

  63.     /**
  64.      * Removes all properties from this configuration. Because this configuration is read-only, this operation is not
  65.      * allowed and will cause an exception.
  66.      */
  67.     @Override
  68.     protected void clearInternal() {
  69.         throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
  70.     }

  71.     /**
  72.      * Removes a property from this configuration. Because this configuration is read-only, this operation is not allowed
  73.      * and will cause an exception.
  74.      *
  75.      * @param key the key of the property to be removed
  76.      */
  77.     @Override
  78.     protected void clearPropertyDirect(final String key) {
  79.         throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
  80.     }
  81. }