Skip to content

Commit 1adb548

Browse files
committed
Merge branch 'versioned-plugins'
This makes RichPlugin extend more useful interfaces. In particular, RichPlugins are now Versioned, a big step toward reproducibility. I actually made this change two years ago, but it got lost in the shuffle till now, because it was originally done in the context of the usage statistics work, which was rejected by the community.
2 parents 0daddfb + e485a09 commit 1adb548

File tree

9 files changed

+78
-54
lines changed

9 files changed

+78
-54
lines changed

src/main/java/org/scijava/AbstractGateway.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,15 @@ public String getTitle() {
274274
}
275275

276276
@Override
277-
public String getVersion() {
278-
return getApp().getVersion();
277+
public String getInfo(final boolean mem) {
278+
return getApp().getInfo(mem);
279279
}
280280

281+
// -- Versioned methods --
282+
281283
@Override
282-
public String getInfo(final boolean mem) {
283-
return getApp().getInfo(mem);
284+
public String getVersion() {
285+
return getApp().getVersion();
284286
}
285287

286288
}

src/main/java/org/scijava/Gateway.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
* @author Mark Hiner
119119
* @author Curtis Rueden
120120
*/
121-
public interface Gateway extends RichPlugin, Versioned {
121+
public interface Gateway extends RichPlugin {
122122

123123
/**
124124
* Perform launch operations associated with this gateway.
@@ -362,10 +362,4 @@ public interface Gateway extends RichPlugin, Versioned {
362362
/** @see org.scijava.app.App#getInfo(boolean) */
363363
String getInfo(boolean mem);
364364

365-
// -- Versioned methods --
366-
367-
/** @see org.scijava.app.App#getVersion() */
368-
@Override
369-
String getVersion();
370-
371365
}

src/main/java/org/scijava/Locatable.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
package org.scijava;
3333

34+
import java.net.URL;
35+
36+
import org.scijava.util.ClassUtils;
37+
3438
/**
3539
* An object whose location is defined by a URL string.
3640
*
@@ -39,6 +43,9 @@
3943
public interface Locatable {
4044

4145
/** Gets the URL string defining the object's location. */
42-
String getLocation();
46+
default String getLocation() {
47+
final URL location = ClassUtils.getLocation(getClass());
48+
return location == null ? null : location.toExternalForm();
49+
}
4350

4451
}

src/main/java/org/scijava/Prioritized.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava;
3333

34+
import org.scijava.util.ClassUtils;
35+
3436
/**
3537
* An object that can be sorted according to priority.
3638
*
@@ -52,4 +54,18 @@ public interface Prioritized extends Comparable<Prioritized> {
5254
*/
5355
void setPriority(double priority);
5456

57+
// -- Comparable methods --
58+
59+
@Override
60+
default int compareTo(final Prioritized that) {
61+
if (that == null) return 1;
62+
63+
// compare priorities
64+
final int priorityCompare = Priority.compare(this, that);
65+
if (priorityCompare != 0) return priorityCompare;
66+
67+
// compare classes
68+
return ClassUtils.compare(getClass(), that.getClass());
69+
}
70+
5571
}

src/main/java/org/scijava/Versioned.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava;
3333

34+
import org.scijava.util.VersionUtils;
35+
3436
/**
3537
* An object that knows its version.
3638
*
@@ -39,6 +41,9 @@
3941
public interface Versioned {
4042

4143
/** Gets the version of the object. */
42-
String getVersion();
44+
default String getVersion() {
45+
return VersionUtils.getVersion(getClass());
46+
}
47+
4348

4449
}

