]> git.basschouten.com Git - openhab-addons.git/blob
39dca7f3da6612abdfe6d3fc0bff4794ff5649ab
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.fs.watch;
14
15 import java.io.File;
16 import java.util.Optional;
17
18 import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
19 import org.openhab.core.automation.module.script.ScriptEngineManager;
20 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
21 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
22 import org.openhab.core.service.ReadyService;
23 import org.osgi.service.component.annotations.Activate;
24 import org.osgi.service.component.annotations.Component;
25 import org.osgi.service.component.annotations.Deactivate;
26 import org.osgi.service.component.annotations.Reference;
27
28 /**
29  * Monitors <openHAB-conf>/automation/js for Javascript files
30  *
31  * @author Jonathan Gilbert - Initial contribution
32  */
33 @Component(immediate = true)
34 public class JSScriptFileWatcher extends ScriptFileWatcher {
35     private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
36
37     @Activate
38     public JSScriptFileWatcher(final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService,
39             final @Reference JSDependencyTracker jsDependencyTracker) {
40         super(manager, jsDependencyTracker, readyService, FILE_DIRECTORY);
41     }
42
43     @Activate
44     @Override
45     public void activate() {
46         super.activate();
47     }
48
49     @Deactivate
50     @Override
51     public void deactivate() {
52         super.deactivate();
53     }
54
55     @Override
56     protected boolean createAndLoad(ScriptFileReference ref) {
57         return super.createAndLoad(new ScriptFileReference(ref.getScriptFileURL()) {
58             @Override
59             public Optional<String> getScriptType() {
60                 assert super.getScriptType().get().equalsIgnoreCase("js");
61                 return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
62             }
63         });
64     }
65 }