private void onMessage(Class<? extends Annotation> cls, JMSNotificationType notificationType, Type type) throws JMSException { when(this.message.getStringProperty(MESSAGE.toString())).thenReturn("foo"); final Extension extension = mock(Extension.class); when(this.beanManager.getExtension(Extension.class)).thenReturn(extension); @SuppressWarnings("unchecked") final ObserverMethod<DestinationChanged> observerMethod = mock(ObserverMethod.class); when(extension.getDestinationObservers(cls)).thenReturn(singleton(observerMethod)); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(of(cls))); when(this.message.getStringProperty(HDR_NOTIFICATION_TYPE.toString())).thenReturn(notificationType.name()); this.eventProducer.onMessage(message); verify(this.message).getStringProperty(MESSAGE.toString()); verify(this.message).getStringProperty(HDR_NOTIFICATION_TYPE.toString()); verify(this.log).info(eq("Destination changed. [type={},destination={}]"), eq(type), anyString()); verify(this.beanManager).getExtension(Extension.class); verify(extension).getDestinationObservers(cls); verify(observerMethod).getObservedQualifiers(); verify(observerMethod).notify(any()); verifyNoMoreInteractions(extension, observerMethod); }
@Test public void onMessage_noMatch() throws JMSException { when(this.message.getStringProperty(MESSAGE.toString())).thenReturn("foo"); final Extension extension = mock(Extension.class); when(this.beanManager.getExtension(Extension.class)).thenReturn(extension); @SuppressWarnings("unchecked") final ObserverMethod<DestinationChanged> observerMethod = mock(ObserverMethod.class); when(extension.getDestinationObservers(OnAdded.class)).thenReturn(singleton(observerMethod)); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(of(OnAdded.class, singletonMap("value", "acme")))); when(this.message.getStringProperty(HDR_NOTIFICATION_TYPE.toString())).thenReturn(JMSNotificationType.TOPIC_CREATED.name()); this.eventProducer.onMessage(message); verify(this.message).getStringProperty(MESSAGE.toString()); verify(this.message).getStringProperty(HDR_NOTIFICATION_TYPE.toString()); verify(this.log).info(eq("Destination changed. [type={},destination={}]"), eq(Type.ADDED), anyString()); verify(this.beanManager).getExtension(Extension.class); verify(extension).getDestinationObservers(OnAdded.class); verify(observerMethod).getObservedQualifiers(); verifyNoMoreInteractions(extension, observerMethod); }
/** * * @param annotation * @param observerMethods * @param destination * @param evt * @return {@code true} if one or more notifiers consumed the event. */ private static <A extends Annotation> boolean notify(Class<A> annotation, Set<ObserverMethod<Message>> observerMethods, String destination, Message evt) { boolean consumed = false; for (ObserverMethod<Message> om : observerMethods) { for (A a : getAnnotations(annotation, om.getObservedQualifiers())) { final String value = ReflectionUtil.invoke(a, "value"); if (!Glob.from(value).matches(destination)) { continue; } try (QuietClosable closable = PathParamProducer.set(value)) { om.notify(evt); consumed = true; } } } return consumed; }
/** * * @param e * @param beanManager */ public void registerMessageEvent(@Observes ProcessObserverMethod<Message, ?> e) { final ObserverMethod<Message> method = e.getObserverMethod(); for (Annotation a : method.getObservedQualifiers()) { if (a instanceof OnConnected) { registerMessageObserver(OnConnected.class, method); // e.veto(); // CDI 2.0 } else if (a instanceof OnSend) { registerMessageObserver(OnSend.class, method); // e.veto(); // CDI 2.0 } else if (a instanceof OnSubscribe) { registerMessageObserver(OnSubscribe.class, method); // e.veto(); // CDI 2.0 } else if (a instanceof OnUnsubscribe) { registerMessageObserver(OnUnsubscribe.class, method); // e.veto(); // CDI 2.0 } else if (a instanceof OnDisconnect) { registerMessageObserver(OnDisconnect.class, method); // e.veto(); // CDI 2.0 } } }
@Test @SuppressWarnings("unchecked") public void register_onConnected() { final ProcessObserverMethod<Message, ?> processObserverMethod = mock(ProcessObserverMethod.class); final ObserverMethod<Message> observerMethod = mock(ObserverMethod.class); when(processObserverMethod.getObserverMethod()).thenReturn(observerMethod); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(OnConnected.Literal.onConnected())); this.extension.registerMessageEvent(processObserverMethod); assertEquals(observerMethod, getMessageObservers(this.extension).get(OnConnected.class).iterator().next()); verify(processObserverMethod).getObserverMethod(); verify(observerMethod).getObservedQualifiers(); verifyNoMoreInteractions(processObserverMethod, observerMethod); }
@Test @SuppressWarnings("unchecked") public void register_onSend() { final ProcessObserverMethod<Message, ?> processObserverMethod = mock(ProcessObserverMethod.class); final ObserverMethod<Message> observerMethod = mock(ObserverMethod.class); when(processObserverMethod.getObserverMethod()).thenReturn(observerMethod); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(onSend(""))); this.extension.registerMessageEvent(processObserverMethod); assertEquals(observerMethod, getMessageObservers(this.extension).get(OnSend.class).iterator().next()); verify(processObserverMethod).getObserverMethod(); verify(observerMethod).getObservedQualifiers(); verifyNoMoreInteractions(processObserverMethod, observerMethod); }
@Test @SuppressWarnings("unchecked") public void register_onSubscribe() { final ProcessObserverMethod<Message, ?> processObserverMethod = mock(ProcessObserverMethod.class); final ObserverMethod<Message> observerMethod = mock(ObserverMethod.class); when(processObserverMethod.getObserverMethod()).thenReturn(observerMethod); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(onSubscribe(""))); this.extension.registerMessageEvent(processObserverMethod); assertEquals(observerMethod, getMessageObservers(this.extension).get(OnSubscribe.class).iterator().next()); verify(processObserverMethod).getObserverMethod(); verify(observerMethod).getObservedQualifiers(); verifyNoMoreInteractions(processObserverMethod, observerMethod); }
@Test @SuppressWarnings("unchecked") public void register_onUnsubscribe() { final ProcessObserverMethod<Message, ?> processObserverMethod = mock(ProcessObserverMethod.class); final ObserverMethod<Message> observerMethod = mock(ObserverMethod.class); when(processObserverMethod.getObserverMethod()).thenReturn(observerMethod); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(onUnsubscribe(""))); this.extension.registerMessageEvent(processObserverMethod); assertEquals(observerMethod, getMessageObservers(this.extension).get(OnUnsubscribe.class).iterator().next()); verify(processObserverMethod).getObserverMethod(); verify(observerMethod).getObservedQualifiers(); verifyNoMoreInteractions(processObserverMethod, observerMethod); }
@Test @SuppressWarnings("unchecked") public void register_onDisconnect() { final ProcessObserverMethod<Message, ?> processObserverMethod = mock(ProcessObserverMethod.class); final ObserverMethod<Message> observerMethod = mock(ObserverMethod.class); when(processObserverMethod.getObserverMethod()).thenReturn(observerMethod); when(observerMethod.getObservedQualifiers()).thenReturn(singleton(onDisconnect())); this.extension.registerMessageEvent(processObserverMethod); assertEquals(observerMethod, getMessageObservers(this.extension).get(OnDisconnect.class).iterator().next()); verify(processObserverMethod).getObserverMethod(); verify(observerMethod).getObservedQualifiers(); verifyNoMoreInteractions(processObserverMethod, observerMethod); }
/** * Tests the size of observer methods in a jar archive */ @Test public void testSize() { logger.info("starting size event test"); Set<ObserverMethod<? super Bill>> observers = beanManager.resolveObserverMethods(new Bill()); assertEquals(observers.size(), 10); }
/** * * @param annotation * @param observerMethods * @param destination * @param evt */ private static <A extends Annotation> void notify(Class<A> annotation, Set<ObserverMethod<DestinationChanged>> observerMethods, String destination, DestinationChanged evt) { for (ObserverMethod<DestinationChanged> om : observerMethods) { for (A a : getAnnotations(annotation, om.getObservedQualifiers())) { final String value = ReflectionUtil.invoke(a, "value"); if (!Glob.from(value).matches(destination)) { continue; } try (QuietClosable closable = PathParamProducer.set(value)) { om.notify(evt); } } } }
/** * * @param cls * @param method */ private <A extends Annotation> void registerMessageObserver(Class<A> cls, ObserverMethod<Message> method) { Set<ObserverMethod<Message>> annotations = this.messageObservers.get(cls); if (annotations == null) { annotations = new HashSet<>(); this.messageObservers.put(cls, annotations); } annotations.add(method); }
/** * * @param cls * @param method */ private <A extends Annotation> void registerDestinationObserver(Class<A> cls, ObserverMethod<DestinationChanged> method) { Set<ObserverMethod<DestinationChanged>> annotations = this.destinationObservers.get(cls); if (annotations == null) { annotations = new HashSet<>(); this.destinationObservers.put(cls, annotations); } annotations.add(method); }
/** * * @param e * @param beanManager */ public void registerDestinationEvent(@Observes ProcessObserverMethod<DestinationChanged, ?> e) { final ObserverMethod<DestinationChanged> method = e.getObserverMethod(); for (Annotation a : method.getObservedQualifiers()) { if (a instanceof OnAdded) registerDestinationObserver(OnAdded.class, method); if (a instanceof OnRemoved) registerDestinationObserver(OnRemoved.class, method); } }
@Test @SuppressWarnings("unchecked") public void getObservers() { final ObserverMethod<Message> observerMethod = mock(ObserverMethod.class); getMessageObservers(this.extension).put(OnSubscribe.class, Collections.singleton(observerMethod)); final Set<ObserverMethod<Message>> results = this.extension.getMessageObservers(OnSubscribe.class); assertEquals(Collections.singleton(observerMethod), results); verifyNoMoreInteractions(observerMethod); }
private Annotation getQualifier(ObserverMethod<?> observerMethod, Class<? extends Annotation> annotationType) { for (Annotation qualifier : observerMethod.getObservedQualifiers()) { if (qualifier.annotationType().equals(annotationType)) { return qualifier; } } return null; }
@Override public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(final T event, final EventMetadataImpl metadata) { final Set<ObserverMethod<? super T>> set = new HashSet<>(); set.addAll(getNotificationManager().resolveObservers(event, metadata, false)); if (isEvent(event)) { final BeanManagerImpl parentBm = getParentBm(); if (parentBm != null) { set.addAll(parentBm.getNotificationManager().resolveObservers(event, metadata, false)); } } // else nothing since extensions are loaded by classloader so we already have it return set; }
@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)); }
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()) && doResolveViewMethod(bean, method) == null) { throw new WebBeansConfigurationException("@Observes " + method + " neither in the ejb view of ejb " + bean.getBeanContext().getEjbName() + " nor static"); } } }
private String getVertxAddress(ObserverMethod<?> observerMethod) { Annotation qualifier = getQualifier(observerMethod, VertxConsumer.class); return qualifier != null ? ((VertxConsumer) qualifier).value() : null; }
@Override public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(T event, Annotation... qualifiers) { // TODO Auto-generated method stub return null; }
@Override public <T> Set<ObserverMethod<? super T>> resolveObserverMethods( final T arg0, final Annotation... arg1) { return this.beanManager.resolveObserverMethods(arg0, arg1); }
/** * * @param qualifier * @return */ public Set<ObserverMethod<Message>> getMessageObservers(Class<? extends Annotation> qualifier) { final Set<ObserverMethod<Message>> observers = this.messageObservers.get(qualifier); return observers == null ? Collections.emptySet() : observers; }
/** * * @param qualifier * @return */ public Set<ObserverMethod<DestinationChanged>> getDestinationObservers(Class<? extends Annotation> qualifier) { final Set<ObserverMethod<DestinationChanged>> observers = this.destinationObservers.get(qualifier); return observers == null ? Collections.emptySet() : observers; }
/** * * @param e * @return */ private static Map<Class<? extends Annotation>, Set<ObserverMethod<Message>>> getMessageObservers(Extension e) { return ReflectionUtil.get(e, "messageObservers"); }