org.apache.openjpa.lib.util
Class J2DoPrivHelper

java.lang.Object
  extended by org.apache.openjpa.lib.util.J2DoPrivHelper
Direct Known Subclasses:
J2DoPriv5Helper

public abstract class J2DoPrivHelper
extends Object

Helper class to obtain the Privilege(Exception)Action object to perform Java 2 doPrivilege security sensitive function call in the following methods:

If these methods are used, the following sample usage patterns should be followed to ensure proper privilege is granted: 1) No security risk method call. E.g. private static final String SEP = J2DoPrivHelper.getLineSeparator(); 2) Methods with no exception thrown. PrivilegedAction is returned from J2DoPrivHelper.*Action(). E.g. ClassLoader loader = (ClassLoader) AccessController.doPrivileged( J2DoPrivHelper.getClassLoaderAction(clazz)); ClassLoader loader = (ClassLoader) (System.getSecurityManager() == null) ? clazz.getClassLoader() : AccessController.doPrivileged( J2DoPrivHelper.getClassLoaderAction(clazz)); 3) Methods with exception thrown. PrivilegedExceptionAction is returned from J2DoPrivHelper.*Action(). E.g. try { method = (Method) AccessController.doPrivileged( J2DoPrivHelper.getDeclaredMethodAction(clazz, name, parameterType)); } catch (PrivilegedActionException pae) { throw (NoSuchMethodException) pae.getException(); } try { method = (System.getSecurityManager() == null) ? clazz.getDeclaredMethod(name,parameterType) : (Method) AccessController.doPrivileged( J2DoPrivHelper.getDeclaredMethodAction( clazz, name, parameterType)); } catch (PrivilegedActionException pae) { throw (NoSuchMethodException) pae.getException() }

Author:
Albert Lee

Constructor Summary
J2DoPrivHelper()
           
 
Method Summary
static PrivilegedExceptionAction acceptAction(ServerSocket ss)
          Return a PrivilegedExceptionAction object for ServerSocket.accept().
static PrivilegedExceptionAction bCClassWrite(serp.bytecode.BCClass bc, File f)
          Return a PrivilegedExceptionAction object for BCClass.write().
static PrivilegedAction deleteAction(File f)
          Return a PrivilegeAction object for f.delete().
static PrivilegedAction existsAction(File f)
          Return a PrivilegeAction object for f.exists().
static PrivilegedAction getAbsoluteFileAction(File f)
          Return a PrivilegeAction object for f.getAbsoluteFile().
static PrivilegedAction getAbsolutePathAction(File f)
          Return a PrivilegeAction object for f.getAbsolutePath().
static PrivilegedAction getBCClassFieldsAction(serp.bytecode.BCClass bcClass, String fieldName)
          Return a PrivilegeAction object for BCClass.getFields().
static PrivilegedExceptionAction getByNameAction(String hostname)
          Return a PrivilegedExceptionAction object for InetAdress.getByName().
static PrivilegedExceptionAction getCanonicalPathAction(File f)
          Return a PrivilegedExceptionAction object for f.getCanonicalPath().
static PrivilegedAction<ClassLoader> getClassLoaderAction(Class clazz)
          Return a PrivilegeAction object for clazz.getClassloader().
static PrivilegedExceptionAction getContentAction(URLConnection con)
          Return a PrivilegedExceptionAction object con.getContent().
static PrivilegedAction<ClassLoader> getContextClassLoaderAction()
          Return a PrivilegeAction object for Thread.currentThread .getContextClassLoader().
static PrivilegedExceptionAction getDeclaredFieldAction(Class clazz, String name)
          Return a PrivilegedExceptionAction object for clazz.getDeclaredField().
static PrivilegedAction getDeclaredFieldsAction(Class clazz)
          Return a PrivilegeAction object for class.getDeclaredFields().
static PrivilegedExceptionAction getDeclaredMethodAction(Class clazz, String name, Class[] parameterTypes)
          Return a PrivilegedExceptionAction object for clazz.getDeclaredMethod().
static PrivilegedAction getDeclaredMethodsAction(Class clazz)
          Return a PrivilegeAction object for clazz.getDeclaredMethods().
static PrivilegedAction getFieldInstructionFieldAction(serp.bytecode.FieldInstruction instruction)
          Return a PrivilegeAction object for FieldInstruction.getField().
static PrivilegedExceptionAction<Class<?>> getForNameAction(String className, boolean initializeBoolean, ClassLoader classLoader)
          Return a PrivilegeAction object for Class.forName().
