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.vaadin.spring.events.support.VaadinEventBusAwareProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
    AccessControlContext acc = null;

    if (System.getSecurityManager() != null && (bean instanceof EventBusAware)) {
        acc = this.applicationContext.getBeanFactory().getAccessControlContext();
    }//  ww  w  .  ja  v a 2 s  .c  om

    if (acc != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {

            @Override
            public Object run() {
                invokeAwareInterfaces(bean);
                return null;
            }

        }, acc);
    } else {
        invokeAwareInterfaces(bean);
    }

    return bean;
}

From source file:org.vaadin.spring.security.support.VaadinSecurityAwareProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {

    AccessControlContext acc = null;

    if (System.getSecurityManager() != null && (bean instanceof VaadinSecurityAware)) {
        acc = this.applicationContext.getBeanFactory().getAccessControlContext();
    }//from  ww  w .ja  v a  2 s .c o m

    if (acc != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {

            @Override
            public Object run() {
                invokeAwareInterfaces(bean);
                return null;
            }

        }, acc);
    } else {
        invokeAwareInterfaces(bean);
    }

    return bean;

}

From source file:org.eclipse.gemini.blueprint.blueprint.container.support.BlueprintContainerServicePublisher.java

private void registerService(ApplicationContext applicationContext) {
    final Dictionary<String, Object> serviceProperties = new Hashtable<String, Object>();

    Bundle bundle = bundleContext.getBundle();
    String symName = bundle.getSymbolicName();
    serviceProperties.put(Constants.BUNDLE_SYMBOLICNAME, symName);
    serviceProperties.put(BLUEPRINT_SYMNAME, symName);

    Version version = OsgiBundleUtils.getBundleVersion(bundle);
    serviceProperties.put(Constants.BUNDLE_VERSION, version);
    serviceProperties.put(BLUEPRINT_VERSION, version);

    log.info("Publishing BlueprintContainer as OSGi service with properties " + serviceProperties);

    // export just the interface
    final String[] serviceNames = new String[] { BlueprintContainer.class.getName() };

    if (log.isDebugEnabled())
        log.debug("Publishing service under classes " + ObjectUtils.nullSafeToString(serviceNames));

    AccessControlContext acc = SecurityUtils.getAccFrom(applicationContext);

    // publish service
    if (System.getSecurityManager() != null) {
        registration = AccessController.doPrivileged(new PrivilegedAction<ServiceRegistration>() {
            public ServiceRegistration run() {
                return bundleContext.registerService(serviceNames, blueprintContainer, serviceProperties);
            }/*  w  w  w  .  ja v a 2 s.c  o  m*/
        }, acc);
    } else {
        registration = bundleContext.registerService(serviceNames, blueprintContainer, serviceProperties);
    }
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.OsgiServiceRegistrationListenerAdapter.java

public void registered(final Object service, final Map serviceProperties) {
    boolean trace = log.isTraceEnabled();

    if (trace)/*w w w .ja va 2s.  c om*/
        log.trace("Invoking registered method with props=" + serviceProperties);

    if (!initialized)
        retrieveTarget();

    boolean isSecurityEnabled = System.getSecurityManager() != null;
    AccessControlContext acc = null;
    if (isSecurityEnabled) {
        acc = SecurityUtils.getAccFrom(beanFactory);
    }

    // first call interface method (if it exists)
    if (isListener) {
        if (trace)
            log.trace("Invoking listener interface methods");

        try {
            if (isSecurityEnabled) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ((OsgiServiceRegistrationListener) target).registered(service, serviceProperties);
                        return null;
                    }
                }, acc);
            } else {
                ((OsgiServiceRegistrationListener) target).registered(service, serviceProperties);
            }
        } catch (Exception ex) {
            if (ex instanceof PrivilegedActionException) {
                ex = ((PrivilegedActionException) ex).getException();
            }
            log.warn("Standard registered method on [" + target.getClass().getName() + "] threw exception", ex);
        }
    }

    if (isSecurityEnabled) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                CustomListenerAdapterUtils.invokeCustomMethods(target, registrationMethods, service,
                        serviceProperties);
                return null;
            }
        }, acc);
    } else {
        CustomListenerAdapterUtils.invokeCustomMethods(target, registrationMethods, service, serviceProperties);
    }
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.OsgiServiceRegistrationListenerAdapter.java

