Skip to content

Commit d6a7eba

Browse files
hinermctrueden
authored andcommitted
Lean on object service for singleton retrieval
Instead of maintaining our own list of singletons, we should be using the ObjectService.
1 parent 05b1537 commit d6a7eba

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

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

+16-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
package org.scijava.plugin;
3333

34-
import java.util.ArrayList;
3534
import java.util.Collections;
3635
import java.util.HashMap;
3736
import java.util.List;
@@ -58,19 +57,16 @@ public abstract class AbstractSingletonService<PT extends SingletonPlugin>
5857
private ObjectService objectService;
5958

6059
// TODO: Listen for PluginsAddedEvent and PluginsRemovedEvent
61-
// and update the list of singletons accordingly.
62-
63-
/** List of singleton plugin instances. */
64-
private List<PT> instances;
60+
// and update the map of singletons accordingly.
6561

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

6864
// -- SingletonService methods --
6965

7066
@Override
7167
public List<PT> getInstances() {
72-
if (instances == null) initInstances();
73-
return instances;
68+
final List<PT> plugins = objectService.getObjects(getPluginType());
69+
return Collections.unmodifiableList(plugins);
7470
}
7571

7672
@SuppressWarnings("unchecked")
@@ -89,7 +85,7 @@ public void initialize() {
8985

9086
@Override
9187
public List<PT> get() {
92-
return new ArrayList<PT>(getInstances());
88+
return createInstances();
9389
}
9490

9591
@Override
@@ -115,27 +111,30 @@ protected List<PT> filterInstances(final List<PT> list) {
115111
// -- Helper methods --
116112

117113
private synchronized void initInstances() {
118-
if (instances != null) return;
119-
120-
final List<PT> list =
121-
Collections.unmodifiableList(filterInstances(getPluginService()
122-
.createInstancesOfType(getPluginType())));
114+
if (instanceMap != null) return;
123115

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

119+
final List<PT> list = getInstances();
127120
for (final PT plugin : list) {
128121
@SuppressWarnings("unchecked")
129122
final Class<? extends PT> ptClass =
130123
(Class<? extends PT>) plugin.getClass();
131124
map.put(ptClass, plugin);
132125
}
133126

134-
log.debug("Found " + list.size() + " " + getPluginType().getSimpleName() +
135-
" plugins.");
136-
137127
instanceMap = map;
138-
instances = list;
128+
}
129+
130+
private List<PT> createInstances() {
131+
final List<PT> instances =
132+
filterInstances(getPluginService().createInstancesOfType(getPluginType()));
133+
134+
log.info("Found " + instances.size() + " " +
135+
getPluginType().getSimpleName() + " plugins.");
136+
137+
return instances;
139138
}
140139

141140
}

0 commit comments

Comments
 (0)