Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions jaxrs-api/src/main/java/jakarta/ws/rs/client/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.net.URL;
import java.security.AccessController;
import java.security.KeyStore;
import java.security.PrivilegedAction;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -61,13 +59,7 @@ public static ClientBuilder newBuilder() {
try {
Object delegate = FactoryFinder.find(JAXRS_DEFAULT_CLIENT_BUILDER_PROPERTY, ClientBuilder.class);
if (!(delegate instanceof ClientBuilder)) {
final CreateErrorMessageAction action = new CreateErrorMessageAction(delegate);
final String errorMessage;
if (System.getSecurityManager() == null) {
errorMessage = action.run();
} else {
errorMessage = AccessController.doPrivileged(action);
}
final String errorMessage = getCreateErrorMessage(delegate);
throw new LinkageError(errorMessage);
}
return (ClientBuilder) delegate;
Expand Down Expand Up @@ -272,25 +264,16 @@ public ClientBuilder keyStore(final KeyStore keyStore, final String password) {
*/
public abstract Client build();

private static final class CreateErrorMessageAction implements PrivilegedAction<String> {
private final Object delegate;

private CreateErrorMessageAction(final Object delegate) {
this.delegate = delegate;
}

@Override
public String run() {
Class<?> pClass = ClientBuilder.class;
String classnameAsResource = pClass.getName().replace('.', '/') + ".class";
ClassLoader loader = pClass.getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
URL targetTypeURL = loader.getResource(classnameAsResource);
return "ClassCastException: attempting to cast"
+ delegate.getClass().getClassLoader().getResource(classnameAsResource)
+ " to " + targetTypeURL;
private static String getCreateErrorMessage(Object delegate) {
Class<?> pClass = ClientBuilder.class;
String classnameAsResource = pClass.getName().replace('.', '/') + ".class";
ClassLoader loader = pClass.getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
URL targetTypeURL = loader.getResource(classnameAsResource);
return "ClassCastException: attempting to cast"
+ delegate.getClass().getClassLoader().getResource(classnameAsResource)
+ " to " + targetTypeURL;
}
}
49 changes: 7 additions & 42 deletions jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
Expand All @@ -43,26 +41,6 @@ private FactoryFinder() {
// prevents instantiation
}

static ClassLoader getContextClassLoader() {
// For performance reasons, check if a security manager is installed. If not there is no need to use a
// privileged action.
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
}
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
LOGGER.log(
Level.WARNING,
"Unable to get context classloader instance.",
ex);
}
return cl;
});
}

/**
* Creates an instance of the specified class using the specified {@code ClassLoader} object.
*
Expand Down Expand Up @@ -109,7 +87,7 @@ private static Object newInstance(final String className, final ClassLoader clas
* @throws ClassNotFoundException if the given class could not be found or could not be instantiated.
*/
static <T> Object find(final String factoryId, final Class<T> service) throws ClassNotFoundException {
ClassLoader classLoader = getContextClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

// First try the TCCL
Object result = findFirstService(factoryId, classLoader, service);
Expand All @@ -118,7 +96,7 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
}

// Next try the class loader from the FactoryFinder
result = findFirstService(factoryId, getClassLoader(), service);
result = findFirstService(factoryId, FactoryFinder.class.getClassLoader(), service);
if (result != null) {
return result;
}
Expand Down Expand Up @@ -164,25 +142,12 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
"Provider for " + factoryId + " cannot be found", null);
}

private static ClassLoader getClassLoader() {
if (System.getSecurityManager() == null) {
return FactoryFinder.class.getClassLoader();
}
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) FactoryFinder.class::getClassLoader);
}

private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return null;
};
if (System.getSecurityManager() == null) {
return action.run();
try {
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return AccessController.doPrivileged(action);
return null;
}
}
49 changes: 7 additions & 42 deletions jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
Expand All @@ -43,26 +41,6 @@ private FactoryFinder() {
// prevents instantiation
}

private static ClassLoader getContextClassLoader() {
// For performance reasons, check if a security manager is installed. If not there is no need to use a
// privileged action.
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
}
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
LOGGER.log(
Level.WARNING,
"Unable to get context classloader instance.",
ex);
}
return cl;
});
}

