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.netspective.commons.io.UriAddressableUniqueFileLocator.java

/**
 * Creates a new file resource locator that will use the specified directory
 * as the base directory for loading templates.
 *
 * @param baseDir the base directory for loading templates
 *//* w  ww  .ja v  a2 s  . c o  m*/
public UriAddressableUniqueFileLocator(final String rootUrl, final File baseDir, final boolean cacheLocations)
        throws IOException {
    this.rootUrl = rootUrl;
    this.cacheLocations = cacheLocations;
    try {
        Object[] retval = (Object[]) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws IOException {
                if (!baseDir.exists()) {
                    throw new FileNotFoundException(baseDir + " does not exist.");
                }
                if (!baseDir.isDirectory()) {
                    throw new IOException(baseDir + " is not a directory.");
                }
                Object[] retval = new Object[2];
                retval[0] = baseDir.getCanonicalFile();
                retval[1] = ((File) retval[0]).getPath() + File.separatorChar;
                return retval;
            }
        });
        this.baseDir = (File) retval[0];
        this.canonicalPath = (String) retval[1];
    } catch (PrivilegedActionException e) {
        throw (IOException) e.getException();
    }
}

From source file:org.solmix.runtime.support.spring.ContainerApplicationContext.java

public ContainerApplicationContext(String[] cfgFiles, ApplicationContext parent, boolean includeDefault,
        NamespaceHandlerResolver nshResolver) {
    super(new String[0], false, parent);
    this.cfgFiles = cfgFiles;
    this.nshResolver = nshResolver;
    this.includeDefault = includeDefault;
    try {//from  ww w .  j  a  v  a  2 s .com
        AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {

            @Override
            public Boolean run() throws Exception {
                refresh();
                return Boolean.TRUE;
            }

        });
    } catch (PrivilegedActionException e) {
        if (e.getException() instanceof RuntimeException) {
            throw (RuntimeException) e.getException();
        }
    }
}

From source file:org.castor.jaxb.CastorJAXBContextFactory.java

/**
 * Registers the {@link CastorJAXBContextFactory} as the default JAXB provider.
 *//*from   w w  w. ja va 2 s  .c om*/
public static void registerContextFactory() {

    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        public Object run() {
            System.setProperty(JAXBCONTEXT_PROPERTY_NAME, CASTOR_JAXBCONTEXT_FACTORY);
            return null;
        }
    });
}

From source file:com.marmalade.studio.android.gcm.s3eGCMClientBroadcastReceiver.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void doRegistrationCallback(String reg_id) {

    try {//from w w  w  . ja  v  a 2s. c  o m

        // Get context
        final Context context = m_Context;

        // Get registration identifier
        final String registration_id = reg_id;

        // Get extension class
        final Class extension_class = Class.forName("s3eGCMClient");

        // Get registration method
        final Method registration_method = extension_class.getMethod("s3eGCMClientRegistrationReceived",
                new Class[] { Context.class, String.class });

        // Access method
        AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws Exception {

                // Set accessible
                if (!registration_method.isAccessible()) {
                    registration_method.setAccessible(true);
                }

                // Invoke
                registration_method.invoke(extension_class.newInstance(), context, registration_id);

                return null;
            }
        });

    } catch (Exception e) {

        // Do nothing
        // e.printStackTrace();

    }
}

From source file:org.eclipse.gemini.blueprint.extender.internal.blueprint.event.EventAdminDispatcher.java

public void beforeRefresh(final BlueprintEvent event) {
    if (dispatcher != null) {
        try {//from   w w w .  j a  va 2 s  . c o  m
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    public Object run() {
                        dispatcher.beforeRefresh(event);
                        return null;
                    }
                });
            } else {
                dispatcher.beforeRefresh(event);
            }
        } catch (Throwable th) {
            log.warn("Cannot dispatch event " + event, th);
        }
    }
}

From source file:org.apache.openjpa.jdbc.schema.DataSourceFactory.java

/**
 * Create a datasource using the given configuration.
 *//*from  w w w .  ja va 2s .com*/
