Move system property parse/access operations to SystemPropertyUtil
This commit is contained in:
parent
1369ba55b4
commit
bd3000858d
@ -58,10 +58,10 @@ final class NioProviderMetadata {
|
||||
int constraintLevel = -1;
|
||||
|
||||
// Use the system property if possible.
|
||||
constraintLevel = SystemPropertyUtil.get(CONSTRAINT_LEVEL_PROPERTY, -1);
|
||||
constraintLevel = SystemPropertyUtil.getInt(CONSTRAINT_LEVEL_PROPERTY, -1);
|
||||
if (constraintLevel < 0 || constraintLevel > 2) {
|
||||
// Try the old property.
|
||||
constraintLevel = SystemPropertyUtil.get(OLD_CONSTRAINT_LEVEL_PROPERTY, -1);
|
||||
constraintLevel = SystemPropertyUtil.getInt(OLD_CONSTRAINT_LEVEL_PROPERTY, -1);
|
||||
if (constraintLevel < 0 || constraintLevel > 2) {
|
||||
constraintLevel = -1;
|
||||
} else {
|
||||
|
@ -30,9 +30,9 @@ final class SelectorUtil {
|
||||
|
||||
static final int DEFAULT_IO_THREADS = Runtime.getRuntime().availableProcessors() * 2;
|
||||
static final long DEFAULT_SELECT_TIMEOUT = 10;
|
||||
static final long SELECT_TIMEOUT;
|
||||
static final long SELECT_TIMEOUT_NANOS;
|
||||
static final boolean EPOOL_BUG_WORKAROUND;
|
||||
static final long SELECT_TIMEOUT = SystemPropertyUtil.getLong("org.jboss.netty.selectTimeout", DEFAULT_SELECT_TIMEOUT);
|
||||
static final long SELECT_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(SELECT_TIMEOUT);
|
||||
static final boolean EPOOL_BUG_WORKAROUND = SystemPropertyUtil.getBoolean("org.jboss.netty.epollBugWorkaround", false);
|
||||
|
||||
// Workaround for JDK NIO bug.
|
||||
//
|
||||
@ -51,23 +51,10 @@ final class SelectorUtil {
|
||||
logger.debug("Unable to get/set System Property '" + key + "'", e);
|
||||
}
|
||||
}
|
||||
long selectTimeout;
|
||||
try {
|
||||
selectTimeout = Long.parseLong(SystemPropertyUtil.get("org.jboss.netty.selectTimeout",
|
||||
String.valueOf(DEFAULT_SELECT_TIMEOUT)));
|
||||
} catch (NumberFormatException e) {
|
||||
selectTimeout = DEFAULT_SELECT_TIMEOUT;
|
||||
}
|
||||
SELECT_TIMEOUT = selectTimeout;
|
||||
SELECT_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(SELECT_TIMEOUT);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Using select timeout of " + SELECT_TIMEOUT);
|
||||
}
|
||||
EPOOL_BUG_WORKAROUND = Boolean.valueOf(System.getProperty("org.jboss.netty.epollBugWorkaround", "false"));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Epoll-bug workaround enabled = " + EPOOL_BUG_WORKAROUND);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static int select(Selector selector) throws IOException {
|
||||
|
@ -34,25 +34,14 @@ import org.jboss.netty.util.internal.SystemPropertyUtil;
|
||||
*/
|
||||
public final class DebugUtil {
|
||||
|
||||
private static final boolean DEBUG_ENABLED =
|
||||
SystemPropertyUtil.getBoolean("org.jboss.netty.debug", false);
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if Netty debug mode is enabled.
|
||||
*/
|
||||
public static boolean isDebugEnabled() {
|
||||
String value;
|
||||
try {
|
||||
value = SystemPropertyUtil.get("org.jboss.netty.debug");
|
||||
} catch (Exception e) {
|
||||
value = null;
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
value = value.trim().toUpperCase();
|
||||
return !value.startsWith("N") &&
|
||||
!value.startsWith("F") &&
|
||||
!value.equals("0");
|
||||
return DEBUG_ENABLED;
|
||||
}
|
||||
|
||||
private DebugUtil() {
|
||||
|
@ -38,7 +38,7 @@ public final class DetectionUtil {
|
||||
private static final boolean HAS_UNSAFE = hasUnsafe(AtomicInteger.class.getClassLoader());
|
||||
private static final boolean IS_WINDOWS;
|
||||
static {
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
String os = SystemPropertyUtil.get("os.name").toLowerCase();
|
||||
// windows
|
||||
IS_WINDOWS = os.indexOf("win") >= 0;
|
||||
}
|
||||
@ -60,8 +60,20 @@ public final class DetectionUtil {
|
||||
}
|
||||
|
||||
private static boolean hasUnsafe(ClassLoader loader) {
|
||||
boolean useUnsafe = Boolean.valueOf(SystemPropertyUtil.get("org.jboss.netty.tryUnsafe", "true"));
|
||||
if (!useUnsafe) {
|
||||
boolean noUnsafe = SystemPropertyUtil.getBoolean("io.netty.noUnsafe", false);
|
||||
if (noUnsafe) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Legacy properties
|
||||
boolean tryUnsafe = false;
|
||||
if (SystemPropertyUtil.contains("io.netty.tryUnsafe")) {
|
||||
tryUnsafe = SystemPropertyUtil.getBoolean("io.netty.tryUnsafe", true);
|
||||
} else {
|
||||
tryUnsafe = SystemPropertyUtil.getBoolean("org.jboss.netty.tryUnsafe", true);
|
||||
}
|
||||
|
||||
if (!tryUnsafe) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -71,6 +83,7 @@ public final class DetectionUtil {
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -15,27 +15,56 @@
|
||||
*/
|
||||
package org.jboss.netty.util.internal;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.jboss.netty.logging.InternalLogger;
|
||||
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||
|
||||
/**
|
||||
* Accesses the system property swallowing a {@link SecurityException}.
|
||||
* A collection of utility methods to retrieve and parse the values of the Java system properties.
|
||||
*/
|
||||
public final class SystemPropertyUtil {
|
||||
|
||||
private static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(SystemPropertyUtil.class);
|
||||
|
||||
private static final Properties props;
|
||||
|
||||
// Retrieve all system properties at once so that there's no need to deal with
|
||||
// security exceptions from next time. Otherwise, we might end up with logging every
|
||||
// security exceptions on every system property access or introducing more complexity
|
||||
// just because of less verbose logging.
|
||||
static {
|
||||
Properties newProps = null;
|
||||
try {
|
||||
newProps = System.getProperties();
|
||||
} catch (SecurityException e) {
|
||||
logger.warn("Unable to access the system properties; default values will be used.", e);
|
||||
newProps = new Properties();
|
||||
}
|
||||
|
||||
props = newProps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the system property with the specified {@code key}
|
||||
* exists.
|
||||
*/
|
||||
public static boolean contains(String key) {
|
||||
if (key == null) {
|
||||
throw new NullPointerException("key");
|
||||
}
|
||||
return props.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Java system property with the specified
|
||||
* {@code key}.
|
||||
* {@code key}, while falling back to {@code null} if the property access fails.
|
||||
*
|
||||
* @return the property value.
|
||||
* {@code null} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
* @return the property value or {@code null}
|
||||
*/
|
||||
public static String get(String key) {
|
||||
try {
|
||||
return System.getProperty(key);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return get(key, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,10 +77,15 @@ public final class SystemPropertyUtil {
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static String get(String key, String def) {
|
||||
String value = get(key);
|
||||
if (value == null) {
|
||||
value = def;
|
||||
if (key == null) {
|
||||
throw new NullPointerException("key");
|
||||
}
|
||||
|
||||
String value = props.getProperty(key);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -64,17 +98,104 @@ public final class SystemPropertyUtil {
|
||||
* {@code def} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static int get(String key, int def) {
|
||||
String value = get(key);
|
||||
public static boolean getBoolean(String key, boolean def) {
|
||||
if (key == null) {
|
||||
throw new NullPointerException("key");
|
||||
}
|
||||
|
||||
String value = props.getProperty(key);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
if (Pattern.matches("-?[0-9]+", value)) {
|
||||
return Integer.parseInt(value);
|
||||
} else {
|
||||
value = value.trim().toLowerCase();
|
||||
if (value.length() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value.equals("true") || value.equals("yes") || value.equals("1")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value.equals("false") || value.equals("no") || value.equals("0")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.warn(
|
||||
"Unable to parse the boolean system property '" + key + "':" + value + " - " +
|
||||
"using the default value: " + def);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Java system property with the specified
|
||||
* {@code key}, while falling back to the specified default value if
|
||||
* the property access fails.
|
||||
*
|
||||
* @return the property value.
|
||||
* {@code def} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static int getInt(String key, int def) {
|
||||
if (key == null) {
|
||||
throw new NullPointerException("key");
|
||||
}
|
||||
|
||||
String value = props.getProperty(key);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
value = value.trim().toLowerCase();
|
||||
if (value.matches("-?[0-9]+")) {
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn(
|
||||
"Unable to parse the integer system property '" + key + "':" + value + " - " +
|
||||
"using the default value: " + def);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Java system property with the specified
|
||||
* {@code key}, while falling back to the specified default value if
|
||||
* the property access fails.
|
||||
*
|
||||
* @return the property value.
|
||||
* {@code def} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static long getLong(String key, long def) {
|
||||
if (key == null) {
|
||||
throw new NullPointerException("key");
|
||||
}
|
||||
|
||||
String value = props.getProperty(key);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
value = value.trim().toLowerCase();
|
||||
if (value.matches("-?[0-9]+")) {
|
||||
try {
|
||||
return Long.parseLong(value);
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn(
|
||||
"Unable to parse the long integer system property '" + key + "':" + value + " - " +
|
||||
"using the default value: " + def);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
private SystemPropertyUtil() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user