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