Skip to content

Rich plugin usage #110

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

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>scijava-common</artifactId>
<version>2.28.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<name>SciJava Common</name>
<description>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.</description>
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/scijava/AbstractGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ public <S extends Service> S get(final Class<S> serviceClass) {
return context().service(serviceClass);
}

@Override
public Service get(final String serviceClassName) {
return context().service(serviceClassName);
}

// -- Gateway methods - services --

@Override
Expand Down Expand Up @@ -246,13 +241,15 @@ public String getTitle() {
}

@Override
public String getVersion() {
return getApp().getVersion();
public String getInfo(final boolean mem) {
return getApp().getInfo(mem);
}

// -- Versioned methods --

@Override
public String getInfo(final boolean mem) {
return getApp().getInfo(mem);
public String getVersion() {
return getApp().getVersion();
}

}
20 changes: 1 addition & 19 deletions src/main/java/org/scijava/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
* @author Mark Hiner
* @author Curtis Rueden
*/
public interface Gateway extends RichPlugin, Versioned {
public interface Gateway extends RichPlugin {

/**
* Returns an implementation of the requested {@link Service}, if it exists in
Expand All @@ -130,18 +130,6 @@ public interface Gateway extends RichPlugin, Versioned {
*/
<S extends Service> S get(Class<S> serviceClass);

/**
* Returns an implementation of the {@link Service} with the given class name,
* if it exists in the underlying {@link Context}.
*
* @param serviceClassName name of the requested {@link Service}
* @return The singleton instance of the requested {@link Service}
* @throws NullContextException if the application context is not set.
* @throws NoSuchServiceException if there is no service matching
* {@code serviceClassName}.
*/
Service get(final String serviceClassName);

// -- Gateway methods - services --

AppEventService appEvent();
Expand Down Expand Up @@ -205,10 +193,4 @@ public interface Gateway extends RichPlugin, Versioned {
/** @see org.scijava.app.App#getInfo(boolean) */
String getInfo(boolean mem);

// -- Versioned methods --

/** @see org.scijava.app.App#getVersion() */
@Override
String getVersion();

}
14 changes: 9 additions & 5 deletions src/main/java/org/scijava/app/AbstractApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,13 @@ public abstract class AbstractApp extends AbstractRichPlugin implements App {
/** JAR manifest with metadata about the application. */
private Manifest manifest;

// -- App methods --

@Override
public String getTitle() {
return getInfo().getName();
}

@Override
public String getVersion() {
return getPOM() == null ? "Unknown" : getPOM().getVersion();
}

@Override
public POM getPOM() {
if (pom == null) {
Expand Down Expand Up @@ -108,4 +105,11 @@ public File getBaseDirectory() {
return AppUtils.getBaseDirectory(getSystemProperty(), getClass(), null);
}

// -- Versioned methods --

@Override
public String getVersion() {
return getPOM() == null ? "Unknown" : getPOM().getVersion();
}

}
5 changes: 4 additions & 1 deletion src/main/java/org/scijava/plugin/AbstractHandlerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public abstract class AbstractHandlerService<DT, PT extends HandlerPlugin<DT>>
@Override
public PT getHandler(final DT data) {
for (final PT handler : getInstances()) {
if (handler.supports(data)) return handler;
if (handler.supports(data)) {
recordUsage(handler);
return handler;
}
}
return null;
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/scijava/plugin/AbstractPTService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;

import org.scijava.service.AbstractService;
import org.scijava.usage.UsageService;

/**
* Abstract base class for {@link PTService}s.
Expand All @@ -48,6 +49,9 @@ public abstract class AbstractPTService<PT extends SciJavaPlugin> extends
@Parameter
private PluginService pluginService;

@Parameter(required = false)
private UsageService usageService;

// -- PTService methods --

@Override
Expand All @@ -60,4 +64,12 @@ public List<PluginInfo<PT>> getPlugins() {
return pluginService.getPluginsOfType(getPluginType());
}

// -- Internal methods --

/** Records the usage of a plugin. */
protected void recordUsage(final Object plugin) {
if (usageService == null) return;
usageService.increment(plugin);
}

}
86 changes: 85 additions & 1 deletion src/main/java/org/scijava/plugin/AbstractRichPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@

package org.scijava.plugin;

import java.net.URL;

import org.scijava.AbstractContextual;
import org.scijava.BasicDetails;
import org.scijava.Prioritized;
import org.scijava.Priority;
import org.scijava.util.ClassUtils;
import org.scijava.util.Manifest;
import org.scijava.util.MiscUtils;

/**
* Abstract base class for {@link RichPlugin} implementations.
Expand Down Expand Up @@ -83,6 +88,76 @@ public void setInfo(final PluginInfo<?> info) {
this.info = info;
}

// -- Identifiable methods --

@Override
public String getIdentifier() {
return "plugin:" + getClass().getName();
}

// -- Locatable methods --

@Override
public String getLocation() {
final URL location = ClassUtils.getLocation(getClass());
return location == null ? null : location.toExternalForm();
}

// -- Versioned methods --

@Override
public String getVersion() {
final Manifest m = Manifest.getManifest(getClass());
return m == null ? null : m.getImplementationVersion();
}

// -- BasicDetails methods --

@Override
public String getName() {
return getInfo().getName();
}

@Override
public String getLabel() {
return getInfo().getLabel();
}

@Override
public String getDescription() {
return getInfo().getDescription();
}

@Override
public boolean is(String key) {
return getInfo().is(key);
}

@Override
public String get(String key) {
return getInfo().get(key);
}

@Override
public void setName(String name) {
throw new UnsupportedOperationException();
}

@Override
public void setLabel(String label) {
throw new UnsupportedOperationException();
}

@Override
public void setDescription(String description) {
throw new UnsupportedOperationException();
}

@Override
public void set(String key, String value) {
throw new UnsupportedOperationException();
}

// -- Comparable methods --

@Override
Expand All @@ -94,7 +169,16 @@ public int compareTo(final Prioritized that) {
if (priorityCompare != 0) return priorityCompare;

// compare classes
return ClassUtils.compare(getClass(), that.getClass());
final int classCompare = ClassUtils.compare(getClass(), that.getClass());
if (classCompare != 0) return classCompare;

if (!(that instanceof BasicDetails)) return 1;
final BasicDetails basicDetails = (BasicDetails) that;

// compare names
final String thisName = getName();
final String thatName = basicDetails.getName();
return MiscUtils.compare(thisName, thatName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public <D extends DT> WrapperPlugin<D> create(final D data) {
@SuppressWarnings("unchecked")
final WrapperPlugin<D> typedInstance = (WrapperPlugin<D>) instance;
typedInstance.set(data);
recordUsage(typedInstance);
return typedInstance;
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/scijava/plugin/RichPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@

package org.scijava.plugin;

import org.scijava.BasicDetails;
import org.scijava.Contextual;
import org.scijava.Identifiable;
import org.scijava.Locatable;
import org.scijava.Prioritized;
import org.scijava.Versioned;

/**
* Base interface for {@link Contextual}, {@link Prioritized} plugins that
Expand All @@ -42,8 +46,8 @@
*
* @author Curtis Rueden
*/
public interface RichPlugin extends Contextual, Prioritized, HasPluginInfo,
SciJavaPlugin
public interface RichPlugin extends SciJavaPlugin, Contextual, Prioritized,
HasPluginInfo, Identifiable, Locatable, Versioned, BasicDetails
{
// NB: Marker interface.
}
2 changes: 2 additions & 0 deletions src/main/java/org/scijava/script/ScriptInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ public String getLocation() {
return new File(path).toURI().normalize().toString();
}

// -- Versioned methods --

@Override
public String getVersion() {
final File file = new File(path);
Expand Down