Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8 #326

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

V8 #326

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.vaadin.spring.events.Event;
import org.vaadin.spring.events.EventBus;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;

/**
* Base implementation of {@link org.vaadin.spring.events.internal.ListenerCollection.Listener} that implements
* the {@link #supports(org.vaadin.spring.events.Event)} method. An event is supported if:
Expand Down Expand Up @@ -56,7 +59,7 @@ public AbstractListenerWrapper(EventBus owningEventBus, Object listenerTarget, S
* Gets the target object that this listener is wrapping.
*/
public Object getListenerTarget() {
return listenerTarget;
return listenerTarget instanceof Reference ? ((Reference) listenerTarget).get() : listenerTarget;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*/
package org.vaadin.spring.events.internal;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.*;

import org.slf4j.Logger;
Expand All @@ -30,9 +36,9 @@
class ListenerCollection implements Serializable {

private static final long serialVersionUID = -6237902400879667320L;
private final Logger logger = LoggerFactory.getLogger(getClass());
private final Set<Listener> listeners = new HashSet<Listener>();
private final Set<Listener> weakListeners = Collections.newSetFromMap(new WeakHashMap<Listener, Boolean>());
private Logger logger = LoggerFactory.getLogger(getClass());
private Set<Listener> listeners = new HashSet<Listener>();
private Set<Listener> weakListeners = Collections.newSetFromMap(new WeakHashMap<Listener, Boolean>());

/**
* Interface defining a listener.
Expand Down Expand Up @@ -188,4 +194,24 @@ private void removeFilteredListenersFromSet(ListenerFilter filter, Set<Listener>
}
}
}

private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException {

this.listeners = (Set<Listener>) ois.readObject();
this.weakListeners = Collections.newSetFromMap(new WeakHashMap<Listener, Boolean>());
this.weakListeners.addAll( (Set<Listener>) ois.readObject() ) ;
this.logger = LoggerFactory.getLogger(getClass());
logger.debug("after readObject weakListeners = {}", Arrays.deepToString(weakListeners.toArray()));
}

private void writeObject(ObjectOutputStream oos)
throws IOException {

oos.writeObject(listeners);
oos.writeObject(new HashSet<>(weakListeners));
logger.debug("writeObject weakListeners = {}", Arrays.deepToString(weakListeners.toArray()));

//oos.writeObject(logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.vaadin.spring.events.*;
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
import org.vaadin.spring.events.annotation.EventBusListenerTopic;
import org.vaadin.spring.util.ClassUtils;

import java.io.IOException;
import java.io.ObjectInputStream;
Expand All @@ -39,6 +40,7 @@ class MethodListenerWrapper extends AbstractListenerWrapper {
private final Class<?> payloadType;
private final boolean payloadMethod;
private transient Method listenerMethod;
private final int methodHash; // TODO: better unique ID
private final String topic;

public MethodListenerWrapper(EventBus owningEventBus, Object listenerTarget, String topic, boolean includingPropagatingEvents, Method listenerMethod) {
Expand All @@ -53,11 +55,23 @@ public MethodListenerWrapper(EventBus owningEventBus, Object listenerTarget, Str
payloadMethod = true;
}
this.listenerMethod = listenerMethod;
this.methodHash = listenerMethod.hashCode();
}

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
// TODO Read listener method info and look up method
ClassUtils.visitClassHierarchy(new ClassUtils.ClassVisitor() {
@Override
public void visit(Class<?> clazz) {
for (Method m : clazz.getDeclaredMethods()) {
if (m.hashCode() == methodHash) {
listenerMethod = m;
return;
}
}
}
}, getListenerTarget().getClass());
}

private void writeObject(ObjectOutputStream oos) throws IOException {
Expand Down Expand Up @@ -95,6 +109,11 @@ public void publish(Event<?> event) {
public boolean supports(Event<?> event) {
boolean supports = super.supports(event);
try {
// prevent GCed weak ref NPE
if (getListenerTarget() == null) {
return false;
}

if (listenerMethod.isAnnotationPresent(EventBusListenerMethod.class)) {
supports = supports && isInterestedListenerMethod(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import javax.annotation.PreDestroy;
import java.io.Serializable;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;

/**
Expand Down Expand Up @@ -60,6 +62,41 @@ public void onEvent(final Event<Object> event) {

};

private ReferenceQueue<Object> mlwWeakReferenceQueue = new ReferenceQueue<>();

private boolean cleanupThreadRun = true;
private Thread cleanupThread = new Thread() {

public void run() {
while(cleanupThreadRun) {
try {
ReferenceWithCleanup ref = (ReferenceWithCleanup)mlwWeakReferenceQueue.remove();
ref.cleanUp();
} catch (InterruptedException e) {
logger.warn("cleanupThread interrupted: ",e);
}
}
}
};

class ReferenceWithCleanup extends WeakReference<Object> {
ListenerCollection.Listener mlwListener;
ReferenceWithCleanup(Object weakOriginalListener) {
super(weakOriginalListener, mlwWeakReferenceQueue);

}

public ReferenceWithCleanup withMlwListener(ListenerCollection.Listener mlwListener) {
this.mlwListener = mlwListener;
return this;
}

public void cleanUp() {
logger.trace("Clear reference to listener [{}]", mlwListener);
listeners.remove(mlwListener);
}
}

/**
* @param scope the scope of the events that this event bus handles.
*/
Expand Down Expand Up @@ -88,12 +125,14 @@ public ScopedEventBus(EventScope scope, EventBus parentEventBus) {
logger.debug("Using parent event bus [{}]", this.parentEventBus);
this.parentEventBus.subscribe(parentListener);
}
cleanupThread.start();
}

@PreDestroy
void destroy() {
logger.trace("Destroying event bus [{}] and removing all listeners", this);
listeners.clear();
cleanupThreadRun = false;
if (parentEventBus != null) {
parentEventBus.unsubscribe(parentListener);
}
Expand Down Expand Up @@ -217,13 +256,18 @@ public void visit(Class<?> clazz) {
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);

MethodListenerWrapper l;
if (weakReference) {
listeners.addWithWeakReference(l);
ReferenceWithCleanup ref = new ReferenceWithCleanup(listener);
l = new MethodListenerWrapper(ScopedEventBus.this, ref, topic,
includingPropagatingEvents, m);
ref.withMlwListener(l);
} else {
listeners.add(l);
l = new MethodListenerWrapper(ScopedEventBus.this, listener, topic,
includingPropagatingEvents, m);
}
listeners.add(l);
foundMethods[0]++;
} else {
throw new IllegalArgumentException(
Expand Down