2929import java .rmi .MarshalledObject ;
3030import java .rmi .UnmarshalException ;
3131import java .rmi .server .Unreferenced ;
32- import java .security .AccessControlContext ;
33- import java .security .AccessController ;
34- import java .security .Permission ;
35- import java .security .Permissions ;
36- import java .security .PrivilegedAction ;
3732import java .security .PrivilegedActionException ;
38- import java .security .PrivilegedExceptionAction ;
39- import java .security .ProtectionDomain ;
4033import java .util .Arrays ;
4134import java .util .Collections ;
4235import java .util .Map ;
4336import java .util .Set ;
37+ import java .util .concurrent .CompletionException ;
4438
4539import javax .management .*;
4640import javax .management .remote .JMXServerErrorException ;
4741import javax .management .remote .NotificationResult ;
4842import javax .security .auth .Subject ;
49- import sun .reflect .misc .ReflectUtil ;
5043
5144import static javax .management .remote .rmi .RMIConnector .Util .cast ;
5245import com .sun .jmx .remote .internal .ServerCommunicatorAdmin ;
@@ -94,7 +87,6 @@ public class RMIConnectionImpl implements RMIConnection, Unreferenced {
9487 * <code>RMIServerImpl</code>. Can be null, equivalent to an
9588 * empty map.
9689 */
97- @ SuppressWarnings ("removal" )
9890 public RMIConnectionImpl (RMIServerImpl rmiServer ,
9991 String connectionId ,
10092 ClassLoader defaultClassLoader ,
@@ -111,54 +103,13 @@ public RMIConnectionImpl(RMIServerImpl rmiServer,
111103 this .mbeanServer = rmiServer .getMBeanServer ();
112104
113105 final ClassLoader dcl = defaultClassLoader ;
114-
115- ClassLoaderRepository repository = AccessController .doPrivileged (
116- new PrivilegedAction <ClassLoaderRepository >() {
117- public ClassLoaderRepository run () {
118- return mbeanServer .getClassLoaderRepository ();
119- }
120- },
121- withPermissions (new MBeanPermission ("*" , "getClassLoaderRepository" ))
122- );
123- this .classLoaderWithRepository = AccessController .doPrivileged (
124- new PrivilegedAction <ClassLoaderWithRepository >() {
125- public ClassLoaderWithRepository run () {
126- return new ClassLoaderWithRepository (
127- repository ,
128- dcl );
129- }
130- },
131- withPermissions (new RuntimePermission ("createClassLoader" ))
132- );
133-
134- this .defaultContextClassLoader =
135- AccessController .doPrivileged (
136- new PrivilegedAction <ClassLoader >() {
137- @ Override
138- public ClassLoader run () {
139- return new CombinedClassLoader (Thread .currentThread ().getContextClassLoader (),
140- dcl );
141- }
142- });
143-
144- serverCommunicatorAdmin = new
145- RMIServerCommunicatorAdmin (EnvHelp .getServerConnectionTimeout (env ));
146-
106+ ClassLoaderRepository repository = mbeanServer .getClassLoaderRepository ();
107+ classLoaderWithRepository = new ClassLoaderWithRepository (repository , dcl );
108+ defaultContextClassLoader = new CombinedClassLoader (Thread .currentThread ().getContextClassLoader (), dcl );
109+ serverCommunicatorAdmin = new RMIServerCommunicatorAdmin (EnvHelp .getServerConnectionTimeout (env ));
147110 this .env = env ;
148111 }
149112
150- @ SuppressWarnings ("removal" )
151- private static AccessControlContext withPermissions (Permission ... perms ){
152- Permissions col = new Permissions ();
153-
154- for (Permission thePerm : perms ) {
155- col .add (thePerm );
156- }
157-
158- final ProtectionDomain pd = new ProtectionDomain (null , col );
159- return new AccessControlContext ( new ProtectionDomain [] { pd });
160- }
161-
162113 private synchronized ServerNotifForwarder getServerNotifFwd () {
163114 // Lazily created when first use. Mainly when
164115 // addNotificationListener is first called.
@@ -397,7 +348,7 @@ public ObjectInstance createMBean(String className,
397348 +", unwrapping params with MBean extended ClassLoader." );
398349
399350 values = nullIsEmpty (unwrap (params ,
400- getClassLoader (loaderName ),
351+ mbeanServer . getClassLoader (loaderName ),
401352 defaultClassLoader ,
402353 Object [].class ,delegationSubject ));
403354
@@ -1249,7 +1200,6 @@ public void removeNotificationListener(ObjectName name,
12491200 }
12501201 }
12511202
1252- @ SuppressWarnings ("removal" )
12531203 public NotificationResult fetchNotifications (long clientSequenceNumber ,
12541204 int maxNotifications ,
12551205 long timeout )
@@ -1274,19 +1224,22 @@ public NotificationResult fetchNotifications(long clientSequenceNumber,
12741224 + "returns null to force the client to stop fetching" );
12751225 return null ;
12761226 }
1277- final long csn = clientSequenceNumber ;
1278- final int mn = maxNotifications ;
1279- final long t = timeout ;
1280- PrivilegedAction <NotificationResult > action =
1281- new PrivilegedAction <NotificationResult >() {
1282- public NotificationResult run () {
1283- return getServerNotifFwd ().fetchNotifs (csn , t , mn );
1284- }
1285- };
1227+
12861228 if (subject == null ) {
1287- return action . run ( );
1229+ return getServerNotifFwd (). fetchNotifs ( clientSequenceNumber , timeout , maxNotifications );
12881230 } else {
1289- return Subject .doAs (subject , action );
1231+ try {
1232+ return Subject .callAs (subject , () -> getServerNotifFwd ().fetchNotifs (clientSequenceNumber , timeout , maxNotifications ));
1233+ } catch (CompletionException ce ) {
1234+ Throwable thr = ce .getCause ();
1235+ if (thr instanceof SecurityException se ) {
1236+ throw se ;
1237+ } else if (thr instanceof IOException ioe ) {
1238+ throw ioe ;
1239+ } else {
1240+ throw new RuntimeException (thr );
1241+ }
1242+ }
12901243 }
12911244 } finally {
12921245 serverCommunicatorAdmin .rspOutgoing ();
@@ -1311,25 +1264,6 @@ public String toString() {
13111264 // private classes
13121265 //------------------------------------------------------------------------
13131266
1314- private class PrivilegedOperation
1315- implements PrivilegedExceptionAction <Object > {
1316-
1317- public PrivilegedOperation (int operation , Object [] params ) {
1318- this .operation = operation ;
1319- this .params = params ;
1320- }
1321-
1322- public Object run () throws Exception {
1323- return doOperation (operation , params );
1324- }
1325-
1326- private int operation ;
1327- private Object [] params ;
1328- }
1329-
1330- //------------------------------------------------------------------------
1331- // private classes
1332- //------------------------------------------------------------------------
13331267 private class RMIServerCommunicatorAdmin extends ServerCommunicatorAdmin {
13341268 public RMIServerCommunicatorAdmin (long timeout ) {
13351269 super (timeout );
@@ -1352,44 +1286,13 @@ protected void doStop() {
13521286 // private methods
13531287 //------------------------------------------------------------------------
13541288
1355- @ SuppressWarnings ("removal" )
1356- private ClassLoader getClassLoader (final ObjectName name )
1357- throws InstanceNotFoundException {
1358- try {
1359- return
1360- AccessController .doPrivileged (
1361- new PrivilegedExceptionAction <ClassLoader >() {
1362- public ClassLoader run () throws InstanceNotFoundException {
1363- return mbeanServer .getClassLoader (name );
1364- }
1365- },
1366- withPermissions (new MBeanPermission ("*" , "getClassLoader" ))
1367- );
1368- } catch (PrivilegedActionException pe ) {
1369- throw (InstanceNotFoundException ) extractException (pe );
1370- }
1371- }
1372-
1373- @ SuppressWarnings ("removal" )
13741289 private ClassLoader getClassLoaderFor (final ObjectName name )
13751290 throws InstanceNotFoundException {
1376- try {
1377- return (ClassLoader )
1378- AccessController .doPrivileged (
1379- new PrivilegedExceptionAction <Object >() {
1380- public Object run () throws InstanceNotFoundException {
1381- return mbeanServer .getClassLoaderFor (name );
1382- }
1383- },
1384- withPermissions (new MBeanPermission ("*" , "getClassLoaderFor" ))
1385- );
1386- } catch (PrivilegedActionException pe ) {
1387- throw (InstanceNotFoundException ) extractException (pe );
1388- }
1291+
1292+ return mbeanServer .getClassLoaderFor (name );
13891293 }
13901294
13911295 /** @throws UnsupportedOperationException {@inheritDoc} */
1392- @ SuppressWarnings ("removal" )
13931296 private Object doPrivilegedOperation (final int operation ,
13941297 final Object [] params ,
13951298 final Subject delegationSubject )
@@ -1402,10 +1305,9 @@ private Object doPrivilegedOperation(final int operation,
14021305 }
14031306 serverCommunicatorAdmin .reqIncoming ();
14041307 try {
1405- PrivilegedOperation op = new PrivilegedOperation (operation , params );
14061308 if (subject == null ) {
14071309 try {
1408- return op . run ( );
1310+ return doOperation ( operation , params );
14091311 } catch (Exception e ) {
14101312 if (e instanceof RuntimeException ) {
14111313 throw (RuntimeException ) e ;
@@ -1414,7 +1316,20 @@ private Object doPrivilegedOperation(final int operation,
14141316 }
14151317 }
14161318 } else {
1417- return Subject .doAs (subject , op );
1319+ try {
1320+ return Subject .callAs (subject , () -> doOperation (operation , params ));
1321+ } catch (CompletionException ce ) {
1322+ Throwable thr = ce .getCause ();
1323+ if (thr instanceof SecurityException se ) {
1324+ throw se ;
1325+ } else if (thr instanceof IOException ioe ) {
1326+ throw ioe ;
1327+ } else if (thr instanceof Exception e1 ) {
1328+ throw new PrivilegedActionException (e1 );
1329+ } else {
1330+ throw new RuntimeException (thr );
1331+ }
1332+ }
14181333 }
14191334 } catch (Error e ) {
14201335 throw new JMXServerErrorException (e .toString (),e );
@@ -1545,24 +1460,15 @@ private Object doOperation(int operation, Object[] params)
15451460 }
15461461 }
15471462
1548- private static class SetCcl implements PrivilegedExceptionAction <ClassLoader > {
1549- private final ClassLoader classLoader ;
1550-
1551- SetCcl (ClassLoader classLoader ) {
1552- this .classLoader = classLoader ;
1553- }
1554-
1555- public ClassLoader run () {
1556- Thread currentThread = Thread .currentThread ();
1557- ClassLoader old = currentThread .getContextClassLoader ();
1558- if (classLoader != old ) {
1559- currentThread .setContextClassLoader (classLoader );
1560- }
1561- return old ;
1463+ private static ClassLoader setCcl (ClassLoader classLoader ) {
1464+ Thread currentThread = Thread .currentThread ();
1465+ ClassLoader old = currentThread .getContextClassLoader ();
1466+ if (classLoader != old ) {
1467+ currentThread .setContextClassLoader (classLoader );
15621468 }
1469+ return old ;
15631470 }
15641471
1565- @ SuppressWarnings ("removal" )
15661472 private <T > T unwrap (final MarshalledObject <?> mo ,
15671473 final ClassLoader cl ,
15681474 final Class <T > wrappedClass ,
@@ -1578,32 +1484,33 @@ private <T> T unwrap(final MarshalledObject<?> mo,
15781484 return null ;
15791485 }
15801486 try {
1581- final ClassLoader old = AccessController . doPrivileged ( new SetCcl ( cl ) );
1487+ ClassLoader old = setCcl ( cl );
15821488 try {
15831489 if (subject != null ) {
1584- return Subject .doAs (subject , (PrivilegedExceptionAction <T >) () -> wrappedClass .cast (mo .get ()));
1490+ try {
1491+ return Subject .callAs (subject , () -> wrappedClass .cast (mo .get ()));
1492+ } catch (CompletionException ce ) {
1493+ Throwable thr = ce .getCause ();
1494+ if (thr instanceof Exception e ) {
1495+ throw e ;
1496+ } else {
1497+ throw new RuntimeException (thr );
1498+ }
1499+ }
15851500 } else {
15861501 return wrappedClass .cast (mo .get ());
15871502 }
15881503 } finally {
1589- AccessController . doPrivileged ( new SetCcl ( old ) );
1504+ setCcl ( old );
15901505 }
1591- } catch (PrivilegedActionException pe ) {
1592- Exception e = extractException (pe );
1506+ } catch (Exception e ) {
15931507 if (e instanceof IOException ) {
15941508 throw (IOException ) e ;
15951509 }
1596- if (e instanceof ClassNotFoundException ) {
1597- throw new UnmarshalException (e .toString (), e );
1598- }
15991510 logger .warning ("unwrap" , "Failed to unmarshall object: " + e );
16001511 logger .debug ("unwrap" , e );
1601- }catch (ClassNotFoundException ex ) {
1602- logger .warning ("unwrap" , "Failed to unmarshall object: " + ex );
1603- logger .debug ("unwrap" , ex );
1604- throw new UnmarshalException (ex .toString (), ex );
1512+ throw new UnmarshalException (e .toString (), e );
16051513 }
1606- return null ;
16071514 }
16081515
16091516 private <T > T unwrap (final MarshalledObject <?> mo ,
@@ -1616,18 +1523,10 @@ private <T> T unwrap(final MarshalledObject<?> mo,
16161523 return null ;
16171524 }
16181525 try {
1619- @ SuppressWarnings ("removal" )
1620- ClassLoader orderCL = AccessController .doPrivileged (
1621- new PrivilegedExceptionAction <ClassLoader >() {
1622- public ClassLoader run () throws Exception {
1623- return new CombinedClassLoader (Thread .currentThread ().getContextClassLoader (),
1624- new OrderClassLoaders (cl1 , cl2 ));
1625- }
1626- }
1627- );
1526+ ClassLoader orderCL = new CombinedClassLoader (Thread .currentThread ().getContextClassLoader (),
1527+ new OrderClassLoaders (cl1 , cl2 ));
16281528 return unwrap (mo , orderCL , wrappedClass ,delegationSubject );
1629- } catch (PrivilegedActionException pe ) {
1630- Exception e = extractException (pe );
1529+ } catch (Exception e ) {
16311530 if (e instanceof IOException ) {
16321531 throw (IOException ) e ;
16331532 }
@@ -1815,7 +1714,6 @@ private CombinedClassLoader(ClassLoader parent, ClassLoader defaultCL) {
18151714 @ Override
18161715 protected Class <?> loadClass (String name , boolean resolve )
18171716 throws ClassNotFoundException {
1818- ReflectUtil .checkPackageAccess (name );
18191717 try {
18201718 super .loadClass (name , resolve );
18211719 } catch (Exception e ) {
0 commit comments