javax.enterprise.inject.spi.ObserverMethod Java Examples

The following examples show how to use javax.enterprise.inject.spi.ObserverMethod. 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 Project: quarkus   Author: quarkusio   File: ObserverInfo.java    License: Apache License 2.0 6 votes vote down vote up
static ObserverInfo create(BeanInfo declaringBean, MethodInfo observerMethod, Injection injection, boolean isAsync,
        List<ObserverTransformer> transformers, BuildContext buildContext, boolean jtaCapabilities) {
    MethodParameterInfo eventParameter = initEventParam(observerMethod, declaringBean.getDeployment());
    AnnotationInstance priorityAnnotation = observerMethod.annotation(DotNames.PRIORITY);
    Integer priority;
    if (priorityAnnotation != null && priorityAnnotation.target().equals(eventParameter)) {
        priority = priorityAnnotation.value().asInt();
    } else {
        priority = ObserverMethod.DEFAULT_PRIORITY;
    }
    return create(declaringBean.getDeployment(), declaringBean.getTarget().get().asClass().name(), declaringBean,
            observerMethod, injection,
            eventParameter,
            observerMethod.parameters().get(eventParameter.position()),
            initQualifiers(declaringBean.getDeployment(), observerMethod, eventParameter),
            initReception(isAsync, declaringBean.getDeployment(), observerMethod),
            initTransactionPhase(isAsync, declaringBean.getDeployment(), observerMethod), isAsync, priority, transformers,
            buildContext, jtaCapabilities, null);
}
 
Example #2
Source Project: tomee   Author: apache   File: CdiPlugin.java    License: Apache License 2.0 6 votes vote down vote up
private static void validateObserverMethods(final CdiEjbBean<?> bean, final Map<ObserverMethod<?>, AnnotatedMethod<?>> methods) {
    final BeanContext beanContext = bean.getBeanContext();
    if (beanContext.isLocalbean()) {
        return;
    }

    for (final Map.Entry<ObserverMethod<?>, AnnotatedMethod<?>> m : methods.entrySet()) {
        final Method method = m.getValue().getJavaMember();
        if (!Modifier.isStatic(method.getModifiers())) {
            final Method viewMethod = doResolveViewMethod(bean, method);
            if (viewMethod == null) {
                throw new WebBeansConfigurationException(
                        "@Observes " + method + " neither in the ejb view of ejb " + bean.getBeanContext().getEjbName() + " nor static");
            } else if (beanContext.getBusinessRemoteInterfaces().contains(viewMethod.getDeclaringClass())) {
                throw new WebBeansConfigurationException(viewMethod + " observer is defined in a @Remote interface");
            }
        }
        if (m.getValue().getParameters().stream().anyMatch(p -> p.isAnnotationPresent(ObservesAsync.class))) {
            throw new WebBeansConfigurationException("@ObservesAsync " + method + " not supported on EJB in CDI 2");
        }
    }
}
 
Example #3
Source Project: quarkus   Author: quarkusio   File: ObserverConfigurator.java    License: Apache License 2.0 5 votes vote down vote up
public ObserverConfigurator(Consumer<ObserverConfigurator> consumer) {
    this.consumer = consumer;
    this.observedQualifiers = new HashSet<>();
    this.priority = ObserverMethod.DEFAULT_PRIORITY;
    this.isAsync = false;
    this.transactionPhase = TransactionPhase.IN_PROGRESS;
}
 
Example #4
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 5 votes vote down vote up
static <T> Notifier<T> createNotifier(Class<?> runtimeType, Type eventType, Set<Annotation> qualifiers,
        ArcContainerImpl container) {
    EventMetadata metadata = new EventMetadataImpl(qualifiers, eventType);
    List<ObserverMethod<? super T>> notifierObserverMethods = new ArrayList<>();
    for (ObserverMethod<? super T> observerMethod : container.resolveObservers(eventType, qualifiers)) {
        notifierObserverMethods.add(observerMethod);
    }
    return new Notifier<>(runtimeType, notifierObserverMethods, metadata);
}
 
Example #5
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private void notifyObservers(T event, ObserverExceptionHandler exceptionHandler,
        Predicate<ObserverMethod<? super T>> predicate) {
    EventContext eventContext = new EventContextImpl<>(event, eventMetadata);
    for (ObserverMethod<? super T> observerMethod : observerMethods) {
        if (predicate.test(observerMethod)) {
            try {
                observerMethod.notify(eventContext);
            } catch (Throwable e) {
                exceptionHandler.handle(e);
            }
        }
    }
}
 
Example #6
Source Project: quarkus   Author: quarkusio   File: BeanManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(T event, Annotation... qualifiers) {
    Type eventType = Types.getCanonicalType(event.getClass());
    if (Types.containsTypeVariable(eventType)) {
        throw new IllegalArgumentException("The runtime type of the event object contains a type variable: " + eventType);
    }
    Set<Annotation> eventQualifiers = Arrays.asList(qualifiers).stream().collect(Collectors.toSet());
    return ArcContainerImpl.instance().resolveObservers(eventType, eventQualifiers).stream().collect(Collectors.toSet());
}
 
Example #7
Source Project: quarkus   Author: quarkusio   File: BeanManagerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveObservers() {
    BeanManager beanManager = Arc.container().beanManager();
    Set<ObserverMethod<? super Long>> observers = beanManager.resolveObserverMethods(Long.valueOf(1),
            new AnnotationLiteral<High>() {
            });
    assertEquals(1, observers.size());
    assertEquals(Number.class, observers.iterator().next().getObservedType());
}
 
