Skip to content

Commit

Permalink
Use the mime types provided by the ScriptEngineFactory (#2595)
Browse files Browse the repository at this point in the history
* Use the mime types provided by the ScriptEngineFactory and not the underlying script engines they provide. This allows mutliple versions of script engines to co-exist, like Nashorn and GraalVM .

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
  • Loading branch information
digitaldan authored Dec 13, 2021
1 parent 3b2d2ad commit eb3f1e9
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public void setScriptEngineFactory(ScriptEngineFactory engineFactory) {
if (!scriptTypes.isEmpty()) {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.get(0));
if (scriptEngine != null) {
javax.script.ScriptEngineFactory factory = scriptEngine.getFactory();
parameterOptions.put(getPreferredMimeType(factory), getLanguageName(factory));
parameterOptions.put(getPreferredMimeType(engineFactory), getLanguageName(scriptEngine.getFactory()));
logger.trace("ParameterOptions: {}", parameterOptions);
} else {
logger.trace("setScriptEngineFactory: engine was null");
Expand All @@ -165,8 +164,7 @@ public void unsetScriptEngineFactory(ScriptEngineFactory engineFactory) {
if (!scriptTypes.isEmpty()) {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.get(0));
if (scriptEngine != null) {
javax.script.ScriptEngineFactory factory = scriptEngine.getFactory();
parameterOptions.remove(getPreferredMimeType(factory));
parameterOptions.remove(getPreferredMimeType(engineFactory));
logger.trace("ParameterOptions: {}", parameterOptions);
} else {
logger.trace("unsetScriptEngineFactory: engine was null");
Expand All @@ -176,10 +174,10 @@ public void unsetScriptEngineFactory(ScriptEngineFactory engineFactory) {
}
}

private String getPreferredMimeType(javax.script.ScriptEngineFactory factory) {
List<String> mimeTypes = new ArrayList<>(factory.getMimeTypes());
private String getPreferredMimeType(ScriptEngineFactory factory) {
List<String> mimeTypes = new ArrayList<>(factory.getScriptTypes());
mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || mimeType.contains("x-"));
return mimeTypes.isEmpty() ? factory.getMimeTypes().get(0) : mimeTypes.get(0);
return mimeTypes.isEmpty() ? factory.getScriptTypes().get(0) : mimeTypes.get(0);
}

private String getLanguageName(javax.script.ScriptEngineFactory factory) {
Expand Down

0 comments on commit eb3f1e9

Please sign in to comment.