com.github.netty.core.util.SystemPropertyUtil Maven / Gradle / Ivy
The newest version!
package com.github.netty.core.util;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* Created by wangzihao on 2018/11/8/008.
* A collection of utility methods to retrieve and parse the values of the Java system properties.
*/
public final class SystemPropertyUtil {
private static final LoggerX LOGGER = LoggerFactoryX.getLogger(SystemPropertyUtil.class);
private SystemPropertyUtil() {
}
/**
* Returns {@code true} if and only if the system property with the specified {@code key}
* exists.
*
* @param key key
* @return contains boolean
*/
public static boolean contains(String key) {
return get(key) != null;
}
/**
* Returns the value of the Java system property with the specified
* {@code key}, while falling back to {@code null} if the property access fails.
*
* @param key key
* @return the property value or {@code null}
*/
public static String get(String key) {
return get(key, null);
}
/**
* Returns the value of the Java system property with the specified
*
* @param key while falling back to the specified default value if the property access fails.
* @param def if there's no such property or if an access to the specified property is not allowed.
* @return the property value.
*/
public static String get(final String key, String def) {
if (key == null) {
throw new NullPointerException("key");
}
if (key.isEmpty()) {
throw new IllegalArgumentException("key must not be empty.");
}
String value = null;
try {
if (System.getSecurityManager() == null) {
value = System.getProperty(key);
} else {
value = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public String run() {
return System.getProperty(key);
}
});
}
} catch (SecurityException e) {
LOGGER.warn("Unable to retrieve a system property '{}'; default values will be used.", key, e);
}
if (value == null) {
return def;
}
return value;
}
/**
* Returns the value of the Java system property with the specified
*
* @param key while falling back to the specified default value if the property access fails.
* @param def if there's no such property or if an access to the specified property is not allowed.
* @return the property value.
*/
public static boolean getBoolean(String key, boolean def) {
String value = get(key);
if (value == null) {
return def;
}
value = value.trim().toLowerCase();
if (value.isEmpty()) {
return def;
}
if ("true".equals(value) || "yes".equals(value) || "1".equals(value)) {
return true;
}
if ("false".equals(value) || "no".equals(value) || "0".equals(value)) {
return false;
}
LOGGER.warn(
"Unable to parse the boolean system property '{}':{} - using the default value: {}",
key, value, def
);
return def;
}
/**
* Returns the value of the Java system property with the specified
*
* @param key while falling back to the specified default value if the property access fails.
* @param def if there's no such property or if an access to the specified property is not allowed.
* @return the property value.
*/
public static float getFloat(String key, float def) {
String value = get(key);
if (value == null) {
return def;
}
value = value.trim();
try {
return Float.parseFloat(value);
} catch (Exception e) {
// Ignore
}
LOGGER.warn(
"Unable to parse the float system property '{}':{} - using the default value: {}",
key, value, def
);
return def;
}
/**
* Returns the value of the Java system property with the specified
*
* @param key while falling back to the specified default value if the property access fails.
* @param def if there's no such property or if an access to the specified property is not allowed.
* @return the property value.
*/
public static int getInt(String key, int def) {
String value = get(key);
if (value == null) {
return def;
}
value = value.trim();
try {
return Integer.parseInt(value);
} catch (Exception e) {
// Ignore
}
LOGGER.warn(
"Unable to parse the integer system property '{}':{} - using the default value: {}",
key, value, def
);
return def;
}
/**
* Returns the value of the Java system property with the specified
*
* @param key while falling back to the specified default value if the property access fails.
* @param def if there's no such property or if an access to the specified property is not allowed.
* @return the property value.
*/
public static long getLong(String key, long def) {
String value = get(key);
if (value == null) {
return def;
}
value = value.trim();
try {
return Long.parseLong(value);
} catch (Exception e) {
// Ignore
}
LOGGER.warn(
"Unable to parse the long integer system property '{}':{} - using the default value: {}",
key, value, def
);
return def;
}
}