From 5c283d9dc6aa3163cefbd20590f702b43d475e0c Mon Sep 17 00:00:00 2001 From: Ragnar Rova Date: Mon, 31 Oct 2022 22:34:54 +0100 Subject: [PATCH] Allow SDK to run in environments prohibiting use of sun.misc.Unsafe Some applications run under strict java.security permissions which do not allow access to sun.misc.Unsafe. BatchSpanProcessor uses Unsafe via jctools, but has a fallback to ArrayBlockingQueue. Extending that fallback rule to cover java security exceptions as well. Since the entire java security manager is marked for deprecation in future java versions, I went with string-matching on the root cause message, which removes deprecation warnings when building with never java but still does the job in those versions which use java.security policies. --- .../sdk/trace/internal/JcTools.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sdk/trace-shaded-deps/src/main/java/io/opentelemetry/sdk/trace/internal/JcTools.java b/sdk/trace-shaded-deps/src/main/java/io/opentelemetry/sdk/trace/internal/JcTools.java index a293c437aff..e099432ea3b 100644 --- a/sdk/trace-shaded-deps/src/main/java/io/opentelemetry/sdk/trace/internal/JcTools.java +++ b/sdk/trace-shaded-deps/src/main/java/io/opentelemetry/sdk/trace/internal/JcTools.java @@ -28,9 +28,28 @@ public static Queue newFixedSizeQueue(int capacity) { } catch (java.lang.NoClassDefFoundError e) { // Happens when modules such as jdk.unsupported are disabled in a custom JRE distribution return new ArrayBlockingQueue<>(capacity); + } catch (java.lang.ExceptionInInitializerError e) { + if (isSunMiscAccessProhibited(e)) { + return new ArrayBlockingQueue<>(capacity); + } else { + throw e; + } } } + private static boolean isSunMiscAccessProhibited(Throwable t) { + String rootCause = rootCause(t).getMessage(); + return rootCause != null && rootCause.contains("accessClassInPackage.sun.misc"); + } + + public static Throwable rootCause(Throwable throwable) { + Throwable c = throwable; + while (c.getCause() != null && c.getCause() != c) { + c = c.getCause(); + } + return c; + } + /** * Returns the capacity of the {@link Queue}. We cast to the implementation so callers do not need * to use the shaded classes.