javax.interceptor.Interceptor Java Examples

The following examples show how to use javax.interceptor.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: BeanManagerTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testResolveInterceptors() {
    BeanManager beanManager = Arc.container().beanManager();
    List<javax.enterprise.inject.spi.Interceptor<?>> interceptors;
    // InterceptionType does not match
    interceptors = beanManager.resolveInterceptors(InterceptionType.AROUND_CONSTRUCT, new DummyBinding.Literal(true, true));
    assertTrue(interceptors.isEmpty());
    // alpha is @Nonbinding
    interceptors = beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, new DummyBinding.Literal(false, true),
            new AnnotationLiteral<UselessBinding>() {
            });
    assertEquals(2, interceptors.size());
    assertEquals(DummyInterceptor.class, interceptors.get(0).getBeanClass());
    assertEquals(LowPriorityInterceptor.class, interceptors.get(1).getBeanClass());
}
 
Example #2
Source File: KrazoCdiExtension.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Search for {@link Controller} annotation and patch AnnotatedType.
 * Note: PLATFORM_AFTER is required so we execute AFTER the Hibernate Validator Extension
 */
public <T> void processAnnotatedType(
        @Observes @Priority(Interceptor.Priority.PLATFORM_AFTER) @WithAnnotations({Controller.class})
                ProcessAnnotatedType<T> pat) {

    AnnotatedType<T> replacement = annotatedTypeProcessor.getReplacement(pat.getAnnotatedType());
    if (replacement != null) {
        log.log(Level.FINE, "Replacing AnnotatedType of class: {0}", replacement.getJavaClass().getName());
        pat.setAnnotatedType(replacement);
    }

}
 
Example #3
Source File: QuartzScheduler.java    From quarkus with Apache License 2.0 5 votes vote down vote up
void start(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent startupEvent) {
    if (scheduler == null) {
        return;
    }
    try {
        scheduler.start();
    } catch (SchedulerException e) {
        throw new IllegalStateException("Unable to start Scheduler", e);
    }
}
 
Example #4
Source File: SimpleScheduler.java    From quarkus with Apache License 2.0 5 votes vote down vote up
void start(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent event) {
    if (scheduledExecutor == null) {
        return;
    }
    // Try to compute the initial delay to execute the checks near to the whole second
    // Note that this does not guarantee anything, it's just best effort
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime trunc = now.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS);
    scheduledExecutor.scheduleAtFixedRate(this::checkTriggers, ChronoUnit.MILLIS.between(now, trunc), CHECK_PERIOD,
            TimeUnit.MILLISECONDS);
}
 
Example #5
Source File: OzarkCdiExtension.java    From ozark with Apache License 2.0 5 votes vote down vote up
/**
 * Search for {@link Controller} annotation and patch AnnotatedType.
 * Note: PLATFORM_AFTER is required so we execute AFTER the Hibernate Validator Extension
 */
public <T> void processAnnotatedType(
        @Observes @Priority(Interceptor.Priority.PLATFORM_AFTER) @WithAnnotations({Controller.class})
                ProcessAnnotatedType<T> pat) {

    AnnotatedType<T> replacement = annotatedTypeProcessor.getReplacement(pat.getAnnotatedType());
    if (replacement != null) {
        log.log(Level.FINE, "Replacing AnnotatedType of class: {0}", replacement.getJavaClass().getName());
        pat.setAnnotatedType(replacement);
    }

}
 
Example #6
Source File: BeanClassRefreshAgent.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isCDIAnnotation(BeanManagerImpl beanManager, Class<? extends Annotation> annotation) {
    if (Interceptor.class.equals(annotation) || Decorator.class.equals(annotation)) {
        return true;
    }

    boolean isBeanAnnotation = beanManager.isScope(annotation);
    if (!isBeanAnnotation) {
        isBeanAnnotation = beanManager.isStereotype(annotation);
    }
    return isBeanAnnotation;
}
 
Example #7
Source File: CdiEjbBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public CdiEjbBean(final BeanContext bc, final WebBeansContext webBeansContext, final Class beanClass, final AnnotatedType<T> at,
                  final InjectionTargetFactoryImpl<T> factory, final BeanAttributes<T> attributes) {
    super(webBeansContext, toSessionType(bc.getComponentType()), at,
            new EJBBeanAttributesImpl<T>(bc, attributes),
            beanClass, factory);
    this.beanContext = bc;
    bc.set(Bean.class, this);
    passivatingId = bc.getDeploymentID() + getReturnType().getName();

    final boolean stateful = BeanType.STATEFUL.equals(bc.getComponentType());
    final boolean isDependent = getScope().equals(Dependent.class);
    isDependentAndStateful = isDependent && stateful;
    if (webBeansContext.getBeanManagerImpl().isPassivatingScope(getScope()) && stateful) {
        if (!getBeanContext().isPassivable()) {
            throw new DefinitionException(
                    getBeanContext().getBeanClass()
                            + " is a not apssivation-capable @Stateful with a scope "
                            + getScope().getSimpleName() + " which need passivation");
        }
        passivable = true;
    } else {
        passivable = false;
    }
    if (!isDependent) {
        for (final Type type : attributes.getTypes()) {
            if (ParameterizedType.class.isInstance(type)) {
                throw new DefinitionException("Parameterized session bean should be @Dependent: " + beanClass);
            }
        }
    }
    if (getAnnotatedType().isAnnotationPresent(Interceptor.class) || getAnnotatedType().isAnnotationPresent(Decorator.class)) {
        throw new DefinitionException("An EJB can't be an interceptor or a decorator: " + beanClass);
    }
}
 
