2828
2929/**
3030 * This utility wrapper class is created to support AXIS2 runs
31- * inside of Java security environments . Due to the access control
32- * checking algorithm, for Java security to function properly,
31+ * inside of Java 2 Security environment . Due to the access control
32+ * checking algorithm, for Java 2 Security to function properly,
3333 * <code>doPrivileged()</code>
3434 * is required in cases where there is application code on the stack frame
35- * accessing system resources (ie, read/write files, opening ports, and etc).
36- * <p/>
37- * This class provides a consistent security model across Java versions by
38- * always using doPrivileged(), ensuring proper privilege elevation regardless
39- * of SecurityManager presence (which was deprecated in Java 17 and removed in Java 21).
35+ * accessing the system resources (ie, read/write files, opening ports, and etc).
36+ * This class also improve performance no matther Security Manager is being enabled
37+ * or not.
4038 * <p/>
4139 * Note: This utility should be used properly, otherwise might introduce
4240 * security holes.
5957
6058
6159public class AccessController {
60+ private static final boolean SUPPORTS_SECURITY_MANAGER = Runtime .version ().feature () < 24 ;
6261
6362 /**
6463 * Performs the specified <code>PrivilegedAction</code> with privileges
65- * enabled. This method always uses doPrivileged for security consistency
66- * across Java versions.
64+ * enabled if a security manager is present.
6765 * <p/>
6866 * If the action's <code>run</code> method throws an (unchecked) exception,
6967 * it will propagate through this method.
@@ -74,7 +72,11 @@ public class AccessController {
7472 * @see #doPrivileged(PrivilegedExceptionAction)
7573 */
7674 public static <T > T doPrivileged (PrivilegedAction <T > action ) {
77- return java .security .AccessController .doPrivileged (action );
75+ if (!SUPPORTS_SECURITY_MANAGER ) {
76+ return (action .run ());
77+ } else {
78+ return java .security .AccessController .doPrivileged (action );
79+ }
7880 }
7981
8082
@@ -83,7 +85,9 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
8385 * enabled and restricted by the specified <code>AccessControlContext</code>.
8486 * The action is performed with the intersection of the permissions
8587 * possessed by the caller's protection domain, and those possessed
86- * by the domains represented by the specified <code>AccessControlContext</code>.
88+ * by the domains represented by the specified
89+ * <code>AccessControlContext</code> if a security manager is present.
90+ * <p/>
8791 * <p/>
8892 * If the action's <code>run</code> method throws an (unchecked) exception,
8993 * it will propagate through this method.
@@ -97,35 +101,49 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
97101 * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
98102 */
99103 public static <T > T doPrivileged (PrivilegedAction <T > action , AccessControlContext context ) {
100- return java .security .AccessController .doPrivileged (action , context );
104+ if (!SUPPORTS_SECURITY_MANAGER ) {
105+ return action .run ();
106+ } else {
107+ return java .security .AccessController .doPrivileged (action , context );
108+ }
101109 }
102110
103111 /**
104112 * Performs the specified <code>PrivilegedExceptionAction</code> with
105- * privileges enabled. The action is performed with <i>all</i> of the
113+ * privileges enabled. The action is performed with <i>all</i> of the
106114 * permissions possessed by the caller's protection domain.
107115 * <p/>
108116 * If the action's <code>run</code> method throws an <i>unchecked</i>
109117 * exception, it will propagate through this method.
110118 *
111119 * @param action the action to be performed.
112120 * @return the value returned by the action's <code>run</code> method.
113- * @throws PrivilegedActionException the specified action's
121+ * @throws PrivilgedActionException the specified action's
114122 * <code>run</code> method threw a <i>checked</i> exception.
115123 * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
116124 * @see #doPrivileged(PrivilegedAction)
117125 */
118126 public static <T > T doPrivileged (PrivilegedExceptionAction <T > action )
119127 throws PrivilegedActionException {
120- return java .security .AccessController .doPrivileged (action );
128+ if (!SUPPORTS_SECURITY_MANAGER ) {
129+ try {
130+ return action .run ();
131+ } catch (java .lang .RuntimeException e ) {
132+ throw e ;
133+ } catch (Exception e ) {
134+ throw new PrivilegedActionException (e );
135+ }
136+ } else {
137+ return java .security .AccessController .doPrivileged (action );
138+ }
121139 }
122140
123141
124142 /**
125143 * Performs the specified <code>PrivilegedExceptionAction</code> with
126144 * privileges enabled and restricted by the specified
127- * <code>AccessControlContext</code>. The action is performed with the
128- * intersection of the permissions possessed by the caller's
145+ * <code>AccessControlContext</code>. The action is performed with the
146+ * intersection of the the permissions possessed by the caller's
129147 * protection domain, and those possessed by the domains represented by the
130148 * specified <code>AccessControlContext</code>.
131149 * <p/>
@@ -146,7 +164,18 @@ public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
146164 public static <T > T doPrivileged (PrivilegedExceptionAction <T > action ,
147165 AccessControlContext context )
148166 throws PrivilegedActionException {
149- return java .security .AccessController .doPrivileged (action , context );
167+
168+ if (!SUPPORTS_SECURITY_MANAGER ) {
169+ try {
170+ return action .run ();
171+ } catch (java .lang .RuntimeException e ) {
172+ throw e ;
173+ } catch (Exception e ) {
174+ throw new PrivilegedActionException (e );
175+ }
176+ } else {
177+ return java .security .AccessController .doPrivileged (action , context );
178+ }
150179 }
151180
152181 /**
@@ -174,7 +203,9 @@ public static AccessControlContext getContext() {
174203 * is not permitted, based on the current security policy.
175204 */
176205 public static void checkPermission (Permission perm ) throws AccessControlException {
177- java .security .AccessController .checkPermission (perm );
206+ if (SUPPORTS_SECURITY_MANAGER ) {
207+ java .security .AccessController .checkPermission (perm );
208+ }
178209 }
179210
180211 /**
0 commit comments