javax.enterprise.inject.spi.Interceptor Java Examples

The following examples show how to use javax.enterprise.inject.spi.Interceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeanContext.java    From tomee with Apache License 2.0 5 votes vote down vote up
private InterceptorData createInterceptorData(final Interceptor<?> i) {
    final InterceptorData data;
    if (CdiInterceptorBean.class.isInstance(i)) {
        final CdiInterceptorBean cdiInterceptorBean = CdiInterceptorBean.class.cast(i);

        data = new InterceptorData(cdiInterceptorBean);
    } else { // TODO: here we are not as good as in previous since we loose inheritance for instance
        data = InterceptorData.scan(i.getBeanClass());
    }
    return data;
}
 
Example #2
Source File: WebappBeanManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public List<Interceptor<?>> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings) {
    final List<Interceptor<?>> interceptors = super.resolveInterceptors(type, interceptorBindings);
    final List<Interceptor<?>> parentInterceptors = getParentBm().resolveInterceptors(type, interceptorBindings);
    for (final Interceptor<?> i : parentInterceptors) {
        if (!interceptors.contains(i)) {
            interceptors.add(i);
        }
    }
    return interceptors;
}
 
Example #3
Source File: DeltaSpikeProxyInvocationHandler.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable
{
    // check if interceptors are defined, otherwise just call the original logik
    List<Interceptor<?>> interceptors = interceptorLookup.lookup(proxy, method);
    if (interceptors != null && !interceptors.isEmpty())
    {
        try
        {
            DeltaSpikeProxyInvocationContext invocationContext = new DeltaSpikeProxyInvocationContext(
                    this, beanManager, interceptors, proxy, method, parameters, null);

            Object returnValue = invocationContext.proceed();

            if (invocationContext.isProceedOriginal())
            {
                return invocationContext.getProceedOriginalReturnValue();
            }

            return returnValue;
        }
        catch (DeltaSpikeProxyInvocationWrapperException e)
        {
            throw e.getCause();
        }
    }

    return proceed(proxy, method, parameters);
}
 
Example #4
Source File: DeltaSpikeProxyInvocationContext.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public DeltaSpikeProxyInvocationContext(DeltaSpikeProxyInvocationHandler invocationHandler,
        BeanManager beanManager, List<Interceptor<H>> interceptors, 
        T target, Method method, Object[] parameters, Object timer)
{
    super(target, method, parameters, timer);

    this.invocationHandler = invocationHandler;
    this.interceptors = interceptors;
    this.beanManager = beanManager;

    this.interceptorIndex = 0;
}
 
Example #5
Source File: DeltaSpikeProxyInterceptorLookup.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public List<Interceptor<?>> lookup(Object instance, Method method)
{
    List<Interceptor<?>> interceptors = cache.get(method);
    
    if (interceptors == null)
    {
        interceptors = resolveInterceptors(instance, method);
        cache.put(method, interceptors);
    }
    
    return interceptors;
}
 
Example #6
Source File: DeltaSpikeProxyInterceptorLookup.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private List<Interceptor<?>> resolveInterceptors(Object instance, Method method)
{
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    
    Annotation[] interceptorBindings = extractInterceptorBindings(beanManager, instance, method);
    if (interceptorBindings.length > 0)
    {
        return beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, interceptorBindings);
    }

    return new ArrayList<>();
}
 
Example #7
Source File: ExceptionControlExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * Listener to ProcessBean event to locate handlers.
 *
 * @param processBean current {@link AnnotatedType}
 * @param beanManager  Activated Bean Manager
 * @throws TypeNotPresentException if any of the actual type arguments refers to a non-existent type declaration
 *                                 when trying to obtain the actual type arguments from a
 *                                 {@link java.lang.reflect.ParameterizedType}
 * @throws java.lang.reflect.MalformedParameterizedTypeException
 *                                 if any of the actual type parameters refer to a parameterized type that cannot
 *                                 be instantiated for any reason when trying to obtain the actual type arguments
 *                                 from a {@link java.lang.reflect.ParameterizedType}
 */
@SuppressWarnings("UnusedDeclaration")
public <T> void findHandlers(@Observes final ProcessBean<?> processBean, final BeanManager beanManager)
{
    if (!isActivated)
    {
        return;
    }

    if (processBean.getBean() instanceof Interceptor || processBean.getBean() instanceof Decorator ||
            !(processBean.getAnnotated() instanceof AnnotatedType))
    {
        return;
    }

    AnnotatedType annotatedType = (AnnotatedType)processBean.getAnnotated();

    if (annotatedType.getJavaClass().isAnnotationPresent(ExceptionHandler.class))
    {
        final Set<AnnotatedMethod<? super T>> methods = annotatedType.getMethods();

        for (AnnotatedMethod<? super T> method : methods)
        {
            if (HandlerMethodImpl.isHandler(method))
            {
                if (method.getJavaMember().getExceptionTypes().length != 0)
                {
                    processBean.addDefinitionError(new IllegalArgumentException(
                        String.format("Handler method %s must not throw exceptions", method.getJavaMember())));
                }

                //beanManager won't be stored in the instance -> no issue with wls12c
                registerHandlerMethod(new HandlerMethodImpl(processBean.getBean(), method, beanManager));
            }
        }
    }
}
 
