Search in sources :

Example 6 with Interceptor

use of org.jboss.invocation.Interceptor in project wildfly by wildfly.

the class BasicComponent method createInterceptors.

protected void createInterceptors(InterceptorFactoryContext context) {
    // Create the post-construct interceptors for the ComponentInstance
    postConstructInterceptor = this.postConstruct.create(context);
    // create the pre-destroy interceptors
    preDestroyInterceptor = this.getPreDestroy().create(context);
    final Map<Method, InterceptorFactory> interceptorFactoryMap = this.getInterceptorFactoryMap();
    // This is an identity map.  This means that only <b>certain</b> {@code Method} objects will
    // match - specifically, they must equal the objects provided to the proxy.
    final IdentityHashMap<Method, Interceptor> interceptorMap = new IdentityHashMap<Method, Interceptor>();
    for (Method method : interceptorFactoryMap.keySet()) {
        interceptorMap.put(method, interceptorFactoryMap.get(method).create(context));
    }
    this.interceptorInstanceMap = interceptorMap;
}
Also used : InterceptorFactory(org.jboss.invocation.InterceptorFactory) IdentityHashMap(java.util.IdentityHashMap) Method(java.lang.reflect.Method) Interceptor(org.jboss.invocation.Interceptor)

Example 7 with Interceptor

use of org.jboss.invocation.Interceptor in project wildfly by wildfly.

the class StatefulComponentDescription method createConfiguration.

@Override
public ComponentConfiguration createConfiguration(final ClassReflectionIndex classIndex, final ClassLoader moduleClassLoader, final ModuleLoader moduleLoader) {
    final ComponentConfiguration statefulComponentConfiguration = new ComponentConfiguration(this, classIndex, moduleClassLoader, moduleLoader);
    // setup the component create service
    statefulComponentConfiguration.setComponentCreateServiceFactory(new StatefulComponentCreateServiceFactory());
    if (getTransactionManagementType() == TransactionManagementType.BEAN) {
        getConfigurators().add(new ComponentConfigurator() {

            @Override
            public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
                final ComponentInstanceInterceptorFactory bmtComponentInterceptorFactory = new ComponentInstanceInterceptorFactory() {

                    @Override
                    protected Interceptor create(Component component, InterceptorFactoryContext context) {
                        if (!(component instanceof StatefulSessionComponent)) {
                            throw EjbLogger.ROOT_LOGGER.componentNotInstanceOfSessionComponent(component, component.getComponentClass(), "stateful");
                        }
                        return new StatefulBMTInterceptor((StatefulSessionComponent) component);
                    }
                };
                configuration.addComponentInterceptor(bmtComponentInterceptorFactory, InterceptorOrder.Component.BMT_TRANSACTION_INTERCEPTOR, false);
            }
        });
    } else {
        getConfigurators().add(new ComponentConfigurator() {

            @Override
            public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
                final EEApplicationClasses applicationClasses = context.getDeploymentUnit().getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
                InterceptorClassDescription interceptorConfig = ComponentDescription.mergeInterceptorConfig(configuration.getComponentClass(), applicationClasses.getClassByName(description.getComponentClassName()), description, MetadataCompleteMarker.isMetadataComplete(context.getDeploymentUnit()));
                configuration.addPostConstructInterceptor(new LifecycleCMTTxInterceptor.Factory(interceptorConfig.getPostConstruct(), false), InterceptorOrder.ComponentPostConstruct.TRANSACTION_INTERCEPTOR);
                configuration.addPreDestroyInterceptor(new LifecycleCMTTxInterceptor.Factory(interceptorConfig.getPreDestroy(), false), InterceptorOrder.ComponentPreDestroy.TRANSACTION_INTERCEPTOR);
                if (description.isPassivationApplicable()) {
                    configuration.addPrePassivateInterceptor(new LifecycleCMTTxInterceptor.Factory(interceptorConfig.getPrePassivate(), false), InterceptorOrder.ComponentPassivation.TRANSACTION_INTERCEPTOR);
                    configuration.addPostActivateInterceptor(new LifecycleCMTTxInterceptor.Factory(interceptorConfig.getPostActivate(), false), InterceptorOrder.ComponentPassivation.TRANSACTION_INTERCEPTOR);
                }
            }
        });
    }
    addStatefulSessionSynchronizationInterceptor();
    return statefulComponentConfiguration;
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ComponentInstanceInterceptorFactory(org.jboss.as.ee.component.ComponentInstanceInterceptorFactory) SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) ComponentConfigurator(org.jboss.as.ee.component.ComponentConfigurator) ComponentTypeIdentityInterceptorFactory(org.jboss.as.ejb3.component.interceptors.ComponentTypeIdentityInterceptorFactory) InterceptorFactory(org.jboss.invocation.InterceptorFactory) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) ComponentInstanceInterceptorFactory(org.jboss.as.ee.component.ComponentInstanceInterceptorFactory) StatefulBMTInterceptor(org.jboss.as.ejb3.tx.StatefulBMTInterceptor) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext) InterceptorFactoryContext(org.jboss.invocation.InterceptorFactoryContext) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) InterceptorClassDescription(org.jboss.as.ee.component.interceptors.InterceptorClassDescription) EEApplicationClasses(org.jboss.as.ee.component.EEApplicationClasses) Component(org.jboss.as.ee.component.Component) Interceptor(org.jboss.invocation.Interceptor) LifecycleCMTTxInterceptor(org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor) StatefulBMTInterceptor(org.jboss.as.ejb3.tx.StatefulBMTInterceptor)

