-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRunner.java
351 lines (310 loc) · 9.7 KB
/
Runner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package bar;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.Future;
import net.imagej.ImageJ;
import org.scijava.Context;
import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.script.ScriptModule;
import org.scijava.script.ScriptService;
import org.scijava.ui.UIService;
import org.scijava.ui.swing.script.TextEditor;
import org.scijava.util.FileUtils;
import ij.IJ;
import ij.plugin.MacroInstaller;
import ij.plugin.Macro_Runner;
/** Runs a JARified script **/
@Plugin(type = Command.class)
public class Runner {
@Parameter
private Context context;
@Parameter
private ScriptService scriptService;
@Parameter
private LogService logService;
@Parameter
private UIService uiService;
@Parameter
private Boolean silent;
@Parameter
private Boolean loaded;
@Parameter
private String exitStatus;
@Parameter
private URL lastLoadedURL;
/* exit status */
private static final String WAS_CANCELED = "canceled";
private static final String WAS_DONE = "done";
private static final String WAS_LOADED = "loaded";
private static final String IO_ERROR = "io error";
private static final String EXCEPTION = "exception";
/** Default constructor-based dependency injection */
public Runner(final Context context) {
context.inject(this);
setSilent(false);
}
/**
* Constructor relying on IJ1.
*
* @param silent
* if errors should be ignored by the {@link LogService}
*/
public Runner(final boolean silent) {
if (context == null)
context = (Context) IJ.runPlugIn("org.scijava.Context", "");
if (scriptService == null)
scriptService = context.getService(ScriptService.class);
if (logService == null)
logService = context.getService(LogService.class);
if (uiService == null)
uiService = context.getService(UIService.class);
setSilent(silent);
}
/** Debug method **/
public static void main(final String... args) throws Exception {
final ImageJ ij = net.imagej.Main.launch(args);
final Runner runner = new Runner(ij.context());
runner.runScript("Data_Analysis", "Distribution_Plotter.ijm");
}
private void runScript(final URL url, final String filename, final Map<String, Object> inputMap) {
try {
runScript(url.openStream(), filename, inputMap);
lastLoadedURL = url;
} catch (final IOException exc) {
error("Could not run " + url.toString(), IO_ERROR);
exc.printStackTrace();
}
}
/**
* Runs a script from an InputStream.
*
* @param in
* the {@link InputStream} loading the script to be executed
* @param filename
* the script filename (or at least its extension)
*/
public void runScript(final InputStream in, final String filename) {
runScript(in, filename, null);
}
/**
* Runs a (JARified) BAR script.
*
* @param path
* the path of the BAR script to be executed, e.g,
* {@code scripts/BAR/Data_Analysis/Distribution_Plotter.ijm};
*/
public void runScript(final String path) {
runScript(Utils.getBARresource(path), path, null);
}
/**
* Runs a (JARified) BAR script.
*
* @param path
* the path of the BAR script to be executed, e.g,
* {@code /scripts/BAR/Data_Analysis/Distribution_Plotter.ijm};
* @param inputMap
* see {@link ScriptService#run(String, Reader, boolean, Map)}
*/
public void runScript(final String path, final Map<String, Object> inputMap) {
runScript(Utils.getBARresource(path), path, inputMap);
}
/**
* Runs a (JARified) BAR script.
*
* @param dir
* the script directory, i.e., the subdirectory in the BAR jar
* relative to {@code scripts/BAR/}
* @param file
* the script filename (or at least its extension)
* @param inputMap
* see {@link ScriptService#run(String, Reader, boolean, Map)}
*/
public void runScript(final String dir, final String file, final Map<String, Object> inputMap) {
final String path = "scripts/BAR/" + dir + "/" + file;
runScript(Utils.getBARresource(path), file, inputMap);
}
/**
* Runs a (JARified) BAR script.
*
* @param dir
* the script directory, i.e., the subdirectory in the BAR jar
* relative to {@code scripts/BAR/}
* @param file
* the script filename
*/
public void runScript(final String dir, final String file) {
final String path = "scripts/BAR/" + dir + "/" + file;
runScript(path);
}
/**
* Runs a script from a jar file.
*
* @param in
* the {@link InputStream} loading the script to be executed
* @param filename
* the script filename (or at least its extension)
* @param inputMap
* see {@link ScriptService#run(String, Reader, boolean, Map)}
*/
public void runScript(final InputStream in, final String filename, final Map<String, Object> inputMap) {
if (in == null) {
error("Could not find " + filename, IO_ERROR);
return;
}
final Reader reader = new InputStreamReader(in);
setLoaded(true);
final Future<ScriptModule> fsm = scriptService.run(filename, reader, true, inputMap);
if (fsm.isCancelled())
setStatus(WAS_CANCELED);
else if (fsm.isDone())
setStatus(WAS_DONE);
}
/**
* Legacy method that supports IJ1 macros that do not use script parameters.
* Ported from {@link Macro_Runner#runMacroFromJar(String, String)}.
*
* @param path
* the path to the IJ1 macro in the BAR jar file relative to
* {@code scripts/}
* @param arg
* the argument string to be retrieved through the IJ1 built-in
* macro function {@code getArgument()}
*/
public void runBARMacro(final String path, final String arg) {
runIJ1Macro("scripts/BAR/" + path, arg);
}
/**
* Legacy method that supports IJ1 macros that do not use script parameters.
* Ported from {@link Macro_Runner#runMacroFromJar(String, String)}.
*
* @param path
* the path to the IJ1 macro in the BAR jar file
* @param arg
* the argument string to be retrieved through the IJ1 built-in
* macro function {@code getArgument()}
*/
public void runIJ1Macro(final String path, final String arg) {
final String macro = readContents(path, true);
setStatus((macro == null) ? EXCEPTION : (new Macro_Runner()).runMacro(macro, arg));
}
public void installIJ1Macro(final String path) {
installIJ1Macro(path, false);
}
public void installIJ1Macro(final String path, final boolean singleTool) {
final String macro = readContents(path, true);
if (macro != null) {
final MacroInstaller mi = new MacroInstaller();
if (singleTool) {
mi.installSingleTool(macro);
} else {
mi.install(macro);
}
}
setStatus((macro == null) ? EXCEPTION : path + " installed");
}
public String readContents(final String resourcePath) {
return readContents(Utils.getBARresource(resourcePath), false);
}
private String readContents(final String resourcePath, final boolean setGlobalFlags) {
return readContents(Utils.getBARresource(resourcePath), setGlobalFlags);
}
public String readContents(final URL url, final boolean setGlobalFlags) {
String contents = null;
try {
final InputStream is = url.openStream();
if (is == null) {
if (setGlobalFlags)
error("Could not find " + url, IO_ERROR);
return contents;
}
final InputStreamReader isr = new InputStreamReader(is);
setLoaded(true);
final StringBuffer sb = new StringBuffer();
final char[] b = new char[8192];
int n;
while ((n = isr.read(b)) > 0)
sb.append(b, 0, n);
contents = sb.toString();
is.close();
} catch (final NullPointerException | IOException ignored) {
if (setGlobalFlags)
error("There was an error reading " + url, IO_ERROR);
} finally {
if (setGlobalFlags)
lastLoadedURL = url;
}
return contents;
}
private void error(final String msg, final String status) {
if (!silent)
logService.error(msg);
setStatus(status);
setLoaded(false);
}
private void setStatus(final String status) {
this.exitStatus = status;
}
private void setLoaded(final boolean loaded) {
if (loaded)
setStatus(WAS_LOADED);
this.loaded = loaded;
}
/**
* return {@code true} if the {@link LogService} is not logging errors. Note
* that passing invalid parameters to scripts may still trigger Console
* warnings.
*/
public Boolean isSilent() {
return silent;
}
/**
* Should {@link LogService} display errors?
*
* @param silent
* if {@code true} errors are ignored silently. Note that passing
* invalid parameters to scripts may still trigger Console
* warnings.
*/
public void setSilent(final Boolean silent) {
this.silent = silent;
}
/**
* Assesses if last script was loaded successfully
*
* @return {@code true} if the last run script could not be loaded otherwise
* {@code false}
*/
public boolean scriptLoaded() {
return loaded;
}
/**
* @return the exit status: {@code cancelled}, {@code done},
* {@code exception}, {@code loaded}, {@code io error}, or in the
* case of IJ1 macros loaded through legacy mechanisms the String
* value returned by the
* {@link Macro_Runner#runMacro(String, String)}
*/
public String getStatus() {
return exitStatus;
}
public void openLastLoadedResource() {
if (lastLoadedURL == null) {
return;
}
final TextEditor editor = new TextEditor(context);
editor.loadTemplate(lastLoadedURL);
editor.setTitle(FileUtils.shortenPath(lastLoadedURL.getPath(), 0));
editor.setVisible(true);
}
public File getLastLoadedResource() {
return FileUtils.urlToFile(lastLoadedURL);
}
}