Skip to content
Merged
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
43 changes: 11 additions & 32 deletions jaxrs-api/src/main/java/jakarta/ws/rs/client/ClientBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -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,14 +59,17 @@ 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);
Class<?> pClass = ClientBuilder.class;
String classnameAsResource = pClass.getName().replace('.', '/') + ".class";
ClassLoader loader = pClass.getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
throw new LinkageError(errorMessage);
URL targetTypeURL = loader.getResource(classnameAsResource);
final String errorMessage = "ClassCastException: attempting to cast"
+ delegate.getClass().getClassLoader().getResource(classnameAsResource)
+ " to " + targetTypeURL;
throw new LinkageError(errorMessage);
}
return (ClientBuilder) delegate;
} catch (Exception ex) {
Expand Down Expand Up @@ -271,26 +272,4 @@ public ClientBuilder keyStore(final KeyStore keyStore, final String password) {
* @return a new client instance.
*/
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;
}
}
}
47 changes: 10 additions & 37 deletions jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -19,8 +19,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
Expand All @@ -43,23 +41,7 @@ private FactoryFinder() {
}

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;
});
return Thread.currentThread().getContextClassLoader();
}

/**
Expand Down Expand Up @@ -165,27 +147,18 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
}

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

private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
return null;
};
if (System.getSecurityManager() == null) {
return action.run();
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return AccessController.doPrivileged(action);
return null;
}
}
45 changes: 10 additions & 35 deletions jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -43,23 +43,7 @@ private FactoryFinder() {
}

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;
});
return Thread.currentThread().getContextClassLoader();
}

/**
Expand Down Expand Up @@ -165,27 +149,18 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
}

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

private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
return null;
};
if (System.getSecurityManager() == null) {
return action.run();
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
return AccessController.doPrivileged(action);
return null;
}
}
10 changes: 1 addition & 9 deletions jaxrs-api/src/main/java/jakarta/ws/rs/ext/RuntimeDelegate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,7 +16,6 @@

package jakarta.ws.rs.ext;

import java.lang.reflect.ReflectPermission;
import java.net.URL;
import java.util.concurrent.CompletionStage;

Expand Down Expand Up @@ -46,7 +45,6 @@ public abstract class RuntimeDelegate {
*/
public static final String JAXRS_RUNTIME_DELEGATE_PROPERTY = "jakarta.ws.rs.ext.RuntimeDelegate";
private static final Object RD_LOCK = new Object();
private static ReflectPermission suppressAccessChecksPermission = new ReflectPermission("suppressAccessChecks");
private static volatile RuntimeDelegate cachedDelegate;

/**
Expand Down Expand Up @@ -124,14 +122,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);
}
synchronized (RD_LOCK) {
RuntimeDelegate.cachedDelegate = rd;
}
Expand Down
47 changes: 10 additions & 37 deletions jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -19,8 +19,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
Expand All @@ -43,23 +41,7 @@ private FactoryFinder() {
}

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;
});
return Thread.currentThread().getContextClassLoader();
}

/**
Expand Down Expand Up @@ -165,27 +147,18 @@ static <T> Object find(final String factoryId, final Class<T> service) throws Cl
}

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

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