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, AccessControlContext context)
        throws PrivilegedActionException 

Source Link

Document

Performs the specified PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext .

Usage

From source file:org.eclipse.gemini.blueprint.service.exporter.support.OsgiServiceFactoryBean.java

/**
 * Registration method./*from   w w  w.  j  av a2s . c o m*/
 * 
 * @param classes
 * @param serviceProperties
 * @return the ServiceRegistration
 */
ServiceRegistration registerService(Class<?>[] classes, final Dictionary serviceProperties) {
    Assert.notEmpty(classes, "at least one class has to be specified for exporting "
            + "(if autoExport is enabled then maybe the object doesn't implement any interface)");

    // create an array of classnames (used for registering the service)
    final String[] names = ClassUtils.toStringArray(classes);
    // sort the names in alphabetical order (eases debugging)
    Arrays.sort(names);

    log.info("Publishing service under classes [" + ObjectUtils.nullSafeToString(names) + "]");

    ServiceFactory serviceFactory = new PublishingServiceFactory(resolver, classes,
            (ExportContextClassLoaderEnum.SERVICE_PROVIDER.equals(contextClassLoader)), classLoader,
            aopClassLoader, bundleContext);

    if (isBeanBundleScoped())
        serviceFactory = new OsgiBundleScope.BundleScopeServiceFactory(serviceFactory);

    if (System.getSecurityManager() != null) {
        AccessControlContext acc = SecurityUtils.getAccFrom(beanFactory);
        final ServiceFactory serviceFactoryFinal = serviceFactory;
        return AccessController.doPrivileged(new PrivilegedAction<ServiceRegistration>() {
            public ServiceRegistration run() {
                return bundleContext.registerService(names, serviceFactoryFinal, serviceProperties);
            }
        }, acc);
    } else {
        return bundleContext.registerService(names, serviceFactory, serviceProperties);
    }
}

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

protected void register() {
    final String filter = createDependencyFilter();
    if (log.isDebugEnabled()) {
        log.debug(context.getDisplayName()
                + " has registered service dependency dependencyDetector with filter: " + filter);
    }/*from   w  ww. j  a  va  2  s.c o m*/

    // send dependency event before registering the filter
    sendInitialBootstrappingEvents(getUnsatisfiedDependencies().keySet());

    if (System.getSecurityManager() != null) {
        AccessControlContext acc = getAcc();
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                OsgiListenerUtils.addServiceListener(bundleContext, listener, filter);
                return null;
            }
        }, acc);
    } else {
        OsgiListenerUtils.addServiceListener(bundleContext, listener, filter);
    }
}

From source file:azkaban.execapp.FlowRunner.java

private boolean evaluateExpression(final String expression) {
    boolean result = false;
    final ScriptEngineManager sem = new ScriptEngineManager();
    final ScriptEngine se = sem.getEngineByName("JavaScript");

    // Restrict permission using the two-argument form of doPrivileged()
    try {/*from   w w w.j ava  2s . c  o  m*/
        final Object object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
            @Override
            public Object run() throws ScriptException {
                return se.eval(expression);
            }
        }, new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, null) }) // no permissions
        );
        if (object != null) {
            result = (boolean) object;
        }
    } catch (final Exception e) {
        this.logger.error("Failed to evaluate the expression.", e);
    }

    this.logger.info("Evaluate expression result: " + result);
    return result;
}