From 1d7067719b5bc0318b58bdfed885057aface9d83 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 30 May 2012 16:38:23 -0700 Subject: [PATCH] Allow to disable the use of sun.misc.Unsafe (#272) - Contributed by @normanmaurer - Added io.netty.noUnsafe property --- .../io/netty/util/internal/DetectionUtil.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/common/src/main/java/io/netty/util/internal/DetectionUtil.java b/common/src/main/java/io/netty/util/internal/DetectionUtil.java index fb23d0882e..448701f948 100644 --- a/common/src/main/java/io/netty/util/internal/DetectionUtil.java +++ b/common/src/main/java/io/netty/util/internal/DetectionUtil.java @@ -26,6 +26,9 @@ import java.util.zip.Deflater; * Utility that detects various properties specific to the current runtime * environment, such as Java version and the availability of the * {@code sun.misc.Unsafe} object. + *

+ * You can disable the use of {@code sun.misc.Unsafe} if you specify + * the system property io.netty.noUnsafe. */ public final class DetectionUtil { @@ -41,6 +44,20 @@ public final class DetectionUtil { } 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 { Class unsafeClazz = Class.forName("sun.misc.Unsafe", true, loader); return hasUnsafeField(unsafeClazz);