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:org.apache.jasper.runtime.PageContextImpl.java

public Object getAttribute(final String name, final int scope) {

    if (name == null) {
        throw new NullPointerException(Localizer.getMessage("jsp.error.attribute.null_name"));
    }//  w w w . j  a va  2  s.  c  o m

    if (System.getSecurityManager() != null) {
        return AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return doGetAttribute(name, scope);
            }
        });
    } else {
        return doGetAttribute(name, scope);
    }

}

From source file:org.apache.bsf.BSFManager.java

/**
 * Compile the application of the given anonymous function of the given
 * language to the given parameters into the given <tt>CodeBuffer</tt>.
 *
 * @param lang language identifier//ww w.  j a  v  a 2s  . c om
 * @param source (context info) the source of this expression
 (e.g., filename)
 * @param lineNo (context info) the line number in source for expr
 * @param columnNo (context info) the column number in source for expr
 * @param funcBody the multi-line, value returning script to evaluate
 * @param paramNames the names of the parameters above assumes
 * @param arguments values of the above parameters
 * @param cb       code buffer to compile into
 *
 * @exception BSFException if anything goes wrong while running the script
 */
public void compileApply(String lang, String source, int lineNo, int columnNo, Object funcBody,
        Vector paramNames, Vector arguments, CodeBuffer cb) throws BSFException {
    logger.debug("BSFManager:compileApply");

    final BSFEngine e = loadScriptingEngine(lang);
    final String sourcef = source;
    final int lineNof = lineNo, columnNof = columnNo;
    final Object funcBodyf = funcBody;
    final Vector paramNamesf = paramNames;
    final Vector argumentsf = arguments;
    final CodeBuffer cbf = cb;

    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws Exception {
                e.compileApply(sourcef, lineNof, columnNof, funcBodyf, paramNamesf, argumentsf, cbf);
                return null;
            }
        });
    } catch (PrivilegedActionException prive) {

        logger.error("Exception :", prive);
        throw (BSFException) prive.getException();
    }
}

From source file:org.apache.openjpa.persistence.validation.ValidatorImpl.java

/**
 * Validates a given instance/*from  ww  w.ja  v a 2  s  .  c o  m*/
 * 
 * @param <T> The instance to validate
 * @param arg0 The class, of type T to validate
 * @param event The event id
 * @return ValidationException if the validator produces one or more
 *         constraint violations.
 */
@Override
public <T> ValidationException validate(T arg0, int event) {
    if (!isValidating(event))
        return null;
    Set<ConstraintViolation<T>> violations = AccessController
            .doPrivileged(J2DoPrivHelper.validateAction(_validator, arg0, getValidationGroup(event)));

    if (violations != null && violations.size() > 0) {
        return new ValidationException(new ConstraintViolationException(
                // A validation constraint failure occurred for class "{0}".
                _loc.get("validate-failed", arg0.getClass().getName()).getMessage(), (Set) violations), true);
    }
    return null;
}

From source file:tools.xor.util.ClassUtil.java

/**
 * Creates a new instance of the given class via the no-arg constructor,
 * invoking the constructor as a privileged action if it is protected or
 * private.//from   w  w  w. ja  va  2 s .  c o m
 * 
 * @param c given class
 * @return a new instance of the given class via the no-arg constructor
 * @throws Exception when creating the instance
 */
public static Object newInstanceAsPrivileged(final Class<?> c) throws Exception {

    try {
        return c.newInstance();

    } catch (Exception e) {
        return AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                try {
                    final Constructor<?> constructor = c.getDeclaredConstructor();
                    constructor.setAccessible(true);
                    return constructor.newInstance();
                } catch (Exception e) {
                    throw ClassUtil.wrapRun(e);
                }
            }
        });
    }
}

From source file:it.cnr.icar.eric.common.AbstractProperties.java

/**
 * Loads system properties to 'properties'.
 *
 * @param properties existing properties (might get overwriten)
 * @return True if load ok. False otherwise.
 *///ww w  . j  a  v a 2  s.c om