Example #8
Source Project: weld-vertx   Author: weld   File: VertxExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Annotation getQualifier(ObserverMethod<?> observerMethod, Class<? extends Annotation> annotationType) {
    for (Annotation qualifier : observerMethod.getObservedQualifiers()) {
        if (qualifier.annotationType().equals(annotationType)) {
            return qualifier;
        }
    }
    return null;
}
 
Example #9
Source Project: tomee   Author: apache   File: WebappBeanManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(final T event, final EventMetadataImpl metadata) {
    final Set<ObserverMethod<? super T>> set = new HashSet<>(super.resolveObserverMethods(event, metadata));

    if (isEvent(event)) {
        final BeanManagerImpl parentBm = getParentBm();
        if (parentBm != null) {
            set.addAll(parentBm.resolveObserverMethods(event, metadata));
        }
    } // else nothing since extensions are loaded by classloader so we already have it

    return set;
}
 
Example #10
Source Project: tomee   Author: apache   File: OpenEJBTransactionService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void registerTransactionSynchronization(final TransactionPhase phase, final ObserverMethod<? super Object> observer, final Object event) throws Exception {
    Set<Annotation> qualifiers = observer.getObservedQualifiers();
    if (qualifiers == null) {
        qualifiers = Collections.emptySet();
    }

    TransactionalEventNotifier.registerTransactionSynchronization(phase, observer, event,
        new EventMetadataImpl(observer.getObservedType(), null, null,
            qualifiers.toArray(new Annotation[qualifiers.size()]), webBeansContext));
}
 
Example #11
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
Notifier(Class<?> runtimeType, List<ObserverMethod<? super T>> observerMethods, EventMetadata eventMetadata) {
    this.runtimeType = runtimeType;
    this.observerMethods = observerMethods;
    this.eventMetadata = eventMetadata;
    this.hasTxObservers = observerMethods.stream().anyMatch(this::isTxObserver);
}
 
Example #12
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
void notify(T event, ObserverExceptionHandler exceptionHandler, boolean async) {
    if (!isEmpty()) {

        Predicate<ObserverMethod<? super T>> predicate = async ? ObserverMethod::isAsync : this::isSyncObserver;

        if (!async && hasTxObservers) {
            // Note that tx observers are never async
            InstanceHandle<TransactionSynchronizationRegistry> registryInstance = Arc.container()
                    .instance(TransactionSynchronizationRegistry.class);

            if (registryInstance.isAvailable() &&
                    registryInstance.get().getTransactionStatus() == javax.transaction.Status.STATUS_ACTIVE) {
                // we have one or more transactional OM, and TransactionSynchronizationRegistry is available
                // we attempt to register a JTA synchronization
                List<DeferredEventNotification<?>> deferredEvents = new ArrayList<>();
                EventContext eventContext = new EventContextImpl<>(event, eventMetadata);

                for (ObserverMethod<? super T> om : observerMethods) {
                    if (isTxObserver(om)) {
                        deferredEvents.add(new DeferredEventNotification<>(om, eventContext,
                                Status.valueOf(om.getTransactionPhase())));
                    }
                }

                Synchronization sync = new ArcSynchronization(deferredEvents);
                TransactionSynchronizationRegistry registry = registryInstance.get();
                try {
                    registry.registerInterposedSynchronization(sync);
                    // registration succeeded, notify all non-tx observers synchronously
                    predicate = predicate.and(this::isNotTxObserver);
                } catch (Exception e) {
                    if (e.getCause() instanceof RollbackException || e.getCause() instanceof IllegalStateException) {
                        // registration failed, AFTER_SUCCESS OMs are accordingly to CDI spec left out
                        predicate = predicate.and(this::isNotAfterSuccess);
                    }
                }
            }
        }

        // Sync notifications
        ManagedContext requestContext = Arc.container().requestContext();
        if (requestContext.isActive()) {
            notifyObservers(event, exceptionHandler, predicate);
        } else {
            try {
                requestContext.activate();
                notifyObservers(event, exceptionHandler, predicate);
            } finally {
                requestContext.terminate();
            }
        }
    }
}
 
Example #13
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isTxObserver(ObserverMethod<?> observer) {
    return !observer.getTransactionPhase().equals(TransactionPhase.IN_PROGRESS);
}
 
Example #14
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isNotAfterSuccess(ObserverMethod<?> observer) {
    return !observer.getTransactionPhase().equals(TransactionPhase.AFTER_SUCCESS);
}
 
Example #15
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isNotTxObserver(ObserverMethod<?> observer) {
    return !isTxObserver(observer);
}
 
Example #16
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isSyncObserver(ObserverMethod<?> observer) {
    return !observer.isAsync();
}
 
Example #17
Source Project: quarkus   Author: quarkusio   File: EventImpl.java    License: Apache License 2.0 4 votes vote down vote up
DeferredEventNotification(ObserverMethod<? super T> observerMethod, EventContext eventContext, Status status) {
    this.observerMethod = observerMethod;
    this.isBeforeCompletion = observerMethod.getTransactionPhase().equals(TransactionPhase.BEFORE_COMPLETION);
    this.eventContext = eventContext;
    this.status = status;
}
 
Example #18
Source Project: quarkus   Author: quarkusio   File: SmallRyeOpenTracingProcessor.java    License: Apache License 2.0 4 votes vote down vote up
@BuildStep
ReflectiveMethodBuildItem registerMethod() throws Exception {
    Method isAsync = ObserverMethod.class.getMethod("isAsync");
    return new ReflectiveMethodBuildItem(isAsync);
}
 
Example #19
Source Project: weld-vertx   Author: weld   File: VertxExtension.java    License: Apache License 2.0 4 votes vote down vote up
private String getVertxAddress(ObserverMethod<?> observerMethod) {
    Annotation qualifier = getQualifier(observerMethod, VertxConsumer.class);
    return qualifier != null ? ((VertxConsumer) qualifier).value() : null;
}