/** * Load the requested system setting as per the documentation in {@link #getStringValue()}, throwing an exception if the value * was not set and had no default. * * @return The requested setting. */ default String getStringValueOrThrow() { return getStringValue().orElseThrow(() -> new IllegalStateException("Either the environment variable " + environmentVariable() + " or the java" + "property " + property() + " must be set.")); }
/** * Load the default value from the setting. */ private static Optional<String> resolveDefault(SystemSetting setting) { return Optional.ofNullable(setting.defaultValue()); }
/** * Load the requested system setting as per the documentation in {@link #getBooleanValue()}, throwing an * exception if the value was not set and had no default. * * @return The requested setting. */ default Boolean getBooleanValueOrThrow() { return getBooleanValue().orElseThrow(() -> new IllegalStateException("Either the environment variable " + environmentVariable() + " or the java" + "property " + property() + " must be set.")); } }
/** * Convert a string to boolean safely (as opposed to the less strict {@link Boolean#parseBoolean(String)}). If a customer * specifies a boolean value it should be "true" or "false" (case insensitive) or an exception will be thrown. */ public static Boolean safeStringToBoolean(SystemSetting setting, String value) { if (value.equalsIgnoreCase("true")) { return true; } else if (value.equalsIgnoreCase("false")) { return false; } throw new IllegalStateException("Environment variable '" + setting.environmentVariable() + "' or system property '" + setting.property() + "' was defined as '" + value + "', but should be 'false' or 'true'"); } }
@Override protected Optional<String> loadSetting(SystemSetting setting) { // CHECKSTYLE:OFF - Customers should be able to specify a credentials provider that only looks at the environment // variables, but not the system properties. For that reason, we're only checking the environment variable here. return Optional.ofNullable(System.getenv(setting.environmentVariable())); // CHECKSTYLE:ON }
@Override protected Optional<String> loadSetting(SystemSetting setting) { // CHECKSTYLE:OFF - Customers should be able to specify a credentials provider that only looks at the system properties, // but not the environment variables. For that reason, we're only checking the system properties here. return Optional.ofNullable(System.getProperty(setting.property())); // CHECKSTYLE:ON }
@Override public Optional<T> loadService() { return implSetting .getStringValue() .map(this::createServiceFromProperty); }
@Override protected Optional<String> loadSetting(SystemSetting setting) { // CHECKSTYLE:OFF - Customers should be able to specify a credentials provider that only looks at the environment // variables, but not the system properties. For that reason, we're only checking the environment variable here. return Optional.ofNullable(System.getenv(setting.environmentVariable())); // CHECKSTYLE:ON }
/** * Convert a string to boolean safely (as opposed to the less strict {@link Boolean#parseBoolean(String)}). If a customer * specifies a boolean value it should be "true" or "false" (case insensitive) or an exception will be thrown. */ public static Boolean safeStringToBoolean(SystemSetting setting, String value) { if (value.equalsIgnoreCase("true")) { return true; } else if (value.equalsIgnoreCase("false")) { return false; } throw new IllegalStateException("Environment variable '" + setting.environmentVariable() + "' or system property '" + setting.property() + "' was defined as '" + value + "', but should be 'false' or 'true'"); } }
/** * Attempt to load this setting from the system properties. */ private static Optional<String> resolveProperty(SystemSetting setting) { // CHECKSTYLE:OFF - This is the only place we're allowed to use System.getProperty return Optional.ofNullable(setting.property()).map(System::getProperty); // CHECKSTYLE:ON }
/** * Attempt to load a system setting from {@link System#getProperty(String)} and {@link System#getenv(String)}. This should be * used in favor of those methods because the SDK should support both methods of configuration. * * The result will be converted to a boolean. * * {@link System#getProperty(String)} takes precedent over {@link System#getenv(String)} if both are specified. * * @return The requested setting, or {@link Optional#empty()} if the values were not set, or the security manager did not * allow reading the setting. */ default Optional<Boolean> getBooleanValue() { return getStringValue().map(value -> SystemSettingUtils.safeStringToBoolean(this, value)); }
/** * Load the requested system setting as per the documentation in {@link #getStringValue()}, throwing an exception if the value * was not set and had no default. * * @return The requested setting. */ default String getStringValueOrThrow() { return getStringValue().orElseThrow(() -> new IllegalStateException("Either the environment variable " + environmentVariable() + " or the java" + "property " + property() + " must be set.")); }
/** * Load the requested system setting as per the documentation in {@link #getBooleanValue()}, throwing an * exception if the value was not set and had no default. * * @return The requested setting. */ default Boolean getBooleanValueOrThrow() { return getBooleanValue().orElseThrow(() -> new IllegalStateException("Either the environment variable " + environmentVariable() + " or the java" + "property " + property() + " must be set.")); } }
/** * Attempt to load this setting from the environment variables. */ private static Optional<String> resolveEnvironmentVariable(SystemSetting setting) { try { // CHECKSTYLE:OFF - This is the only place we're allowed to use System.getenv return Optional.ofNullable(setting.environmentVariable()).map(System::getenv); // CHECKSTYLE:ON } catch (SecurityException e) { LOG.debug("Unable to load the environment variable '{}' because the security manager did not allow the SDK" + " to read this system property. This setting will be assumed to be null", setting.environmentVariable(), e); return Optional.empty(); } }
/** * Attempt to load this setting from the system properties. */ private static Optional<String> resolveProperty(SystemSetting setting) { // CHECKSTYLE:OFF - This is the only place we're allowed to use System.getProperty return Optional.ofNullable(setting.property()).map(System::getProperty); // CHECKSTYLE:ON }
/** * Attempt to load a system setting from {@link System#getProperty(String)} and {@link System#getenv(String)}. This should be * used in favor of those methods because the SDK should support both methods of configuration. * * The result will be converted to a boolean. * * {@link System#getProperty(String)} takes precedent over {@link System#getenv(String)} if both are specified. * * @return The requested setting, or {@link Optional#empty()} if the values were not set, or the security manager did not * allow reading the setting. */ default Optional<Boolean> getBooleanValue() { return getStringValue().map(value -> SystemSettingUtils.safeStringToBoolean(this, value)); }
/** * Load the default value from the setting. */ private static Optional<String> resolveDefault(SystemSetting setting) { return Optional.ofNullable(setting.defaultValue()); }
/** * Attempt to load this setting from the environment variables. */ private static Optional<String> resolveEnvironmentVariable(SystemSetting setting) { try { // CHECKSTYLE:OFF - This is the only place we're allowed to use System.getenv return Optional.ofNullable(setting.environmentVariable()).map(System::getenv); // CHECKSTYLE:ON } catch (SecurityException e) { LOG.debug("Unable to load the environment variable '{}' because the security manager did not allow the SDK" + " to read this system property. This setting will be assumed to be null", setting.environmentVariable(), e); return Optional.empty(); } }
@Override protected Optional<String> loadSetting(SystemSetting setting) { // CHECKSTYLE:OFF - Customers should be able to specify a credentials provider that only looks at the system properties, // but not the environment variables. For that reason, we're only checking the system properties here. return Optional.ofNullable(System.getProperty(setting.property())); // CHECKSTYLE:ON }
public void set(SystemSetting setting, String value) { set(setting.environmentVariable(), value); }