Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for the removal of the securitymanager in moxy. #1320

Merged
merged 4 commits into from
Sep 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -94,6 +97,102 @@ public class PrivilegedAccessHelper {
primitiveClasses.put("short", short.class);
}

/**
* INTERNAL
* A task that returns a result and shall not throw an exception.
* Implementors define a single {@code call()} method with no arguments.
*
* @param <T> the result type of method call
*/
@FunctionalInterface
public interface PrivilegedCallable<T> {
T call();
}

/**
* INTERNAL
* A task that returns a result and may throw an exception.
* Implementors define a single {@code call()} method with no arguments.
*
* @param <T> the result type of method call
*/
@FunctionalInterface
public interface PrivilegedExceptionCallable<T> {
T call() throws Exception;
}

/**
* INTERNAL
* Specific {@code Exception} supplier for {@link PrivilegedExceptionCallable}.
*
* @param <E> specific {@link Exception} type
*/
@FunctionalInterface
public interface CallableExceptionSupplier<E extends Exception> {
E get(Exception e);
}

/**
* INTERNAL
* Executes provided {@link PrivilegedCallable} task using {@link AccessController#doPrivileged(PrivilegedAction)}
* when privileged access is enabled.
*
* @param <T> {@link PrivilegedCallable} return type
* @param task task to execute
*/
@SuppressWarnings("removal")
public static <T> T callDoPrivileged(PrivilegedCallable<T> task) {
if (shouldUsePrivilegedAccess()) {
return AccessController.doPrivileged((PrivilegedAction<T>) task::call);
} else {
return task.call();
}
}

/**
* INTERNAL
* Executes provided {@link PrivilegedExceptionCallable} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
* when privileged access is enabled.
*
* @param <T> {@link PrivilegedExceptionCallable} return type
* @param task task to execute
*/
@SuppressWarnings("removal")
public static <T> T callDoPrivilegedWithException(PrivilegedExceptionCallable<T> task) throws Exception {
if (shouldUsePrivilegedAccess()) {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) task::call);
// AccessController.doPrivileged wraps Exception instances with PrivilegedActionException. Let's unwrap them
// to provide the same exception output as original callable without AccessController.doPrivileged
} catch (PrivilegedActionException pae) {
throw pae.getException();
}
} else {
return task.call();
}
}

/**
* INTERNAL
* Executes provided {@link PrivilegedExceptionCallable} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
* when privileged access is enabled.
* If {@link Exception} is thrown from task, it will be processed by provided {@link CallableExceptionSupplier}.
*
* @param <T> {@link PrivilegedExceptionCallable} return type
* @param <E> specific {@link Exception} type
* @param task task to execute
* @param exception specific {@link Exception} supplier
*/
@SuppressWarnings("removal")
public static <T,E extends Exception> T callDoPrivilegedWithException(
PrivilegedExceptionCallable<T> task, CallableExceptionSupplier<E> exception) throws E {
try {
return callDoPrivilegedWithException(task);
} catch (Exception e) {
throw exception.get(e);
}
}

