Module org.eclipse.persistence.core
Class PrivilegedAccessHelper
- java.lang.Object
-
- org.eclipse.persistence.internal.security.PrivilegedAccessHelper
-
public class PrivilegedAccessHelper extends Object
INTERNAL: Privileged Access Helper provides a utility so all calls that require privileged access can use the same code. Do privileged blocks can be used with a security manager to grant a code base (eclipselink.jar) access to certain Java operations such as reflection. Generally a security manager is not enabled in a JVM, so this is not an issue. If a security manager is enabled, then either the application can be configured to have access to operations such as reflection, or only EclipseLink can be given access. If only EclipseLink is desired to be given access then do privileged must be enabled through the System property "eclipselink.security.usedoprivileged"=true. Note the usage of do privileged has major impacts on performance, so should normally be avoided.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePrivilegedAccessHelper.CallableExceptionSupplier<E extends Exception>INTERNAL SpecificExceptionsupplier forPrivilegedAccessHelper.PrivilegedExceptionCallable.static interfacePrivilegedAccessHelper.CallableThrowableSupplier<T extends Throwable>INTERNAL SpecificThrowablesupplier forPrivilegedAccessHelper.PrivilegedExceptionCallable.static interfacePrivilegedAccessHelper.PrivilegedCallable<T>INTERNAL A task that returns a result and shall not throw an exception.static interfacePrivilegedAccessHelper.PrivilegedExceptionCallable<T>INTERNAL A task that returns a result and may throw an exception.static interfacePrivilegedAccessHelper.PrivilegedExceptionConsumerINTERNAL A task that does not return any result and may throw an exception.
-
Constructor Summary
Constructors Constructor Description PrivilegedAccessHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TcallDoPrivileged(PrivilegedAccessHelper.PrivilegedCallable<T> task)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedCallabletask usingAccessController.doPrivileged(PrivilegedAction)when privileged access is enabled.static <T> TcallDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.static <T,E extends Exception>
TcallDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task, PrivilegedAccessHelper.CallableExceptionSupplier<E> exception)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.static voidcallDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumertask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.static <E extends Exception>
voidcallDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task, PrivilegedAccessHelper.CallableExceptionSupplier<E> exception)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumertask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.static <T> TcallDoPrivilegedWithThrowable(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.static <T,E extends Throwable>
TcallDoPrivilegedWithThrowable(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task, PrivilegedAccessHelper.CallableThrowableSupplier<E> throwable)INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.static <T> Class<T>getClassForName(String className)Execute a java Class.forName().static <T> Class<T>getClassForName(String className, boolean initialize, ClassLoader loader)Execute a java Class.forName() wrap the call in a doPrivileged block if necessary.static ClassLoadergetClassLoaderForClass(Class<?> clazz)Gets the class loader for a given class.static <T> Constructor<T>getConstructorFor(Class<T> javaClass, Class<?>[] args, boolean shouldSetAccessible)Get the public constructor for the given class and given arguments and wrap it in doPrivileged if necessary.static ClassLoadergetContextClassLoader(Thread thread)Get the context ClassLoader for a thread.static <T> Constructor<T>getDeclaredConstructorFor(Class<T> javaClass, Class<?>[] args, boolean shouldSetAccessible)Get the constructor for the given class and given arguments (regardless of whether it is public or private))and wrap it in doPrivileged if necessary.static FieldgetDeclaredField(Class<?> javaClass, String fieldName, boolean shouldSetAccessible)Get a field actually declared in a class and wrap the call in doPrivileged if necessary.static Field[]getDeclaredFields(Class<?> clazz)Get the list of fields in a class.static MethodgetDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] methodParameterTypes)Return a method on a given class with the given method name and parameter types.static Method[]getDeclaredMethods(Class<?> clazz)Get the list of methods in a class.static FieldgetField(Class<?> javaClass, String fieldName, boolean shouldSetAccessible)Get a field in a class or its superclasses and wrap the call in doPrivileged if necessary.static Field[]getFields(Class<?> clazz)Get the list of public fields in a class.static <T> Class<T>getFieldType(Field field)Get the return type for a given method.static StringgetLineSeparator()INTERNAL: Get the line separator character.static MethodgetMethod(Class<?> javaClass, String methodName, Class<?>[] methodParameterTypes, boolean shouldSetAccessible)Get a method declared in the given class.static Class<?>[]getMethodParameterTypes(Method method)Get the list of parameter types for a given method.static <T> Class<T>getMethodReturnType(Method method)Get the return type for a given method.static Method[]getMethods(Class<?> clazz)Get the list of methods in a class.static MethodgetPublicMethod(Class<?> javaClass, String methodName, Class<?>[] methodParameterTypes, boolean shouldSetAccessible)Get a public method declared in the given class.static StringgetSystemProperty(String key)INTERNAL: Get theSystemproperty indicated by the specifiedkey.static StringgetSystemProperty(String key, String def)INTERNAL: Get theSystemproperty indicated by the specifiedkey.static booleangetSystemPropertyBoolean(String key, boolean def)INTERNAL: Get boolean value of theSystemproperty indicated by the specifiedkey.static <T> TgetValueFromField(Field field, Object object)Get the value of the given field in the given object.static <T> TinvokeConstructor(Constructor<T> constructor, Object[] args)Construct an object with the given Constructor and the given array of arguments.static <T> TinvokeMethod(Method method, Object object)Invoke the givenMethod on a givenObject.static <T> TinvokeMethod(Method method, Object object, Object[] parameters)Invoke the givenMethod on a givenObject using the array of parameters given.static <T> TnewInstanceFromClass(Class<T> clazz)Get a new instance of a class using the default constructor.static voidsetDefaultUseDoPrivilegedValue(boolean def)INTERNAL It will be used to set default value of property "eclipselink.security.usedoprivileged" if not passed as system property.static voidsetValueInField(Field field, Object object, Object value)Set the value of a given field in the given object with the value given.static booleanshouldUsePrivilegedAccess()This method checks to see if calls should be made to doPrivileged.
-
-
-
Method Detail
-
callDoPrivileged
public static <T> T callDoPrivileged(PrivilegedAccessHelper.PrivilegedCallable<T> task)
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedCallabletask usingAccessController.doPrivileged(PrivilegedAction)when privileged access is enabled.- Type Parameters:
T-PrivilegedAccessHelper.PrivilegedCallablereturn type- Parameters:
task- task to execute
-
callDoPrivilegedWithException
public static <T> T callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task) throws Exception
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.- Type Parameters:
T-PrivilegedAccessHelper.PrivilegedExceptionCallablereturn type- Parameters:
task- task to execute- Throws:
Exception
-
callDoPrivilegedWithThrowable
public static <T> T callDoPrivilegedWithThrowable(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task) throws Throwable
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.- Type Parameters:
T-PrivilegedAccessHelper.PrivilegedExceptionCallablereturn type- Parameters:
task- task to execute- Throws:
Throwable
-
callDoPrivilegedWithException
public static void callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task) throws Exception
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumertask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled.- Parameters:
task- task to execute- Throws:
Exception
-
callDoPrivilegedWithException
public static <T,E extends Exception> T callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task, PrivilegedAccessHelper.CallableExceptionSupplier<E> exception) throws E extends Exception
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled. IfExceptionis thrown from task, it will be processed by providedPrivilegedAccessHelper.CallableExceptionSupplier.- Type Parameters:
T-PrivilegedAccessHelper.PrivilegedExceptionCallablereturn typeE- specificExceptiontype- Parameters:
task- task to executeexception- specificExceptionsupplier- Throws:
E extends Exception
-
callDoPrivilegedWithThrowable
public static <T,E extends Throwable> T callDoPrivilegedWithThrowable(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task, PrivilegedAccessHelper.CallableThrowableSupplier<E> throwable) throws E extends Throwable
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallabletask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled. IfThrowableis thrown from task, it will be processed by providedPrivilegedAccessHelper.CallableThrowableSupplier.- Type Parameters:
T-PrivilegedAccessHelper.PrivilegedExceptionCallablereturn typeE- specificThrowabletype- Parameters:
task- task to executethrowable- specificThrowablesupplier- Throws:
E extends Throwable
-
callDoPrivilegedWithException
public static <E extends Exception> void callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task, PrivilegedAccessHelper.CallableExceptionSupplier<E> exception) throws E extends Exception
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumertask usingAccessController.doPrivileged(PrivilegedExceptionAction)when privileged access is enabled. IfExceptionis thrown from task, it will be processed by providedPrivilegedAccessHelper.CallableExceptionSupplier.
-
setDefaultUseDoPrivilegedValue
public static void setDefaultUseDoPrivilegedValue(boolean def)
INTERNAL It will be used to set default value of property "eclipselink.security.usedoprivileged" if not passed as system property. This is used by GlassfishPlatform.
-
getClassForName
public static <T> Class<T> getClassForName(String className) throws ClassNotFoundException
Execute a java Class.forName(). Wrap the call in a doPrivileged block if necessary.- Throws:
ClassNotFoundException
-
getClassForName
public static <T> Class<T> getClassForName(String className, boolean initialize, ClassLoader loader) throws ClassNotFoundException
Execute a java Class.forName() wrap the call in a doPrivileged block if necessary.- Throws:
ClassNotFoundException
-
getClassLoaderForClass
public static ClassLoader getClassLoaderForClass(Class<?> clazz)
Gets the class loader for a given class. Wraps the call in a privileged block if necessary
-
getConstructorFor
public static <T> Constructor<T> getConstructorFor(Class<T> javaClass, Class<?>[] args, boolean shouldSetAccessible) throws NoSuchMethodException
Get the public constructor for the given class and given arguments and wrap it in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstantiationPolicy.- Parameters:
javaClass- The class to get the Constructor forargs- An array of classes representing the argument types of the constructorshouldSetAccessible- whether or not to call the setAccessible API- Throws:
NoSuchMethodException
-
getContextClassLoader
public static ClassLoader getContextClassLoader(Thread thread)
Get the context ClassLoader for a thread. Wrap the call in a doPrivileged block if necessary.
-
getDeclaredConstructorFor
public static <T> Constructor<T> getDeclaredConstructorFor(Class<T> javaClass, Class<?>[] args, boolean shouldSetAccessible) throws NoSuchMethodException
Get the constructor for the given class and given arguments (regardless of whether it is public or private))and wrap it in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstantiationPolicy.- Parameters:
javaClass- The class to get the Constructor forargs- An array of classes representing the argument types of the constructorshouldSetAccessible- whether or not to call the setAccessible API- Throws:
NoSuchMethodException
-
getField
public static Field getField(Class<?> javaClass, String fieldName, boolean shouldSetAccessible) throws NoSuchFieldException
Get a field in a class or its superclasses and wrap the call in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstanceVariableAttributeAccessor.- Parameters:
javaClass- The class to get the field fromfieldName- The name of the fieldshouldSetAccessible- whether or not to call the setAccessible API- Throws:
NoSuchFieldException
-
getDeclaredField
public static Field getDeclaredField(Class<?> javaClass, String fieldName, boolean shouldSetAccessible) throws NoSuchFieldException
Get a field actually declared in a class and wrap the call in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstanceVariableAttributeAccessor.- Parameters:
javaClass- The class to get the field fromfieldName- The name of the fieldshouldSetAccessible- whether or not to call the setAccessible API- Throws:
NoSuchFieldException
-
getDeclaredFields
public static Field[] getDeclaredFields(Class<?> clazz)
Get the list of fields in a class. Wrap the call in doPrivileged if necessary Excludes inherited fields.- Parameters:
clazz- the class to get the fields from.
-
getFields
public static Field[] getFields(Class<?> clazz)
Get the list of public fields in a class. Wrap the call in doPrivileged if necessary- Parameters:
clazz- the class to get the fields from.
-
getDeclaredMethod
public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] methodParameterTypes) throws NoSuchMethodException
Return a method on a given class with the given method name and parameter types. This call will NOT traverse the superclasses. Wrap the call in doPrivileged if necessary.- Parameters:
clazz- the class to get the method frommethodName- the name of the method to getmethodParameterTypes- a list of classes representing the classes of the parameters of the method.- Throws:
NoSuchMethodException
-
getMethod
public static Method getMethod(Class<?> javaClass, String methodName, Class<?>[] methodParameterTypes, boolean shouldSetAccessible) throws NoSuchMethodException
Get a method declared in the given class. Wrap the call in doPrivileged if necessary. This call will traverse the superclasses. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within MethodBasedAttributeAccessor.- Parameters:
javaClass- The class to get the method frommethodName- The name of the method to getmethodParameterTypes- A list of classes representing the classes of the parameters of the mthodshouldSetAccessible- whether or not to call the setAccessible API- Throws:
NoSuchMethodException
-
getPublicMethod
public static Method getPublicMethod(Class<?> javaClass, String methodName, Class<?>[] methodParameterTypes, boolean shouldSetAccessible) throws NoSuchMethodException
Get a public method declared in the given class. Wrap the call in doPrivileged if necessary. This call will traverse the superclasses. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within MethodBasedAttributeAccessor.- Parameters:
javaClass- The class to get the method frommethodName- The name of the method to getmethodParameterTypes- A list of classes representing the classes of the parameters of the methodshouldSetAccessible- whether or not to call the setAccessible API- Throws:
NoSuchMethodException
-
getDeclaredMethods
public static Method[] getDeclaredMethods(Class<?> clazz)
Get the list of methods in a class. Wrap the call in doPrivileged if necessary. Excludes inherited methods.- Parameters:
clazz- the class to get the methods from.
-
getFieldType
public static <T> Class<T> getFieldType(Field field)
Get the return type for a given method. Wrap the call in doPrivileged if necessary.
-
getSystemProperty
public static String getSystemProperty(String key)
INTERNAL: Get theSystemproperty indicated by the specifiedkey.
-
getSystemProperty
public static String getSystemProperty(String key, String def)
INTERNAL: Get theSystemproperty indicated by the specifiedkey.
-
getSystemPropertyBoolean
public static boolean getSystemPropertyBoolean(String key, boolean def)
INTERNAL: Get boolean value of theSystemproperty indicated by the specifiedkey.- Parameters:
key- The name of theSystemproperty.def- The default value.- Returns:
trueif the property value is"true"(case insensitive) or the property is not defined anddefistrue;falseotherwise.- Since:
- 2.6.3
-
getLineSeparator
public static String getLineSeparator()
INTERNAL: Get the line separator character.- Returns:
- The
Stringcontaining the platform-appropriate characters for line separator.
-
getMethodParameterTypes
public static Class<?>[] getMethodParameterTypes(Method method)
Get the list of parameter types for a given method. Wrap the call in doPrivileged if necessary.- Parameters:
method- The method to get the parameter types of
-
getMethodReturnType
public static <T> Class<T> getMethodReturnType(Method method)
Get the return type for a given method. Wrap the call in doPrivileged if necessary.
-
getMethods
public static Method[] getMethods(Class<?> clazz)
Get the list of methods in a class. Wrap the call in doPrivileged if necessary. This call will traverse the superclasses.- Parameters:
clazz- the class to get the methods from.
-
getValueFromField
public static <T> T getValueFromField(Field field, Object object) throws IllegalAccessException
Get the value of the given field in the given object.- Throws:
IllegalAccessException
-
invokeConstructor
public static <T> T invokeConstructor(Constructor<T> constructor, Object[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException
Construct an object with the given Constructor and the given array of arguments. Wrap the call in a doPrivileged block if necessary.
-
invokeMethod
public static <T> T invokeMethod(Method method, Object object) throws IllegalAccessException, InvocationTargetException
Invoke the givenMethod on a givenObject. Assumes method does not take parameters. Wrap in a doPrivileged block if necessary.
-
invokeMethod
public static <T> T invokeMethod(Method method, Object object, Object[] parameters) throws IllegalAccessException, InvocationTargetException
Invoke the givenMethod on a givenObject using the array of parameters given. Wrap in a doPrivileged block if necessary.
-
newInstanceFromClass
public static <T> T newInstanceFromClass(Class<T> clazz) throws IllegalAccessException, InstantiationException
Get a new instance of a class using the default constructor. Wrap the call in a privileged block if necessary.
-
setValueInField
public static void setValueInField(Field field, Object object, Object value) throws IllegalAccessException
Set the value of a given field in the given object with the value given. Wrap the call in a privileged block if necessary.- Throws:
IllegalAccessException
-
shouldUsePrivilegedAccess
public static boolean shouldUsePrivilegedAccess()
This method checks to see if calls should be made to doPrivileged. It will only return true if a security manager is enabled, and the "eclipselink.security.usedoprivileged" property is set.Note: it is not possible to run EclipseLink using doPrivileged blocks when there is no SecurityManager enabled.
-
-