public static DataSource newDataSource(JDBCConfiguration conf, boolean factory2) {
    String driver = (factory2) ? conf.getConnection2DriverName() : conf.getConnectionDriverName();
    if (StringUtils.isEmpty(driver))
        throw new UserException(_loc.get("no-driver", conf)).setFatal(true);

    ClassLoader loader = conf.getClassResolverInstance().getClassLoader(DataSourceFactory.class, null);
    String props = (factory2) ? conf.getConnection2Properties() : conf.getConnectionProperties();
    try {
        Class<?> driverClass;
        try {
            driverClass = Class.forName(driver, true, loader);
        } catch (ClassNotFoundException cnfe) {
            // try with the core class loader
            driverClass = Class.forName(driver);
        }

        if (Driver.class.isAssignableFrom(driverClass)) {
            DriverDataSource ds = conf.newDriverDataSourceInstance();
            ds.setClassLoader(loader);
            ds.setConnectionDriverName(driver);
            ds.setConnectionProperties(Configurations.parseProperties(props));

            if (!factory2) {
                ds.setConnectionFactoryProperties(
                        Configurations.parseProperties(conf.getConnectionFactoryProperties()));
                ds.setConnectionURL(conf.getConnectionURL());
                ds.setConnectionUserName(conf.getConnectionUserName());
                ds.setConnectionPassword(conf.getConnectionPassword());
            } else {
                ds.setConnectionFactoryProperties(
                        Configurations.parseProperties(conf.getConnectionFactory2Properties()));
                ds.setConnectionURL(conf.getConnection2URL());
                ds.setConnectionUserName(conf.getConnection2UserName());
                ds.setConnectionPassword(conf.getConnection2Password());
            }
            return ds;
        }

        // see if their driver name is actually a data source
        if (DataSource.class.isAssignableFrom(driverClass)) {
            return (DataSource) Configurations.newInstance(driver, conf, props,
                    AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(DataSource.class)));
        }
    } catch (OpenJPAException ke) {
        throw ke;
    } catch (Exception e) {
        throw newConnectException(conf, factory2, e);
    }

    // not a driver or a data source; die
    throw new UserException(_loc.get("bad-driver", driver)).setFatal(true);
}

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

private static SSLContext getSslContext() {
    final Boolean check = AccessController
            .doPrivileged((PrivilegedAction<Boolean>) () -> (System.getProperty(KEY_STORE) == null
                    || System.getProperty(KEY_STORE_PASS) == null || System.getProperty(TRUST_STORE) == null
                    || System.getProperty(TRUST_STORE_PASS) == null));

    if (check) {/*w ww. j  ava  2s  . co  m*/
        throw new IllegalArgumentException("KeyStore and TrustStore system properties must be set.");
    }

    final KeyStore[] trustStore = new KeyStore[1];
    final KeyStore[] keyStore = new KeyStore[1];

    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        trustStore[0] = getKeyStore(System.getProperty(TRUST_STORE), System.getProperty(TRUST_STORE_PASS));
        keyStore[0] = getKeyStore(System.getProperty(KEY_STORE), System.getProperty(KEY_STORE_PASS));
        return null;
    });

    SSLContext sslContext = null;

    try {
        sslContext = SSLContexts.custom().loadKeyMaterial(keyStore[0],
                AccessController
                        .doPrivileged((PrivilegedAction<String>) () -> System.getProperty(KEY_STORE_PASS))
                        .toCharArray())
                .loadTrustMaterial(trustStore[0]).useTLS().build();
        sslContext.getDefaultSSLParameters().setNeedClientAuth(true);
        sslContext.getDefaultSSLParameters().setWantClientAuth(true);
    } catch (UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException
            | KeyManagementException e) {
        throw new IllegalArgumentException(
                "Unable to use javax.net.ssl.keyStorePassword to load key material to create SSL context for Solr client.");
    }

    return sslContext;
}

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

private Boolean isConfiguredForBasicAuth() {
    return AccessController.doPrivileged(
            (PrivilegedAction<Boolean>) () -> Boolean.valueOf(System.getProperty("solr.useBasicAuth")));
}

From source file:org.eclipse.gemini.blueprint.test.AbstractOnTheFlyBundleCreatorTests.java

private void initializeJarCreator() {
    AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            jarCreator = new JarCreator();
            return null;
        }/* w w  w .  j  a  v  a  2  s .c  o  m*/
    });
}

From source file:org.apache.hadoop.hbase.tool.coprocessor.CoprocessorValidator.java

private ResolverUrlClassLoader createClassLoader(URL[] urls, ClassLoader parent) {
    return AccessController.doPrivileged(new PrivilegedAction<ResolverUrlClassLoader>() {
        @Override/*from   w w  w  .j  a  v  a2s .co  m*/
        public ResolverUrlClassLoader run() {
            return new ResolverUrlClassLoader(urls, parent);
        }
    });
}