Skip to content

Commit 7709d73

Browse files
committed
Add service for queuing startup operations
This will be useful for multiple reasons: * The ImageJ Server's --server flag can now be combined with --headless to launch a blocking server, without --server blocking in UI mode. * We may finally be able to fix imagej/imagej-legacy#89 by queuing the operation with the StartupService.
1 parent ac4f1fb commit 7709d73

File tree

6 files changed

+137
-1
lines changed

6 files changed

+137
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.70.2-SNAPSHOT</version>
13+
<version>2.71.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<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 downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.scijava.prefs.PrefService;
5959
import org.scijava.script.ScriptService;
6060
import org.scijava.service.Service;
61+
import org.scijava.startup.StartupService;
6162
import org.scijava.text.TextService;
6263
import org.scijava.thread.ThreadService;
6364
import org.scijava.tool.IconService;
@@ -102,6 +103,9 @@ public void launch(final String... args) {
102103
// NB: When running headless, the HeadlessUI will be used.
103104
if (mainCount == 0) ui().showUI();
104105

106+
// perform all pending startup operations
107+
startup().executeOperations();
108+
105109
if (ui().isHeadless()) {
106110
// now that CLI processing/execution is done, we can shut down
107111
getContext().dispose();
@@ -233,6 +237,11 @@ public ScriptService script() {
233237
return get(ScriptService.class);
234238
}
235239

240+
@Override
241+
public StartupService startup() {
242+
return get(StartupService.class);
243+
}
244+
236245
@Override
237246
public StatusService status() {
238247
return get(StatusService.class);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.scijava.plugin.RichPlugin;
5757
import org.scijava.script.ScriptService;
5858
import org.scijava.service.Service;
59+
import org.scijava.startup.StartupService;
5960
import org.scijava.text.TextService;
6061
import org.scijava.thread.ThreadService;
6162
import org.scijava.tool.IconService;
@@ -311,6 +312,13 @@ public interface Gateway extends RichPlugin {
311312
*/
312313
ScriptService script();
313314

315+
/**
316+
* Gets this application context's {@link StartupService}.
317+
*
318+
* @return The {@link StartupService} of this application context.
319+
*/
320+
StartupService startup();
321+
314322
/**
315323
* Gets this application context's {@link StatusService}.
316324
*
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.startup;
34+
35+
import java.util.ArrayDeque;
36+
import java.util.Deque;
37+
38+
import org.scijava.plugin.Plugin;
39+
import org.scijava.service.AbstractService;
40+
import org.scijava.service.Service;
41+
42+
/**
43+
* Default implementation of {@link StartupService}.
44+
*
45+
* @author Curtis Rueden
46+
*/
47+
@Plugin(type = Service.class)
48+
public class DefaultStartupService extends AbstractService implements
49+
StartupService
50+
{
51+
52+
private final Deque<Runnable> operations = new ArrayDeque<>();
53+
54+
@Override
55+
public void addOperation(final Runnable operation) {
56+
operations.add(operation);
57+
}
58+
59+
@Override
60+
public void executeOperations() {
61+
while (!operations.isEmpty()) {
62+
final Runnable operation = operations.pop();
63+
operation.run();
64+
}
65+
}
66+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.startup;
34+
35+
import org.scijava.service.SciJavaService;
36+
37+
/**
38+
* Interface for service managing startup operations.
39+
*
40+
* @author Curtis Rueden
41+
*/
42+
public interface StartupService extends SciJavaService {
43+
44+
/** Adds an operation that will run as soon as the application starts up. */
45+
void addOperation(Runnable r);
46+
47+
/**
48+
* Execute all registered startup operations, in the order they were
49+
* registered, blocking until complete.
50+
*/
51+
void executeOperations();
52+
}

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public void testFull() {
114114
org.scijava.run.DefaultRunService.class,
115115
org.scijava.script.DefaultScriptHeaderService.class,
116116
org.scijava.script.process.DefaultScriptProcessorService.class,
117+
org.scijava.startup.DefaultStartupService.class,
117118
org.scijava.task.DefaultTaskService.class,
118119
org.scijava.text.DefaultTextService.class,
119120
org.scijava.thread.DefaultThreadService.class,

0 commit comments

Comments
 (0)