public void unregistered(final Object service, final Map serviceProperties) {

    boolean trace = log.isTraceEnabled();

    if (trace)/* w  w  w  .j  a v  a  2 s. com*/
        log.trace("Invoking unregistered method with props=" + serviceProperties);

    if (!initialized)
        retrieveTarget();

    boolean isSecurityEnabled = System.getSecurityManager() != null;
    AccessControlContext acc = null;

    if (isSecurityEnabled) {
        acc = SecurityUtils.getAccFrom(beanFactory);
    }

    // first call interface method (if it exists)
    if (isListener) {
        if (trace)
            log.trace("Invoking listener interface methods");

        try {
            if (isSecurityEnabled) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ((OsgiServiceRegistrationListener) target).unregistered(service, serviceProperties);
                        return null;
                    }
                }, acc);
            } else {
                ((OsgiServiceRegistrationListener) target).unregistered(service, serviceProperties);
            }
        } catch (Exception ex) {
            log.warn("Standard unregistered method on [" + target.getClass().getName() + "] threw exception",
                    ex);
        }
    }

    if (isSecurityEnabled) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                CustomListenerAdapterUtils.invokeCustomMethods(target, unregistrationMethods, service,
                        serviceProperties);
                return null;
            }
        }, acc);
    } else {
        CustomListenerAdapterUtils.invokeCustomMethods(target, unregistrationMethods, service,
                serviceProperties);
    }
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.OsgiServiceLifecycleListenerAdapter.java

public void bind(final Object service, final Map properties) throws Exception {
    boolean trace = log.isTraceEnabled();
    if (trace)//from  w  w w  .  ja  v  a2s. c  o  m
        log.trace("Invoking bind method for service " + ObjectUtils.identityToString(service) + " with props="
                + properties);

    if (!initialized)
        retrieveTarget();

    boolean isSecurityEnabled = (System.getSecurityManager() != null);
    AccessControlContext acc = null;

    if (isSecurityEnabled) {
        acc = SecurityUtils.getAccFrom(beanFactory);
    }

    // first call interface method (if it exists)
    if (isLifecycleListener) {
        if (trace)
            log.trace("Invoking listener interface methods");

        try {
            if (isSecurityEnabled) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ((OsgiServiceLifecycleListener) target).bind(service, properties);
                        return null;
                    }
                }, acc);
            } else {
                ((OsgiServiceLifecycleListener) target).bind(service, properties);
            }
        } catch (Exception ex) {
            if (ex instanceof PrivilegedActionException) {
                ex = ((PrivilegedActionException) ex).getException();
            }
            log.warn("standard bind method on [" + target.getClass().getName() + "] threw exception", ex);
        }
    }

    if (isSecurityEnabled) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                CustomListenerAdapterUtils.invokeCustomMethods(target, bindMethods, service, properties);
                invokeCustomServiceReferenceMethod(target, bindReference, service);
                return null;
            }
        }, acc);
    } else {
        CustomListenerAdapterUtils.invokeCustomMethods(target, bindMethods, service, properties);
        invokeCustomServiceReferenceMethod(target, bindReference, service);
    }
}

From source file:com.dragome.callbackevictor.serverside.ContinuationClassLoader.java

/**
 * Define a class given its bytes/*from  w ww . j av  a  2s  .co  m*/
 *
 * @param classData the bytecode data for the class
 * @param classname the name of the class
 *
 * @return the Class instance created from the given data
 */
protected Class<?> defineClassFromData(final byte[] classData, final String classname) {
    return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
        public Class<?> run() {
            // define a package if necessary.
            int i = classname.lastIndexOf('.');
            if (i > 0) {
                final String packageName = classname.substring(0, i);
                final Package pkg = getPackage(packageName);
                if (pkg == null) {
                    definePackage(packageName, null, null, null, null, null, null, null);
                }
            }

            final byte[] newData = transformer.transform(classData);
            final ProtectionDomain domain = this.getClass().getProtectionDomain();
            return defineClass(classname, newData, 0, newData.length, domain);
        }
    }, acc);
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.OsgiServiceLifecycleListenerAdapter.java

