All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scijava.plugin.DefaultPluginService 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 downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.

There is a newer version: 2.99.0
Show newest version
/*
 * #%L
 * SciJava Common shared library for SciJava software.
 * %%
 * Copyright (C) 2009 - 2016 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.plugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.scijava.InstantiableException;
import org.scijava.event.EventService;
import org.scijava.log.LogService;
import org.scijava.plugin.event.PluginsAddedEvent;
import org.scijava.plugin.event.PluginsRemovedEvent;
import org.scijava.service.AbstractService;
import org.scijava.service.Service;
import org.scijava.util.ListUtils;

/**
 * Default service for keeping track of available plugins.
 * 

* Available plugins are discovered using indexes generated by using * scijava-common as annotation processor. Loading of the actual plugin * classes can be deferred until a particular plugin is actually needed. *

*

* Plugins are added or removed via the plugin service are reported via the * event service. (No events are published for plugins directly added to or * removed from the {@link PluginIndex}.) *

* * @author Curtis Rueden * @author Johannes Schindelin * @see SciJavaPlugin * @see Plugin */ @Plugin(type = Service.class) public class DefaultPluginService extends AbstractService implements PluginService { @Parameter private LogService log; @Parameter private EventService eventService; /** Index of registered plugins. */ private PluginIndex pluginIndex; // -- PluginService methods -- @Override public PluginIndex getIndex() { return pluginIndex; } @Override public void reloadPlugins() { // clear all old plugins, and notify interested parties final List> oldPlugins = pluginIndex.getAll(); pluginIndex.clear(); if (oldPlugins.size() > 0) { eventService.publish(new PluginsRemovedEvent(oldPlugins)); } // re-discover all available plugins, and notify interested parties pluginIndex.discover(); final List> newPlugins = pluginIndex.getAll(); if (newPlugins.size() > 0) { eventService.publish(new PluginsAddedEvent(newPlugins)); } logExceptions(); } @Override public void addPlugin(final PluginInfo plugin) { if (pluginIndex.add(plugin)) { eventService.publish(new PluginsAddedEvent(plugin)); } } @Override public > void addPlugins(final Collection plugins) { if (pluginIndex.addAll(plugins)) { eventService.publish(new PluginsAddedEvent(plugins)); } } @Override public void removePlugin(final PluginInfo plugin) { if (pluginIndex.remove(plugin)) { eventService.publish(new PluginsRemovedEvent(plugin)); } } @Override public > void removePlugins( final Collection plugins) { if (pluginIndex.removeAll(plugins)) { eventService.publish(new PluginsRemovedEvent(plugins)); } } @Override public List> getPlugins() { return pluginIndex.getAll(); } @Override public

PluginInfo getPlugin( final Class

pluginClass) { return ListUtils.first(getPluginsOfClass(pluginClass)); } @Override public PluginInfo getPlugin(final Class

pluginClass, final Class type) { return ListUtils.first(getPluginsOfClass(pluginClass, type)); } @Override public PluginInfo getPlugin(final String className) { return ListUtils.first(getPluginsOfClass(className)); } @Override public List> getPluginsOfType( final Class type) { return pluginIndex.getPlugins(type); } @Override public

List> getPluginsOfClass(final Class

pluginClass) { // NB: We must scan *all* plugins for a match. In theory, the same plugin // Class could be associated with multiple PluginInfo entries of differing // type anyway. If performance of this method is insufficient, the solution // will be to rework the PluginIndex data structure to include an index on // plugin class names. return getPluginsOfClass(pluginClass, SciJavaPlugin.class); } @Override public List> getPluginsOfClass(final Class

pluginClass, final Class type) { final ArrayList> result = new ArrayList<>(); findPluginsOfClass(pluginClass, getPluginsOfType(type), result); filterNonmatchingClasses(pluginClass, result); return result; } @Override public List> getPluginsOfClass( final String className) { // NB: Since we cannot load the class in question, and cannot know its type // hierarch(y/ies) even if we did, we must scan *all* plugins for a match. return getPluginsOfClass(className, SciJavaPlugin.class); } @Override public List> getPluginsOfClass(final String className, final Class type) { final ArrayList> result = new ArrayList<>(); findPluginsOfClass(className, getPluginsOfType(type), result); return result; } @Override public List createInstancesOfType( final Class type) { final List> plugins = getPluginsOfType(type); return createInstances(plugins); } @Override public List createInstances( final List> infos) { final ArrayList list = new ArrayList<>(); for (final PluginInfo info : infos) { final PT p = createInstance(info); if (p != null) list.add(p); } return list; } @Override public PT createInstance(final PluginInfo info) { try { final PT p = info.createInstance(); context().inject(p); return p; } catch (final Throwable t) { log.error("Cannot create plugin: " + info, t); } return null; } // -- Service methods -- @Override public void initialize() { pluginIndex = context().getPluginIndex(); log.debug("Found " + pluginIndex.size() + " plugins."); if (log.isDebug()) { for (final PluginInfo info : pluginIndex) { log.debug("- " + info); } } logExceptions(); super.initialize(); } // -- Utility methods -- /** * Transfers plugins of the given class from the source list to the * destination list. Note that because this method compares class name * strings, it does not need to actually load the class in question. * * @param className The class name of the desired plugins. * @param srcList The list to scan for matching plugins. * @param destList The list to which matching plugins are added. */ public static > void findPluginsOfClass( final String className, final List> srcList, final List destList) { for (final PluginInfo info : srcList) { if (info.getClassName().equals(className)) { @SuppressWarnings("unchecked") final T match = (T) info; destList.add(match); } } } /** * Gets the plugin type of the given plugin class, as declared by its * {@code @Plugin} annotation (i.e., {@link Plugin#type()}). * * @param pluginClass The plugin class whose plugin type is needed. * @return The plugin type, or null if no {@link Plugin} annotation exists for * the given class. */ public static Class getPluginType( final Class

pluginClass) { final Plugin annotation = pluginClass.getAnnotation(Plugin.class); if (annotation == null) return null; @SuppressWarnings("unchecked") final Class type = (Class) annotation.type(); return type; } // -- Helper methods -- /** * Transfers plugins of the given class from the source list to the * destination list. Note that because this method compares class objects, it * must load the classes in question. * * @param pluginClass The class of the desired plugins. * @param srcList The list to scan for matching plugins. * @param destList The list to which matching plugins are added. */ private > void findPluginsOfClass( final Class pluginClass, final List> srcList, final List destList) { final String className = pluginClass.getName(); for (final PluginInfo info : srcList) { try { final Class clazz2 = info.getPluginClass(); if (clazz2 == pluginClass || (info.getClassName().equals(className) && info.loadClass() == pluginClass)) { @SuppressWarnings("unchecked") final T match = (T) info; destList.add(match); } } catch (InstantiableException exc) { log.debug("Ignoring plugin: " + info, exc); } } } /** * Filters the given list to include only entries with matching * classes (not just class names). */ private void filterNonmatchingClasses(final Class

pluginClass, final ArrayList> result) { for (final Iterator> iter = result.iterator(); iter.hasNext(); ) { try { if (iter.next().loadClass() != pluginClass) iter.remove(); } catch (InstantiableException exc) { log.debug(exc); iter.remove(); } } } /** Logs any exceptions that occurred during the last plugin discovery. */ private void logExceptions() { final Map exceptions = pluginIndex.getExceptions(); final int excCount = exceptions.size(); if (excCount > 0) { log.warn(excCount + " exceptions occurred during plugin discovery."); if (log.isDebug()) { for (final String name : exceptions.keySet()) { final Throwable t = exceptions.get(name); log.debug(name, t); } } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy