FileDocCategorySizeDatePackage
PrivilegedAccessHelper.javaAPI DocGlassfish v2 API16939Tue May 22 16:54:42 BST 2007oracle.toplink.essentials.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 For users that wish to use a security manager and disable the use of doPrivileged, users can set one of two system flags (through the java -Dxxxxx option): oracle.j2ee.toplink.security.usedoprivileged=false oracle.j2ee.security.usedoprivileged=false

Fields Summary
private static boolean
shouldUsePrivilegedAccess
private static boolean
shouldSecurityManagerBeChecked
Constructors Summary
Methods Summary
private static java.lang.reflect.FieldfindDeclaredField(java.lang.Class javaClass, java.lang.String fieldName)
Finding a field within a class potentially has to navigate through it's superclasses to eventually find the field. This method is called by the public getDeclaredField() method and does a recursive search for the named field in the given classes or it's superclasses.


                                                     
             
        try {
            return javaClass.getDeclaredField(fieldName);
        } catch (NoSuchFieldException ex) {
            Class superclass = javaClass.getSuperclass();
            if (superclass == null) {
                throw ex;
            } else {
                return findDeclaredField(superclass, fieldName);
            }
        }
    
private static java.lang.reflect.MethodfindMethod(java.lang.Class javaClass, java.lang.String methodName, java.lang.Class[] methodParameterTypes)
Finding a method within a class potentially has to navigate through it's superclasses to eventually find the method. This method is called by the public getDeclaredMethod() method and does a recursive search for the named method in the given classes or it's superclasses.

        try {
            return javaClass.getDeclaredMethod(methodName, methodParameterTypes);
        } catch (NoSuchMethodException ex) {
            Class superclass = javaClass.getSuperclass();
            if (superclass == null) {
                throw ex;
            } else {
                try{
                    return findMethod(superclass, methodName, methodParameterTypes);
                }catch (NoSuchMethodException lastEx){
                    throw ex;
                }
            }
        }
    
public static java.lang.ClassgetClassForName(java.lang.String className)
Execute a java Class.forName(). Wrap the call in a doPrivileged block if necessary.

param
className

        return Class.forName(className);
    
public static java.lang.ClassgetClassForName(java.lang.String className, boolean initialize, java.lang.ClassLoader loader)
Execute a java Class.forName() wrap the call in a doPrivileged block if necessary.

param
className
param
initialize
param
loader
throws
java.lang.ClassNotFoundException

        return Class.forName(className, initialize, loader);
    
public static java.lang.ClassLoadergetClassLoaderForClass(java.lang.Class clazz)
Gets the class loader for a given class. Wraps the call in a privileged block if necessary

        return clazz.getClassLoader();
    
public static java.lang.reflect.ConstructorgetConstructorFor(java.lang.Class javaClass, java.lang.Class[] args, boolean shouldSetAccessible)
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.

param
javaClass The class to get the Constructor for
param
args An array of classes representing the argument types of the constructor
param
shouldSetAccessible whether or not to call the setAccessible API
throws
java.lang.NoSuchMethodException

        Constructor result = javaClass.getConstructor(args);
        if (shouldSetAccessible) {
            result.setAccessible(true);
        }
        return result;
    
public static java.lang.ClassLoadergetContextClassLoader(java.lang.Thread thread)
Get the context ClassLoader for a thread. Wrap the call in a doPrivileged block if necessary.

        return thread.getContextClassLoader();
    
public static java.lang.reflect.ConstructorgetDeclaredConstructorFor(java.lang.Class javaClass, java.lang.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. 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.

param
javaClass The class to get the Constructor for
param
args An array of classes representing the argument types of the constructor
param
shouldSetAccessible whether or not to call the setAccessible API
throws
java.lang.NoSuchMethodException

        Constructor result = javaClass.getDeclaredConstructor(args);
        if (shouldSetAccessible) {
            result.setAccessible(true);
        }
        return result;
    
public static java.lang.reflect.FieldgetDeclaredField(java.lang.Class javaClass, java.lang.String fieldName, boolean shouldSetAccessible)
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.

param
javaClass The class to get the field from
param
fieldName The name of the field
param
shouldSetAccessible whether or not to call the setAccessible API
throws
java.lang.NoSuchFieldException

        Field field = javaClass.getDeclaredField(fieldName);
        if (shouldSetAccessible) {
            field.setAccessible(true);
        }
        return field;
    
public static java.lang.reflect.Field[]getDeclaredFields(java.lang.Class clazz)
Get the list of fields in a class. Wrap the call in doPrivileged if necessary Excludes inherited fields.

param
clazz the class to get the fields from.

        return clazz.getDeclaredFields();
    
public static java.lang.reflect.MethodgetDeclaredMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] methodParameterTypes)
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.

param
method the class to get the method from
param
methodName the name of the method to get
param
methodParameters a list of classes representing the classes of the parameters of the method.

         return clazz.getDeclaredMethod(methodName, methodParameterTypes);
    
public static java.lang.reflect.Method[]getDeclaredMethods(java.lang.Class clazz)
Get the list of methods in a class. Wrap the call in doPrivileged if necessary. Excludes inherited methods.

