Skip to content

Commit

Permalink
Removed AccessController checks (AccessController deprecated in Java 21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ok2c committed Dec 14, 2023
1 parent afc2325 commit ba929e0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -85,7 +82,6 @@
import org.apache.hc.core5.pool.PoolEntry;
import org.apache.hc.core5.pool.PoolStats;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.Asserts;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

Expand Down Expand Up @@ -265,19 +261,7 @@ private Socket createSocket(final HttpHost targetHost) throws IOException {
}

final InetSocketAddress targetAddress = addressResolver.resolve(targetHost);
// Run this under a doPrivileged to support lib users that run under a SecurityManager this allows granting connect permissions
// only to this library
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisecondsIntBound());
return null;
});
} catch (final PrivilegedActionException e) {
Asserts.check(e.getCause() instanceof IOException,
"method contract violation only checked exceptions are wrapped: " + e.getCause());
// only checked exceptions are wrapped - error and RTExceptions are rethrown by doPrivileged
throw (IOException) e.getCause();
}
sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisecondsIntBound());
if (URIScheme.HTTPS.same(targetHost.getSchemeName())) {
final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
sock, targetHost.getHostName(), targetAddress.getPort(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand All @@ -52,7 +49,6 @@
import org.apache.hc.core5.io.Closer;
import org.apache.hc.core5.net.NamedEndpoint;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.Asserts;
import org.apache.hc.core5.util.Timeout;

class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements ConnectionInitiator {
Expand Down Expand Up @@ -330,17 +326,7 @@ private void processConnectionRequest(final SocketChannel socketChannel, final I
// Run this under a doPrivileged to support lib users that run under a SecurityManager this allows granting connect permissions
// only to this library
validateAddress(remoteAddress);
final boolean connected;
try {
connected = AccessController.doPrivileged(
(PrivilegedExceptionAction<Boolean>) () -> socketChannel.connect(remoteAddress));
} catch (final PrivilegedActionException e) {
Asserts.check(e.getCause() instanceof IOException,
"method contract violation only checked exceptions are wrapped: " + e.getCause());
// only checked exceptions are wrapped - error and RTExceptions are rethrown by doPrivileged
throw (IOException) e.getCause();
}

final boolean connected = socketChannel.connect(remoteAddress);
final SelectionKey key = socketChannel.register(this.selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
final IOSession ioSession = new IOSessionImpl("c", key, socketChannel);
final InternalDataChannel dataChannel = new InternalDataChannel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
package org.apache.hc.core5.util;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.apache.hc.core5.annotation.Internal;

Expand All @@ -40,7 +38,7 @@ public static void callSetter(final Object object, final String setterName, fina
try {
final Class<?> clazz = object.getClass();
final Method method = clazz.getMethod("set" + setterName, type);
setAccessible(method);
method.setAccessible(true);
method.invoke(object, value);
} catch (final Exception ignore) {
}
Expand All @@ -50,20 +48,13 @@ public static <T> T callGetter(final Object object, final String getterName, fin
try {
final Class<?> clazz = object.getClass();
final Method method = clazz.getMethod("get" + getterName);
setAccessible(method);
method.setAccessible(true);
return resultType.cast(method.invoke(object));
} catch (final Exception ignore) {
return null;
}
}

private static void setAccessible(final Method method) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
method.setAccessible(true);
return null;
});
}

public static int determineJRELevel() {
final String s = System.getProperty("java.version");
final String[] parts = s.split("\\.");
Expand Down

0 comments on commit ba929e0

Please sign in to comment.