-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
NashornScriptEngineFactory.java
84 lines (71 loc) · 2.96 KB
/
NashornScriptEngineFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jsscriptingnashorn.internal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.module.script.AbstractScriptEngineFactory;
import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.osgi.service.component.annotations.Component;
/**
* This is an implementation of a {@link ScriptEngineFactory} for Nashorn.
*
* @author Wouter Born - Initial contribution
*/
@Component(service = ScriptEngineFactory.class)
@NonNullByDefault
public class NashornScriptEngineFactory extends AbstractScriptEngineFactory {
private final org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory factory = new org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory();
private final List<String> scriptTypes = createScriptTypes();
private List<String> createScriptTypes() {
List<String> extensions = List.of("nashornjs");
String mimeTypeVersion = ";version=ECMAScript-5.1";
List<String> mimeTypes = factory.getMimeTypes().stream().map(mimeType -> mimeType + mimeTypeVersion)
.collect(Collectors.toUnmodifiableList());
return Stream.of(extensions, mimeTypes).flatMap(List::stream).collect(Collectors.toUnmodifiableList());
}
@Override
public List<String> getScriptTypes() {
return scriptTypes;
}
@Override
public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValues) {
Set<String> expressions = new HashSet<>();
for (Entry<String, Object> entry : scopeValues.entrySet()) {
scriptEngine.put(entry.getKey(), entry.getValue());
if (entry.getValue() instanceof Class) {
expressions.add(String.format("%s = %<s.static;", entry.getKey()));
}
}
String scriptToEval = String.join("\n", expressions);
try {
scriptEngine.eval(scriptToEval);
} catch (ScriptException ex) {
logger.error("ScriptException while importing scope: {}", ex.getMessage());
}
}
@Override
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
return scriptTypes.contains(scriptType)
? factory.getScriptEngine(NashornScriptEngineFactory.class.getClassLoader())
: null;
}
}