Skip to content

Commit 32f0fe4

Browse files
committed
Context: add ctor arg for strict service handling
By default, strict mode is enabled. But you can pass strict=false if you want context creation to continue (rather than throwing IllegalArgumentException) even when a required service fails to instantiate.
1 parent c9a44ae commit 32f0fe4

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/main/java/org/scijava/Context.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ public Context(final Collection<Class<? extends Service>> serviceClasses) {
122122
this(serviceClasses, null);
123123
}
124124

125+
/**
126+
* Creates a new SciJava application context with the specified services (and
127+
* any required service dependencies).
128+
*
129+
* @param serviceClasses A collection of types that implement the
130+
* {@link Service} interface (e.g., {@code DisplayService.class}).
131+
* @param strict Whether context creation will fail fast when there is
132+
* is an error instantiating a required service.
133+
*/
134+
public Context(final Collection<Class<? extends Service>> serviceClasses,
135+
final boolean strict)
136+
{
137+
this(serviceClasses, null, strict);
138+
}
139+
125140
/**
126141
* Creates a new SciJava application with the specified PluginIndex. This
127142
* allows a base set of available plugins to be defined, and is useful when
@@ -140,6 +155,27 @@ public Context(final PluginIndex pluginIndex) {
140155
this(Arrays.<Class<? extends Service>> asList(Service.class), pluginIndex);
141156
}
142157

158+
/**
159+
* Creates a new SciJava application context with the specified services (and
160+
* any required service dependencies). Service dependency candidates are
161+
* selected from those discovered by the given {@link PluginIndex}'s
162+
* associated {@link org.scijava.plugin.PluginFinder}.
163+
*
164+
* @param serviceClasses A collection of types that implement the
165+
* {@link Service} interface (e.g., {@code DisplayService.class}).
166+
* @param pluginIndex The plugin index to use when discovering and indexing
167+
* plugins. If you wish to completely control how services are
168+
* discovered (i.e., use your own
169+
* {@link org.scijava.plugin.PluginFinder} implementation), then you
170+
* can pass a custom {@link PluginIndex} here. Passing null will
171+
* result in a default plugin index being constructed and used.
172+
*/
173+
public Context(final Collection<Class<? extends Service>> serviceClasses,
174+
final PluginIndex pluginIndex)
175+
{
176+
this(serviceClasses, pluginIndex, strict());
177+
}
178+
143179
/**
144180
* Creates a new SciJava application context with the specified services (and
145181
* any required service dependencies). Service dependency candidates are
@@ -163,16 +199,19 @@ public Context(final PluginIndex pluginIndex) {
163199
* {@link org.scijava.plugin.PluginFinder} implementation), then you
164200
* can pass a custom {@link PluginIndex} here. Passing null will
165201
* result in a default plugin index being constructed and used.
202+
* @param strict Whether context creation will fail fast when there is
203+
* is an error instantiating a required service.
166204
*/
167205
public Context(final Collection<Class<? extends Service>> serviceClasses,
168-
final PluginIndex pluginIndex)
206+
final PluginIndex pluginIndex, final boolean strict)
169207
{
170208
serviceIndex = new ServiceIndex();
171209

172210
this.pluginIndex = pluginIndex == null ? new PluginIndex() : pluginIndex;
173211
this.pluginIndex.discover();
174212

175-
final ServiceHelper serviceHelper = new ServiceHelper(this, serviceClasses);
213+
final ServiceHelper serviceHelper =
214+
new ServiceHelper(this, serviceClasses, strict);
176215
serviceHelper.loadServices();
177216
}
178217

@@ -353,4 +392,8 @@ private String createMissingServiceMessage(
353392
return msg.toString();
354393
}
355394

395+
private static boolean strict() {
396+
return true;
397+
}
398+
356399
}

0 commit comments

Comments
 (0)