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:SecuritySupport.java

InputStream getResourceAsStream(final ClassLoader cl, final String name) {
    return (InputStream) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            InputStream ris;/*from www .j  ava  2s .com*/
            if (cl == null) {
                ris = ClassLoader.getSystemResourceAsStream(name);
            } else {
                ris = cl.getResourceAsStream(name);
            }
            return ris;
        }
    });
}

From source file:org.elasticsearch.xpack.security.authc.kerberos.SpnegoHttpClientConfigCallbackHandler.java

/**
 * Privileged Wrapper that invokes action with Subject.doAs to perform work as
 * given subject./* ww w .  ja  v a  2s. c  o m*/
 *
 * @param subject {@link Subject} to be used for this work
 * @param action {@link PrivilegedExceptionAction} action for performing inside
 *            Subject.doAs
 * @param acc the {@link AccessControlContext} to be tied to the specified
 *            subject and action see
 *            {@link Subject#doAsPrivileged(Subject, PrivilegedExceptionAction, AccessControlContext)
 * @return the value returned by the PrivilegedExceptionAction's run method
 * @throws PrivilegedActionException
 */
static <T> T doAsPrivilegedWrapper(final Subject subject, final PrivilegedExceptionAction<T> action,
        final AccessControlContext acc) throws PrivilegedActionException {
    try {
        return AccessController.doPrivileged(
                (PrivilegedExceptionAction<T>) () -> Subject.doAsPrivileged(subject, action, acc));
    } catch (PrivilegedActionException pae) {
        if (pae.getCause() instanceof PrivilegedActionException) {
            throw (PrivilegedActionException) pae.getCause();
        }
        throw pae;
    }
}

From source file:org.apache.openjpa.meta.SequenceMetaData.java

/**
 * Create a new uninitialized instance of this sequence.
 *//*from   w ww . j a va  2s.  c o m*/
protected Seq instantiate(ClassLoader envLoader) {
    if (NAME_SYSTEM.equals(_name))
        return _repos.getConfiguration().getSequenceInstance();

    try {
        PluginValue plugin = newPluginValue("sequence-plugin");
        plugin.setString(_plugin);
        String clsName = plugin.getClassName();

        Class cls = null;
        try {
            cls = Class.forName(clsName, true,
                    AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(Seq.class)));
        } catch (ClassNotFoundException cnfe) {
            // Target sequence type is loaded by the ClassLoader responsible for OpenJPA classes.
            // This can happen if the custom sequence implementation is a class that belongs to
            // a child ClassLoader - a situation that can easily happen in a JEE environment.
            // Fall back to the envLoader to try load the class.
            cls = Class.forName(clsName, true, envLoader);
        }

        StringBuilder props = new StringBuilder();
        if (plugin.getProperties() != null)
            props.append(plugin.getProperties());
        addStandardProperties(props);

        // allow user-class specification of either our sequence
        // interface or a factory class
        Seq seq;
        if (Seq.class.isAssignableFrom(cls)) {
            seq = (Seq) AccessController.doPrivileged(J2DoPrivHelper.newInstanceAction(cls));
            Configurations.configureInstance(seq, _repos.getConfiguration(), props.toString());
            if (_type != Seq.TYPE_DEFAULT)
                seq.setType(_type);
        } else if (_factory != null)
            seq = _factory.toSequence(cls, props.toString());
        else
            throw new MetaDataException(_loc.get("not-seq-cls", _name, cls));
        return seq;
    } catch (OpenJPAException ke) {
        throw ke;
    } catch (Exception e) {
        if (e instanceof PrivilegedActionException)
            e = ((PrivilegedActionException) e).getException();
        throw new MetaDataException(_loc.get("cant-init-seq", _name)).setCause(e);
    }
}

From source file:org.apache.openjpa.lib.conf.ConfigurationImpl.java

/**
 * Automatically load global values from the system's
 * {@link ProductDerivation}s, and from System properties.
 *//*from  w w w  . j a v a2s . co m*/
public boolean loadGlobals() {
    MultiClassLoader loader = AccessController.doPrivileged(J2DoPrivHelper.newMultiClassLoaderAction());
    loader.addClassLoader(AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction()));
    loader.addClassLoader(getClass().getClassLoader());
    ConfigurationProvider provider = ProductDerivations.loadGlobals(loader);
    if (provider != null)
        provider.setInto(this);

    // let system properties override other globals
    try {
        fromProperties(new HashMap(AccessController.doPrivileged(J2DoPrivHelper.getPropertiesAction())));
    } catch (SecurityException se) {
        // security manager might disallow
    }

    _globals = true;
    if (provider == null) {
        Log log = getConfigurationLog();
        if (log.isTraceEnabled())
            log.trace(_loc.get("no-default-providers"));
        return false;
    }
    return true;
}

