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 File: ObserverInfo.java    From quarkus with 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 File: CdiPlugin.java    From tomee with 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 File: ObserverConfigurator.java    From quarkus with 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 File: OpenEJBTransactionService.java    From tomee with 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 #5
Source File: WebappBeanManager.java    From tomee with 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 #6
Source File: VertxExtension.java    From weld-vertx with 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 #7
Source File: BeanManagerTest.java    From quarkus with 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 File: BeanManagerImpl.java    From quarkus with 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 #9
Source File: EventImpl.java    From quarkus with 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 #10
Source File: EventImpl.java    From quarkus with 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 #11
Source File: EventImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private boolean isNotTxObserver(ObserverMethod<?> observer) {
    return !isTxObserver(observer);
}
 
Example #12
Source File: EventImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private boolean isSyncObserver(ObserverMethod<?> observer) {
    return !observer.isAsync();
}
 
Example #13
Source File: EventImpl.java    From quarkus with 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 #14
Source File: EventImpl.java    From quarkus with 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 File: EventImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private boolean isTxObserver(ObserverMethod<?> observer) {
    return !observer.getTransactionPhase().equals(TransactionPhase.IN_PROGRESS);
}
 
Example #16
Source File: SmallRyeOpenTracingProcessor.java    From quarkus with 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 #17
Source File: VertxExtension.java    From weld-vertx with 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;
}
 
Example #18
Source File: EventImpl.java    From quarkus with 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 #19
Source File: EventImpl.java    From quarkus with 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);
}