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.solmix.runtime.support.spring.ContainerApplicationContext.java

static String getSpringValidationMode() {
    return AccessController.doPrivileged(new PrivilegedAction<String>() {

        @Override//from  www .  j  a  v a2  s  .com
        public String run() {
            String mode = System.getProperty("solmix.spring.validation.mode");
            if (mode == null) {
                mode = System.getProperty("spring.validation.mode");
            }
            return mode;
        }
    });
}

From source file:org.apache.openjpa.lib.meta.ClassArgParser.java

/**
 * Return the names of the class(es) from the given arg.
 *
 * @param arg a class name, .java file, .class file, or metadata
 * file naming the type(s) to act on/*ww w  .  j  a va 2 s .c  om*/
 * @throws IllegalArgumentException with appropriate message on error
 */
public String[] parseTypeNames(String arg) {
    if (arg == null)
        return new String[0];

    try {
        File file = Files.getFile(arg, _loader);
        if (arg.endsWith(".class"))
            return new String[] { getFromClassFile(file) };
        if (arg.endsWith(".java"))
            return new String[] { getFromJavaFile(file) };
        if ((AccessController.doPrivileged(J2DoPrivHelper.existsAction(file))).booleanValue()) {
            Collection<String> col = getFromMetaDataFile(file);
            return col.toArray(new String[col.size()]);
        }
    } catch (Exception e) {
        throw new NestableRuntimeException(_loc.get("class-arg", arg).getMessage(), e);
    }

    // must be a class name
    return new String[] { arg };
}

From source file:org.apache.openjpa.jdbc.ant.ReverseMappingToolTask.java

protected void executeOn(String[] files) throws Exception {
    ClassLoader loader = getClassLoader();
    if (!StringUtils.isEmpty(dirName))
        flags.directory = Files.getFile(dirName, loader);
    if (!StringUtils.isEmpty(typeMap))
        flags.typeMap = Configurations.parseProperties(typeMap);

    // load customizer properties
    Properties customProps = new Properties();
    File propsFile = Files.getFile(customizerProperties, loader);
    if (propsFile != null
            && (AccessController.doPrivileged(J2DoPrivHelper.existsAction(propsFile))).booleanValue()) {
        FileInputStream fis = null;
        try {/*from  ww w. ja  va2s  .  c  o m*/
            fis = AccessController.doPrivileged(J2DoPrivHelper.newFileInputStreamAction(propsFile));
        } catch (PrivilegedActionException pae) {
            throw (FileNotFoundException) pae.getException();
        }
        customProps.load(fis);
    }

    // create and configure customizer
    JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
    flags.customizer = (ReverseCustomizer) Configurations.newInstance(customizerClass, conf, (String) null,
            AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(ReverseCustomizer.class)));
    if (flags.customizer != null)
        flags.customizer.setConfiguration(customProps);

    ReverseMappingTool.run(conf, files, flags, loader);
}

From source file:com.reelfx.controller.WindowsController.java

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

        @Override/*  w  w w  .j a va  2 s . c  om*/
        public Object run() {
            try {
                if (MERGED_OUTPUT_FILE.exists() && !MERGED_OUTPUT_FILE.delete())
                    throw new Exception("Can't delete the old preview file on Windows!");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    });
}

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

/**
 * Apply the given anonymous function of the given language to the given
 * parameters and return the resulting value.
 *
 * @param lang language identifier/*from  w  w w  .ja va2s .c o m*/
 * @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
 *
 * @exception BSFException if anything goes wrong while running the script
 */
public Object apply(String lang, String source, int lineNo, int columnNo, Object funcBody, Vector paramNames,
        Vector arguments) throws BSFException {
    logger.debug("BSFManager:apply");

    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;
    Object result = null;

    try {
        final Object resultf = AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws Exception {
                return e.apply(sourcef, lineNof, columnNof, funcBodyf, paramNamesf, argumentsf);
            }
        });
        result = resultf;
    } catch (PrivilegedActionException prive) {

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

    return result;
}

From source file:org.codice.ddf.security.ocsp.checker.OcspChecker.java