Example #8
Source File: BeanManagerImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public List<Interceptor<?>> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings) {
    return ArcContainerImpl.instance().resolveInterceptors(Objects.requireNonNull(type), interceptorBindings);
}
 
Example #9
Source File: MockInterceptorTest.java    From weld-junit with Apache License 2.0 4 votes vote down vote up
@Test
public void testDisabledInterceptor() {
    List<Interceptor<?>> interceptors = weld.getBeanManager().resolveInterceptors(InterceptionType.AROUND_INVOKE, FooBinding.Literal.INSTANCE);
    assertEquals(1, interceptors.size());
    assertEquals(MockInterceptor.class, interceptors.get(0).getBeanClass());
}
 
Example #10
Source File: MockInterceptorTest.java    From weld-junit with Apache License 2.0 4 votes vote down vote up
@Test
public void testDisabledInterceptor() {
    List<Interceptor<?>> interceptors = weld.getBeanManager().resolveInterceptors(InterceptionType.AROUND_INVOKE, FooBinding.Literal.INSTANCE);
    assertEquals(1, interceptors.size());
    assertEquals(MockInterceptor.class, interceptors.get(0).getBeanClass());
}
 
Example #11
Source File: InterceptorInvoker.java    From cxf with Apache License 2.0 4 votes vote down vote up
public InterceptorInvoker(Interceptor<?> interceptor, Object interceptorInstance) {
    this.interceptor = interceptor;
    this.interceptorInstance = interceptorInstance;
}
 
Example #12
Source File: CDIInterceptorWrapperImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static Map<Method, List<InterceptorInvoker>> initInterceptorInvokers(BeanManager beanManager,
                                                                             CreationalContext<?> creationalContext,
                                                                             Class<?> restClient) {
    Map<Method, List<InterceptorInvoker>> invokers = new HashMap<>();
    // Interceptor as a key in a map is not entirely correct (custom interceptors) but should work in most cases
    Map<Interceptor<?>, Object> interceptorInstances = new HashMap<>();
    
    AnnotatedType<?> restClientType = beanManager.createAnnotatedType(restClient);

    List<Annotation> classBindings = getBindings(restClientType.getAnnotations(), beanManager);

    for (AnnotatedMethod<?> method : restClientType.getMethods()) {
        Method javaMethod = method.getJavaMember();
        if (javaMethod.isDefault() || method.isStatic()) {
            continue;
        }
        List<Annotation> methodBindings = getBindings(method.getAnnotations(), beanManager);

        if (!classBindings.isEmpty() || !methodBindings.isEmpty()) {
            Annotation[] interceptorBindings = merge(methodBindings, classBindings);

            List<Interceptor<?>> interceptors =
                new ArrayList<>(beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, 
                                                                interceptorBindings));
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest("Resolved interceptors from beanManager, " + beanManager + ":" + interceptors);
            }

            if (!interceptors.isEmpty()) {
                List<InterceptorInvoker> chain = new ArrayList<>();
                for (Interceptor<?> interceptor : interceptors) {
                    chain.add(
                        new InterceptorInvoker(interceptor, 
                                               interceptorInstances.computeIfAbsent(interceptor, 
                                                   i -> beanManager.getReference(i, 
                                                                                 i.getBeanClass(), 
                                                                                 creationalContext))));
                }
                invokers.put(javaMethod, chain);
            }
        }
    }
    return invokers.isEmpty() ? Collections.emptyMap() : invokers;
}
 
Example #13
Source File: BeanContext.java    From tomee with Apache License 2.0 4 votes vote down vote up
private boolean isEjbInterceptor(final Interceptor<?> pc) {
    final Set<Annotation> interceptorBindings = pc.getInterceptorBindings();
    return interceptorBindings == null || interceptorBindings.isEmpty();
}
 
Example #14
Source File: ProxyRefreshAgent.java    From HotswapAgent with GNU General Public License v2.0 3 votes vote down vote up
private static void recreateInterceptedProxy(ClassLoader appClassLoader, Bean bean, WebBeansContext wbc) {

        if (!(bean instanceof OwbBean) || bean instanceof Interceptor || bean instanceof Decorator) {
            return;
        }

        OwbBean owbBean = (OwbBean) bean;

        AbstractProducer producer = (AbstractProducer) owbBean.getProducer();
        AnnotatedType annotatedType = ((InjectionTargetBean) owbBean).getAnnotatedType();

        producer.defineInterceptorStack(bean, annotatedType, wbc);
    }