View Javadoc

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  
18  package org.apache.commons.configuration;
19  
20  import java.util.HashMap;
21  
22  /**
23   * <p>A Configuration implementation that reads the platform specific
24   * environment variables using the map returned by {@link System#getenv()}.</p>
25   *
26   * <p>This configuration implementation is read-only. It allows read access to the
27   * defined OS environment variables, but their values cannot be changed. Any
28   * attempts to add or remove a property will throw an
29   * {@link UnsupportedOperationException}</p>
30   *
31   * <p>Usage of this class is easy: After an instance has been created the get
32   * methods provided by the {@code Configuration} interface can be used
33   * for querying environment variables, e.g.:</p>
34   *
35   * <pre>
36   * Configuration envConfig = new EnvironmentConfiguration();
37   * System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME");
38   * </pre>
39   *
40   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
41   * @version $Id: EnvironmentConfiguration.java 1210171 2011-12-04 18:32:07Z oheger $
42   * @since 1.5
43   */
44  public class EnvironmentConfiguration extends MapConfiguration
45  {
46      /**
47       * Create a Configuration based on the environment variables.
48       *
49       * @see System#getenv()
50       */
51      public EnvironmentConfiguration()
52      {
53          super(new HashMap<String, Object>(System.getenv()));
54      }
55  
56      /**
57       * Adds a property to this configuration. Because this configuration is
58       * read-only, this operation is not allowed and will cause an exception.
59       *
60       * @param key the key of the property to be added
61       * @param value the property value
62       */
63      @Override
64      protected void addPropertyDirect(String key, Object value)
65      {
66          throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
67      }
68  
69      /**
70       * Removes a property from this configuration. Because this configuration is
71       * read-only, this operation is not allowed and will cause an exception.
72       *
73       * @param key the key of the property to be removed
74       */
75      @Override
76      public void clearProperty(String key)
77      {
78          throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
79      }
80  
81      /**
82       * Removes all properties from this configuration. Because this
83       * configuration is read-only, this operation is not allowed and will cause
84       * an exception.
85       */
86      @Override
87      public void clear()
88      {
89          throw new UnsupportedOperationException("EnvironmentConfiguration is read-only!");
90      }
91  }