Allow to disable the use of sun.misc.Unsafe (#272)

- Contributed by @normanmaurer
- Added io.netty.noUnsafe property
This commit is contained in:
Trustin Lee 2012-05-30 16:38:23 -07:00
parent 42abb6df3a
commit 1d7067719b

View File

@ -26,6 +26,9 @@ import java.util.zip.Deflater;
* Utility that detects various properties specific to the current runtime * Utility that detects various properties specific to the current runtime
* environment, such as Java version and the availability of the * environment, such as Java version and the availability of the
* {@code sun.misc.Unsafe} object. * {@code sun.misc.Unsafe} object.
* <p>
* You can disable the use of {@code sun.misc.Unsafe} if you specify
* the system property <strong>io.netty.noUnsafe</strong>.
*/ */
public final class DetectionUtil { public final class DetectionUtil {
@ -41,6 +44,20 @@ public final class DetectionUtil {
} }
private static boolean hasUnsafe(ClassLoader loader) { private static boolean hasUnsafe(ClassLoader loader) {
String value = SystemPropertyUtil.get("io.netty.noUnsafe");
if (value != null) {
return false;
}
// Legacy properties
value = SystemPropertyUtil.get("io.netty.tryUnsafe");
if (value == null) {
value = SystemPropertyUtil.get("org.jboss.netty.tryUnsafe", "true");
}
if ("true".equalsIgnoreCase(value)) {
return false;
}
try { try {
Class<?> unsafeClazz = Class.forName("sun.misc.Unsafe", true, loader); Class<?> unsafeClazz = Class.forName("sun.misc.Unsafe", true, loader);
return hasUnsafeField(unsafeClazz); return hasUnsafeField(unsafeClazz);