Skip to content

Commit 6fa80b1

Browse files
committed
Gateway: add a handy launch(String...) method
This performs launch operations standard to most SciJava applications. This code migrated from a static method of imagej/imagej: net.imagej.Main.launch(String...) With this change, the standard launch process is accessible to all SciJava applications in a DRY way, while still being configurable for applications with special needs. Typically, such applications will define their own Gateway implementation which extends AbstractGateway; hence, these applications can override launch(String...) to modify their launch behavior. We considered creating a LaunchService with LaunchAction plugins, but it seemed potentially overcomplicated.
1 parent 6c1f4e9 commit 6fa80b1

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-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.52.2-SNAPSHOT</version>
13+
<version>2.53.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 both ImageJ and SCIFIO.</description>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ public AbstractGateway(final String appName, final Context context) {
8989

9090
// -- Gateway methods --
9191

92+
@Override
93+
public void launch(final String... args) {
94+
// parse command line arguments
95+
console().processArgs(args);
96+
97+
// launch main methods
98+
final int mainCount = main().execMains();
99+
100+
// display the user interface (NB: does not block)
101+
if (mainCount == 0 && !ui().isHeadless()) ui().showUI();
102+
103+
if (ui().isHeadless()) {
104+
// now that CLI processing/execution is done, we can shut down
105+
getContext().dispose();
106+
}
107+
}
108+
92109
@Override
93110
public String getShortName() {
94111
return getClass().getName().toLowerCase();

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@
120120
*/
121121
public interface Gateway extends RichPlugin, Versioned {
122122

123+
/**
124+
* Perform launch operations associated with this gateway.
125+
* <p>
126+
* Typical operations might include:
127+
* </p>
128+
* <ul>
129+
* <li>Handle the given command line arguments using the
130+
* {@link ConsoleService}.</li>
131+
* <li>Execute registered main classes of the {@link MainService}.</li>
132+
* <li>Display the default user interface using the {@link UIService}.</li>
133+
* <li>In some circumstances (e.g., when running headless), dispose the
134+
* context after launch operations are complete.</li>
135+
* </ul>
136+
*
137+
* @param args The arguments to pass to the application.
138+
*/
139+
void launch(String... args);
140+
123141
/**
124142
* Gets a very succinct name for use referring to this gateway, e.g. as a
125143
* variable name for scripting.

0 commit comments

Comments
 (0)