1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package software.amazon.awssdk.utils;
17
18 import java.util.Optional;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 import software.amazon.awssdk.utils.internal.SystemSettingUtils;
21
22 /**
23  * An interface implemented by enums in other packages in order to define the system settings the want loaded. An enum
24  * is expected to implement this interface, and then have values loaded from the {@link System} using methods like
25  * {@link #getStringValue()}.
26  */

27 @SdkProtectedApi
28 public interface SystemSetting {
29     /**
30      * The system property of the setting (or null if there is no property for this setting).
31      */

32     String property();
33
34     /**
35      * The environment variable of the setting (or null if there is no environment variable for this setting).
36      */

37     String environmentVariable();
38
39     /**
40      * The default value of the setting (or empty if there is no default). This value will be applied if the customer did not
41      * specify a setting.
42      */

43     String defaultValue();
44
45     /**
46      * Attempt to load a system setting from {@link System#getProperty(String)} and {@link System#getenv(String)}. This should be
47      * used in favor of those methods because the SDK should support both methods of configuration.
48      *
49      * {@link System#getProperty(String)} takes precedent over {@link System#getenv(String)} if both are specified.
50      *
51      * @return The requested setting, or {@link Optional#empty()} if the values were not set, or the security manager did not
52      *         allow reading the setting.
53      */

54     default Optional<String> getStringValue() {
55         return SystemSettingUtils.resolveSetting(this);
56     }
57
58     /**
59      * Load the requested system setting as per the documentation in {@link #getStringValue()}, throwing an exception if the value
60      * was not set and had no default.
61      *
62      * @return The requested setting.
63      */

64     default String getStringValueOrThrow() {
65         return getStringValue().orElseThrow(() ->
66                 new IllegalStateException("Either the environment variable " + environmentVariable() + " or the java"
67                                           + "property " + property() + " must be set."));
68     }
69
70     /**
71      * Attempt to load a system setting from {@link System#getProperty(String)} and {@link System#getenv(String)}. This should be
72      * used in favor of those methods because the SDK should support both methods of configuration.
73      *
74      * The result will be converted to an integer.
75      *
76      * {@link System#getProperty(String)} takes precedent over {@link System#getenv(String)} if both are specified.
77      *
78      * @return The requested setting, or {@link Optional#empty()} if the values were not set, or the security manager did not
79      *         allow reading the setting.
80      */

81     default Optional<Integer> getIntegerValue() {
82         return getStringValue().map(Integer::parseInt);
83     }
84
85
86     /**
87      * Load the requested system setting as per the documentation in {@link #getIntegerValue()}, throwing an exception if the
88      * value was not set and had no default.
89      *
90      * @return The requested setting.
91      */

92     default Integer getIntegerValueOrThrow() {
93         return Integer.parseInt(getStringValueOrThrow());
94     }
95
96     /**
97      * Attempt to load a system setting from {@link System#getProperty(String)} and {@link System#getenv(String)}. This should be
98      * used in favor of those methods because the SDK should support both methods of configuration.
99      *
100      * The result will be converted to a boolean.
101      *
102      * {@link System#getProperty(String)} takes precedent over {@link System#getenv(String)} if both are specified.
103      *
104      * @return The requested setting, or {@link Optional#empty()} if the values were not set, or the security manager did not
105      *         allow reading the setting.
106      */

107     default Optional<Boolean> getBooleanValue() {
108         return getStringValue().map(value -> SystemSettingUtils.safeStringToBoolean(this, value));
109     }
110
111     /**
112      * Load the requested system setting as per the documentation in {@link #getBooleanValue()}, throwing an
113      * exception if the value was not set and had no default.
114      *
115      * @return The requested setting.
116      */

117     default Boolean getBooleanValueOrThrow() {
118         return getBooleanValue().orElseThrow(() ->
119                 new IllegalStateException("Either the environment variable " + environmentVariable() + " or the java"
120                                           + "property " + property() + " must be set."));
121     }
122 }
123