Example usage for java.security AccessController doPrivileged

List of usage examples for java.security AccessController doPrivileged

Introduction

In this page you can find the example usage for java.security AccessController doPrivileged.

Prototype

@CallerSensitive
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException 

Source Link

Document

Performs the specified PrivilegedExceptionAction with privileges enabled.

Usage

From source file:Main.java

/**
 * Execute a callable with a clean thread context and security context as
 * to avoid any passing on of thread context and security context to another
 * thread that may be spawned from here and may end up holding copies in the end.
 * Any exception thrown will be forwarded via a generic runtime exception.
 * //from   ww  w.  j a va 2  s.  co m
 * @param contextLoader A class loader to set as context class loader 
 * @param callable A {@link Callable} to invoke
 * @return Return value from the {@link Callable} invocation
 */
public static <T> T cleanContextExecute(ClassLoader contextLoader, final Callable<T> callable) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(contextLoader);
    try {
        return AccessController.doPrivileged(new PrivilegedAction<T>() {
            public T run() {
                try {
                    return callable.call();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}

From source file:com.paulwithers.bp106.DemoUtils.java

public static String convertObjectToString(final Object o) {
    String retVal_ = "";
    try {/*from  w ww .j av a2s .com*/
        retVal_ = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
            public String run() throws Exception {
                return ReflectionToStringBuilder.toString(o);
            }
        });
    } catch (AccessControlException e) {
        e.printStackTrace();
    } catch (PrivilegedActionException e) {
        e.printStackTrace();
    }
    return retVal_;
}

From source file:org.apache.commons.weaver.privilizer.example.Setup.java

/**
 * This simply allows us to to set up test classes by doing
 * privileged things without granting privileges to the test
 * code itself and thus making it impossible to test the effects
 * of privilization.//from  w w w. j  a  va  2 s  . c  om
 */
public static void setProperty(final String name, final String value) {
    AccessController.doPrivileged(new PrivilegedAction<Void>() {
        public Void run() {
            System.setProperty(name, value);
            return null;
        }
    });
}

From source file:ECCProvider.java

public ECCProvider() {
    super("Java2s", 1.0, INFO);
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            put("KeyFactory.ECC", "com.Java2sKeyFactory");
            put("KeyPairGenerator.ECC", "com.Java2sKeyPairGenerator");
            return null;
        }//from  w  w w  .j a  v a 2s  .c  om
    });
}

From source file:SecuritySupport.java

static ClassLoader getContextClassLoader() {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            ClassLoader cl = null;
            try {
                cl = Thread.currentThread().getContextClassLoader();
            } catch (SecurityException ex) {
            }/*from   w w w .j  a v a 2 s  .co m*/
            return cl;
        }
    });
}

From source file:org.apache.axis.components.logger.LogFactory.java

private static final org.apache.commons.logging.LogFactory getLogFactory() {
    return (org.apache.commons.logging.LogFactory) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return DiscoverSingleton.find(org.apache.commons.logging.LogFactory.class,
                    org.apache.commons.logging.LogFactory.FACTORY_PROPERTIES,
                    org.apache.commons.logging.LogFactory.FACTORY_DEFAULT);
        }/*w  ww.j ava2s. c o  m*/
    });
}

From source file:Main.java

/**
 * Gets all of the fields in this class and the super classes
 *
 * @param beanClass/* w ww  .ja v  a 2 s  . co m*/
 * @return
 */
static private List<Field> getFields(final Class<?> beanClass) {
    // This class must remain private due to Java 2 Security concerns
    List<Field> fields = AccessController.doPrivileged(new PrivilegedAction<List<Field>>() {
        public List<Field> run() {
            List<Field> fields = new ArrayList<Field>();
            Class<?> cls = beanClass;
            while (cls != null) {
                Field[] fieldArray = cls.getDeclaredFields();
                for (Field field : fieldArray) {
                    fields.add(field);
                }
                cls = cls.getSuperclass();
            }
            return fields;
        }
    });

    return fields;
}

From source file:Main.java

/**
 * Get an annotation.  This is wrappered to avoid a Java2Security violation.
 * @param cls Class that contains annotation 
 * @param annotation Class of requrested Annotation
 * @return annotation or null//from  w  ww. j  ava 2 s. co  m
 */
private static <T extends Annotation> T getAnnotation(final AnnotatedElement element,
        final Class<T> annotation) {
    return AccessController.doPrivileged(new PrivilegedAction<T>() {
        public T run() {
            return element.getAnnotation(annotation);
        }
    });
}

From source file:SecurityActions.java

static ClassLoader getTCL() {
    if (System.getSecurityManager() == null) {
        return Thread.currentThread().getContextClassLoader();
    } else {/*from w  ww  . ja  va2  s  . co m*/
        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            public ClassLoader run() {
                return Thread.currentThread().getContextClassLoader();
            }
        });
    }
}

From source file:Main.java

/**
 * Stupid... but there is places all over that hold on to
 * AccessControlContexts and Context Class Loaders. This easily consitutes a
 * leak. So, for stuff that doesn't need anything from the context class
 * loader this is a way of keeping context to a minimum;
 * /*from   w w w . j av  a  2s  .com*/
 * Execute a callable with a clean thread context and security context as
 * to avoid any passing on of thread context and security context to another
 * thread that may be spawned from here and may end up holding copies in the end.
 * Any exception thrown will be propagated.
 * 
 * @param contextLoader A class loader to set as context class loader 
 * @param callable A {@link Callable} to invoke
 * @return Return value from the {@link Callable} invocation
 * @throws Exception
 */
public static <T> T cleanContextExceptionExecute(ClassLoader contextLoader, final Callable<T> callable)
        throws Exception {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(contextLoader);
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
            public T run() throws Exception {
                return callable.call();
            }
        });
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}