Example #8
Source File: CdiEjbBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public T createNewPojo(final CreationalContext<T> creationalContext) {
    final CreationalContextImpl<T> ccImpl = CreationalContextImpl.class.cast(creationalContext);
    // super.produce(cc) will not work since we need the unproxied instance - decorator case
    final Map<javax.enterprise.inject.spi.Interceptor<?>, Object> interceptorInstances
            = webBeansContext.getInterceptorResolutionService().createInterceptorInstances(getInterceptorInfo(), ccImpl);
    final InterceptorResolutionService.BeanInterceptorInfo interceptorInfo = super.getInterceptorInfo();
    if (interceptorInfo != null) {
        final Map<Constructor<?>, InterceptorResolutionService.BusinessMethodInterceptorInfo> constructorInterceptorInfos =
                interceptorInfo.getConstructorInterceptorInfos();
        if (!constructorInterceptorInfos.isEmpty()) { // were missed by OWB
            final javax.enterprise.inject.spi.Interceptor<?>[] ejbInterceptors = constructorInterceptorInfos.values().iterator().next().getEjbInterceptors();

            if (null != ejbInterceptors) {
                for (final javax.enterprise.inject.spi.Interceptor interceptorBean : ejbInterceptors) {
                    if (!interceptorInstances.containsKey(interceptorBean)) {
                        ccImpl.putContextual(interceptorBean);
                        interceptorInstances.put(interceptorBean, interceptorBean.create(ccImpl));
                    }
                }
            }
        }
    }
    final T produce = super.produce(interceptorInstances, ccImpl);
    if (produce == null) { // user didnt call ic.proceed() in @AroundConstruct
        return super.newInstance(ccImpl);
    }
    return (T) produce;
}
 
Example #9
Source File: GlobalInterceptorExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
protected void promoteInterceptors(@Observes ProcessAnnotatedType pat)
{
    if (priorityAnnotationInstance == null) //not CDI 1.1 or the extension is deactivated
    {
        return;
    }

    String beanClassName = pat.getAnnotatedType().getJavaClass().getName();
    if (beanClassName.startsWith(DS_PACKAGE_NAME))
    {
        if (pat.getAnnotatedType().isAnnotationPresent(Interceptor.class))
        {
            //noinspection unchecked
            pat.setAnnotatedType(new GlobalInterceptorWrapper(pat.getAnnotatedType(), priorityAnnotationInstance));
        }
        //currently not needed, because we don't use our interceptors internally -> check for the future
        else if (!beanClassName.contains(".test."))
        {
            for (Annotation annotation : pat.getAnnotatedType().getAnnotations())
            {
                if (beanManager.isInterceptorBinding(annotation.annotationType()))
                {
                    //once we see this warning we need to introduce double-call prevention logic due to WELD-1780
                    LOG.warning(beanClassName + " is an bean from DeltaSpike which is intercepted.");
                }
            }
        }
    }
}
 
Example #10
Source File: OrderedObserver.java    From javaee8-cookbook with Apache License 2.0 4 votes vote down vote up
public void thisEventBefore(@Observes @Priority(Interceptor.Priority.APPLICATION - 200) MyEvent event) {
    System.out.println("thisEventBefore: " + event.getValue());
}
 
Example #11
Source File: OrderedObserver.java    From javaee8-cookbook with Apache License 2.0 4 votes vote down vote up
public void thisEventAfter(@Observes @Priority(Interceptor.Priority.APPLICATION + 200) MyEvent event) {
    System.out.println("thisEventAfter: " + event.getValue());
}
 
Example #12
Source File: ClassScanning.java    From weld-junit with Apache License 2.0 4 votes vote down vote up
private static boolean hasBeanDefiningAnnotation(Class<?> clazz) {
    return isAnnotated(clazz, NormalScope.class) || isAnnotated(clazz, Dependent.class) ||
            isAnnotated(clazz, Interceptor.class) || isAnnotated(clazz, Decorator.class) ||
            isAnnotated(clazz, Stereotype.class);
}
 
Example #13
Source File: MockitoExtension.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public int getPriority() {
    return Interceptor.Priority.PLATFORM_AFTER+1000;
}
 
Example #14
Source File: BeanTestExtension.java    From BeanTest with Apache License 2.0 3 votes vote down vote up
/**
 * Replaces the meta data of the {@link ProcessAnnotatedType}.
 * 
 * <p>
 * The ProcessAnnotatedType's meta data will be replaced, if the annotated type has one of the following annotations:
 * <ul>
 * <li> {@link Stateless}
 * <li> {@link MessageDriven}
 * <li> {@link Interceptor}
 * <li> {@link Singleton}
 * </ul>
 *
 * @param <X> the type of the ProcessAnnotatedType
 * @param pat the annotated type representing the class being processed
 */
public <X> void processInjectionTarget(@Observes @WithAnnotations({Stateless.class, MessageDriven.class, Interceptor.class, Singleton.class}) ProcessAnnotatedType<X> pat) {
    if (pat.getAnnotatedType().isAnnotationPresent(Stateless.class) || pat.getAnnotatedType().isAnnotationPresent(MessageDriven.class)) {
        modifiyAnnotatedTypeMetadata(pat);
    } else if (pat.getAnnotatedType().isAnnotationPresent(Interceptor.class)) {
        processInterceptorDependencies(pat);
    } else if(pat.getAnnotatedType().isAnnotationPresent(Singleton.class)) {
        addApplicationScopedAndTransactionalToSingleton(pat);
    }
}