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:com.samczsun.helios.handler.addons.JarLauncher.java

@Override
public void run(File file) {
    JarFile jarFile = null;//  w w  w  .  j a  va 2  s  . c o m
    InputStream inputStream = null;
    try {
        System.out.println("Loading addon: " + file.getAbsolutePath());
        jarFile = new JarFile(file);
        ZipEntry entry = jarFile.getEntry("addon.json");
        if (entry != null) {
            inputStream = jarFile.getInputStream(entry);
            JsonObject jsonObject = JsonObject
                    .readFrom(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String main = jsonObject.get("main").asString();
            URL[] url = new URL[] { file.toURI().toURL() };
            ClassLoader classLoader = AccessController
                    .doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(url,
                            Helios.class.getClassLoader()));
            Class<?> clazz = Class.forName(main, true, classLoader);
            if (Addon.class.isAssignableFrom(clazz)) {
                Addon addon = Addon.class.cast(clazz.newInstance());
                registerAddon(addon.getName(), addon);
            } else {
                throw new IllegalArgumentException("Addon main does not extend Addon");
            }
        } else {
            throw new IllegalArgumentException("No addon.json found");
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        IOUtils.closeQuietly(jarFile);
        IOUtils.closeQuietly(inputStream);
    }
}

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

@Override
public SolrClient newClient(String core) {
    notNull(core, "Solr core name cannot be null");

    String clientType = AccessController
            .doPrivileged((PrivilegedAction<String>) () -> System.getProperty("solr.client", "HttpSolrClient"));
    SolrClientFactory factory;/*from w  w  w . ja  va  2  s  .  c om*/

    if ("EmbeddedSolrServer".equals(clientType)) {
        factory = new EmbeddedSolrFactory();
    } else if ("CloudSolrClient".equals(clientType)) {
        factory = new SolrCloudClientFactory();
    } else { // Use HttpSolrClient by default
        factory = httpSolrClientFactory;
    }

    return newClientFunction.apply(factory, core);
}

From source file:com.github.persapiens.jsfboot.jetty.JsfJettyServerCustomizer.java

@Override
public void customize(Server server) {
    Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
    final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];

    try {/*from   w ww .j  ava  2s.  com*/
        ClassPathResource classPathResource = new ClassPathResource(
                this.jettyProperties.getClassPathResource());
        webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString()));

        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader()));
                return null;
            }
        });

        LOGGER.info(
                "Setting Jetty classLoader to " + this.jettyProperties.getClassPathResource() + " directory");
    } catch (IOException exception) {
        LOGGER.error("Unable to configure Jetty classLoader to " + this.jettyProperties.getClassPathResource()
                + " directory " + exception.getMessage());

        throw new RuntimeException(exception);
    }
}

From source file:com.heliosdecompiler.helios.controller.addons.JarLauncher.java