static String getLineSeparator()
          Return the value of the "line.separator" system property.
static PrivilegedAction getParentAction(ClassLoader loader)
          Return a PrivilegeAction object for loader.getParent().
static String getPathSeparator()
          Return the value of the "path.separator" system property.
static PrivilegedAction getPropertiesAction()
          Return a PrivilegeAction object for System.getProperties().
static PrivilegedAction getPropertyAction(String name)
          Return a PrivilegeAction object for System.getProperty().
static PrivilegedAction getResourceAction(ClassLoader loader, String resource)
          Return a PrivilegeAction object for loader.getResource().
static PrivilegedAction getResourceAction(Class clazz, String resource)
          Return a PrivilegeAction object for clazz.getResource().
static PrivilegedExceptionAction getResourcesAction(ClassLoader loader, String resource)
          Return a PrivilegedExceptionAction object for loader.getResources().
static PrivilegedAction getSystemClassLoaderAction()
          Return a PrivilegeAction object for ClassLoader.getSystemClassLoader().
static PrivilegedAction isDirectoryAction(File f)
          Return a PrivilegeAction object for f.isDirectory().
static PrivilegedAction isFileAction(File f)
          Return a PrivilegeAction object for f.isFile().
static PrivilegedAction lengthAction(File f)
          Return a PrivilegeAction object for f.length().
static PrivilegedAction listFilesAction(File f)
          Return a PrivilegeAction object for f.listFiles().
static PrivilegedAction loadProjectClassAction(serp.bytecode.Project project, Class clazz)
          Return a PrivilegeAction object for Project.loadClass().
static PrivilegedAction loadProjectClassAction(serp.bytecode.Project project, String clazzName)
          Return a PrivilegeAction object for Project.loadClass().
static PrivilegedAction mkdirsAction(File f)
          Return a PrivilegeAction object for f.mkdirs().
static PrivilegedAction newBCClassLoaderAction(serp.bytecode.Project project)
           
static PrivilegedAction newBCClassLoaderAction(serp.bytecode.Project project, ClassLoader parent)
          Return a PrivilegeAction object for new BCClassLoader().
static PrivilegedAction newCodeAction()
          Return a PrivilegeAction object for new serp.bytecode.Code().
static PrivilegedAction newDaemonThreadAction(Runnable target, String name)
          Return a PrivilegedAction object for new Thread().
static PrivilegedExceptionAction newFileInputStreamAction(File f)
          Return a PrivilegedExceptionAction object for new FileInputStream().
static PrivilegedExceptionAction newFileOutputStreamAction(File f)
          Return a PrivilegedExceptionAction object for new FileOutputStream().
static PrivilegedExceptionAction newFileOutputStreamAction(String f, boolean append)
          Return a PrivilegedExceptionAction object for new FileOutputStream().
static
<T> PrivilegedExceptionAction<T>
newInstanceAction(Class<T> clazz)
          Return a PrivilegedExceptionAction object for clazz.newInstance().
static PrivilegedAction<MultiClassLoader> newMultiClassLoaderAction()
          Return a PrivilegeAction object for new MultiClassLoader().
static PrivilegedExceptionAction newServerSocketAction(int port)
          Return a PrivilegedExceptionAction object for new ServerSocket().
static PrivilegedExceptionAction newSocketAction(InetAddress host, int port)
          Return a PrivilegedExceptionAction object for new Socket().
static PrivilegedAction newTemporaryClassLoaderAction(ClassLoader parent)
          Return a PrivilegeAction object for new TemporaryClassLoader().
static PrivilegedExceptionAction newZipFileAction(File f)
          Return a PrivilegedExceptionAction object for new ZipFile().
static PrivilegedExceptionAction openStreamAction(URL url)
          Return a PrivilegedExceptionAction object for url.openStream().
static PrivilegedAction renameToAction(File from, File to)
          Return a PrivilegeAction object for f.renameTo().
static PrivilegedAction setAccessibleAction(AccessibleObject aObj, boolean flag)
          Return a PrivilegeAction object for aObj.setAccessible().
static PrivilegedAction<Boolean> setContextClassLoaderAction(ClassLoader loader)
          Return a PrivilegeAction object for Thread.currentThread .setContextClassLoader().
static PrivilegedExceptionAction toURLAction(File file)
          Return a PrivilegedExceptionAction object for f.toURL().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

J2DoPrivHelper

