]> git.basschouten.com Git - openhab-addons.git/blob
c83fba16ee49c05e883359d2d063019b53bd6837
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jsscripting.internal;
14
15 import javax.script.Invocable;
16 import javax.script.ScriptEngine;
17
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.graalvm.polyglot.PolyglotException;
20 import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
26  *
27  * @author Jonathan Gilbert - Initial contribution
28  */
29 class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
30         extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
31
32     private static final String SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER = "openhab-transformation-script-";
33
34     private @Nullable Logger logger;
35
36     public DebuggingGraalScriptEngine(T delegate) {
37         super(delegate);
38     }
39
40     @Override
41     public Exception afterThrowsInvocation(Exception e) {
42         if (logger == null) {
43             initializeLogger();
44         }
45
46         Throwable cause = e.getCause();
47         if (cause instanceof IllegalArgumentException) {
48             logger.error("Failed to execute script:", e);
49         }
50         if (cause instanceof PolyglotException) {
51             logger.error("Failed to execute script:", cause);
52         }
53         return e;
54     }
55
56     /**
57      * Initializes the logger.
58      * This cannot be done on script engine creation because the context variables are not yet initialized.
59      * Therefore, the logger needs to be initialized on the first use after script engine creation.
60      */
61     private void initializeLogger() {
62         Object fileName = delegate.getContext().getAttribute("javax.script.filename");
63         Object ruleUID = delegate.getContext().getAttribute("ruleUID");
64         Object ohEngineIdentifier = delegate.getContext().getAttribute("oh.engine-identifier");
65
66         String identifier = "stack";
67         if (fileName != null) {
68             identifier = fileName.toString().replaceAll("^.*[/\\\\]", "");
69         } else if (ruleUID != null) {
70             identifier = ruleUID.toString();
71         } else if (ohEngineIdentifier != null) {
72             if (ohEngineIdentifier.toString().startsWith(SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER)) {
73                 identifier = ohEngineIdentifier.toString().replaceAll(SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER,
74                         "transformation.");
75             }
76         }
77
78         logger = LoggerFactory.getLogger("org.openhab.automation.script.javascript." + identifier);
79     }
80 }