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.internal;
17
18 import static software.amazon.awssdk.utils.OptionalUtils.firstPresent;
19
20 import java.util.Optional;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import software.amazon.awssdk.annotations.SdkInternalApi;
24 import software.amazon.awssdk.utils.SystemSetting;
25
26 /**
27 * A set of static utility methods for shared code in {@link SystemSetting}.
28 */
29 @SdkInternalApi
30 public final class SystemSettingUtils {
31 private static final Logger LOG = LoggerFactory.getLogger(SystemSettingUtils.class);
32
33 private SystemSettingUtils() {
34 }
35
36 /**
37 * Resolve the value of this system setting, loading it from the System by checking:
38 * <ol>
39 * <li>The system properties.</li>
40 * <li>The environment variables.</li>
41 * <li>The default value.</li>
42 * </ol>
43 */
44 public static Optional<String> resolveSetting(SystemSetting setting) {
45 return firstPresent(resolveProperty(setting), () -> resolveEnvironmentVariable(setting), () -> resolveDefault(setting))
46 .map(String::trim);
47 }
48
49 /**
50 * Attempt to load this setting from the system properties.
51 */
52 private static Optional<String> resolveProperty(SystemSetting setting) {
53 // CHECKSTYLE:OFF - This is the only place we're allowed to use System.getProperty
54 return Optional.ofNullable(setting.property()).map(System::getProperty);
55 // CHECKSTYLE:ON
56 }
57
58 /**
59 * Attempt to load this setting from the environment variables.
60 */
61 private static Optional<String> resolveEnvironmentVariable(SystemSetting setting) {
62 try {
63 // CHECKSTYLE:OFF - This is the only place we're allowed to use System.getenv
64 return Optional.ofNullable(setting.environmentVariable()).map(System::getenv);
65 // CHECKSTYLE:ON
66 } catch (SecurityException e) {
67 LOG.debug("Unable to load the environment variable '{}' because the security manager did not allow the SDK" +
68 " to read this system property. This setting will be assumed to be null", setting.environmentVariable(), e);
69 return Optional.empty();
70 }
71 }
72
73 /**
74 * Load the default value from the setting.
75 */
76 private static Optional<String> resolveDefault(SystemSetting setting) {
77 return Optional.ofNullable(setting.defaultValue());
78 }
79
80 /**
81 * Convert a string to boolean safely (as opposed to the less strict {@link Boolean#parseBoolean(String)}). If a customer
82 * specifies a boolean value it should be "true" or "false" (case insensitive) or an exception will be thrown.
83 */
84 public static Boolean safeStringToBoolean(SystemSetting setting, String value) {
85 if (value.equalsIgnoreCase("true")) {
86 return true;
87 } else if (value.equalsIgnoreCase("false")) {
88 return false;
89 }
90
91 throw new IllegalStateException("Environment variable '" + setting.environmentVariable() + "' or system property '" +
92 setting.property() + "' was defined as '" + value + "', but should be 'false' or 'true'");
93 }
94 }
95