public J2DoPrivHelper()
Method Detail

getLineSeparator

public static final String getLineSeparator()
Return the value of the "line.separator" system property. Requires security policy: 'permission java.util.PropertyPermission "read";'


getPathSeparator

public static final String getPathSeparator()
Return the value of the "path.separator" system property. Requires security policy: 'permission java.util.PropertyPermission "read";'


setAccessibleAction

public static final PrivilegedAction setAccessibleAction(AccessibleObject aObj,
                                                         boolean flag)
Return a PrivilegeAction object for aObj.setAccessible(). Requires security policy: 'permission java.lang.reflect.ReflectPermission "suppressAccessChecks";'


getForNameAction

public static final PrivilegedExceptionAction<Class<?>> getForNameAction(String className,
                                                                         boolean initializeBoolean,
                                                                         ClassLoader classLoader)
Return a PrivilegeAction object for Class.forName(). Notes: doPriv of Class.forName call is required only if the input classloader argument is null. E.g. Class.forName("x", false, Collection.class.getClassLoader()); Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
Class

getClassLoaderAction

public static final PrivilegedAction<ClassLoader> getClassLoaderAction(Class clazz)
Return a PrivilegeAction object for clazz.getClassloader(). Notes: No doPrivilege wrapping is required in the caller if: "the caller's class loader is not null and the caller's class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested". E.g. this.getClass().getClassLoader(); Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
Classloader

getDeclaredFieldAction

public static final PrivilegedExceptionAction getDeclaredFieldAction(Class clazz,
                                                                     String name)
Return a PrivilegedExceptionAction object for clazz.getDeclaredField(). Requires security policy: 'permission java.lang.RuntimePermission "accessDeclaredMembers";'

Returns:
Field
Throws:
NoSuchFieldException

getDeclaredFieldsAction

public static final PrivilegedAction getDeclaredFieldsAction(Class clazz)
Return a PrivilegeAction object for class.getDeclaredFields(). Requires security policy: 'permission java.lang.RuntimePermission "accessDeclaredMembers";'

Returns:
Field[]

getDeclaredMethodAction

public static final PrivilegedExceptionAction getDeclaredMethodAction(Class clazz,
                                                                      String name,
                                                                      Class[] parameterTypes)
Return a PrivilegedExceptionAction object for clazz.getDeclaredMethod(). Requires security policy 'permission java.lang.RuntimePermission "accessDeclaredMembers";'

Returns:
Method
Throws:
NoSuchMethodException

getDeclaredMethodsAction

public static final PrivilegedAction getDeclaredMethodsAction(Class clazz)
Return a PrivilegeAction object for clazz.getDeclaredMethods(). Requires security policy: 'permission java.lang.RuntimePermission "accessDeclaredMembers";'

Returns:
Method[]

getResourceAction

public static final PrivilegedAction getResourceAction(Class clazz,
                                                       String resource)
