Skip to content

Commit

Permalink
fallback to rhino if no default javascript is found .3 (lscproject#28…
Browse files Browse the repository at this point in the history
…0) (#304)

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

   - avoid NullPointerException even if rhino and js are missing

* #304: Using java 8 compatible Optional code

---------

Co-authored-by: philha <[email protected]>
Co-authored-by: Abhishek Pai <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2024
1 parent bfd929d commit 418acd8
Showing 1 changed file with 7 additions and 5 deletions.
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

0 comments on commit 418acd8

Please sign in to comment.