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.eclipse.gemini.blueprint.test.AbstractDependencyManagerTests.java

/**
 * {@inheritDoc}//from ww  w  .  j av a  2s .  c  o  m
 * 
 * <p/>Sets specific log4j property to avoid class loading problems during
 * start up related to the thread context class loader.
 */
protected void preProcessBundleContext(BundleContext platformBundleContext) throws Exception {
    AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            System.setProperty("log4j.ignoreTCL", "true");
            return null;
        }
    });

    super.preProcessBundleContext(platformBundleContext);
}

From source file:org.acoveo.tools.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 2 s  .  c o m
 */
private static Field getDeclaredField(Class cls, String name) {
    Field[] fields = AccessController.doPrivileged(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.kernel.ResultPacker.java

/**
 * Pack the given result into the user-defined result class.
 *//*from  w w  w  .  ja v a 2 s. c  o m*/
private Object packUserType(Object[] result) {
    try {
        // use the constructor first, if we have one
        if (_constructor != null)
            return _constructor.newInstance(result);

        Object user = AccessController.doPrivileged(J2DoPrivHelper.newInstanceAction(_resultClass));
        for (int i = 0; i < _aliases.length; i++) {
            if (_sets[i] instanceof Method) {
                Method meth = (Method) _sets[i];
                meth.invoke(user, new Object[] { Filters.convert(result[i], meth.getParameterTypes()[0]) });
            } else if (_sets[i] instanceof Field) {
                Field field = (Field) _sets[i];
                field.set(user, Filters.convert(result[i], field.getType()));
            } else if (_put != null) {
                _put.invoke(user, new Object[] { _aliases[i], result[i] });
            }
        }
        return user;
    } catch (OpenJPAException ke) {
        throw ke;
    } catch (PrivilegedActionException pae) {
        throw new UserException(_loc.get("pack-instantiation-err", _resultClass), pae.getException());
    } catch (InstantiationException ie) {
        throw new UserException(_loc.get("pack-instantiation-err", _resultClass), ie);
    } catch (Exception e) {
        throw new UserException(_loc.get("pack-err", _resultClass), e);
    }
}

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

private static List<URL> getResourceURLs(String rsrc, ClassLoader loader) throws IOException {
    List<URL> answer = null;
    if (RSRC_DEFAULT.equals(rsrc) && defaultPersistenceFiles != null) {
        answer = new ArrayList();
        answer.addAll(defaultPersistenceFiles);
    }/*  w  w w .ja va 2s.c om*/
    Enumeration<URL> urls = null;
    try {
        urls = (Enumeration) AccessController.doPrivileged(J2DoPrivHelper.getResourcesAction(loader, rsrc));
        if (!urls.hasMoreElements()) {
            if (!rsrc.startsWith("META-INF"))
                urls = (Enumeration) AccessController
                        .doPrivileged(J2DoPrivHelper.getResourcesAction(loader, "META-INF/" + rsrc));
        }
    } catch (PrivilegedActionException pae) {
        throw (IOException) pae.getException();
    }

    if (urls.hasMoreElements()) {
        if (answer == null) {
            answer = Collections.list(urls);
        } else {
            answer.addAll(Collections.list(urls));
        }
    }

    return answer;
}

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

static ClassLoader classLoaderOf(Class<?> cls) {
    return AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(cls));
}

From source file:org.nebulaframework.grid.cluster.manager.services.jobs.ClusterJobServiceImpl.java

protected ClassLoader createArchiveClassLoader(final GridArchive archive, final UUID owner) {

    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

        @Override/*from   w  w  w.  j  a  v a  2s  .c  o  m*/
        public ClassLoader run() {
            return new GridArchiveClassLoader(archive, createNodeClassLoader(owner));
        }

    });
}

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

static ClassLoader parentClassLoaderOf(ClassLoader loader) {
    return AccessController.doPrivileged(J2DoPrivHelper.getParentAction(loader));
}

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

/**
 * Load given file, or return false if it is not a file this provider
 * understands./* w w w.  ja v a 2  s  .co  m*/
 *
 * @param anchor optional named anchor within a multiple-configuration file
 */
