Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
include env vars in property lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
pb82 committed May 2, 2018
1 parent 034270f commit 7b5f5fa
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,110 @@ public final class ConfigurationUtils {

private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtils.class);

private ConfigurationUtils() {
// no-op
}
private ConfigurationUtils() { }

/**
* Try to retrieve a system property and returns null if SecurityManager blocks it.
* Try to retrieve a system property and returns the defaultValue if SecurityManager blocks it.
*
* @param key Name of the system property to get the string for.
* @param defaultValue Value to be returned on unsuccessful operation or if the propety is not set.
*
* @return the value of the System property
*/
public static String tryGetProperty(String key) {
return tryGetProperty(key, null);
private static String tryGetProperty(String key, String defaultValue) {
try {
return System.getProperty(key, defaultValue);
} catch (SecurityException e) {
logger.error("Could not get value of property {} due to SecurityManager. Using default value.", key);
return null;
}
}

/**
* Try to retrieve a system property and returns the defaultValue if SecurityManager blocks it.
*
* @param key Name of the system property to get the string for.
* @param key Name of the system property to get the integer for.
* @param defaultValue Value to be returned on unsuccessful operation or if the propety is not set.
*
* @return the value of the System property
*/
public static String tryGetProperty(String key, String defaultValue) {
private static Integer tryGetIntegerProperty(String key, Integer defaultValue) {
try {
return System.getProperty(key, defaultValue);
return Integer.getInteger(key, defaultValue);
} catch (SecurityException e) {
logger.error("Could not get value of property {} due to SecurityManager. Using null value.", key);
return null;
logger.error("Could not get value of property {} due to SecurityManager. Using default value.", key, e);
return defaultValue;
}
}

/**
* Try to retrieve a system property and returns null if SecurityManager blocks it.
*
* @param key Name of the system property to get the integer for.
* @return the value of the System property
* Format a key given system property key as an environment variable, e.g.:
* custom.aerogear.apns.push.host would become CUSTOM_AEROGEAR_APNS_PUSH_HOST
* @param key String System Property Key
* @return String Key as environment variable
*/
public static Integer tryGetIntegerProperty(String key) {
return tryGetIntegerProperty(key, null);
private static String formatEnvironmentVariable(String key) {
return key.toUpperCase().replaceAll("\\.", "_");
}

/**
* Try to retrieve a system property and returns the defaultValue if SecurityManager blocks it.
*
* @param key Name of the system property to get the integer for.
* @param defaultValue Value to be returned on unsuccessful operation or if the propety is not set.
*
* @return the value of the System property
* Get a global string property. This method will first try to get the value from an
* environment variable and if that does not exist it will look up a system property.
* @param key Name of the variable
* @param defaultValue Returned if neither env var nor system property are defined
* @return String the value of the Environment or System Property if defined, the given
* default value otherwise
*/
public static Integer tryGetIntegerProperty(String key, Integer defaultValue) {
public static String tryGetGlobalProperty(String key, String defaultValue) {
try {
return Integer.getInteger(key, defaultValue);
String value = System.getenv(formatEnvironmentVariable(key));
if (value == null) {
value = tryGetProperty(key, defaultValue);
}
return value;
} catch (SecurityException e) {
logger.error("Could not get value of property {} due to SecurityManager. Using null value.", key, e);
logger.error("Could not get value of global property {} due to SecurityManager. Using default value.", key, e);
return defaultValue;
}
}

/**
* Same as `tryGetGlobalProperty` but with null as implicit default value
* @param key Variable name
* @return Environment or System property value or null if not found
*/
public static String tryGetGlobalProperty(String key) {
return tryGetGlobalProperty(key, null);
}

/**
* Get a global integer property. This method will first try to get the value from an
* environment variable and if that does not exist it will look up a system property.
* @param key Name of the variable
* @param defaultValue Returned if neither env var nor system property are defined
* @return String the value of the Environment or System Property if defined, the given
* default value otherwise
*/
public static Integer tryGetGlobalIntegerProperty(String key, Integer defaultValue) {
try {
String value = System.getenv(formatEnvironmentVariable(key));
if (value == null) {
return tryGetIntegerProperty(key, defaultValue);
} else {
return Integer.parseInt(value);
}
} catch (SecurityException | NumberFormatException e) {
logger.error("Could not get value of global property {} due to SecurityManager. Using default value.", key, e);
return defaultValue;
}
}

/**
* Same as `tryGetGlobalIntegerProperty` but with null as implicit default value
* @param key Variable name
* @return Environment or System property value or null if not found
*/
public static Integer tryGetGlobalIntegerProperty(String key) {
return tryGetGlobalIntegerProperty(key, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ public void cleanupTestProperty() {
@Test
public void testExistingTryGetProperty(){
System.setProperty(TEST_PROPERTY_NAME, "MyNiceValue");
assertThat(ConfigurationUtils.tryGetProperty(TEST_PROPERTY_NAME)).isEqualTo("MyNiceValue");
assertThat(ConfigurationUtils.tryGetGlobalProperty(TEST_PROPERTY_NAME)).isEqualTo("MyNiceValue");
}

@Test
public void testNonExistingTryGetProperty(){
assertThat(ConfigurationUtils.tryGetProperty(TEST_PROPERTY_NAME)).isNull();
assertThat(ConfigurationUtils.tryGetGlobalProperty(TEST_PROPERTY_NAME)).isNull();
}

@Test
public void testExistingTryGetIntegerProperty() {
System.setProperty(TEST_PROPERTY_NAME, "123456");
assertThat(ConfigurationUtils.tryGetIntegerProperty(TEST_PROPERTY_NAME)).isEqualTo(123456);
assertThat(ConfigurationUtils.tryGetGlobalIntegerProperty(TEST_PROPERTY_NAME)).isEqualTo(123456);
}

@Test
public void testNonExistingTryGetIntegerProperty() {
assertThat(ConfigurationUtils.tryGetIntegerProperty(TEST_PROPERTY_NAME)).isNull();
assertThat(ConfigurationUtils.tryGetGlobalIntegerProperty(TEST_PROPERTY_NAME)).isNull();
}

@Test
public void testNonExistingTryGetIntegerPropertyWithDefaultValue() {
assertThat(ConfigurationUtils.tryGetIntegerProperty(TEST_PROPERTY_NAME, 123)).isEqualTo(123);
assertThat(ConfigurationUtils.tryGetGlobalIntegerProperty(TEST_PROPERTY_NAME, 123)).isEqualTo(123);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class KeycloakConfigurationEndpoint {
@Produces(MediaType.APPLICATION_JSON)
public Response configurationFile() throws JsonProcessingException {

final String realmName = ConfigurationUtils.tryGetProperty(REALM_NAME_PROPERTY);
final String keycloakServerURL = removeDefaultHttpPorts(ConfigurationUtils.tryGetProperty(REALM_URL_PROPERTY));
final String realmName = ConfigurationUtils.tryGetGlobalProperty(REALM_NAME_PROPERTY);
final String keycloakServerURL = removeDefaultHttpPorts(ConfigurationUtils.tryGetGlobalProperty(REALM_URL_PROPERTY));

final Config config = new Config(realmName, keycloakServerURL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ private static <T> T getProperty(VariantType type, ConfigurationProperty propert
Class<T> expectedType) {
String systemPropertyName = getSystemPropertyName(type, property);
if (expectedType == String.class) {
return (T) ConfigurationUtils.tryGetProperty(systemPropertyName, (String) defaultValue);
return (T) ConfigurationUtils.tryGetGlobalProperty(systemPropertyName, (String) defaultValue);
} else if (expectedType == Integer.class) {
return (T) ConfigurationUtils.tryGetIntegerProperty(systemPropertyName, (Integer) defaultValue);
return (T) ConfigurationUtils.tryGetGlobalIntegerProperty(systemPropertyName, (Integer) defaultValue);
} else {
throw new IllegalStateException("Unexpected type: " + expectedType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListSet;

import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetIntegerProperty;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetProperty;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetGlobalIntegerProperty;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetGlobalProperty;

@Stateless
@SenderType(VariantType.IOS)
Expand All @@ -61,8 +61,8 @@ public class PushyApnsSender implements PushNotificationSender {

public static final String CUSTOM_AEROGEAR_APNS_PUSH_HOST = "custom.aerogear.apns.push.host";
public static final String CUSTOM_AEROGEAR_APNS_PUSH_PORT = "custom.aerogear.apns.push.port";
private static final String customAerogearApnsPushHost = tryGetProperty(CUSTOM_AEROGEAR_APNS_PUSH_HOST);
private static final Integer customAerogearApnsPushPort = tryGetIntegerProperty(CUSTOM_AEROGEAR_APNS_PUSH_PORT);
private static final String customAerogearApnsPushHost = tryGetGlobalProperty(CUSTOM_AEROGEAR_APNS_PUSH_HOST);
private static final Integer customAerogearApnsPushPort = tryGetGlobalIntegerProperty(CUSTOM_AEROGEAR_APNS_PUSH_PORT);

private static final Counter promPrushRequestsIOS = Counter.build()
.name("aerogear_ups_push_requests_ios")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ConfigurableFCMSender(String key) {
protected HttpURLConnection getConnection(String url) throws IOException {

// let's see if there is a different URL we should post to (e.g. load/stress testing)
final String fcmURL = ConfigurationUtils.tryGetProperty(CUSTOM_AEROGEAR_FCM_PUSH_HOST, FCM_ENDPOINT_HOST);
final String fcmURL = ConfigurationUtils.tryGetGlobalProperty(CUSTOM_AEROGEAR_FCM_PUSH_HOST, FCM_ENDPOINT_HOST);

return (HttpURLConnection) new URL(fcmURL).openConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@

import static org.jboss.aerogear.unifiedpush.message.sender.apns.PushyApnsSender.CUSTOM_AEROGEAR_APNS_PUSH_HOST;
import static org.jboss.aerogear.unifiedpush.message.sender.apns.PushyApnsSender.CUSTOM_AEROGEAR_APNS_PUSH_PORT;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetIntegerProperty;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetProperty;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetGlobalIntegerProperty;
import static org.jboss.aerogear.unifiedpush.system.ConfigurationUtils.tryGetGlobalProperty;

/**
* Checks the health of the push networks.
*/
@Stateless
public class HealthNetworkServiceImpl implements HealthNetworkService {
private static final String customAerogearApnsPushHost = tryGetProperty(CUSTOM_AEROGEAR_APNS_PUSH_HOST);
private static final Integer customAerogearApnsPushPort = tryGetIntegerProperty(CUSTOM_AEROGEAR_APNS_PUSH_PORT);
private static final String customAerogearApnsPushHost = tryGetGlobalProperty(CUSTOM_AEROGEAR_APNS_PUSH_HOST);
private static final Integer customAerogearApnsPushPort = tryGetGlobalIntegerProperty(CUSTOM_AEROGEAR_APNS_PUSH_PORT);

private static final String FCM_SEND_ENDPOINT = ConfigurableFCMSender.FCM_ENDPOINT_HOST.substring("https://".length(), ConfigurableFCMSender.FCM_ENDPOINT_HOST.indexOf('/', "https://".length()));
public static final String WNS_SEND_ENDPOINT = "db3.notify.windows.com";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public long countMessagesForPushApplication(String pushApplicationId) {
* <i>older</i> than 30 days!
*/
public void deleteOutdatedFlatPushInformationData() {
final Date historyDate = DateUtils.calculatePastDate(ConfigurationUtils.tryGetIntegerProperty(AEROGEAR_METRICS_STORAGE_MAX_DAYS, 30));
final Date historyDate = DateUtils.calculatePastDate(ConfigurationUtils.tryGetGlobalIntegerProperty(AEROGEAR_METRICS_STORAGE_MAX_DAYS, 30));
logger.trace("Delete all until {}", historyDate.getTime());
flatPushMessageInformationDao.deletePushInformationOlderThan(historyDate);
}
Expand Down

0 comments on commit 7b5f5fa

Please sign in to comment.