All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.as.ee.concurrent.ContextServiceImpl Maven / Gradle / Ivy

There is a newer version: 35.0.0.Beta1
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.jboss.as.ee.concurrent;

import org.glassfish.enterprise.concurrent.spi.ContextSetupProvider;
import org.wildfly.security.manager.WildFlySecurityManager;

import java.lang.reflect.Proxy;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;

import static org.jboss.as.ee.logging.EeLogger.ROOT_LOGGER;
import static org.wildfly.common.Assert.checkArrayBounds;
import static org.wildfly.common.Assert.checkNotNullParam;

/**
 * An extension of Jakarta EE RI {@link org.glassfish.enterprise.concurrent.ContextServiceImpl}, which properly supports a security manager.
 * @author Eduardo Martins
 */
public class ContextServiceImpl extends org.glassfish.enterprise.concurrent.ContextServiceImpl {

    private final ContextServiceTypesConfiguration contextServiceTypesConfiguration;

    /**
     *
     * @param name
     * @param contextSetupProvider
     */
    public ContextServiceImpl(String name, ContextSetupProvider contextSetupProvider, ContextServiceTypesConfiguration contextServiceTypesConfiguration) {
        super(name, contextSetupProvider, null);
        this.contextServiceTypesConfiguration = contextServiceTypesConfiguration;
    }

    private  T internalCreateContextualProxy(T instance, Map executionProperties, Class intf) {
        checkNotNullParam("instance", instance);
        checkNotNullParam("intf", intf);

        IdentityAwareProxyInvocationHandler handler = new IdentityAwareProxyInvocationHandler(this, instance, executionProperties);
        Object proxy = Proxy.newProxyInstance(instance.getClass().getClassLoader(), new Class[]{intf}, handler);
        return  intf.cast(proxy);
    }

    @Override
    public  T createContextualProxy(final T instance, final Map executionProperties, final Class intf) {
        if (WildFlySecurityManager.isChecking()) {
            return AccessController.doPrivileged(new PrivilegedAction() {
                @Override
                public T run() {
                    return internalCreateContextualProxy(instance, executionProperties, intf);
                }
            });
        } else {
            return internalCreateContextualProxy(instance, executionProperties, intf);
        }
    }

    private Object internalCreateContextualProxy(Object instance, Map executionProperties,
            Class... interfaces) {
        checkNotNullParam("instance", instance);
        checkArrayBounds(checkNotNullParam("interfaces", interfaces), 0, 1);

        Class instanceClass = instance.getClass();
        for (Class thisInterface : interfaces) {
            if (!thisInterface.isAssignableFrom(instanceClass)) {
                throw ROOT_LOGGER.classDoesNotImplementAllInterfaces();
            }
        }
        IdentityAwareProxyInvocationHandler handler = new IdentityAwareProxyInvocationHandler(this, instance, executionProperties);
        Object proxy = Proxy.newProxyInstance(instance.getClass().getClassLoader(), interfaces, handler);
        return proxy;
    }

    @Override
    public Object createContextualProxy(final Object instance, final Map executionProperties, final Class... interfaces) {
        if (WildFlySecurityManager.isChecking()) {
            return AccessController.doPrivileged(new PrivilegedAction() {
                @Override
                public Object run() {
                    return internalCreateContextualProxy(instance, executionProperties, interfaces);
                }
            });
        } else {
            return internalCreateContextualProxy(instance, executionProperties, interfaces);
        }
    }

    public ContextServiceTypesConfiguration getContextServiceTypesConfiguration() {
        return contextServiceTypesConfiguration;
    }

    // TODO *FOLLOW UP* revisit RI impl of the async methods, which quality seems to have issues (e.g. each method uses a new Managed Executor instance...)
}