protected static boolean loadSystemProperties(Properties properties) {
    log.trace(CommonResourceBundle.getInstance().getString("message.LoadPropsFromSystemProps"));
    try {
        AccessController.doPrivileged(new ReadSystemPropsPrivilegedAction(properties));
        return true;
    } catch (Exception e) {
        // silently ignore missing System properties
        log.debug(CommonResourceBundle.getInstance().getString("message.IgnoringSystemPropsDueToException",
                new Object[] { e.toString() }));
        return false;
    }
}

From source file:org.apache.axiom.om.util.StAXUtils.java

public static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, final Reader in)
        throws XMLStreamException {

    final XMLInputFactory inputFactory = getXMLInputFactory(configuration);
    try {//from  w  ww  .j a v  a  2 s  .  c  om
        XMLStreamReader reader = (XMLStreamReader) AccessController
                .doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException {
                        return inputFactory.createXMLStreamReader(in);
                    }
                });
        if (isDebugEnabled) {
            log.debug("XMLStreamReader is " + reader.getClass().getName());
        }
        return reader;
    } catch (PrivilegedActionException pae) {
        throw (XMLStreamException) pae.getException();
    }
}

From source file:org.apache.hadoop.mapred.pipes.SubmitterToAccels.java

@Override
public int run(String[] args) throws Exception {
    CommandLineParser cli = new CommandLineParser();
    if (args.length == 0) {
        cli.printUsage();//from  w  w w  .ja v a  2 s . c o m
        return 1;
    }

    cli.addOption("input", false, "input path to the maps", "path");
    cli.addOption("output", false, "output path from the reduces", "path");

    cli.addOption("cpubin", false, "URI to application cpu executable", "class");
    cli.addOption("gpubin", false, "URI to application gpu executable", "class");

    Parser parser = cli.createParser();
    try {
        GenericOptionsParser genericParser = new GenericOptionsParser(getConf(), args);
        CommandLine results = parser.parse(cli.options, genericParser.getRemainingArgs());
        JobConf job = new JobConf(getConf());

        if (results.hasOption("input")) {
            FileInputFormat.setInputPaths(job, (String) results.getOptionValue("input"));
        }
        if (results.hasOption("output")) {
            FileOutputFormat.setOutputPath(job, new Path((String) results.getOptionValue("output")));
        }
        if (results.hasOption("cpubin")) {
            setCPUExecutable(job, (String) results.getOptionValue("cpubin"));
        }
        if (results.hasOption("gpubin")) {
            setGPUExecutable(job, (String) results.getOptionValue("gpubin"));
        }
        // if they gave us a jar file, include it into the class path
        String jarFile = job.getJar();
        if (jarFile != null) {
            final URL[] urls = new URL[] { FileSystem.getLocal(job).pathToFile(new Path(jarFile)).toURL() };
            //FindBugs complains that creating a URLClassLoader should be
            //in a doPrivileged() block. 
            ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
                    return new URLClassLoader(urls);
                }
            });
            job.setClassLoader(loader);
        }
        runJob(job);
        return 0;
    } catch (ParseException pe) {
        LOG.info("Error :" + pe);
        cli.printUsage();
        return 1;
    }
}

From source file:org.apache.roller.planet.business.jpa.JPAPersistenceStrategy.java

/**
 * Get the context class loader associated with the current thread. This is
 * done in a doPrivileged block because it is a secure method.
 * @return the current thread's context class loader.
 *//*w w  w  .  j ava2  s .co m*/
protected static ClassLoader getContextClassLoader() {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });
}

From source file:org.apache.roller.weblogger.business.jpa.JPAPersistenceStrategy.java

/**
 * Get the context class loader associated with the current thread. This is
 * done in a doPrivileged block because it is a secure method.
 * @return the current thread's context class loader.
 *//*  w w w . j a  va  2s.  c o  m*/
private static ClassLoader getContextClassLoader() {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });
}

From source file:com.reelfx.model.ScreenRecorder.java

public static void deleteOutput() {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override/* ww  w .  ja  va2 s.  c  om*/
        public Object run() {
            try {
                if (OUTPUT_FILE.exists() && !OUTPUT_FILE.delete())
                    throw new Exception("Can't delete the old video file!");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            return null;
        }
    });
}