Skip to content

Commit 1b245c1

Browse files
committed
Don't use ObjectIndex when retrieving singletons
There was an inconsistency in that getInstances was using a private list of singleton instances, but getInstance(Class) was trying to use the ObjectIndex. Unfortunately, the use of the ObjectIndex is currently bugged due to its loading ALL instances when any single instance type is requested. Thus we update the getInstance(Class) implementation to use a private hash of available instances. A proper ObjectIndex implementation will need to wait for 3.0.0.
1 parent 15af383 commit 1b245c1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
import java.util.ArrayList;
3535
import java.util.Collections;
36+
import java.util.HashMap;
3637
import java.util.List;
38+
import java.util.Map;
3739

3840
import org.scijava.log.LogService;
3941
import org.scijava.object.LazyObjects;
@@ -61,6 +63,8 @@ public abstract class AbstractSingletonService<PT extends SingletonPlugin>
6163
/** List of singleton plugin instances. */
6264
private List<PT> instances;
6365

66+
private Map<Class<? extends PT>, PT> instanceMap;
67+
6468
// -- SingletonService methods --
6569

6670
@Override
@@ -69,10 +73,11 @@ public List<PT> getInstances() {
6973
return instances;
7074
}
7175

76+
@SuppressWarnings("unchecked")
7277
@Override
7378
public <P extends PT> P getInstance(final Class<P> pluginClass) {
74-
final List<P> objects = objectService.getObjects(pluginClass);
75-
return objects == null || objects.isEmpty() ? null : objects.get(0);
79+
if (instanceMap == null) initInstances();
80+
return (P) instanceMap.get(pluginClass);
7681
}
7782

7883
// -- Service methods --
@@ -98,6 +103,14 @@ private synchronized void initInstances() {
98103
Collections.unmodifiableList(filterInstances(getPluginService()
99104
.createInstancesOfType(getPluginType())));
100105

106+
instanceMap = new HashMap<Class<? extends PT>, PT>();
107+
108+
for (PT plugin : instances) {
109+
@SuppressWarnings("unchecked")
110+
Class<? extends PT> ptClass = (Class<? extends PT>) plugin.getClass();
111+
instanceMap.put(ptClass, plugin);
112+
}
113+
101114
log.info("Found " + instances.size() + " " +
102115
getPluginType().getSimpleName() + " plugins.");
103116
}

0 commit comments

Comments
 (0)