/**
* Creates an instance of the specified class using the specified {@code ClassLoader} object.
*
Expand Down Expand Up @@ -109,7 +87,7 @@ private static Object newInstance(final String className, final ClassLoader clas
* @throws ClassNotFoundException if the given class could not be found or could not be instantiated.
*/
static <T> Object find(final String factoryId, final Class<T> service) throws ClassNotFoundException {
ClassLoader classLoader = getContextClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

// First try the TCCL
Object result = findFirstService(factoryId, classLoader, service);
Expand All @@ -118,7 +96,7 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
}

// Next try the class loader from the FactoryFinder
result = findFirstService(factoryId, getClassLoader(), service);
result = findFirstService(factoryId, FactoryFinder.class.getClassLoader(), service);
if (result != null) {
return result;
}
Expand Down Expand Up @@ -164,25 +142,12 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
"Provider for " + factoryId + " cannot be found", null);
}

private static ClassLoader getClassLoader() {
if (System.getSecurityManager() == null) {
return FactoryFinder.class.getClassLoader();
}
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) FactoryFinder.class::getClassLoader);
}

private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return null;
};
if (System.getSecurityManager() == null) {
return action.run();
try {
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return AccessController.doPrivileged(action);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package jakarta.ws.rs.ext;

import java.lang.reflect.ReflectPermission;
import java.net.URL;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -48,7 +47,6 @@ public abstract class RuntimeDelegate {
*/
public static final String JAXRS_RUNTIME_DELEGATE_PROPERTY = "jakarta.ws.rs.ext.RuntimeDelegate";
private static final Lock RD_LOCK = new ReentrantLock();
private static ReflectPermission suppressAccessChecksPermission = new ReflectPermission("suppressAccessChecks");
private static volatile RuntimeDelegate cachedDelegate;

/**
Expand Down Expand Up @@ -129,14 +127,8 @@ private static RuntimeDelegate findDelegate() {
* {@link #getInstance} then an implementation will be sought as described in {@link #getInstance}.
*
* @param rd the runtime delegate instance
* @throws SecurityException if there is a security manager and the permission ReflectPermission("suppressAccessChecks")
* has not been granted.
*/
public static void setInstance(final RuntimeDelegate rd) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(suppressAccessChecksPermission);
}
RD_LOCK.lock();
try {
RuntimeDelegate.cachedDelegate = rd;
Expand Down
49 changes: 7 additions & 42 deletions jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
Expand All @@ -43,26 +41,6 @@ private FactoryFinder() {
// prevents instantiation
}

private static ClassLoader getContextClassLoader() {
// For performance reasons, check if a security manager is installed. If not there is no need to use a
// privileged action.
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
}
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
LOGGER.log(
Level.WARNING,
"Unable to get context classloader instance.",
ex);
}
return cl;
});
}

/**
* Creates an instance of the specified class using the specified {@code ClassLoader} object.
*
Expand Down Expand Up @@ -109,7 +87,7 @@ private static Object newInstance(final String className, final ClassLoader clas
* @throws ClassNotFoundException if the given class could not be found or could not be instantiated.
*/
static <T> Object find(final String factoryId, final Class<T> service) throws ClassNotFoundException {
ClassLoader classLoader = getContextClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

// First try the TCCL
Object result = findFirstService(factoryId, classLoader, service);
Expand All @@ -118,7 +96,7 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
}

// Next try the class loader from the FactoryFinder
result = findFirstService(factoryId, getClassLoader(), service);
result = findFirstService(factoryId, FactoryFinder.class.getClassLoader(), service);
if (result != null) {
return result;
}
Expand Down Expand Up @@ -164,25 +142,12 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
"Provider for " + factoryId + " cannot be found", null);
}

private static ClassLoader getClassLoader() {
if (System.getSecurityManager() == null) {
return FactoryFinder.class.getClassLoader();
}
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) FactoryFinder.class::getClassLoader);
}

private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return null;
};
if (System.getSecurityManager() == null) {
return action.run();
try {
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return AccessController.doPrivileged(action);
return null;
}
}