Skip to content

Commit

Permalink
Allow SDK to run in environments prohibiting use of sun.misc.Unsafe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rrva committed Oct 31, 2022
1 parent f56b909 commit 5c283d9
Showing 1 changed file with 19 additions and 0 deletions.
Expand Up @@ -28,9 +28,28 @@ public static <T> Queue<T> 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.
Expand Down

0 comments on commit 5c283d9

Please sign in to comment.