/**
* INTERNAL
* It will be used to set default value of property "eclipselink.security.usedoprivileged"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Map;
import java.util.Vector;

import jakarta.xml.bind.annotation.adapters.XmlAdapter;

import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.descriptors.InheritancePolicy;
import org.eclipse.persistence.internal.descriptors.InstantiationPolicy;
Expand All @@ -31,7 +33,6 @@
import org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest;
import org.eclipse.persistence.internal.jpa.rs.weaving.RestAdapterClassWriter;
import org.eclipse.persistence.internal.queries.CollectionContainerPolicy;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.jaxb.DefaultXMLNameTransformer;
import org.eclipse.persistence.jpa.rs.exceptions.JPARSException;
import org.eclipse.persistence.jpa.rs.util.xmladapters.RelationshipLinkAdapter;
Expand Down Expand Up @@ -328,7 +329,8 @@ private static void convertMappingToXMLChoiceMapping(ClassDescriptor jaxbDescrip
xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), Link.class);
xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), refDesc.getJavaClass());

xmlChoiceMapping.setConverter(new XMLJavaTypeConverter(Class.forName(adapterClassName, true, cl)));
xmlChoiceMapping.setConverter(new XMLJavaTypeConverter(
(Class<? extends XmlAdapter<?,?>>) Class.forName(adapterClassName, true, cl)));
jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName());
jaxbDescriptor.addMapping(xmlChoiceMapping);

Expand All @@ -343,7 +345,8 @@ private static void convertMappingToXMLChoiceMapping(ClassDescriptor jaxbDescrip
xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), refDesc.getJavaClass());

xmlChoiceMapping.setContainerPolicy(jaxbMapping.getContainerPolicy());
xmlChoiceMapping.setConverter(new XMLJavaTypeConverter(Class.forName(adapterClassName, true, cl)));
xmlChoiceMapping.setConverter(new XMLJavaTypeConverter(
(Class<? extends XmlAdapter<?,?>>) Class.forName(adapterClassName, true, cl)));
jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName());
jaxbDescriptor.addMapping(xmlChoiceMapping);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
package org.eclipse.persistence.internal.jaxb;

import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedActionException;

import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.descriptors.InstantiationPolicy;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.internal.security.PrivilegedClassForName;
import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;

/**
* Purpose:
Expand Down Expand Up @@ -60,20 +56,12 @@ public void convertClassNamesToClasses(ClassLoader loader) {
if(parameterTypeNames != null) {
Class<?>[] values = new Class<?>[parameterTypeNames.length];
for(int i = 0; i < values.length; i++) {
try{
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
try {
values[i] = AccessController.doPrivileged(new PrivilegedClassForName<>(parameterTypeNames[i], true, loader));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(parameterTypeNames[i], exception.getException());
}
} else {
values[i] = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(parameterTypeNames[i], true, loader);
}
} catch (ClassNotFoundException exc){
throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exc);
}
}
final String parameterTypeName = parameterTypeNames[i];
values[i] = PrivilegedAccessHelper.callDoPrivilegedWithException(
() -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(parameterTypeName, true, loader),
(ex) -> ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, ex)
);
}
this.parameterTypes = values;
}
}
Expand Down Expand Up @@ -102,30 +90,20 @@ protected void initializeMethod() throws DescriptorException {
*/
@Override
protected Object buildNewInstanceUsingFactory() throws DescriptorException {
try {
// If the method is static, the first argument is ignored and can be null
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
try {
return AccessController.doPrivileged(new PrivilegedMethodInvoker(getMethod(), getFactory(), this.defaultValues));
} catch (PrivilegedActionException exception) {
Exception throwableException = exception.getException();
if (throwableException instanceof IllegalAccessException) {
throw DescriptorException.illegalAccessWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), throwableException);
return PrivilegedAccessHelper.callDoPrivilegedWithException(
() -> PrivilegedAccessHelper.invokeMethod(getMethod(), getFactory(), this.defaultValues),
(ex) -> {
if (ex instanceof IllegalAccessException) {
return DescriptorException.illegalAccessWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), ex);
} else if (ex instanceof InvocationTargetException) {
return DescriptorException.targetInvocationWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), ex);
} else if (ex instanceof NullPointerException) {
return DescriptorException.nullPointerWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), ex);
} else {
throw DescriptorException.targetInvocationWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), throwableException);
return new RuntimeException("Unexpected exception from MultiArgInstantiationPolicy.buildNewInstanceUsingFactory()", ex);
}
}
} else {
return PrivilegedAccessHelper.invokeMethod(getMethod(), getFactory(), this.defaultValues);
}
} catch (IllegalAccessException exception) {
throw DescriptorException.illegalAccessWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), exception);
} catch (InvocationTargetException exception) {
throw DescriptorException.targetInvocationWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), exception);
} catch (NullPointerException exception) {
// Some JVMs will throw a NULL pointer exception here
throw DescriptorException.nullPointerWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception);
}
);
}

}
Loading