public void unbind(final Object service, final Map properties) throws Exception {
    boolean trace = log.isTraceEnabled();
    if (!initialized)
        retrieveTarget();/* w ww  .  jav a2 s.  co  m*/

    if (trace)
        log.trace("Invoking unbind method for service " + ObjectUtils.identityToString(service) + " with props="
                + properties);

    boolean isSecurityEnabled = (System.getSecurityManager() != null);
    AccessControlContext acc = null;

    if (isSecurityEnabled) {
        acc = SecurityUtils.getAccFrom(beanFactory);
    }

    // first call interface method (if it exists)
    if (isLifecycleListener) {
        if (trace)
            log.trace("Invoking listener interface methods");
        try {
            if (isSecurityEnabled) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ((OsgiServiceLifecycleListener) target).unbind(service, properties);
                        return null;
                    }
                }, acc);
            } else {
                ((OsgiServiceLifecycleListener) target).unbind(service, properties);
            }
        } catch (Exception ex) {
            log.warn("Standard unbind method on [" + target.getClass().getName() + "] threw exception", ex);
        }
    }

    if (isSecurityEnabled) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                CustomListenerAdapterUtils.invokeCustomMethods(target, unbindMethods, service, properties);
                invokeCustomServiceReferenceMethod(target, unbindReference, service);
                return null;
            }
        }, acc);
    } else {
        CustomListenerAdapterUtils.invokeCustomMethods(target, unbindMethods, service, properties);
        invokeCustomServiceReferenceMethod(target, unbindReference, service);
    }
}

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

protected void findServiceDependencies() throws Exception {
    try {//from   w  w w  . ja  va2s . c om
        if (System.getSecurityManager() != null) {
            final AccessControlContext acc = getAcc();

            PrivilegedUtils.executeWithCustomTCCL(context.getClassLoader(),
                    new PrivilegedUtils.UnprivilegedThrowableExecution<Object>() {
                        public Object run() throws Throwable {
                            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                                public Object run() throws Exception {
                                    doFindDependencies();
                                    return null;
                                }
                            }, acc);
                            return null;
                        }
                    });
        } else {
            doFindDependencies();
        }
    } catch (Throwable th) {
        if (th instanceof Exception)
            throw ((Exception) th);
        throw (Error) th;
    }

    Collection<String> unsatisfiedDependencyValues = getUnsatisfiedDependencies().values();

    if (log.isDebugEnabled()) {
        int numDependencies;
        int numUnsatisfiedDependencies;
        synchronized (monitor) {
            numDependencies = dependencies.size();
            numUnsatisfiedDependencies = unsatisfiedDependencies.size();
        }
        log.debug(numDependencies + " OSGi service dependencies, " + numUnsatisfiedDependencies
                + " unsatisfied (for beans " + unsatisfiedDependencyValues + ") in "
                + context.getDisplayName());
    }

    if (!isSatisfied()) {
        log.info(context.getDisplayName() + " is waiting for unsatisfied dependencies ["
                + unsatisfiedDependencyValues + "]");
    }
    if (log.isTraceEnabled()) {
        Collection<String> dependencyValues;
        synchronized (monitor) {
            dependencyValues = new ArrayList<String>(dependencies.values());
        }
        log.trace("Total OSGi service dependencies beans " + dependencyValues);
        log.trace("Unsatified OSGi service dependencies beans " + unsatisfiedDependencyValues);
    }
}

From source file:com.dragome.callbackevictor.serverside.DragomeContinuationClassLoader.java

/**
 * Define a class given its bytes//from  ww  w. ja  va2 s. c o  m
 *
 * @param classData the bytecode data for the class
 * @param classname the name of the class
 *
 * @return the Class instance created from the given data
 */
public Class defineClassFromData(final byte[] classData, final String classname) {
    return (Class) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            // define a package if necessary.
            int i = classname.lastIndexOf('.');
            if (i > 0) {
                String packageName = classname.substring(0, i);
                Package pkg = getPackage(packageName);
                if (pkg == null) {
                    definePackage(packageName, null, null, null, null, null, null, null);
                }
            }

            byte[] newData;
            if (!bytecodeTransformer.requiresTransformation(classname) || loadingClass.contains(classname))
                newData = classData;
            else {
                loadingClass.add(classname);
                Thread.currentThread().setContextClassLoader(last);
                newData = transformer.transform(classData);
                Thread.currentThread().setContextClassLoader(DragomeContinuationClassLoader.this);
            }

            ProtectionDomain domain = this.getClass().getProtectionDomain();
            return defineClass(classname, newData, 0, newData.length, domain);
        }
    }, acc);
}