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.bsf.BSFManager.java

/**
 * Evaluate the given expression of the given language and return the
 * resulting value./*  w  ww  . j a va2 s .  c  o m*/
 *
 * @param lang language identifier
 * @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 expr the expression to evaluate
 *
 * @exception BSFException if anything goes wrong while running the script
 */
public Object eval(String lang, String source, int lineNo, int columnNo, Object expr) throws BSFException {
    logger.debug("BSFManager:eval");

    final BSFEngine e = loadScriptingEngine(lang);
    final String sourcef = source;
    final int lineNof = lineNo, columnNof = columnNo;
    final Object exprf = expr;
    Object result = null;

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

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

    return result;
}

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

public Enumeration getAttributeNamesInScope(final int scope) {
    if (System.getSecurityManager() != null) {
        return (Enumeration) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return doGetAttributeNamesInScope(scope);
            }//from  ww  w  .ja va 2  s. c  o  m
        });
    } else {
        return doGetAttributeNamesInScope(scope);
    }
}

From source file:org.apache.catalina.session.PersistentManagerBase.java

/**
 * Clear all sessions from the Store.//w  w  w.  j a va 2 s  . co  m
 */
public void clearStore() {

    if (store == null)
        return;

    try {
        if (System.getSecurityManager() != null) {
            try {
                AccessController.doPrivileged(new PrivilegedStoreClear());
            } catch (PrivilegedActionException ex) {
                Exception exception = ex.getException();
                log.error("Exception clearing the Store: " + exception);
                exception.printStackTrace();
            }
        } else {
            store.clear();
        }
    } catch (IOException e) {
        log.error("Exception clearing the Store: " + e);
        e.printStackTrace();
    }

}

From source file:de.betterform.agent.betty.AppletProcessor.java

private void ensureContextClassLoader() {
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
            return null;
        }//from w w  w  . j a  v  a 2  s.c om
    });
}

From source file:com.asakusafw.runtime.util.hadoop.ConfigurationProvider.java

private static ClassLoader createLoader(ClassLoader current, URL defaultConfigPath) {
    assert current != null;
    if (defaultConfigPath == null) {
        return current;
    }//from  www  .j a v  a 2 s. co m
    ClassLoader cached = null;
    String configPath = defaultConfigPath.toExternalForm();
    synchronized (CACHE_CLASS_LOADER) {
        ClassLoaderHolder holder = CACHE_CLASS_LOADER.get(current);
        if (holder != null) {
            cached = holder.get();
            if (cached != null && holder.configPath.equals(configPath)) {
                return cached;
            }
        }
    }
    ClassLoader ehnahced = AccessController.doPrivileged(
            (PrivilegedAction<ClassLoader>) () -> new URLClassLoader(new URL[] { defaultConfigPath }, current));
    synchronized (CACHE_CLASS_LOADER) {
        CACHE_CLASS_LOADER.put(current, new ClassLoaderHolder(ehnahced, configPath));
    }
    return ehnahced;
}

From source file:groovy.ui.GroovyMain.java

private static void setupContextClassLoader(GroovyShell shell) {
    final Thread current = Thread.currentThread();
    class DoSetContext implements PrivilegedAction {
        ClassLoader classLoader;/*from   w  w w.  j  a  va  2  s  . co m*/

        public DoSetContext(ClassLoader loader) {
            classLoader = loader;
        }

        public Object run() {
            current.setContextClassLoader(classLoader);
            return null;
        }
    }

    AccessController.doPrivileged(new DoSetContext(shell.getClassLoader()));
}

From source file:org.apache.openjpa.meta.AbstractCFMetaDataFactory.java

/**
 * Assign default source files to the given queries.
 *
 * @param clsNames map of class names to metadatas
 * @return set of existing files used by these metadatas, or
 * null if no existing files//from   ww  w. ja  va 2s. c  o m
 */