From source file:org.nebulaframework.core.job.archive.GridArchive.java

/**
 * Returns the {@code GridJob} classes with in the given {@code .nar} file.
 * Uses {@link GridArchiveClassLoader}./*w  w w .  j  a v a 2s  . c o  m*/
 * 
 * @param file
 *            {@code File} instance for {@code .nar} file.
 * 
 * @return Fully qualified class names of {@code GridJob} classes in the
 *         file.
 * 
 * @throws IOException
 *             if occurred during File I/O operations
 * 
 * @see GridArchiveClassLoader
 */
protected static String[] findJobClassNames(final File file) throws IOException {

    // Instantiate ClassLoader for given File
    ClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        public GridArchiveClassLoader run() {
            return new GridArchiveClassLoader(file);
        }

    });

    // Find ClassNames of all classes inside the file (except in NEBULA-INF)
    // Content inside .jar files will not be processed
    String[] allClassNames = getAllClassNames(file);

    // Holds Class<?> instances loaded by ClassLoader, for all classes
    List<String> jobClassNames = new ArrayList<String>();

    for (String className : allClassNames) {
        try {
            // Load each Class and check if its a GridJob Class
            if (isGridJobClass(classLoader.loadClass(className))) {
                jobClassNames.add(className);
            }
        } catch (ClassNotFoundException e) {
            // Log and continue with rest
            log.debug("[GridArchive] Unable to load class " + className);
        }
    }
    return jobClassNames.toArray(new String[] {});
}

From source file:org.codice.solr.factory.impl.HttpClientBuilder.java

private String getPlainTextSolrPassword() {
    return encryptionService.decryptValue(AccessController
            .doPrivileged((PrivilegedAction<String>) () -> System.getProperty("solr.password")));
}

From source file:org.apache.struts2.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"));
    }//from  www  . j  av  a  2s  .  c  o m

    if (SecurityUtil.isPackageProtectionEnabled()) {
        return AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return doGetAttribute(name, scope);
            }
        });
    } else {
        return doGetAttribute(name, scope);
    }

}

From source file:org.nebulaframework.deployment.classloading.GridArchiveClassLoader.java

/**
 * Creates a temporary file which consists of the {@code byte[]} of a given
 * {@code GridArchive}.//ww w .  j  a v a  2 s .  co  m
 * 
 * @param archive
 *            {@code GridArchive}
 * @return A {@code File} reference for new temporary file
 * 
 * @throws IOException
 *             if IOException occurs during {@code File} handling
 */
protected File createTempArchiveFile(final GridArchive archive) throws Exception {

    try {
        // Run with Privileges
        return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {

            @Override
            public File run() throws IOException {
                // Create Temp File
                File archiveFile = File.createTempFile("archivetemp", "nar");
                archiveFile.deleteOnExit(); // Mark to delete

                // Write the byte[]
                FileOutputStream fout = new FileOutputStream(archiveFile);
                fout.write(archive.getBytes());
                fout.flush();
                fout.close();

                return archiveFile;
            }

        });
    } catch (PrivilegedActionException e) {
        throw e.getException();
    }
}

From source file:SecuritySupport.java

boolean getFileExists(final File f) {
    return ((Boolean) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return new Boolean(f.exists());
        }/*from www .ja v a 2  s . c  o  m*/
    })).booleanValue();
}

From source file:org.nebulaframework.grid.cluster.node.services.job.execution.TaskExecutor.java

/**
 * Creates the ClassLoader to be used for remote class loading.
 * /*from  w w w . j  a va2 s .c  o m*/
 * @param jobId JobId
 * @param classLoadingService Remote Class Loading Service Proxy
 * @param archive GridArchive, if available (or null)
 * @return ClassLoader instance
 */
private static ClassLoader createClassLoader(final String jobId, final ClassLoadingService classLoadingService,
        final GridArchive archive) {

    ClassLoader classLoader = null;

    // Configure Thread Context Class Loader to use
    // GridNodeClassLoader

    final ClassLoader nodeClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

        public ClassLoader run() {
            ClassLoader current = Thread.currentThread().getContextClassLoader();
            return new GridNodeClassLoader(jobId, classLoadingService, current);
        }
    });

    classLoader = nodeClassLoader;

    // If its an archived Job, configure to use
    // GridArchvieClassLoader
    // chained to GridNodeClassLoader
    if (archive != null) {

        ClassLoader archiveLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

            public ClassLoader run() {
                // Archive Class Loader
                return new GridArchiveClassLoader(archive, nodeClassLoader);
            }
        });
        classLoader = archiveLoader;
    }

    return classLoader;
}