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.openjpa.lib.meta.CFMetaDataParser.java

/**
 * Load the given class name against the given package and the set
 * of accepted standard packages. Return null if the class cannot be loaded.
 *//*from  w w  w .ja  va2  s . c  o  m*/
public static Class<?> classForName(String name, String pkg, boolean resolve, ClassLoader loader) {
    if (StringUtils.isEmpty(name))
        return null;

    if (loader == null)
        loader = AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
    boolean fullName = name.indexOf('.') != -1;
    boolean noPackage = StringUtils.isEmpty(pkg);
    try {
        if (fullName || noPackage)
            return Strings.toClass(name, resolve, loader);
        return Strings.toClass(pkg + "." + name, resolve, loader);
    } catch (RuntimeException re) {
    }

    // if not a full name, now try the name without a package
    if (!fullName && !noPackage) {
        try {
            return Strings.toClass(name, resolve, loader);
        } catch (RuntimeException re) {
        }
    }

    // try with standard packages
    if (!fullName) {
        for (int i = 0; i < PACKAGES.length; i++) {
            try {
                return Strings.toClass(PACKAGES[i] + name, resolve, loader);
            } catch (RuntimeException re) {
            }
        }
    }
    return null;
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Invokes <code>cls.getDeclaredFields()</code>, and returns the field
 * that matches the <code>name</code> argument.  Avoids the exception
 * thrown by <code>Class.getDeclaredField()</code> for performance reasons.
 *
 * @since 0.9.8//from   w w w .j a v  a  2s  .co m
 */
private static Field getDeclaredField(Class cls, String name) {
    Field[] fields = AccessController.doPrivileged(J2DoPrivHelper.getDeclaredFieldsAction(cls));
    for (int i = 0; i < fields.length; i++) {
        if (name.equals(fields[i].getName()))
            return fields[i];
    }
    return null;
}

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

/**
 * Copy a file. Return false if <code>from</code> does not exist.
 *//*from  ww  w  .  j a va 2  s. c o  m*/
public static boolean copy(File from, File to) throws IOException {
    if (from == null || to == null
            || !(AccessController.doPrivileged(J2DoPrivHelper.existsAction(from))).booleanValue())
        return false;

    FileInputStream in = null;
    FileOutputStream out = null;
    try {
        in = AccessController.doPrivileged(J2DoPrivHelper.newFileInputStreamAction(from));
        BufferedInputStream inbuf = new BufferedInputStream(in);
        out = AccessController.doPrivileged(J2DoPrivHelper.newFileOutputStreamAction(to));
        BufferedOutputStream outbuf = new BufferedOutputStream(out);
        for (int b; (b = inbuf.read()) != -1; outbuf.write(b))
            ;
        outbuf.flush();
        return true;
    } catch (PrivilegedActionException pae) {
        throw (FileNotFoundException) pae.getException();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (Exception e) {
            }
        if (out != null)
            try {
                out.close();
            } catch (Exception e) {
            }
    }
}

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

@Override
public ConfigurationProvider loadGlobals(ClassLoader loader) throws IOException {
    String[] prefixes = ProductDerivations.getConfigurationPrefixes();
    String rsrc = null;//from ww w .  j a  v a  2 s . c  o  m
    for (int i = 0; i < prefixes.length && StringUtils.isEmpty(rsrc); i++)
        rsrc = (String) AccessController
                .doPrivileged(J2DoPrivHelper.getPropertyAction(prefixes[i] + ".properties"));
    boolean explicit = !StringUtils.isEmpty(rsrc);
    String anchor = null;
    int idx = (!explicit) ? -1 : rsrc.lastIndexOf('#');
    if (idx != -1) {
        // separate name from <resrouce>#<name> string
        if (idx < rsrc.length() - 1)
            anchor = rsrc.substring(idx + 1);
        rsrc = rsrc.substring(0, idx);
    }
    if (StringUtils.isEmpty(rsrc))
        rsrc = RSRC_GLOBAL;
    else if (!rsrc.endsWith(".xml"))
        return null;

    ConfigurationProviderImpl cp = new ConfigurationProviderImpl();
    if (load(cp, rsrc, anchor, null, loader, explicit) == Boolean.TRUE)
        return cp;
    return null;
}

From source file:FileErrorManager.java

/**
 * Gets the location of the email store.
 *
 * @return the File location.//ww  w .  ja  v a2s. c  o m
 */
private File getEmailStore() {
    String dir = manager.getProperty(getClass().getName().concat(".pattern"));
    if (dir == null) {
        dir = AccessController.doPrivileged(new PrivilegedAction<String>() {

            @Override
            public String run() {
                return System.getProperty("java.io.tmpdir", ".");
            }
        });
    }
    return new File(dir);
}

From source file:org.apache.struts2.jasper.runtime.PageContextImpl.java

public void setAttribute(final String name, final Object o, final int scope) {

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

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

}

From source file:com.asakusafw.runtime.stage.launcher.LauncherOptionsParser.java

private URLClassLoader buildApplicationClassLoader(List<Path> libraryPaths, String applicationClassName)
        throws IOException, InterruptedException {
    List<URL> libraries = processLibraries(libraryPaths, applicationClassName);
    ClassLoader parent = configuration.getClassLoader();
    URLClassLoader application = AccessController
            .doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(
                    libraries.toArray(new URL[libraries.size()]), parent));
    this.applicationResources.add(application);
    configuration.setClassLoader(application);
    return application;
}

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

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

    final XMLInputFactory inputFactory = getXMLInputFactory(configuration);
    try {/*from w  w w .  j  ava2s.  c  o m*/
        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.nebulaframework.grid.cluster.manager.services.jobs.ClusterJobServiceImpl.java

/**
 * Creates a {@link GridNodeClassLoader} to remotely load necessary class
 * definitions./*from  w ww . ja  v a2 s .c om*/
 * 
 * @param owner
 *            Owner GridNode to load classes from
 * 
 * @return ClassLoader instance
 */
protected ClassLoader createNodeClassLoader(final UUID owner) {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

        @Override
        public ClassLoader run() {
            ClassLoadingService service = ClusterManager.getInstance().getClassLoadingService();
            return new GridNodeClassLoader(owner, service);
        }

    });
}

From source file:org.apache.jasper.runtime.PageContextImpl.java

public Object getAttribute(final String name) {

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

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

}