Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback to rhino if no default javascript is found .3 (lscproject#280) #304

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/org/lsc/utils/ScriptingEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

import javax.script.*;
Expand Down Expand Up @@ -31,7 +32,7 @@ public class ScriptingEvaluator {

private Map<String, ScriptableEvaluator> instancesTypeCache;

private ScriptableEvaluator defaultImplementation;
private Optional<ScriptableEvaluator> defaultImplementation;

static {
implementetionsCache = new HashMap<String, Class<? extends ScriptableEvaluator>>();
Expand Down Expand Up @@ -87,10 +88,11 @@ else if ("graal.js".equals(name)) {
bindings.put( "polyglot.js.nashorn-compat", true);
JScriptEvaluator graaljsevaluator = new JScriptEvaluator(graaljsEngine);
instancesTypeCache.put("gj", graaljsevaluator);
defaultImplementation = graaljsevaluator;
defaultImplementation = Optional.of(graaljsevaluator);
}
else {
defaultImplementation = instancesTypeCache.get("js");
defaultImplementation = Optional.ofNullable(Optional.ofNullable(instancesTypeCache.get("js"))
.orElse(instancesTypeCache.get("rjs")));
}
}

Expand All @@ -111,13 +113,13 @@ public static void contribute(String implementationName,
implementetionsCache.put(implementationName, implementationClass);
}

private ScriptableEvaluator identifyScriptingEngine(String expression) {
private ScriptableEvaluator identifyScriptingEngine(String expression) throws LscServiceException {
String[] parts = expression.split(":");
if (parts != null && parts.length > 0 && parts[0].length() < 10
&& instancesTypeCache.containsKey(parts[0])) {
return instancesTypeCache.get(parts[0]);
}
return defaultImplementation;
return defaultImplementation.orElseThrow(() -> new LscServiceException("Missing Script evaluator"));
}

/**
Expand Down
Loading