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

always set default js #280 #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions src/main/java/org/lsc/utils/ErrorMissingScriptEvaluator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.lsc.utils;

import org.lsc.Task;
import org.lsc.exception.LscServiceException;

import java.util.List;
import java.util.Map;

public class ErrorMissingScriptEvaluator
implements ScriptableEvaluator {
@Override
public String evalToString(Task task, String expression, Map<String, Object> params) throws LscServiceException {
throw new LscServiceException("Missing Script evaluator");
}

@Override
public List<Object> evalToObjectList(Task task, String expression, Map<String, Object> params) throws LscServiceException {
throw new LscServiceException("Missing Script evaluator");
}

@Override
public List<byte[]> evalToByteArrayList(Task task, String expression, Map<String, Object> params) throws LscServiceException {
throw new LscServiceException("Missing Script evaluator");
}

@Override
public byte[] evalToByteArray(Task task, String expression, Map<String, Object> params) throws LscServiceException {
throw new LscServiceException("Missing Script evaluator");
}

@Override
public Boolean evalToBoolean(Task task, String expression, Map<String, Object> params) throws LscServiceException {
throw new LscServiceException("Missing Script evaluator");
}


}
11 changes: 10 additions & 1 deletion src/main/java/org/lsc/utils/ScriptingEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ScriptingEvaluator {
}

private ScriptingEvaluator() {
defaultImplementation = new ErrorMissingScriptEvaluator();
instancesTypeCache = new HashMap<String, ScriptableEvaluator>();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory sef : factories) {
Expand Down Expand Up @@ -90,7 +91,15 @@ else if ("graal.js".equals(name)) {
defaultImplementation = graaljsevaluator;
}
else {
defaultImplementation = instancesTypeCache.get("js");
ScriptableEvaluator fetchedDefaultImplementation = null;
for ( String name: new String[] {"js","rjs"} ) {
fetchedDefaultImplementation = instancesTypeCache.get(name);
if ( fetchedDefaultImplementation != null ) {
defaultImplementation = fetchedDefaultImplementation;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If understand well, this change means if you don't find any "js" implementation, you defaults on "rjs" which is in fact Rhino.
It's hard to understand that when reading your code at first sight. Wouldn't it be possible to make it more obvious?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you understand well, trying to get the better default implementation.

it might be possible to make it more obvious, but making things obvious is not as obvious as it sound ;-)
Does just a comment would help ?
Or {"js","rjs"} set in a variable named 'preferredDefaultImplementations' ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would just be more explicit, even if it means repeating myself:

defaultImplementation = instancesTypeCache.get("js");
if (defaultImplementation == null) {
  defaultImplementation = instancesTypeCache.get("rjs");
}

with the optional solution it's even easier to write it:

			defaultImplementation = Optional.ofNullable(instancesTypeCache.get("js"))
					.or(() -> Optional.ofNullable(instancesTypeCache.get("rjs")));

It's very easy and clear (to me at least ^^) how to add new cases if needed later (I guess it's the purpose of your array).


}
}

Expand Down
Loading