roryqi commented on code in PR #10052:
URL: https://github.com/apache/gravitino/pull/10052#discussion_r3009198249
##########
clients/client-java/src/main/java/org/apache/gravitino/client/KerberosTokenProvider.java:
##########
@@ -349,5 +350,39 @@ private void extractPrincipalFromSubject(Subject subject) {
.map(Object::toString)
.orElse(null);
}
+
+ /**
+ * Returns the current {@link Subject} in a JDK-version-neutral way.
+ *
+ * <ul>
+ * <li>JDK 18+: uses {@code Subject.current()} via reflection, which is
the designated
+ * replacement for the removed {@code
Subject.getSubject(AccessControlContext)}
+ * <li>JDK 17 and below: uses the deprecated {@code
Subject.getSubject(AccessControlContext)}
+ * </ul>
+ *
+ * @return the current Subject, or {@code null} if none is established
+ */
+ @SuppressWarnings("removal")
+ private static Subject getCurrentSubject() {
+ // Use reflection to call Subject.current() (JDK 18+) because this
project compiles on
+ // JDK 17, where Subject.current() does not exist as a compile-time API.
+ // Fall back to the deprecated Subject.getSubject(AccessControlContext)
on JDK 17 and below.
+ try {
+ java.lang.reflect.Method currentMethod =
Subject.class.getMethod("current");
+ return (Subject) currentMethod.invoke(null);
+ } catch (NoSuchMethodException e) {
+ // JDK 17 and below: Subject.current() does not exist, use the legacy
API.
+ // AccessController.getContext() may also throw
UnsupportedOperationException on JDK 17
+ // when the Security Manager is explicitly disabled; treat that as no
existing subject.
+ try {
+ java.security.AccessControlContext context =
java.security.AccessController.getContext();
+ return Subject.getSubject(context);
+ } catch (UnsupportedOperationException ignored) {
+ return null;
Review Comment:
It's not a good choice when returning null value.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]