Skip to content

Commit e57683b

Browse files
committed
Add a callback mechanism for script execution
The intended use case is for ScriptProcessor plugins to use it when they need to do something every time (or deferred until the first time) a script executes. Initially, this will be useful for dependency grabbing.
1 parent 1541cf1 commit e57683b

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.net.MalformedURLException;
4141
import java.net.URL;
4242
import java.text.SimpleDateFormat;
43+
import java.util.ArrayList;
4344
import java.util.Date;
4445
import java.util.List;
4546

@@ -52,6 +53,7 @@
5253
import org.scijava.module.ModuleItem;
5354
import org.scijava.plugin.Parameter;
5455
import org.scijava.script.process.ParameterScriptProcessor;
56+
import org.scijava.script.process.ScriptCallback;
5557
import org.scijava.script.process.ScriptProcessorService;
5658
import org.scijava.util.DigestUtils;
5759
import org.scijava.util.FileUtils;
@@ -88,6 +90,9 @@ public class ScriptInfo extends AbstractModuleInfo implements Contextual {
8890
/** Script language in which the script should be executed. */
8991
private ScriptLanguage scriptLanguage;
9092

93+
/** Routines to be invoked prior to script execution. */
94+
private ArrayList<ScriptCallback> callbacks;
95+
9196
/**
9297
* Creates a script metadata object which describes the given script file.
9398
*
@@ -245,6 +250,18 @@ public void setReturnValueAppended(final boolean appendReturnValue) {
245250
this.appendReturnValue = appendReturnValue;
246251
}
247252

253+
/**
254+
* Gets the list of routines which should be invoked each time the script is
255+
* about to execute.
256+
*
257+
* @return Reference to the mutable list of {@link Runnable} objects which the
258+
* {@link ScriptModule} will run prior to executing the script itself.
259+
*/
260+
public List<ScriptCallback> callbacks() {
261+
if (callbacks == null) callbacks = new ArrayList<>();
262+
return callbacks;
263+
}
264+
248265
// -- AbstractModuleInfo methods --
249266

250267
/**

src/main/java/org/scijava/script/ScriptModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.scijava.module.Module;
5151
import org.scijava.module.ModuleItem;
5252
import org.scijava.plugin.Parameter;
53+
import org.scijava.script.process.ScriptCallback;
5354

5455
/**
5556
* A {@link Module} which executes a script.
@@ -149,9 +150,14 @@ public void run() {
149150
engine.put(name, getInput(name));
150151
}
151152

152-
// execute script!
153153
returnValue = null;
154154
try {
155+
// invoke the callbacks
156+
for (final ScriptCallback c : getInfo().callbacks()) {
157+
c.invoke(this);
158+
}
159+
160+
// execute script!
155161
final Reader reader = getInfo().getReader();
156162
if (reader == null) returnValue = engine.eval(new FileReader(path));
157163
else returnValue = engine.eval(reader);
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, 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.script.process;
33+
34+
import javax.script.ScriptException;
35+
36+
import org.scijava.script.ScriptModule;
37+
38+
/**
39+
* A routine which will be invoked just prior to script execution.
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public interface ScriptCallback {
44+
45+
/**
46+
* Invokes the callback routine.
47+
*
48+
* @param module The {@link ScriptModule} instance which will
49+
* execute the script.
50+
*/
51+
void invoke(final ScriptModule module) throws ScriptException;
52+
}

0 commit comments

Comments
 (0)