/**
 * Returns an {@link X509CertificateHolder} containing the issuer of the passed in {@param cert}.
 * Search is performed in the system truststore.
 *
 * @param cert - the {@link Certificate} to get the issuer from.
 * @return {@link X509CertificateHolder} containing the issuer of the passed in {@param cert}.
 * @throws OcspCheckerException if the issuer cannot be resolved.
 *//*from w  w  w.j av  a  2s  .c om*/
private X509CertificateHolder resolveIssuerCertificate(Certificate cert) throws OcspCheckerException {
    X500Name issuerName = cert.getIssuer();

    String trustStorePath = AccessController
            .doPrivileged((PrivilegedAction<String>) SecurityConstants::getTruststorePath);
    String trustStorePass = AccessController
            .doPrivileged((PrivilegedAction<String>) SecurityConstants::getTruststorePassword);

    if (isBlank(trustStorePath) || isBlank(trustStorePass)) {
        throw new OcspCheckerException("Problem retrieving truststore properties." + NOT_VERIFIED_MSG);
    }

    KeyStore truststore;

    try (InputStream truststoreInputStream = new FileInputStream(trustStorePath)) {
        truststore = SecurityConstants.newTruststore();
        truststore.load(truststoreInputStream, trustStorePass.toCharArray());
        SecurityLogger.audit("Truststore on path {} was read by {}.", trustStorePath,
                this.getClass().getSimpleName());
    } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException e) {
        throw new OcspCheckerException(String.format("Problem loading truststore on path %s", trustStorePath),
                e);
    }

    try {
        return getCertFromTruststoreWithX500Name(issuerName, truststore);
    } catch (OcspCheckerException e) {
        throw new OcspCheckerException(
                "Problem finding the certificate issuer in truststore." + NOT_VERIFIED_MSG, e);
    }
}

From source file:ddf.catalog.source.solr.rest.SolrRest.java

private String getUsername() {
    return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("solr.username"));
}

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

public static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, final InputStream in,
        final String encoding) throws XMLStreamException {

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

From source file:org.apache.hadoop.hbase.util.CoprocessorClassLoader.java

/**
 * Get a CoprocessorClassLoader for a coprocessor jar path from cache.
 * If not in cache, create one./*from ww  w.  j a  va2 s  . co  m*/
 *
 * @param path the path to the coprocessor jar file to load classes from
 * @param parent the parent class loader for exempted classes
 * @param pathPrefix a prefix used in temp path name to store the jar file locally
 * @param conf the configuration used to create the class loader, if needed
 * @return a CoprocessorClassLoader for the coprocessor jar path
 * @throws IOException
 */
public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent,
        final String pathPrefix, final Configuration conf) throws IOException {
    CoprocessorClassLoader cl = getIfCached(path);
    String pathStr = path.toString();
    if (cl != null) {
        LOG.debug("Found classloader " + cl + " for " + pathStr);
        return cl;
    }

    if (!pathStr.endsWith(".jar")) {
        throw new IOException(pathStr + ": not a jar file?");
    }

    Lock lock = locker.acquireLock(pathStr);
    try {
        cl = getIfCached(path);
        if (cl != null) {
            LOG.debug("Found classloader " + cl + " for " + pathStr);
            return cl;
        }

        cl = AccessController.doPrivileged(new PrivilegedAction<CoprocessorClassLoader>() {
            @Override
            public CoprocessorClassLoader run() {
                return new CoprocessorClassLoader(parent);
            }
        });

        cl.init(path, pathPrefix, conf);

        // Cache class loader as a weak value, will be GC'ed when no reference left
        CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl);
        if (prev != null) {
            // Lost update race, use already added class loader
            LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" + " is already cached for " + pathStr);
            cl = prev;
        }
        return cl;
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.openjpa.persistence.PersistenceProductDerivation.java

@Override
public List getAnchorsInResource(String resource) throws Exception {
    ConfigurationParser parser = new ConfigurationParser(null);
    try {/*  w  w w .  j  a va2 s .c o m*/
        ClassLoader loader = (ClassLoader) AccessController
                .doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
        List<URL> urls = getResourceURLs(resource, loader);
        if (urls != null) {
            for (URL url : urls) {
                parser.parse(url);
            }
        }
        return getUnitNames(parser);
    } catch (IOException e) {
        // not all configuration files are XML; return null if unparsable
        return null;
    }
}