Skip to content

Commit 34dae6d

Browse files
committed
Merge branch 'run-classes'
This migrates the JavaRunner plugin type, and corresponding JavaService, from the scijava/scripting-java component, and generalizes them to support running other flavors of code besides Java classes. This branch also introduces a ParseService which generalizes and exposes the ScriptInfo parseAttrs method as public API, backed by the new SciJava Expression Parser 3.0.0. Thanks to these new services, the "--run" CLI flag is now implemented by a single ConsoleArgument plugin (org.scijava.run.console.RunArgument), which leans on the RunService and ParseService to do most of the work. Closes #226.
2 parents 60d5d13 + b254d8e commit 34dae6d

32 files changed

+1894
-104
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.53.2-SNAPSHOT</version>
13+
<version>2.54.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/command/console/RunArgument.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,22 @@
4444
import org.scijava.plugin.Plugin;
4545

4646
/**
47-
* Handles the {@code --run} command line argument.
48-
*
49-
* @author Curtis Rueden
50-
* @author Johannes Schindelin
51-
* @author Mark Hiner hinerm at gmail.com
47+
* @deprecated Use {@link org.scijava.run.console.RunArgument} instead.
5248
*/
49+
@Deprecated
5350
@Plugin(type = ConsoleArgument.class)
5451
public class RunArgument extends AbstractConsoleArgument {
5552

5653
@Parameter
5754
private CommandService commandService;
5855

5956
@Parameter
60-
private LogService logService;
57+
private LogService log;
6158

6259
// -- Constructor --
6360

6461
public RunArgument() {
65-
super(2, "--run", "--class");
62+
super(2, "--class");
6663
}
6764

6865
// -- ConsoleArgument methods --
@@ -72,7 +69,10 @@ public void handle(final LinkedList<String> args) {
7269
if (!supports(args))
7370
return;
7471

75-
args.removeFirst(); // --run
72+
log.warn("The --class flag is deprecated, and will\n" +
73+
"be removed in a future release. Use --run instead.");
74+
75+
args.removeFirst(); // --class
7676
final String commandToRun = args.removeFirst();
7777
final String paramString = ConsoleUtils.hasParam(args) ? args.removeFirst() : "";
7878

@@ -100,12 +100,12 @@ private void run(final String commandToRun, final String optionString) {
100100
return;
101101

102102
// TODO: parse the optionString a la ImageJ1
103-
final Map<String, Object> inputMap = ConsoleUtils.parseParameterString(optionString, info, logService);
103+
final Map<String, Object> inputMap = ConsoleUtils.parseParameterString(optionString, info, log);
104104

105105
try {
106106
commandService.run(info, true, inputMap).get();
107107
} catch (final Exception exc) {
108-
logService.error(exc);
108+
log.error(exc);
109109
}
110110
}
111111

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.command.run;
33+
34+
import java.lang.reflect.InvocationTargetException;
35+
import java.util.Map;
36+
37+
import org.scijava.command.Command;
38+
import org.scijava.command.CommandInfo;
39+
import org.scijava.command.CommandService;
40+
import org.scijava.plugin.Parameter;
41+
import org.scijava.plugin.Plugin;
42+
import org.scijava.run.AbstractCodeRunner;
43+
import org.scijava.run.CodeRunner;
44+
45+
/**
46+
* Runs the given {@link Command} class.
47+
*
48+
* @author Curtis Rueden
49+
*/
50+
@Plugin(type = CodeRunner.class)
51+
public class CommandCodeRunner extends AbstractCodeRunner {
52+
53+
@Parameter
54+
private CommandService commandService;
55+
56+
// -- CodeRunner methods --
57+
58+
@Override
59+
public void run(final Object code, final Object... args)
60+
throws InvocationTargetException
61+
{
62+
final Class<? extends Command> c = getCommandClass(code);
63+
if (c != null) waitFor(commandService.run(c, true, args));
64+
65+
final CommandInfo info = getCommandInfo(code);
66+
if (info != null) waitFor(commandService.run(info, true, args));
67+
}
68+
69+
@Override
70+
public void run(final Object code, final Map<String, Object> inputMap)
71+
throws InvocationTargetException
72+
{
73+
final Class<? extends Command> c = getCommandClass(code);
74+
if (c != null) waitFor(commandService.run(c, true, inputMap));
75+
76+
final CommandInfo info = getCommandInfo(code);
77+
if (info != null) waitFor(commandService.run(info, true, inputMap));
78+
}
79+
80+
// -- Typed methods --
81+
82+
@Override
83+
public boolean supports(final Object code) {
84+
return getCommandClass(code) != null || getCommandInfo(code) != null;
85+
}
86+
87+
// -- Helper methods --
88+
89+
private Class<? extends Command> getCommandClass(final Object code) {
90+
if (!(code instanceof Class)) return null;
91+
final Class<?> c = (Class<?>) code;
92+
if (!Command.class.isAssignableFrom(c)) return null;
93+
@SuppressWarnings("unchecked")
94+
final Class<? extends Command> commandClass = (Class<? extends Command>) c;
95+
return commandClass;
96+
}
97+
98+
private CommandInfo getCommandInfo(final Object code) {
99+
if (!(code instanceof String)) return null;
100+
final String command = (String) code;
101+
102+
final CommandInfo info = commandService.getCommand(command);
103+
if (info != null) return info;
104+
105+
// command was not a class name; search for command by title instead
106+
for (final CommandInfo ci : commandService.getCommands()) {
107+
if (command.equals(ci.getTitle())) return ci;
108+
}
109+
110+
return null;
111+
}
112+
113+
}

src/main/java/org/scijava/console/AbstractConsoleArgument.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,17 @@ public Class<LinkedList<String>> getType() {
8686
protected boolean isFlag(final LinkedList<String> args) {
8787
return flags.isEmpty() || flags.contains(args.getFirst());
8888
}
89+
90+
/**
91+
* If the next argument is an appropriate parameter to a
92+
* {@link ConsoleArgument}, retrieves it; otherwise, returns null.
93+
*
94+
* @return The first argument of the given list, if it does not
95+
* start with a {@code '-'} character; or null otherwise.
96+
*/
97+
protected String getParam(final LinkedList<String> args) {
98+
if (args.isEmpty()) return null;
99+
final String arg = args.getFirst();
100+
return arg.startsWith("-") ? null : arg;
101+
}
89102
}

src/main/java/org/scijava/console/ConsoleUtils.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,45 +39,33 @@
3939
import org.scijava.log.LogService;
4040
import org.scijava.module.ModuleInfo;
4141
import org.scijava.module.ModuleItem;
42+
import org.scijava.parse.ParseService;
4243

43-
/**
44-
* Helper class for {@link ConsoleArgument}s.
45-
*
46-
* @author Mark Hiner hinerm at gmail.com
47-
*/
44+
/** @deprecated Use alternatives instead (see individual method docs). */
45+
@Deprecated
4846
public final class ConsoleUtils {
4947

50-
/**
51-
* @see #parseParameterString(String, ModuleInfo, LogService)
52-
*/
48+
/** @deprecated Use {@link ParseService} instead. */
49+
@Deprecated
5350
public static Map<String, Object> parseParameterString(final String parameterString) {
5451
return parseParameterString(parameterString, (CommandInfo)null);
5552
}
5653

57-
/**
58-
* @see #parseParameterString(String, ModuleInfo, LogService)
59-
*/
54+
/** @deprecated Use {@link ParseService} instead. */
55+
@Deprecated
6056
public static Map<String, Object> parseParameterString(final String parameterString, final ModuleInfo info) {
6157
return parseParameterString(parameterString, info, null);
6258
}
6359

64-
/**
65-
* @see #parseParameterString(String, ModuleInfo, LogService)
66-
*/
67-
public static Map<String, Object> parseParameterString(final String parameterString, final LogService logService) {
68-
return parseParameterString(parameterString, null, logService);
60+
/** @deprecated Use {@link ParseService} instead. */
61+
@Deprecated
62+
public static Map<String, Object> parseParameterString(final String parameterString, final LogService log) {
63+
return parseParameterString(parameterString, null, log);
6964
}
7065

71-
/**
72-
* Helper method for turning a parameter string into a {@code Map} of
73-
* key:value pairs. If a {@link ModuleInfo} is provided, the parameter
74-
* string is assumed to be a comma-separated list of values, ordered
75-
* according to the {@code ModuleInfo's} inputs. Otherwise, the parameter
76-
* string is assumed to be a comma-separated list of "key=value" pairs.
77-
*
78-
* TODO reconcile with attribute parsing of {@link ScriptInfo}
79-
*/
80-
public static Map<String, Object> parseParameterString(final String parameterString, final ModuleInfo info, final LogService logService) {
66+
/** @deprecated Use {@link ParseService} instead. */
67+
@Deprecated
68+
public static Map<String, Object> parseParameterString(final String parameterString, final ModuleInfo info, final LogService log) {
8169
final Map<String, Object> inputMap = new HashMap<String, Object>();
8270

8371
if (!parameterString.isEmpty()) {
@@ -93,8 +81,8 @@ public static Map<String, Object> parseParameterString(final String parameterStr
9381
else if (inputs != null && inputs.hasNext() && split.length == 1) {
9482
inputMap.put(inputs.next().getName(), split[0]);
9583
}
96-
else if (logService != null)
97-
logService.error("Parameters must be formatted as a comma-separated list of key=value pairs");
84+
else if (log != null)
85+
log.error("Parameters must be formatted as a comma-separated list of key=value pairs");
9886

9987
}
10088
}
@@ -104,12 +92,10 @@ else if (logService != null)
10492
}
10593

10694
/**
107-
* Test if the next argument is an appropriate parameter to a
108-
* {@link ConsoleArgument}.
109-
*
110-
* @return {@code true} if the first argument of the given list does not
111-
* start with a {@code '-'} character.
95+
* @deprecated Use {@link AbstractConsoleArgument#getParam(LinkedList)}
96+
* instead.
11297
*/
98+
@Deprecated
11399
public static boolean hasParam(final LinkedList<String> args) {
114100
return !(args.isEmpty() || args.getFirst().startsWith("-"));
115101
}

src/main/java/org/scijava/console/HeadlessArgument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* and the enclosing {@link Context} will not be used after the
4343
* {@link ConsoleService} argument processing is complete.
4444
*
45-
* @author Mark Hiner hinerm at gmail.com
45+
* @author Mark Hiner
4646
*/
4747
@Plugin(type = ConsoleArgument.class)
4848
public class HeadlessArgument extends AbstractConsoleArgument {

src/main/java/org/scijava/convert/CastingConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
* Minimal {@link Converter} implementation to do direct casting.
4141
*
42-
* @author Mark Hiner hinerm at gmail.com
42+
* @author Mark Hiner
4343
*/
4444
@Plugin(type = Converter.class, priority = Priority.FIRST_PRIORITY)
4545
public class CastingConverter extends AbstractConverter<Object, Object> {

0 commit comments

Comments
 (0)