Return a PrivilegeAction object for clazz.getResource(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
URL

newInstanceAction

public static final <T> PrivilegedExceptionAction<T> newInstanceAction(Class<T> clazz)
                                                            throws IllegalAccessException,
                                                                   InstantiationException
Return a PrivilegedExceptionAction object for clazz.newInstance(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
A new instance of the provided class.
Throws:
IllegalAccessException
InstantiationException

getParentAction

public static final PrivilegedAction getParentAction(ClassLoader loader)
Return a PrivilegeAction object for loader.getParent(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
ClassLoader

getResourceAction

public static final PrivilegedAction getResourceAction(ClassLoader loader,
                                                       String resource)
Return a PrivilegeAction object for loader.getResource(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
URL

getResourcesAction

public static final PrivilegedExceptionAction getResourcesAction(ClassLoader loader,
                                                                 String resource)
                                                          throws IOException
Return a PrivilegedExceptionAction object for loader.getResources(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Enumeration
Throws:
IOException

getSystemClassLoaderAction

public static final PrivilegedAction getSystemClassLoaderAction()
Return a PrivilegeAction object for ClassLoader.getSystemClassLoader(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
ClassLoader

deleteAction

public static final PrivilegedAction deleteAction(File f)
Return a PrivilegeAction object for f.delete(). Requires security policy: 'permission java.io.FilePermission "delete";'

Returns:
Boolean

existsAction

public static final PrivilegedAction existsAction(File f)
Return a PrivilegeAction object for f.exists(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Boolean

getAbsoluteFileAction

public static final PrivilegedAction getAbsoluteFileAction(File f)
Return a PrivilegeAction object for f.getAbsoluteFile(). Requires security policy: 'permission java.util.PropertyPermission "read";'

Returns:
File

getAbsolutePathAction

public static final PrivilegedAction getAbsolutePathAction(File f)
Return a PrivilegeAction object for f.getAbsolutePath(). Requires security policy: 'permission java.util.PropertyPermission "read";'

Returns:
String

getCanonicalPathAction

public static final PrivilegedExceptionAction getCanonicalPathAction(File f)
                                                              throws IOException
Return a PrivilegedExceptionAction object for f.getCanonicalPath(). Requires security policy: 'permission java.util.PropertyPermission "read";'

Returns:
String
Throws:
IOException

isDirectoryAction

public static final PrivilegedAction isDirectoryAction(File f)
Return a PrivilegeAction object for f.isDirectory(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Boolean

isFileAction

public static final PrivilegedAction isFileAction(File f)
Return a PrivilegeAction object for f.isFile(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Boolean

lengthAction

public static final PrivilegedAction lengthAction(File f)
Return a PrivilegeAction object for f.length(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Long

listFilesAction

public static final PrivilegedAction listFilesAction(File f)
Return a PrivilegeAction object for f.listFiles(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
File[]

mkdirsAction

public static final PrivilegedAction mkdirsAction(File f)
Return a PrivilegeAction object for f.mkdirs(). Requires security policy: 'permission java.io.FilePermission "write";'

Returns:
Boolean

renameToAction

public static final PrivilegedAction renameToAction(File from,
                                                    File to)
Return a PrivilegeAction object for f.renameTo(). Requires security policy: 'permission java.io.FilePermission "write";'

Returns:
Boolean

toURLAction

public static final PrivilegedExceptionAction toURLAction(File file)
                                                   throws MalformedURLException
Return a PrivilegedExceptionAction object for f.toURL(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Boolean
Throws:
MalformedURLException

newFileInputStreamAction

public static final PrivilegedExceptionAction newFileInputStreamAction(File f)
                                                                throws FileNotFoundException
Return a PrivilegedExceptionAction object for new FileInputStream(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
FileInputStream
Throws:
FileNotFoundException

newFileOutputStreamAction

public static final PrivilegedExceptionAction newFileOutputStreamAction(File f)
                                                                 throws FileNotFoundException
Return a PrivilegedExceptionAction object for new FileOutputStream(). Requires security policy: 'permission java.io.FilePermission "write";'

Returns:
FileOutputStream
Throws:
FileNotFoundException

newFileOutputStreamAction

public static final PrivilegedExceptionAction newFileOutputStreamAction(String f,
                                                                        boolean append)
                                                                 throws FileNotFoundException
Return a PrivilegedExceptionAction object for new FileOutputStream(). Requires security policy: 'permission java.io.FilePermission "write";'

Returns:
FileOutputStream
Throws:
FileNotFoundException

getByNameAction

public static final PrivilegedExceptionAction getByNameAction(String hostname)
                                                       throws UnknownHostException
Return a PrivilegedExceptionAction object for InetAdress.getByName(). Requires security policy: 'permission java.net.SocketPermission "connect";'

Returns:
InetAddress
Throws:
UnknownHostException

newSocketAction

public static final PrivilegedExceptionAction newSocketAction(InetAddress host,
                                                              int port)
                                                       throws IOException
Return a PrivilegedExceptionAction object for new Socket(). Requires security policy: 'permission java.net.SocketPermission "connect";'

Returns:
Socket
Throws:
IOException

newServerSocketAction

public static final PrivilegedExceptionAction newServerSocketAction(int port)
                                                             throws IOException
Return a PrivilegedExceptionAction object for new ServerSocket(). Requires security policy: 'permission java.net.SocketPermission "listen";'

Returns:
ServerSocket
Throws:
IOException

acceptAction

public static final PrivilegedExceptionAction acceptAction(ServerSocket ss)
                                                    throws IOException
Return a PrivilegedExceptionAction object for ServerSocket.accept(). Requires security policy: 'permission java.net.SocketPermission "listen";'

Returns:
Socket
Throws:
IOException

getPropertiesAction

public static final PrivilegedAction getPropertiesAction()
Return a PrivilegeAction object for System.getProperties(). Requires security policy: 'permission java.util.PropertyPermission "read";'

Returns:
Properties

getPropertyAction

public static final PrivilegedAction getPropertyAction(String name)
Return a PrivilegeAction object for System.getProperty(). Requires security policy: 'permission java.util.PropertyPermission "read";'

Returns:
String

getContextClassLoaderAction

public static final PrivilegedAction<ClassLoader> getContextClassLoaderAction()
Return a PrivilegeAction object for Thread.currentThread .getContextClassLoader(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
ClassLoader

setContextClassLoaderAction

public static final PrivilegedAction<Boolean> setContextClassLoaderAction(ClassLoader loader)
Return a PrivilegeAction object for Thread.currentThread .setContextClassLoader(). Requires security policy: 'permission java.lang.RuntimePermission "setContextClassLoader";'

Returns:
ClassLoader

newDaemonThreadAction

public static final PrivilegedAction newDaemonThreadAction(Runnable target,
                                                           String name)
Return a PrivilegedAction object for new Thread(). Requires security policy: 'permission java.lang.RuntimePermission "modifyThreadGroup";' 'permission java.lang.RuntimePermission "modifyThread";'

Returns:
Thread

openStreamAction

public static final PrivilegedExceptionAction openStreamAction(URL url)
                                                        throws IOException
Return a PrivilegedExceptionAction object for url.openStream(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
InputStream
Throws:
IOException

getContentAction

public static final PrivilegedExceptionAction getContentAction(URLConnection con)
                                                        throws IOException
Return a PrivilegedExceptionAction object con.getContent(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
Object
Throws:
IOException

newZipFileAction

public static final PrivilegedExceptionAction newZipFileAction(File f)
                                                        throws IOException
Return a PrivilegedExceptionAction object for new ZipFile(). Requires security policy: 'permission java.io.FilePermission "read";'

Returns:
ZipFile
Throws:
IOException

newCodeAction

public static final PrivilegedAction newCodeAction()
Return a PrivilegeAction object for new serp.bytecode.Code(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
serp.bytecode.Code

newTemporaryClassLoaderAction

public static final PrivilegedAction newTemporaryClassLoaderAction(ClassLoader parent)
Return a PrivilegeAction object for new TemporaryClassLoader(). Requires security policy: 'permission java.lang.RuntimePermission "createClassLoader";'

Returns:
TemporaryClassLoader

newMultiClassLoaderAction

public static final PrivilegedAction<MultiClassLoader> newMultiClassLoaderAction()
Return a PrivilegeAction object for new MultiClassLoader(). Requires security policy: 'permission java.lang.RuntimePermission "createClassLoader";'

Returns:
MultiClassLoader

newBCClassLoaderAction

public static final PrivilegedAction newBCClassLoaderAction(serp.bytecode.Project project,
                                                            ClassLoader parent)
Return a PrivilegeAction object for new BCClassLoader(). Requires security policy: 'permission java.lang.RuntimePermission "createClassLoader";'

Returns:
BCClassLoader

newBCClassLoaderAction

public static final PrivilegedAction newBCClassLoaderAction(serp.bytecode.Project project)

bCClassWrite

public static final PrivilegedExceptionAction bCClassWrite(serp.bytecode.BCClass bc,
                                                           File f)
                                                    throws IOException
Return a PrivilegedExceptionAction object for BCClass.write(). Requires security policy: 'permission java.io.FilePermission "write";'

Returns:
File
Throws:
IOException

getBCClassFieldsAction

public static final PrivilegedAction getBCClassFieldsAction(serp.bytecode.BCClass bcClass,
                                                            String fieldName)
Return a PrivilegeAction object for BCClass.getFields(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
BCField

getFieldInstructionFieldAction

public static final PrivilegedAction getFieldInstructionFieldAction(serp.bytecode.FieldInstruction instruction)
Return a PrivilegeAction object for FieldInstruction.getField(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
BCField

loadProjectClassAction

public static final PrivilegedAction loadProjectClassAction(serp.bytecode.Project project,
                                                            Class clazz)
Return a PrivilegeAction object for Project.loadClass(). Requires security policy: 'permission java.lang.RuntimePermission "createClassLoader";'

Returns:
BCClass

loadProjectClassAction

public static final PrivilegedAction loadProjectClassAction(serp.bytecode.Project project,
                                                            String clazzName)
Return a PrivilegeAction object for Project.loadClass(). Requires security policy: 'permission java.lang.RuntimePermission "getClassLoader";'

Returns:
BCClass


Copyright © 2006-2013 Apache Software Foundation. All Rights Reserved.