]> git.basschouten.com Git - openhab-addons.git/blob
70aed1e6f1885ebacdde342ad6f122630c07d725
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nio.file.Path;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
21 import org.openhab.core.automation.module.script.ScriptDependencyTracker;
22 import org.openhab.core.automation.module.script.ScriptEngineManager;
23 import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
24 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
25 import org.openhab.core.service.ReadyService;
26 import org.openhab.core.service.StartLevelService;
27 import org.openhab.core.service.WatchService;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31
32 /**
33  * Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
34  *
35  * @author Jonathan Gilbert - Initial contribution
36  * @author Jan N. Klug - Refactored to new WatchService
37  */
38 @Component(immediate = true, service = { ScriptFileWatcher.class, ScriptDependencyTracker.Listener.class })
39 @NonNullByDefault
40 public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
41     private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
42
43     @Activate
44     public JSScriptFileWatcher(final @Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService,
45             final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService,
46             final @Reference StartLevelService startLevelService) {
47         super(watchService, manager, readyService, startLevelService, FILE_DIRECTORY, true);
48     }
49
50     @Override
51     protected Optional<String> getScriptType(Path scriptFilePath) {
52         if (!scriptFilePath.startsWith(FILE_DIRECTORY + File.separator + "node_modules")
53                 && "js".equals(super.getScriptType(scriptFilePath).orElse(null))) {
54             return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
55         } else {
56             return Optional.empty();
57         }
58     }
59 }