src/main/java/org/scijava/app/AbstractApp.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,13 @@ public abstract class AbstractApp extends AbstractRichPlugin implements App {
5656
/** JAR manifest with metadata about the application. */
5757
private Manifest manifest;
5858

59+
// -- App methods --
60+
5961
@Override
6062
public String getTitle() {
6163
return getInfo().getName();
6264
}
6365

64-
@Override
65-
public String getVersion() {
66-
// NB: We do not use VersionUtils.getVersion(c, groupId, artifactId)
67-
// because that method does not cache the parsed Manifest and/or POM.
68-
// We might have them already parsed here, and if not, we want to
69-
// parse then cache locally, rather than discarding them afterwards.
70-
71-
// try the manifest first, since it might know its build number
72-
final Manifest m = getManifest();
73-
if (m != null) {
74-
final String v = m.getVersion();
75-
if (v != null) return v;
76-
}
77-
// try the POM
78-
final POM p = getPOM();
79-
if (p != null) {
80-
final String v = p.getVersion();
81-
if (v != null) return v;
82-
}
83-
return "Unknown";
84-
}
85-
8666
@Override
8767
public POM getPOM() {
8868
if (pom == null) {
@@ -145,4 +125,28 @@ public void quit() {
145125
getContext().dispose();
146126
}
147127

128+
// -- Versioned methods --
129+
130+
@Override
131+
public String getVersion() {
132+
// NB: We do not use VersionUtils.getVersion(c, groupId, artifactId)
133+
// because that method does not cache the parsed Manifest and/or POM.
134+
// We might have them already parsed here, and if not, we want to
135+
// parse then cache locally, rather than discarding them afterwards.
136+
137+
// try the manifest first, since it might know its build number
138+
final Manifest m = getManifest();
139+
if (m != null) {
140+
final String v = m.getVersion();
141+
if (v != null) return v;
142+
}
143+
// try the POM
144+
final POM p = getPOM();
145+
if (p != null) {
146+
final String v = p.getVersion();
147+
if (v != null) return v;
148+
}
149+
return "Unknown";
150+
}
151+
148152
}

src/main/java/org/scijava/plugin/AbstractRichPlugin.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
package org.scijava.plugin;
3333

3434
import org.scijava.AbstractContextual;
35-
import org.scijava.Prioritized;
3635
import org.scijava.Priority;
37-
import org.scijava.util.ClassUtils;
3836

3937
/**
4038
* Abstract base class for {@link RichPlugin} implementations.
@@ -83,18 +81,4 @@ public void setInfo(final PluginInfo<?> info) {
8381
this.info = info;
8482
}
8583

86-
// -- Comparable methods --
87-
88-
@Override
89-
public int compareTo(final Prioritized that) {
90-
if (that == null) return 1;
91-
92-
// compare priorities
93-
final int priorityCompare = Priority.compare(this, that);
94-
if (priorityCompare != 0) return priorityCompare;
95-
96-
// compare classes
97-
return ClassUtils.compare(getClass(), that.getClass());
98-
}
99-
10084
}

src/main/java/org/scijava/plugin/RichPlugin.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
package org.scijava.plugin;
3333

3434
import org.scijava.Contextual;
35+
import org.scijava.Identifiable;
36+
import org.scijava.Locatable;
3537
import org.scijava.Prioritized;
38+
import org.scijava.Versioned;
3639

3740
/**
3841
* Base interface for {@link Contextual}, {@link Prioritized} plugins that
@@ -42,8 +45,15 @@
4245
*
4346
* @author Curtis Rueden
4447
*/
45-
public interface RichPlugin extends Contextual, Prioritized, HasPluginInfo,
46-
SciJavaPlugin
48+
public interface RichPlugin extends SciJavaPlugin, Contextual, Prioritized,
49+
HasPluginInfo, Identifiable, Locatable, Versioned
4750
{
48-
// NB: Marker interface.
51+
52+
// -- Identifiable methods --
53+
54+
@Override
55+
default String getIdentifier() {
56+
return "plugin:" + getClass().getName();
57+
}
58+
4959
}

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ public String getLocation() {
318318
return new File(path).toURI().normalize().toString();
319319
}
320320

321+
// -- Versioned methods --
322+
321323
@Override
322324
public String getVersion() {
323325
final File file = new File(path);

0 commit comments

Comments
 (0)