2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.javascript.internal;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.io.Reader;
21 import java.util.concurrent.ConcurrentHashMap;
23 import javax.script.Compilable;
24 import javax.script.CompiledScript;
25 import javax.script.ScriptEngine;
26 import javax.script.ScriptEngineManager;
27 import javax.script.ScriptException;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.openhab.core.transform.TransformationException;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Simple cache for compiled JavaScript files.
38 * @author Thomas Kordelle - pre compiled scripts
42 @Component(service = JavaScriptEngineManager.class)
43 public class JavaScriptEngineManager {
45 private final Logger logger = LoggerFactory.getLogger(JavaScriptEngineManager.class);
46 private final ScriptEngineManager manager = new ScriptEngineManager();
47 /* keep memory foot print low. max 2 concurrent threads are estimated */
48 private final Map<String, CompiledScript> compiledScriptMap = new ConcurrentHashMap<>(4, 0.5f, 2);
51 * Get a pre compiled script {@link CompiledScript} from cache. If it is not in the cache, then load it from
52 * storage and put a pre compiled version into the cache.
54 * @param filename name of the JavaScript file to load
55 * @return a pre compiled script {@link CompiledScript}
56 * @throws TransformationException if compile of JavaScript failed
58 protected CompiledScript getScript(final String filename) throws TransformationException {
59 synchronized (compiledScriptMap) {
60 if (compiledScriptMap.containsKey(filename)) {
61 logger.debug("Loading JavaScript {} from cache.", filename);
62 return compiledScriptMap.get(filename);
64 final String path = TransformationScriptWatcher.TRANSFORM_FOLDER + File.separator + filename;
65 logger.debug("Loading script {} from storage ", path);
66 try (final Reader reader = new InputStreamReader(new FileInputStream(path))) {
67 final ScriptEngine engine = manager.getEngineByName("javascript");
68 final CompiledScript cScript = ((Compilable) engine).compile(reader);
69 logger.debug("Putting compiled JavaScript {} to cache.", cScript);
70 compiledScriptMap.put(filename, cScript);
72 } catch (IOException | ScriptException e) {
73 throw new TransformationException("An error occurred while loading JavaScript. " + e.getMessage(),
81 * remove a pre compiled script from cache.
83 * @param fileName name of the script file to remove
85 protected void removeFromCache(String fileName) {
86 logger.debug("Removing JavaScript {} from cache.", fileName);
87 compiledScriptMap.remove(fileName);