Skip to content

Commit

Permalink
Update event bus to support Vaadin Flow and a new Spring version
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Lumme committed Feb 14, 2019
1 parent d86c01a commit 617f9c9
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 108 deletions.
36 changes: 33 additions & 3 deletions addons/eventbus/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.vaadin.spring</groupId>
Expand All @@ -14,6 +15,22 @@

<name>Vaadin4Spring Add-ons - Event Bus</name>

<properties>
<spring.version>5.1.5.RELEASE</spring.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
Expand All @@ -23,6 +40,7 @@
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring</artifactId>
<version>${vaadin.platform.spring.version}</version>
</dependency>
<dependency>
<groupId>org.vaadin.spring.extensions</groupId>
Expand All @@ -36,8 +54,20 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@
* <li>Events are scoped</li>
* </ul>
* <p>
* There are four event scopes, and therefore four event bus types (each with their own sub interface):
* There are three event scopes, and therefore three event bus types (each with their own sub interface):
* <ol>
* <li>{@link EventScope#APPLICATION} events are published to the entire application.</li>
* <li>{@link EventScope#SESSION} events are published to the current session.</li>
* <li>{@link EventScope#UI} events are published to the current UI.</li>
* <li>{@link EventScope#VIEW} events are published to the current view.</li>
* </ol>
* <p>
* The event buses are chained in the following way:
* <ul>
* <li>Application events are propagated to the session event bus.</li>
* <li>Session events are propagated to the UI event bus.</li>
* <li>UI events are propagated to the view event bus.</li>
* </ul>
* Furthermore, {@link org.springframework.context.ApplicationEventPublisher} events can be propagated to any event bus
* by using {@link org.vaadin.spring.events.support.ApplicationContextEventBroker}.
Expand Down Expand Up @@ -272,12 +270,4 @@ interface SessionEventBus extends EventBus {
*/
interface UIEventBus extends EventBus {
}

/**
* Interface implemented by the view scoped event bus.
*
* @see org.vaadin.spring.events.EventScope#VIEW
*/
interface ViewEventBus extends EventBus {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,4 @@ interface UIEventBusAware extends EventBusAware {
*/
void setUIEventBus(EventBus.UIEventBus uiEventBus);
}

/**
* Interface to be implemented by beans that want to get notified of the
* view event bus.
*/
interface ViewEventBusAware extends EventBusAware {
/**
* Sets the view scoped event bus.
*/
void setViewEventBus(EventBus.ViewEventBus viewEventBus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public enum EventScope {
*/
UI,

/**
* The event is specific to the current view.
*/
VIEW,

/**
* Undefined event scope. An internal event scope used only when no scope has been explicitly defined.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package org.vaadin.spring.events.config;

import com.vaadin.spring.internal.UIScopeImpl;
import com.vaadin.spring.internal.VaadinSessionScope;
import com.vaadin.spring.internal.ViewScopeImpl;
import com.vaadin.flow.spring.scopes.VaadinSessionScope;
import com.vaadin.flow.spring.scopes.VaadinUIScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
Expand Down Expand Up @@ -61,30 +60,16 @@ EventBus.SessionEventBus sessionEventBus() {
}

@Bean
@Scope(value = UIScopeImpl.VAADIN_UI_SCOPE_NAME, proxyMode = ScopedProxyMode.INTERFACES)
@Scope(value = VaadinUIScope.VAADIN_UI_SCOPE_NAME, proxyMode = ScopedProxyMode.INTERFACES)
@EventBusProxy
EventBus.UIEventBus proxiedUiEventBus() {
return uiEventBus();
}

@Bean
@Scope(value = UIScopeImpl.VAADIN_UI_SCOPE_NAME, proxyMode = ScopedProxyMode.NO)
@Scope(value = VaadinUIScope.VAADIN_UI_SCOPE_NAME, proxyMode = ScopedProxyMode.NO)
@Primary
EventBus.UIEventBus uiEventBus() {
return new ScopedEventBus.DefaultUIEventBus(sessionEventBus());
}

@Bean
@Scope(value = ViewScopeImpl.VAADIN_VIEW_SCOPE_NAME, proxyMode = ScopedProxyMode.INTERFACES)
@EventBusProxy
EventBus.ViewEventBus proxiedViewEventBus() {
return viewEventBus();
}

@Bean
@Scope(value = ViewScopeImpl.VAADIN_VIEW_SCOPE_NAME, proxyMode = ScopedProxyMode.NO)
@Primary
EventBus.ViewEventBus viewEventBus() {
return new ScopedEventBus.DefualtViewEventBus(uiEventBus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public abstract class ScopedEventBus implements EventBus, Serializable {

private static final long serialVersionUID = 1637290543180920954L;
private static final long serialVersionUID = -582697574672947883L;
private final Logger logger = LoggerFactory.getLogger(getClass());
private final EventScope eventScope;

Expand Down Expand Up @@ -210,25 +210,22 @@ private void subscribe(final Object listener, final String topic, final boolean
listener, this, includingPropagatingEvents, weakReference);

final int[] foundMethods = new int[1];
ClassUtils.visitClassHierarchy(new ClassUtils.ClassVisitor() {
@Override
public void visit(Class<?> clazz) {
for (Method m : clazz.getDeclaredMethods()) {
if (m.isAnnotationPresent(EventBusListenerMethod.class)) {
if (m.getParameterTypes().length == 1) {
logger.trace("Found listener method [{}] in listener [{}]", m.getName(), listener);
MethodListenerWrapper l = new MethodListenerWrapper(ScopedEventBus.this, listener, topic,
includingPropagatingEvents, m);
if (weakReference) {
listeners.addWithWeakReference(l);
} else {
listeners.add(l);
}
foundMethods[0]++;
ClassUtils.visitClassHierarchy(clazz -> {
for (Method m : clazz.getDeclaredMethods()) {
if (m.isAnnotationPresent(EventBusListenerMethod.class)) {
if (m.getParameterTypes().length == 1) {
logger.trace("Found listener method [{}] in listener [{}]", m.getName(), listener);
MethodListenerWrapper l = new MethodListenerWrapper(ScopedEventBus.this, listener, topic,
includingPropagatingEvents, m);
if (weakReference) {
listeners.addWithWeakReference(l);
} else {
throw new IllegalArgumentException(
"Listener method " + m.getName() + " does not have the required signature");
listeners.add(l);
}
foundMethods[0]++;
} else {
throw new IllegalArgumentException(
"Listener method " + m.getName() + " does not have the required signature");
}
}
}
Expand All @@ -247,13 +244,8 @@ public <T> void unsubscribe(EventBusListener<T> listener) {
@Override
public void unsubscribe(final Object listener) {
logger.trace("Unsubscribing listener [{}] from event bus [{}]", listener, this);
listeners.removeAll(new ListenerCollection.ListenerFilter() {
@Override
public boolean passes(ListenerCollection.Listener l) {
return (l instanceof AbstractListenerWrapper)
&& (((AbstractListenerWrapper) l).getListenerTarget() == listener);
}
});
listeners.removeAll(l -> (l instanceof AbstractListenerWrapper)
&& (((AbstractListenerWrapper) l).getListenerTarget() == listener));
}

/**
Expand Down Expand Up @@ -301,14 +293,4 @@ public DefaultUIEventBus(SessionEventBus parentEventBus) {
super(EventScope.UI, parentEventBus);
}
}

/**
* Default implementation of {@link org.vaadin.spring.events.EventBus.ViewEventBus}.
*/
public static class DefualtViewEventBus extends ScopedEventBus implements ViewEventBus {

public DefualtViewEventBus(UIEventBus parentEventBus) {
super(EventScope.VIEW, parentEventBus);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,9 @@ public Object postProcessBeforeInitialization(final Object bean, String beanName
}

if (acc != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {

@Override
public Object run() {
invokeAwareInterfaces(bean);
return null;
}

AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareInterfaces(bean);
return null;
}, acc);
} else {
invokeAwareInterfaces(bean);
Expand All @@ -86,9 +81,6 @@ private void invokeAwareInterfaces(Object bean) {
if (bean instanceof EventBusAware.UIEventBusAware) {
((EventBusAware.UIEventBusAware) bean).setUIEventBus(this.applicationContext.getBean(EventBus.UIEventBus.class));
}
if (bean instanceof EventBusAware.ViewEventBusAware) {
((EventBusAware.ViewEventBusAware) bean).setViewEventBus(this.applicationContext.getBean(EventBus.ViewEventBus.class));
}

}
}
Expand Down
Loading

0 comments on commit 617f9c9

Please sign in to comment.