@Override
public void run(File file) {
    JarFile jarFile = null;//from  w w  w  .  j  a va2s . c  o m
    InputStream inputStream = null;
    try {
        System.out.println("Loading addon: " + file.getAbsolutePath());
        jarFile = new JarFile(file);
        ZipEntry entry = jarFile.getEntry("addon.json");
        if (entry != null) {
            inputStream = jarFile.getInputStream(entry);
            JsonObject jsonObject = new Gson()
                    .fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);
            String main = jsonObject.get("main").getAsString();
            URL[] url = new URL[] { file.toURI().toURL() };
            ClassLoader classLoader = AccessController
                    .doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(url,
                            Helios.class.getClassLoader()));
            Class<?> clazz = Class.forName(main, true, classLoader);
            if (Addon.class.isAssignableFrom(clazz)) {
                Addon addon = Addon.class.cast(clazz.newInstance());
                //                    registerAddon(addon.getName(), addon);
            } else {
                throw new IllegalArgumentException("Addon main does not extend Addon");
            }
        } else {
            throw new IllegalArgumentException("No addon.json found");
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        IOUtils.closeQuietly(jarFile);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.heliosdecompiler.helios.handler.addons.JarLauncher.java

@Override
public void run(File file) {
    JarFile jarFile = null;//from ww w.  j  ava 2  s.  c om
    InputStream inputStream = null;
    try {
        System.out.println("Loading addon: " + file.getAbsolutePath());
        jarFile = new JarFile(file);
        ZipEntry entry = jarFile.getEntry("addon.json");
        if (entry != null) {
            inputStream = jarFile.getInputStream(entry);
            JsonObject jsonObject = Json.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
                    .asObject();
            String main = jsonObject.get("main").asString();
            URL[] url = new URL[] { file.toURI().toURL() };
            ClassLoader classLoader = AccessController
                    .doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(url,
                            Helios.class.getClassLoader()));
            Class<?> clazz = Class.forName(main, true, classLoader);
            if (Addon.class.isAssignableFrom(clazz)) {
                Addon addon = Addon.class.cast(clazz.newInstance());
                registerAddon(addon.getName(), addon);
            } else {
                throw new IllegalArgumentException("Addon main does not extend Addon");
            }
        } else {
            throw new IllegalArgumentException("No addon.json found");
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        IOUtils.closeQuietly(jarFile);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.triage.bytecodemaster.CachingGroovyClassLoader.java

@Override
protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su) {
    // These six lines copied from Groovy itself, with the intention to
    // return a subclass
    InnerLoader loader = AccessController.doPrivileged(new PrivilegedAction<InnerLoader>() {
        public InnerLoader run() {
            return new InnerLoader(CachingGroovyClassLoader.this);
        }//from w ww . ja  v a 2s  .c  om
    });
    return new BytecodeClassCollector(classBytes, loader, unit, su);
}

From source file:org.jboss.instrument.classloading.JBoss5ClassLoader.java

protected JBoss5ClassLoader(BaseClassLoader classLoader) {
    Assert.notNull(classLoader, "ClassLoader must not be null");
    this.classLoader = classLoader;

    try {//from  w  w  w.  ja  va2  s.  co m
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            AccessController.doPrivileged(new InstantiationAction());
        } else {
            doInstantiate();
        }
    } catch (Exception e) {
        throw new IllegalStateException(
                "Could not initialize JBoss ClassLoader because JBoss5 API classes are not available", e);
    }
}

From source file:org.apache.openjpa.lib.util.Files.java

/**
 * Revert the given backup file to the original location. If the given
 * file's name does not end in '~', the '~' is appended before proceeding.
 * If the backup file does not exist or could not be reverted, returns null.
 *///  w  ww  . j  a  v a 2  s .  co m
public static File revert(File backup, boolean copy) {
    if (backup == null)
        return null;
    if (!backup.getName().endsWith("~"))
        backup = new File(backup.getPath() + "~");
    if (!(AccessController.doPrivileged(J2DoPrivHelper.existsAction(backup))).booleanValue())
        return null;

    // create new file object copy so we don't modify the original
    String path = AccessController.doPrivileged(J2DoPrivHelper.getAbsolutePathAction(backup));
    File clone = new File(path);
    File orig = new File(path.substring(0, path.length() - 1));
    if (!(AccessController.doPrivileged(J2DoPrivHelper.renameToAction(clone, orig))).booleanValue())
        return null;
    if (copy) {
        try {
            copy(orig, backup);
        } catch (IOException ioe) {
            throw new NestableRuntimeException(ioe);
        }
    }
    return orig;
}

From source file:tech.beshu.ror.httpclient.ApacheHttpCoreClient.java

public ApacheHttpCoreClient(ESContext esContext, boolean validate) {
    AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
        this.hcHttpClient = validate ? HttpAsyncClients.createDefault() : getNonValidatedHttpClient();
        this.hcHttpClient.start();
        return null;
    });//from w  w  w .  ja v  a 2  s  . c o m
    this.logger = esContext.logger(getClass());
    this.context = esContext;
    esContext.getShutDownObservable().addObserver((x, y) -> {
        try {
            hcHttpClient.close();
        } catch (IOException e) {
            logger.error("cannot shut down Apache HTTP Core client.. ", e);
        }
    });
}

From source file:SecuritySupport.java

static InputStream getResourceAsStream(final ClassLoader cl, final String name) {
    return (InputStream) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            InputStream ris;//from w  ww .  j  av a 2  s  . c  o m
            if (cl == null) {
                ris = ClassLoader.getSystemResourceAsStream(name);
            } else {
                ris = cl.getResourceAsStream(name);
            }
            return ris;
        }
    });
}