private Set assignDefaultQueryFiles(QueryMetaData[] queries, Map clsNames) {
    Set files = null;
    for (int i = 0; i < queries.length; i++) {
        if (queries[i].getSourceMode() != MODE_QUERY)
            continue;
        if (queries[i].getSourceFile() == null) {
            File defaultFile = defaultSourceFile(queries[i], clsNames);
            queries[i].setSource(defaultFile, queries[i].getSourceScope(), queries[i].getSourceType(),
                    defaultFile == null ? "" : defaultFile.getPath());
        }
        if ((AccessController.doPrivileged(J2DoPrivHelper.existsAction(queries[i].getSourceFile())))
                .booleanValue()) {
            if (files == null)
                files = new HashSet();
            files.add(queries[i].getSourceFile());
        }
    }
    return files;
}

From source file:org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.java

/**
 * Cancel waiting due to timeout.//from  w  w w .j a v  a 2 s .c om
 */
private void timeout() {
    ApplicationContextException e;
    List<OsgiServiceDependencyEvent> events = null;
    String filterAsString = null;

    synchronized (monitor) {
        // deregister listener to get an accurate snapshot of the
        // unsatisfied dependencies.

        if (dependencyDetector != null) {
            dependencyDetector.deregister();
            events = dependencyDetector.getUnsatisfiedDependenciesAsEvents();
            filterAsString = dependencyDetector.createUnsatisfiedDependencyFilter();
        }
    }

    Filter filter = (filterAsString != null ? OsgiFilterUtils.createFilter(filterAsString) : null);

    log.warn("Timeout occurred before finding service dependencies for [" + delegateContext.getDisplayName()
            + "]");

    String bundleName = null;
    if (System.getSecurityManager() != null) {
        bundleName = AccessController.doPrivileged(new PrivilegedAction<String>() {
            public String run() {
                return OsgiStringUtils.nullSafeSymbolicName(getBundle());
            }
        });
    } else {
        bundleName = OsgiStringUtils.nullSafeSymbolicName(getBundle());
    }

    // generate exception
    e = new ApplicationContextException("Application context " + "initialization for '" + bundleName
            + "' has timed out waiting for " + filterAsString);
    e.fillInStackTrace();

    // send notification
    delegatedMulticaster.multicastEvent(new BootstrappingDependenciesFailedEvent(delegateContext,
            delegateContext.getBundle(), e, events, filter));

    fail(e, true);
}

From source file:org.apache.openjpa.util.ProxyManagerImpl.java

/**
 * Return the cached factory proxy for the given bean type.
 *//*from   w  w w.  j av  a  2 s .  c om*/
private ProxyBean getFactoryProxyBean(Object orig) {
    final Class type = orig.getClass();
    if (isUnproxyable(type))
        return null;

    // we don't lock here; ok if two proxies get generated for same type
    ProxyBean proxy = (ProxyBean) _proxies.get(type);
    if (proxy == null && !_proxies.containsKey(type)) {
        ClassLoader l = GeneratedClasses.getMostDerivedLoader(type, ProxyBean.class);
        Class pcls = loadBuildTimeProxy(type, l);
        if (pcls == null) {
            // TODO Move this to J2DOPrivHelper? 
            BCClass bc = AccessController.doPrivileged(new PrivilegedAction<BCClass>() {
                public BCClass run() {
                    return generateProxyBeanBytecode(type, true);
                }
            });
            if (bc != null)
                pcls = GeneratedClasses.loadBCClass(bc, l);
        }
        if (pcls != null)
            proxy = (ProxyBean) instantiateProxy(pcls, findCopyConstructor(type), new Object[] { orig });
        _proxies.put(type, proxy);
    }
    return proxy;
}

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

/**
 * Gets the fields that are possible candidate for being persisted. The  
 * result depends on the current access style of the given class. 
 *//*from   www . j a va  2s . c  om*/
List<Field> getPersistentFields(ClassMetaData meta, boolean ignoreTransient) {
    boolean explicit = meta.isExplicitAccess();
    boolean unknown = AccessCode.isUnknown(meta);
    boolean isField = AccessCode.isField(meta);

    if (explicit || unknown || isField) {
        Field[] fields = AccessController
                .doPrivileged(J2DoPrivHelper.getDeclaredFieldsAction(meta.getDescribedType()));

        return filter(fields, fieldFilter, ignoreTransient ? null : nonTransientFilter,
                unknown || isField ? null : annotatedFilter,
                explicit ? (isField ? null : fieldAccessFilter) : null);
    }
    return Collections.EMPTY_LIST;
}