Example 8 with Interceptor

use of org.jboss.invocation.Interceptor in project wildfly by wildfly.

the class AsyncFutureInterceptorFactory method create.

@Override
public Interceptor create(final InterceptorFactoryContext context) {
    final SessionBeanComponent component = (SessionBeanComponent) context.getContextData().get(Component.class);
    if (component.isSecurityDomainKnown()) {
        return new Interceptor() {

            @Override
            public Object processInvocation(final InterceptorContext context) throws Exception {
                if (!context.isBlockingCaller()) {
                    return context.proceed();
                }
                final InterceptorContext asyncInterceptorContext = context.clone();
                asyncInterceptorContext.putPrivateData(InvocationType.class, InvocationType.ASYNC);
                final CancellationFlag flag = new CancellationFlag();
                final SecurityDomain securityDomain = context.getPrivateData(SecurityDomain.class);
                final StartupCountdown.Frame frame = StartupCountdown.current();
                final SecurityIdentity currentIdentity = securityDomain == null ? null : securityDomain.getCurrentSecurityIdentity();
                final Connection remoteConnection = getConnection();
                Callable<Object> invocationTask = () -> {
                    setConnection(remoteConnection);
                    StartupCountdown.restore(frame);
                    try {
                        return asyncInterceptorContext.proceed();
                    } finally {
                        StartupCountdown.restore(null);
                        clearConnection();
                    }
                };
                final AsyncInvocationTask task = new AsyncInvocationTask(flag) {

                    @Override
                    protected Object runInvocation() throws Exception {
                        if (currentIdentity != null) {
                            return currentIdentity.runAs(invocationTask);
                        } else {
                            return invocationTask.call();
                        }
                    }
                };
                asyncInterceptorContext.putPrivateData(CancellationFlag.class, flag);
                asyncInterceptorContext.setBlockingCaller(false);
                return execute(component, task);
            }
        };
    } else {
        return new Interceptor() {

            @Override
            public Object processInvocation(final InterceptorContext context) throws Exception {
                if (!context.isBlockingCaller()) {
                    return context.proceed();
                }
                final InterceptorContext asyncInterceptorContext = context.clone();
                asyncInterceptorContext.putPrivateData(InvocationType.class, InvocationType.ASYNC);
                final CancellationFlag flag = new CancellationFlag();
                final SecurityContext securityContext;
                if (WildFlySecurityManager.isChecking()) {
                    securityContext = AccessController.doPrivileged(new PrivilegedAction<SecurityContext>() {

                        @Override
                        public SecurityContext run() {
                            return SecurityContextAssociation.getSecurityContext();
                        }
                    });
                } else {
                    securityContext = SecurityContextAssociation.getSecurityContext();
                }
                // clone the original security context so that changes to the original security context in a separate (caller/unrelated) thread doesn't affect
                // the security context associated with the async invocation thread
                final SecurityContext clonedSecurityContext;
                if (securityContext instanceof JBossSecurityContext) {
                    clonedSecurityContext = (SecurityContext) ((JBossSecurityContext) securityContext).clone();
                } else {
                    // we can't do anything if it isn't a JBossSecurityContext so just use the original one
                    clonedSecurityContext = securityContext;
                }
                final Connection remoteConnection = getConnection();
                final StartupCountdown.Frame frame = StartupCountdown.current();
                final AsyncInvocationTask task = new AsyncInvocationTask(flag) {

                    @Override
                    protected Object runInvocation() throws Exception {
                        setSecurityContextOnAssociation(clonedSecurityContext);
                        setConnection(remoteConnection);
                        StartupCountdown.restore(frame);
                        try {
                            return asyncInterceptorContext.proceed();
                        } finally {
                            StartupCountdown.restore(null);
                            try {
                                clearSecurityContextOnAssociation();
                            } finally {
                                clearConnection();
                            }
                        }
                    }
                };
                asyncInterceptorContext.putPrivateData(CancellationFlag.class, flag);
                asyncInterceptorContext.setBlockingCaller(false);
                return execute(component, task);
            }
        };
    }
}
Also used : Connection(org.jboss.remoting3.Connection) SecurityDomain(org.wildfly.security.auth.server.SecurityDomain) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) PrivilegedAction(java.security.PrivilegedAction) SessionBeanComponent(org.jboss.as.ejb3.component.session.SessionBeanComponent) InterceptorContext(org.jboss.invocation.InterceptorContext) SecurityContext(org.jboss.security.SecurityContext) JBossSecurityContext(org.jboss.security.plugins.JBossSecurityContext) JBossSecurityContext(org.jboss.security.plugins.JBossSecurityContext) SessionBeanComponent(org.jboss.as.ejb3.component.session.SessionBeanComponent) Component(org.jboss.as.ee.component.Component) Interceptor(org.jboss.invocation.Interceptor) StartupCountdown(org.jboss.as.ee.component.deployers.StartupCountdown)

