
org.scijava.event.DefaultEventService Maven / Gradle / Ivy
Go to download
SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by both ImageJ and SCIFIO.
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2014 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.event;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.WeakHashMap;
import org.bushe.swing.event.annotation.AbstractProxySubscriber;
import org.bushe.swing.event.annotation.BaseProxySubscriber;
import org.bushe.swing.event.annotation.ReferenceStrength;
import org.scijava.Priority;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.service.AbstractService;
import org.scijava.service.Service;
import org.scijava.thread.ThreadService;
/**
* Default service for publishing and subscribing to SciJava events.
*
* @author Curtis Rueden
* @author Grant Harris
*/
@Plugin(type = Service.class, priority = DefaultEventService.PRIORITY)
public class DefaultEventService extends AbstractService implements
EventService
{
/**
* The default event service's priority.
*
* It is paramount that the event service be created and initialized before
* other services. Any service created before the event service may not have
* its event handling methods registered correctly.
*
*
* Alternative event service implementations that wish to prioritize
* themselves above this one can still ensure preferential usage via
* {@code priority = DefaultEventService.PRIORITY + 1} or similar.
*
*/
public static final double PRIORITY = 10 * Priority.VERY_HIGH_PRIORITY;
@Parameter
private LogService log;
@Parameter
private ThreadService threadService;
private DefaultEventBus eventBus;
// -- EventService methods --
@Override
public void publish(final E e) {
e.setContext(getContext());
eventBus.publishNow(e);
}
@Override
public void publishLater(final E e) {
e.setContext(getContext());
eventBus.publishLater(e);
}
@Override
public List> subscribe(final Object o) {
final List> subscribers =
new ArrayList>();
subscribeRecursively(subscribers, o.getClass(), o);
return subscribers;
}
@Override
public void unsubscribe(final Collection> subscribers) {
for (final EventSubscriber> subscriber : subscribers) {
unsubscribe(subscriber);
}
}
@Override
public List> getSubscribers(
final Class c)
{
// HACK - It appears that EventBus API is incorrect, in that
// EventBus#getSubscribers(Class) returns a List when it should
// actually be a List>. This method works around the
// problem with casts.
@SuppressWarnings("rawtypes")
final List list = eventBus.getSubscribers(c);
@SuppressWarnings("unchecked")
final List> typedList = list;
return typedList;
}
// -- Service methods --
@Override
public void initialize() {
eventBus = new DefaultEventBus(threadService, log);
super.initialize();
}
// -- Disposable methods --
@Override
public void dispose() {
eventBus.clearAllSubscribers();
}
// -- Helper methods --
/**
* Recursively scans for @{@link EventHandler} annotated methods, and
* subscribes them to the event service.
*/
private void subscribeRecursively(
final List> subscribers, final Class> type,
final Object o)
{
if (type == null || type == Object.class) return;
for (final Method m : type.getDeclaredMethods()) {
final EventHandler ann = m.getAnnotation(EventHandler.class);
if (ann == null) continue; // not an event handler method
final Class extends SciJavaEvent> eventClass = getEventClass(m);
if (eventClass == null) {
log.warn("Invalid EventHandler method: " + m);
continue;
}
subscribers.add(subscribe(eventClass, o, m));
}
subscribeRecursively(subscribers, type.getSuperclass(), o);
}
private void subscribe(final Class c,
final EventSubscriber subscriber)
{
eventBus.subscribe(c, subscriber);
}
private void unsubscribe(
final EventSubscriber subscriber)
{
unsubscribe(subscriber.getEventClass(), subscriber);
}
private void unsubscribe(final Class c,
final EventSubscriber subscriber)
{
eventBus.unsubscribe(c, subscriber);
}
private EventSubscriber subscribe(
final Class c, final Object o, final Method m)
{
final ProxySubscriber subscriber = new ProxySubscriber(c, o, m);
subscribe(c, subscriber);
return subscriber;
}
/** Gets the event class parameter of the given method. */
private Class extends SciJavaEvent> getEventClass(final Method m) {
final Class>[] c = m.getParameterTypes();
if (c == null || c.length != 1) return null; // wrong number of args
if (!SciJavaEvent.class.isAssignableFrom(c[0])) return null; // wrong class
@SuppressWarnings("unchecked")
final Class extends SciJavaEvent> eventClass =
(Class extends SciJavaEvent>) c[0];
return eventClass;
}
// -- Event handlers garbage collection preventer --
private WeakHashMap
© 2015 - 2025 Weber Informatics LLC | Privacy Policy