param
clazz the class to get the methods from.

        return clazz.getDeclaredMethods();
    
public static java.lang.reflect.FieldgetField(java.lang.Class javaClass, java.lang.String fieldName, boolean shouldSetAccessible)
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.

param
javaClass The class to get the field from
param
fieldName The name of the field
param
shouldSetAccessible whether or not to call the setAccessible API
throws
java.lang.NoSuchFieldException

        Field field = (Field)findDeclaredField(javaClass, fieldName);
        if (shouldSetAccessible) {
            field.setAccessible(true);
        }
            return field;
    
public static java.lang.ClassgetFieldType(java.lang.reflect.Field field)
Get the return type for a given method. Wrap the call in doPrivileged if necessary.

param
field

        return field.getType();
    
public static java.lang.StringgetLineSeparator()
Get the line separator character. Previous versions of TopLink always did this in a privileged block so we will continue to do so.

        if (shouldUsePrivilegedAccess()) {
            return (String)AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        return System.getProperty("file.separator");
                    }
                });
        } else {
            return oracle.toplink.essentials.internal.helper.Helper.cr();
        }
    
public static java.lang.reflect.MethodgetMethod(java.lang.Class javaClass, java.lang.String methodName, java.lang.Class[] methodParameterTypes, boolean shouldSetAccessible)
Get a method declared in the given class. Wrap the call in doPrivileged if necessary. This call will traver 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.

param
javaClass The class to get the method from
param
methodName The name of the method to get
param
methodParameterTypes A list of classes representing the classes of the parameters of the mthod
param
shouldSetAccessible whether or not to call the setAccessible API
throws
java.lang.NoSuchMethodException

        Method method = findMethod(javaClass, methodName, methodParameterTypes);
        if (shouldSetAccessible) {
            method.setAccessible(true);
        }
        return method;
    
public static java.lang.Class[]getMethodParameterTypes(java.lang.reflect.Method method)
Get the list of parameter types for a given method. Wrap the call in doPrivileged if necessary.

param
method The method to get the parameter types of

        return method.getParameterTypes();
    
public static java.lang.ClassgetMethodReturnType(java.lang.reflect.Method method)
Get the return type for a given method. Wrap the call in doPrivileged if necessary.

param
method

        return method.getReturnType();
    
public static java.lang.reflect.Method[]getMethods(java.lang.Class clazz)
Get the list of methods in a class. Wrap the call in doPrivileged if necessary. This call will traver the superclasses.

param
clazz the class to get the methods from.

        return clazz.getMethods();
    
public static java.lang.ObjectgetValueFromField(java.lang.reflect.Field field, java.lang.Object object)
Get the value of the given field in the given object.

        return field.get(object);
    
public static java.lang.ObjectinvokeConstructor(java.lang.reflect.Constructor constructor, java.lang.Object[] args)
Construct an object with the given Constructor and the given array of arguments. Wrap the call in a doPrivileged block if necessary.

        return constructor.newInstance(args);
    
public static java.lang.ObjectinvokeMethod(java.lang.reflect.Method method, java.lang.Object object, java.lang.Object[] parameters)
Invoke the givenMethod on a givenObject using the array of parameters given. Wrap in a doPrivileged block if necessary.

        // Ensure the method is accessible.
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        return method.invoke(object, parameters);
    
public static java.lang.ObjectnewInstanceFromClass(java.lang.Class clazz)
Get a new instance of a class using the default constructor. Wrap the call in a privileged block if necessary.

        return clazz.newInstance();
    
public static voidsetValueInField(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value)
Set the value of a given field in the given object with the value given. Wrap the call in a privileged block if necessary.

        field.set(object, value);
    
public static booleanshouldUsePrivilegedAccess()
This method checks to see if calls should be made to doPrivileged. In general, if a security manager is enabled, it will return true and if one is not enabled, it will return false. It will, however, always return false if either of the following two java properties is set. oracle.j2ee.toplink.security.usedoprivileged=false oracle.j2ee.security.usedoprivileged=false Note: it is not possible to run TopLink using doPrivileged blocks when there is no SecurityManager enabled.

        // We will only detect whether to use doPrivileged once.
        if (shouldSecurityManagerBeChecked) {
            shouldSecurityManagerBeChecked = false;

            Boolean privilegedPropertySet = (Boolean)AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        boolean propertySet;

                        // check TopLink and OC4j doPrivileged flag.
                        String usePrivileged = System.getProperty("oracle.j2ee.toplink.security.usedoprivileged");
                        String oc4jUsePrivileged = System.getProperty("oracle.j2ee.security.usedoprivileged");
                        propertySet = (((usePrivileged != null) && usePrivileged.equalsIgnoreCase("false")) || ((oc4jUsePrivileged != null) && oc4jUsePrivileged.equalsIgnoreCase("false")));
                        return new Boolean(propertySet);
                    }
                });
            if (privilegedPropertySet.booleanValue()) {
                shouldUsePrivilegedAccess = false;
            } else {
                shouldUsePrivilegedAccess = (System.getSecurityManager() != null);
            }
        }
        return shouldUsePrivilegedAccess;