Example 9 with Interceptor

use of org.jboss.invocation.Interceptor in project wildfly by wildfly.

the class SessionBeanHomeInterceptorFactory method create.

@Override
public Interceptor create(final InterceptorFactoryContext context) {
    return new Interceptor() {

        @Override
        public Object processInvocation(final InterceptorContext context) throws Exception {
            final ComponentView view = viewToCreate.getValue();
            try {
                INIT_METHOD.set(method);
                INIT_PARAMETERS.set(context.getParameters());
                final ManagedReference instance = view.createInstance();
                return instance.getInstance();
            } finally {
                INIT_METHOD.remove();
                INIT_PARAMETERS.remove();
            }
        }
    };
}
Also used : ComponentView(org.jboss.as.ee.component.ComponentView) InterceptorContext(org.jboss.invocation.InterceptorContext) ManagedReference(org.jboss.as.naming.ManagedReference) Interceptor(org.jboss.invocation.Interceptor)

Example 10 with Interceptor

use of org.jboss.invocation.Interceptor in project wildfly by wildfly.

the class ViewService method start.

public void start(final StartContext context) throws StartException {
    // Construct the view
    View view = new View(privateData);
    view.initializeInterceptors();
    this.view = view;
    final SimpleInterceptorFactoryContext factoryContext = new SimpleInterceptorFactoryContext();
    final Component component = view.getComponent();
    factoryContext.getContextData().put(Component.class, component);
    factoryContext.getContextData().put(ComponentView.class, view);
    clientPostConstructInterceptor = clientPostConstruct.create(factoryContext);
    clientPreDestroyInterceptor = clientPreDestroy.create(factoryContext);
    final Map<Method, InterceptorFactory> clientInterceptorFactories = ViewService.this.clientInterceptorFactories;
    clientInterceptors = new IdentityHashMap<Method, Interceptor>(clientInterceptorFactories.size());
    for (Method method : clientInterceptorFactories.keySet()) {
        clientInterceptors.put(method, clientInterceptorFactories.get(method).create(factoryContext));
    }
}
Also used : SimpleInterceptorFactoryContext(org.jboss.invocation.SimpleInterceptorFactoryContext) InterceptorFactory(org.jboss.invocation.InterceptorFactory) Method(java.lang.reflect.Method) Interceptor(org.jboss.invocation.Interceptor)

Aggregations

Interceptor (org.jboss.invocation.Interceptor)16 InterceptorContext (org.jboss.invocation.InterceptorContext)8 Method (java.lang.reflect.Method)5 InterceptorFactory (org.jboss.invocation.InterceptorFactory)5 ImmediateInterceptorFactory (org.jboss.invocation.ImmediateInterceptorFactory)4 ArrayList (java.util.ArrayList)2 Component (org.jboss.as.ee.component.Component)2 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)2 EJBViewDescription (org.jboss.as.ejb3.component.EJBViewDescription)2 SessionBeanComponentDescription (org.jboss.as.ejb3.component.session.SessionBeanComponentDescription)2 LifecycleCMTTxInterceptor (org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor)2 DeploymentPhaseContext (org.jboss.as.server.deployment.DeploymentPhaseContext)2 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)2 SimpleInterceptorFactoryContext (org.jboss.invocation.SimpleInterceptorFactoryContext)2 PrivilegedAction (java.security.PrivilegedAction)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1