Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

Commit

Permalink
ScriptInterface supports generics
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel-keller committed Mar 24, 2018
1 parent 0bed312 commit d953ae1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/qwazr/scripts/JavaRunThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;
import java.util.Objects;

class JavaRunThread extends RunThreadAbstract {
class JavaRunThread extends RunThreadAbstract<Object> {

private final Map<String, Object> variables;
private final Class<?> scriptClass;
Expand All @@ -45,13 +45,13 @@ class JavaRunThread extends RunThreadAbstract {
}

@Override
protected boolean runner() throws Exception {
protected Object runner() throws Exception {
Objects.requireNonNull(scriptClass, "Cannot create instance of " + scriptClass);
final Object script = scriptClass.newInstance();
if (libraryService != null)
libraryService.inject(script);
if (script instanceof ScriptInterface)
return ((ScriptInterface) script).run(variables);
return ((ScriptInterface<?>) script).run(variables);
else if (script instanceof Runnable)
((Runnable) script).run();
else
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qwazr/scripts/JsRunThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.HashMap;
import java.util.Map;

class JsRunThread extends RunThreadAbstract {
class JsRunThread extends RunThreadAbstract<Boolean> {

private final SimpleScriptContext scriptContext;
private final ScriptEngine scriptEngine;
Expand All @@ -53,7 +53,7 @@ class JsRunThread extends RunThreadAbstract {
}

@Override
protected boolean runner() throws IOException, ScriptException {
protected Boolean runner() throws IOException, ScriptException {
try (final FileReader fileReader = new FileReader(scriptFile)) {
Object result = scriptEngine.eval(fileReader, scriptContext);
if (result == null)
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/qwazr/scripts/RunThreadAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class RunThreadAbstract implements ScriptRunThread, Runnable, Closeable {
public abstract class RunThreadAbstract<T> implements ScriptRunThread, Runnable, Closeable {

private static final Logger logger = LoggerUtils.getLogger(RunThreadAbstract.class);

Expand All @@ -39,7 +39,7 @@ public abstract class RunThreadAbstract implements ScriptRunThread, Runnable, Cl
private volatile Long endTime;
private volatile Long expirationTime;
private volatile Exception exception;
private volatile Boolean result;
private volatile T result;

protected final String httpAddressKey;

Expand Down Expand Up @@ -88,7 +88,7 @@ final public Exception getException() {
}

@Override
final public Boolean getResult() {
final public Object getResult() {
return result;
}

Expand All @@ -98,8 +98,8 @@ final public String getUUID() {
}

@Override
final public ScriptRunStatus getStatus() {
return new ScriptRunStatus(httpAddressKey, scriptName, uuid, state, startTime, endTime, initialBinding,
final public ScriptRunStatus<T> getStatus() {
return new ScriptRunStatus<>(httpAddressKey, scriptName, uuid, state, startTime, endTime, initialBinding,
exception, result);
}

Expand All @@ -118,7 +118,7 @@ final public String getErr() {
return errorWriter == null ? StringUtils.EMPTY : errorWriter.toString();
}

protected abstract boolean runner() throws Exception;
protected abstract T runner() throws Exception;

@Override
final public void run() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qwazr/scripts/ScriptInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Map;

public interface ScriptInterface {
public interface ScriptInterface<T> {

boolean run(Map<String, ?> variables) throws Exception;
T run(Map<String, ?> variables) throws Exception;
}
8 changes: 4 additions & 4 deletions src/main/java/com/qwazr/scripts/ScriptRunStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
creatorVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
public class ScriptRunStatus {
public class ScriptRunStatus<T> {

public enum ScriptState {
ready, running, terminated, error
Expand All @@ -54,15 +54,15 @@ public enum ScriptState {
public final Date endTime;
public final Map<String, Object> bindings;
public final String error;
public final Boolean result;
public final T result;

@JsonCreator
ScriptRunStatus(@JsonProperty("node") String node, @JsonProperty("name") String name,
@JsonProperty("_status") String statusPath, @JsonProperty("_std_out") String stdOutPath,
@JsonProperty("_std_err") String stdErrPath, @JsonProperty("uuid") String uuid,
@JsonProperty("state") ScriptState state, @JsonProperty("start") Date startTime,
@JsonProperty("end") Date endTime, @JsonProperty("bindings") Map<String, Object> bindings,
@JsonProperty("error") String error, @JsonProperty("result") Boolean result) {
@JsonProperty("error") String error, @JsonProperty("result") T result) {
this.node = node;
this.statusPath = statusPath;
this.stdOutPath = stdOutPath;
Expand All @@ -78,7 +78,7 @@ public enum ScriptState {
}

ScriptRunStatus(String node, String name, String uuid, ScriptState state, Long startTime, Long endTime,
Map<String, Object> bindings, Exception exception, Boolean result) {
Map<String, Object> bindings, Exception exception, T result) {
this(node, name, node + "/scripts/status/" + uuid, node + "/scripts/status/" + uuid + "/out",
node + "/scripts/status/" + uuid + "/err", uuid, state, startTime == null ? null : new Date(startTime),
endTime == null ? null : new Date(endTime), bindings, exception == null ? null : exception.getMessage(),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qwazr/scripts/ScriptRunThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.qwazr.scripts;

public interface ScriptRunThread {
public interface ScriptRunThread<T> {

Exception getException();

Boolean getResult();
T getResult();

String getUUID();

Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/qwazr/scripts/TaskVariablesScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

package com.qwazr.scripts;

import com.qwazr.scripts.ScriptInterface;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class TaskVariablesScript implements ScriptInterface {
public class TaskVariablesScript implements ScriptInterface<Boolean> {

final public static AtomicInteger EXECUTION_COUNT = new AtomicInteger(0);

@Override
public boolean run(Map<String, ?> variables) throws Exception {
public Boolean run(Map<String, ?> variables) throws Exception {
if (variables.get("ScriptTest") == null)
throw new Exception("Error");
EXECUTION_COUNT.incrementAndGet();
Expand Down

0 comments on commit d953ae1

Please sign in to comment.