Use SystemPropertyUtil to access system properties

Motivation:

We should use SystemPropertyUtil to access system properties and so always handle SecurityExceptions.

Modifications:

Use SystemPropertyUtil everywhere.

Result:

Better and consist handling of SecurityException.
This commit is contained in:
Norman Maurer 2017-03-09 12:15:34 +01:00
parent 476d2aea76
commit 9e6e1a3e7b
5 changed files with 9 additions and 32 deletions

View File

@ -17,6 +17,7 @@ package io.netty.util;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SocketUtils; import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
@ -121,12 +122,13 @@ public final class NetUtil {
/** /**
* {@code true} if IPv4 should be used even if the system supports both IPv4 and IPv6. * {@code true} if IPv4 should be used even if the system supports both IPv4 and IPv6.
*/ */
private static final boolean IPV4_PREFERRED = Boolean.getBoolean("java.net.preferIPv4Stack"); private static final boolean IPV4_PREFERRED = SystemPropertyUtil.getBoolean("java.net.preferIPv4Stack", false);
/** /**
* {@code true} if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. * {@code true} if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.
*/ */
private static final boolean IPV6_ADDRESSES_PREFERRED = Boolean.getBoolean("java.net.preferIPv6Addresses"); private static final boolean IPV6_ADDRESSES_PREFERRED =
SystemPropertyUtil.getBoolean("java.net.preferIPv6Addresses", false);
/** /**
* The logger being used by this class * The logger being used by this class

View File

@ -915,18 +915,7 @@ public final class PlatformDependent {
} }
static int majorVersionFromJavaSpecificationVersion() { static int majorVersionFromJavaSpecificationVersion() {
try { return majorVersion(SystemPropertyUtil.get("java.specification.version", "1.6"));
final String javaSpecVersion = AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
return System.getProperty("java.specification.version");
}
});
return majorVersion(javaSpecVersion);
} catch (SecurityException e) {
logger.debug("security exception while reading java.specification.version", e);
return 6;
}
} }
static int majorVersion(final String javaSpecVersion) { static int majorVersion(final String javaSpecVersion) {

View File

@ -27,7 +27,7 @@ import static io.netty.util.internal.ObjectUtil.*;
public final class StringUtil { public final class StringUtil {
public static final String EMPTY_STRING = ""; public static final String EMPTY_STRING = "";
public static final String NEWLINE = System.getProperty("line.separator"); public static final String NEWLINE = SystemPropertyUtil.get("line.separator", "\n");
public static final char DOUBLE_QUOTE = '\"'; public static final char DOUBLE_QUOTE = '\"';
public static final char COMMA = ','; public static final char COMMA = ',';

View File

@ -75,7 +75,7 @@ public final class SystemPropertyUtil {
} }
}); });
} }
} catch (Exception e) { } catch (SecurityException e) {
logger.warn("Unable to retrieve a system property '{}'; default values will be used.", key, e); logger.warn("Unable to retrieve a system property '{}'; default values will be used.", key, e);
} }

View File

@ -26,8 +26,6 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Random; import java.util.Random;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
@ -74,21 +72,9 @@ public final class ThreadLocalRandom extends Random {
private static volatile long seedGeneratorEndTime; private static volatile long seedGeneratorEndTime;
static { static {
initialSeedUniquifier = AccessController.doPrivileged(new PrivilegedAction<Long>() { initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0);
@Override
public Long run() {
return Long.getLong("io.netty.initialSeedUniquifier", 0);
}
});
if (initialSeedUniquifier == 0) { if (initialSeedUniquifier == 0) {
boolean secureRandom = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { boolean secureRandom = SystemPropertyUtil.getBoolean("java.util.secureRandomSeed", false);
@Override
public Boolean run() {
return Boolean.getBoolean("java.util.secureRandomSeed");
}
});
if (secureRandom) { if (secureRandom) {
seedQueue = new LinkedBlockingQueue<Long>(); seedQueue = new LinkedBlockingQueue<Long>();
seedGeneratorStartTime = System.nanoTime(); seedGeneratorStartTime = System.nanoTime();