Example usage for java.security PrivilegedAction PrivilegedAction

List of usage examples for java.security PrivilegedAction PrivilegedAction

Introduction

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

Prototype

PrivilegedAction

Source Link

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.
 * /*  www.  ja va2 s  .c  o  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: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  ww  .  j a va2s . 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:org.mule.util.compression.CompressionHelper.java

public static synchronized CompressionStrategy getDefaultCompressionStrategy() {
    if (defaultStrategy == null) {
        defaultStrategy = AccessController.doPrivileged(new PrivilegedAction<CompressionStrategy>() {
            @Override/*ww w. ja v  a2s  .c  om*/
            public CompressionStrategy run() {
                try {
                    Object o = ClassUtils
                            .loadClass(CompressionStrategy.COMPRESSION_DEFAULT, CompressionHelper.class)
                            .newInstance();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Found CompressionStrategy: " + o.getClass().getName());
                    }
                    return (CompressionStrategy) o;
                } catch (Exception e) {
                    // TODO MULE-863: What should we really do?  Document this?
                    logger.warn("Failed to build compression strategy: " + e.getMessage());
                }
                return null;
            }
        });
    }
    return defaultStrategy;
}

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  www. j  a v  a  2 s  .co  m*/
    });
}

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) {
            }/*  w  w  w  . ja va2s .  com*/
            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);
        }/*from  w ww.  j a  v  a 2  s .  com*/
    });
}

From source file:Main.java

/**
 * Gets all of the fields in this class and the super classes
 *
 * @param beanClass//from   w  w w  .j av a 2  s.  c o  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// w ww . j  a v a 2s  .  c  o  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  w  w .jav a 2  s  .  c om*/
        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            public ClassLoader run() {
                return Thread.currentThread().getContextClassLoader();
            }
        });
    }
}

From source file:org.jbpm.util.WidMVELEvaluator.java

public static Object eval(final String expression) {
    ExpressionCompiler compiler = new ExpressionCompiler(getRevisedExpression(expression), WID_PARSER_CONTEXT);

    if (KiePolicyHelper.isPolicyEnabled()) {
        return AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override/*from  www .j  a  va  2  s . co  m*/
            public Object run() {
                return MVEL.executeExpression(compiler.compile(), new HashMap());
            }
        }, KiePolicyHelper.getAccessContext());
    } else {
        return MVEL.executeExpression(compiler.compile(), new HashMap());
    }
}