◐ Shell
reader mode source ↗
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
</parent>

<artifactId>scijava-common</artifactId>
<version>2.41.1-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>
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public boolean registerAppMenus(final Object menus) {
}

@Override
public List<? extends Platform> filterInstances(final List<Platform> list) {
final Iterator<Platform> iter = list.iterator();
while (iter.hasNext()) {
if (!iter.next().isTarget()) {
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/org/scijava/plugin/AbstractSingletonService.java
Original file line number Diff line number Diff line change
@@ -31,7 +31,6 @@

package org.scijava.plugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -58,19 +57,16 @@ public abstract class AbstractSingletonService<PT extends SingletonPlugin>
private ObjectService objectService;

// TODO: Listen for PluginsAddedEvent and PluginsRemovedEvent
// and update the list of singletons accordingly.

/** List of singleton plugin instances. */
private List<PT> instances;

private Map<Class<? extends PT>, PT> instanceMap;

// -- SingletonService methods --

@Override
public List<PT> getInstances() {
if (instances == null) initInstances();
return instances;
}

@SuppressWarnings("unchecked")
Expand All @@ -85,12 +81,18 @@ public <P extends PT> P getInstance(final Class<P> pluginClass) {
@Override
public void initialize() {
// add singleton instances to the object index... IN THE FUTURE!
objectService.getIndex().addLater(new LazyObjects<Object>() {

@Override
public ArrayList<Object> get() {
return new ArrayList<Object>(getInstances());
}
});
}

@@ -102,34 +104,37 @@ public ArrayList<Object> get() {
* @param list the initial list of instances
* @return the filtered list of instances
*/
protected List<? extends PT> filterInstances(final List<PT> list) {
return list;
}

// -- Helper methods --

private synchronized void initInstances() {
if (instances != null) return;

final List<PT> list =
Collections.unmodifiableList(filterInstances(getPluginService()
.createInstancesOfType(getPluginType())));

final HashMap<Class<? extends PT>, PT> map =
new HashMap<Class<? extends PT>, PT>();

for (final PT plugin : list) {
@SuppressWarnings("unchecked")
final Class<? extends PT> ptClass =
(Class<? extends PT>) plugin.getClass();
map.put(ptClass, plugin);
}

log.debug("Found " + list.size() + " " + getPluginType().getSimpleName() +
" plugins.");

instanceMap = map;
instances = list;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/scijava/script/DefaultScriptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ public Collection<ScriptInfo> get() {
return scripts().values();
}

});
}

Expand Down
118 changes: 117 additions & 1 deletion src/test/java/org/scijava/object/ObjectIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

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

Expand All @@ -60,11 +63,30 @@ public void testGetAll() {
objectIndex.add(o1);
objectIndex.add(o2);
objectIndex.add(o3);
final List<Object> all = objectIndex.getAll();
assertEquals(3, all.size());
assertSame(o1, all.get(0));
assertSame(o2, all.get(1));
assertSame(o3, all.get(2));
}

@Test
Expand Down Expand Up @@ -222,4 +244,98 @@ public void testToString() {
assertArrayEquals(expected, actual);
}

}
Toggle all file notes Toggle all file annotations