Search in sources :

Example 1 with WeldInvocationContextImpl

use of org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl in project core by weld.

the class InterceptorImpl method intercept.

@Override
public Object intercept(InterceptionType type, T instance, final InvocationContext ctx) {
    final org.jboss.weld.interceptor.spi.model.InterceptionType interceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type.name());
    final List<InterceptorMethodInvocation> methodInvocations = interceptorMetadata.getInterceptorInvocation(instance, interceptionType).getInterceptorMethodInvocations();
    Set<Annotation> interceptorBindings = null;
    if (ctx instanceof org.jboss.weld.interceptor.WeldInvocationContext) {
        interceptorBindings = Reflections.<org.jboss.weld.interceptor.WeldInvocationContext>cast(ctx).getInterceptorBindings();
    }
    try {
        /*
             * Calling Interceptor.intercept() may result in multiple interceptor method invocations (provided the interceptor class interceptor methods on
             * superclasses). This requires cooperation with InvocationContext.
             *
             * We use a wrapper InvocationContext for the purpose of executing the chain of interceptor methods of this interceptor.
             */
        return new WeldInvocationContextImpl(ctx, methodInvocations, interceptorBindings, null).proceed();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new WeldException(e);
    }
}
Also used : WeldException(org.jboss.weld.exceptions.WeldException) Annotation(java.lang.annotation.Annotation) WeldException(org.jboss.weld.exceptions.WeldException) WeldInvocationContextImpl(org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl) InterceptorMethodInvocation(org.jboss.weld.interceptor.proxy.InterceptorMethodInvocation)

Example 2 with WeldInvocationContextImpl

use of org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl in project core by weld.

the class ConstructorInterceptionInstantiator method registerAroundConstructCallback.

private void registerAroundConstructCallback(CreationalContextImpl<T> ctx, BeanManagerImpl manager) {
    final InterceptionContext interceptionContext = InterceptionContext.forConstructorInterception(model, ctx, manager, annotatedType);
    AroundConstructCallback<T> callback = new AroundConstructCallback<T>() {

        @Override
        public T aroundConstruct(final ConstructionHandle<T> handle, AnnotatedConstructor<T> constructor, Object[] parameters, Map<String, Object> data) {
            /*
                 * The AroundConstruct interceptor method can access the constructed instance using InvocationContext.getTarget
                 * method after the InvocationContext.proceed completes.
                 */
            final AtomicReference<T> target = new AtomicReference<T>();
            List<InterceptorMethodInvocation> chain = interceptionContext.buildInterceptorMethodInvocationsForConstructorInterception();
            InvocationContext invocationContext = new WeldInvocationContextImpl(constructor.getJavaMember(), parameters, data, chain, model.getMemberInterceptorBindings(getConstructor())) {

                @Override
                protected Object interceptorChainCompleted() throws Exception {
                    // all the interceptors were invoked, call the constructor now
                    T instance = handle.proceed(getParameters(), getContextData());
                    target.set(instance);
                    return null;
                }

                @Override
                public Object getTarget() {
                    return target.get();
                }
            };
            try {
                invocationContext.proceed();
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new WeldException(e);
            }
            T instance = target.get();
            if (instance == null) {
                // CDI-598
                throw InterceptorLogger.LOG.targetInstanceNotCreated(constructor);
            }
            return instance;
        }
    };
    ctx.registerAroundConstructCallback(callback);
    ctx.setAroundConstructInterceptionContext(interceptionContext);
}
Also used : InterceptionContext(org.jboss.weld.interceptor.proxy.InterceptionContext) WeldException(org.jboss.weld.exceptions.WeldException) ConstructionHandle(org.jboss.weld.construction.api.ConstructionHandle) AtomicReference(java.util.concurrent.atomic.AtomicReference) WeldException(org.jboss.weld.exceptions.WeldException) AroundConstructCallback(org.jboss.weld.construction.api.AroundConstructCallback) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor) InvocationContext(javax.interceptor.InvocationContext) WeldInvocationContextImpl(org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl) Map(java.util.Map) InterceptorMethodInvocation(org.jboss.weld.interceptor.proxy.InterceptorMethodInvocation)

Example 3 with WeldInvocationContextImpl

use of org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl in project core by weld.

the class AbstractInterceptor method checkConstructor.

protected void checkConstructor(InvocationContext ctx, Class<?> expectedDeclaringClass) {
    assertTrue(ctx instanceof WeldInvocationContextImpl);
    WeldInvocationContextImpl weldContext = (WeldInvocationContextImpl) ctx;
    Constructor<?> constructor = weldContext.getConstructor();
    assertNotNull(constructor);
    assertEquals(expectedDeclaringClass, constructor.getDeclaringClass());
}
Also used : WeldInvocationContextImpl(org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl)

Aggregations

WeldInvocationContextImpl (org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl)3 WeldException (org.jboss.weld.exceptions.WeldException)2 InterceptorMethodInvocation (org.jboss.weld.interceptor.proxy.InterceptorMethodInvocation)2 Annotation (java.lang.annotation.Annotation)1 Map (java.util.Map)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AnnotatedConstructor (javax.enterprise.inject.spi.AnnotatedConstructor)1 InvocationContext (javax.interceptor.InvocationContext)1 AroundConstructCallback (org.jboss.weld.construction.api.AroundConstructCallback)1 ConstructionHandle (org.jboss.weld.construction.api.ConstructionHandle)1 InterceptionContext (org.jboss.weld.interceptor.proxy.InterceptionContext)1