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 interface
PrivilegedAccessHelper.CallableExceptionSupplier<E extends Exception>
INTERNAL SpecificException
supplier forPrivilegedAccessHelper.PrivilegedExceptionCallable
.static interface
PrivilegedAccessHelper.CallableThrowableSupplier<T extends Throwable>
INTERNAL SpecificThrowable
supplier forPrivilegedAccessHelper.PrivilegedExceptionCallable
.static interface
PrivilegedAccessHelper.PrivilegedCallable<T>
INTERNAL A task that returns a result and shall not throw an exception.static interface
PrivilegedAccessHelper.PrivilegedExceptionCallable<T>
INTERNAL A task that returns a result and may throw an exception.static interface
PrivilegedAccessHelper.PrivilegedExceptionConsumer
INTERNAL 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> T
callDoPrivileged(PrivilegedAccessHelper.PrivilegedCallable<T> task)
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedCallable
task usingAccessController.doPrivileged(PrivilegedAction)
when privileged access is enabled.static <T> T
callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task)
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallable
task 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.PrivilegedExceptionCallable
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled.static void
callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task)
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumer
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled.static <E extends Exception>
voidcallDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task, PrivilegedAccessHelper.CallableExceptionSupplier<E> exception)
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumer
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled.static <T> T
callDoPrivilegedWithThrowable(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task)
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallable
task 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.PrivilegedExceptionCallable
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled.static Class
getClassForName(String className)
Execute a java Class.forName().static Class
getClassForName(String className, boolean initialize, ClassLoader loader)
Execute a java Class.forName() wrap the call in a doPrivileged block if necessary.static ClassLoader
getClassLoaderForClass(Class clazz)
Gets the class loader for a given class.static Constructor
getConstructorFor(Class javaClass, Class[] args, boolean shouldSetAccessible)
Get the public constructor for the given class and given arguments and wrap it in doPrivileged if necessary.static ClassLoader
getContextClassLoader(Thread thread)
Get the context ClassLoader for a thread.static Constructor
getDeclaredConstructorFor(Class 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 Field
getDeclaredField(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 Method
getDeclaredMethod(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 Field
getField(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 Class
getFieldType(Field field)
Get the return type for a given method.static String
getLineSeparator()
INTERNAL: Get the line separator character.static Method
getMethod(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 Class
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 Method
getPublicMethod(Class javaClass, String methodName, Class[] methodParameterTypes, boolean shouldSetAccessible)
Get a public method declared in the given class.static String
getSystemProperty(String key)
INTERNAL: Get theSystem
property indicated by the specifiedkey
.static String
getSystemProperty(String key, String def)
INTERNAL: Get theSystem
property indicated by the specifiedkey
.static boolean
getSystemPropertyBoolean(String key, boolean def)
INTERNAL: Get boolean value of theSystem
property indicated by the specifiedkey
.static <T> T
getValueFromField(Field field, Object object)
Get the value of the given field in the given object.static <T> T
invokeConstructor(Constructor<T> constructor, Object[] args)
Construct an object with the given Constructor and the given array of arguments.static <T> T
invokeMethod(Method method, Object object)
Invoke the givenMethod on a givenObject.static <T> T
invokeMethod(Method method, Object object, Object[] parameters)
Invoke the givenMethod on a givenObject using the array of parameters given.static <T> T
newInstanceFromClass(Class<T> clazz)
Get a new instance of a class using the default constructor.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.static void
setValueInField(Field field, Object object, Object value)
Set the value of a given field in the given object with the value given.static boolean
shouldUsePrivilegedAccess()
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.PrivilegedCallable
task usingAccessController.doPrivileged(PrivilegedAction)
when privileged access is enabled.- Type Parameters:
T
-PrivilegedAccessHelper.PrivilegedCallable
return type- Parameters:
task
- task to execute
-
callDoPrivilegedWithException
public static <T> T callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task) throws Exception
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallable
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled.- Type Parameters:
T
-PrivilegedAccessHelper.PrivilegedExceptionCallable
return type- Parameters:
task
- task to execute- Throws:
Exception
-
callDoPrivilegedWithThrowable
public static <T> T callDoPrivilegedWithThrowable(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task) throws Throwable
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionCallable
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled.- Type Parameters:
T
-PrivilegedAccessHelper.PrivilegedExceptionCallable
return type- Parameters:
task
- task to execute- Throws:
Throwable
-
callDoPrivilegedWithException
public static void callDoPrivilegedWithException(PrivilegedAccessHelper.PrivilegedExceptionConsumer task) throws Exception
INTERNAL Executes providedPrivilegedAccessHelper.PrivilegedExceptionConsumer
task 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.PrivilegedExceptionCallable
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled. IfException
is thrown from task, it will be processed by providedPrivilegedAccessHelper.CallableExceptionSupplier
.- Type Parameters:
T
-PrivilegedAccessHelper.PrivilegedExceptionCallable
return typeE
- specificException
type- Parameters:
task
- task to executeexception
- specificException
supplier- 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.PrivilegedExceptionCallable
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled. IfThrowable
is thrown from task, it will be processed by providedPrivilegedAccessHelper.CallableThrowableSupplier
.- Type Parameters:
T
-PrivilegedAccessHelper.PrivilegedExceptionCallable
return typeE
- specificThrowable
type- Parameters:
task
- task to executethrowable
- specificThrowable
supplier- 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.PrivilegedExceptionConsumer
task usingAccessController.doPrivileged(PrivilegedExceptionAction)
when privileged access is enabled. IfException
is 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 Class getClassForName(String className) throws ClassNotFoundException
Execute a java Class.forName(). Wrap the call in a doPrivileged block if necessary.- Parameters:
className
-- Throws:
ClassNotFoundException
-
getClassForName
public static Class 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 Constructor getConstructorFor(Class 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 Constructor getDeclaredConstructorFor(Class 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 Class getFieldType(Field field)
Get the return type for a given method. Wrap the call in doPrivileged if necessary.- Parameters:
field
-
-
getSystemProperty
public static final String getSystemProperty(String key)
INTERNAL: Get theSystem
property indicated by the specifiedkey
.
-
getSystemProperty
public static final String getSystemProperty(String key, String def)
INTERNAL: Get theSystem
property indicated by the specifiedkey
.
-
getSystemPropertyBoolean
public static final boolean getSystemPropertyBoolean(String key, boolean def)
INTERNAL: Get boolean value of theSystem
property indicated by the specifiedkey
.- Parameters:
key
- The name of theSystem
property.def
- The default value.- Returns:
true
if the property value is"true"
(case insensitive) or the property is not defined anddef
istrue
;false
otherwise.- Since:
- 2.6.3
-
getLineSeparator
public static final String getLineSeparator()
INTERNAL: Get the line separator character.- Returns:
- The
String
containing 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 Class getMethodReturnType(Method method)
Get the return type for a given method. Wrap the call in doPrivileged if necessary.- Parameters:
method
-
-
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.
-
-