public static ConfigurationProvider load(File file, String anchor, ClassLoader loader) {
    if (file == null)
        return null;
    if (loader == null)
        loader = AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
    ConfigurationProvider provider = null;
    StringBuilder errs = null;
    Throwable err = null;
    // most specific to least
    for (int i = _derivations.length - 1; i >= 0; i--) {
        try {
            provider = _derivations[i].load(file, anchor);
            if (provider != null)
                return provider;
        } catch (Throwable t) {
            err = t;
            errs = (errs == null) ? new StringBuilder() : errs.append("\n");
            errs.append(_derivations[i].getClass().getName() + ":" + t);
        }
    }
    String aPath = AccessController.doPrivileged(J2DoPrivHelper.getAbsolutePathAction(file));
    reportErrors(errs, aPath, err);
    String rsrc = aPath + "#" + anchor;
    MissingResourceException ex = new MissingResourceException(rsrc, ProductDerivations.class.getName(), rsrc);
    ex.initCause(err);
    throw ex;
}

From source file:org.eclipse.gemini.blueprint.io.OsgiBundleResourcePatternResolver.java

/**
 * Special classpath method. Will try to detect the imported bundles (which are part of the classpath) and look for
 * resources in all of them. This implementation will try to determine the bundles that compose the current bundle
 * classpath and then it will inspect the bundle space of each of them individually.
 * //from   w  ww .ja  v  a 2  s. co m
 * <p/> Since the bundle space is considered, runtime classpath entries such as dynamic imports are not supported
 * (yet).
 * 
 * @param locationPattern
 * @param type
 * @return classpath resources
 */
@SuppressWarnings("unchecked")
private Resource[] findClassPathMatchingResources(String locationPattern, int type) throws IOException {

    if (resolver == null)
        throw new IllegalArgumentException(
                "PackageAdmin service/a started bundle is required for classpath matching");

    final ImportedBundle[] importedBundles = resolver.getImportedBundles(bundle);

    // eliminate classpath path
    final String path = OsgiResourceUtils.stripPrefix(locationPattern);

    final Collection<String> foundPaths = new LinkedHashSet<String>();

    // 1. search the imported packages

    // find folder path matching
    final String rootDirPath = determineFolderPattern(path);

    if (System.getSecurityManager() != null) {
        try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                public Object run() throws IOException {
                    for (int i = 0; i < importedBundles.length; i++) {
                        final ImportedBundle importedBundle = importedBundles[i];
                        if (!bundle.equals(importedBundle.getBundle())) {
                            findImportedBundleMatchingResource(importedBundle, rootDirPath, path, foundPaths);
                        }
                    }
                    return null;
                }
            });
        } catch (PrivilegedActionException pe) {
            throw (IOException) pe.getException();
        }
    } else {
        for (int i = 0; i < importedBundles.length; i++) {
            final ImportedBundle importedBundle = importedBundles[i];
            if (!bundle.equals(importedBundle.getBundle())) {
                findImportedBundleMatchingResource(importedBundle, rootDirPath, path, foundPaths);
            }
        }
    }

    // 2. search the target bundle
    findSyntheticClassPathMatchingResource(bundle, path, foundPaths);

    // 3. resolve the entries using the official class-path method (as some of them might be hidden)
    List<Resource> resources = new ArrayList<Resource>(foundPaths.size());

    for (String resourcePath : foundPaths) {
        // classpath*: -> getResources()
        if (OsgiResourceUtils.PREFIX_TYPE_CLASS_ALL_SPACE == type) {
            CollectionUtils.mergeArrayIntoCollection(
                    convertURLEnumerationToResourceArray(bundle.getResources(resourcePath), resourcePath),
                    resources);
        }
        // classpath -> getResource()
        else {
            URL url = bundle.getResource(resourcePath);
            if (url != null)
                resources.add(new UrlContextResource(url, resourcePath));
        }
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Fitered " + foundPaths + " to " + resources);
    }

    return (Resource[]) resources.toArray(new Resource[resources.size()]);
}

From source file:de.tuberlin.uebb.jbop.access.ClassAccessor.java

private static <T> T executePrivileged(final PrivilegedAction<T> action) throws JBOPClassException {
    try {//from  w  w w .  ja va  2 s. c om
        return AccessController.doPrivileged(action);
    } catch (final RuntimeException re) {
        throw new JBOPClassException(re.getMessage(), re.getCause());
    }
}