]> git.basschouten.com Git - openhab-addons.git/blob
3b728cc1f34e6210baab4ea2e5d14c4d8b7134a3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.nio.file.WatchEvent;
18 import java.util.Optional;
19
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
22 import org.openhab.core.automation.module.script.ScriptEngineManager;
23 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
24 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
25 import org.openhab.core.service.ReadyService;
26
27 /**
28  * Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
29  *
30  * @author Jonathan Gilbert - Initial contribution
31  */
32 public class JSScriptFileWatcher extends ScriptFileWatcher {
33     private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
34     private static final String IGNORE_DIR_NAME = "node_modules";
35
36     private final String ignorePath;
37
38     public JSScriptFileWatcher(final ScriptEngineManager manager, final ReadyService readyService,
39             JSDependencyTracker dependencyTracker) {
40         super(manager, dependencyTracker, readyService, FILE_DIRECTORY);
41
42         ignorePath = pathToWatch + File.separator + "node_modules";
43     }
44
45     @Override
46     protected void processWatchEvent(@Nullable WatchEvent<?> event, WatchEvent.@Nullable Kind<?> kind,
47             @Nullable Path path) {
48         if (!path.startsWith(ignorePath)) {
49             super.processWatchEvent(event, kind, path);
50         }
51     }
52
53     @Override
54     protected boolean createAndLoad(ScriptFileReference ref) {
55         return super.createAndLoad(new ScriptFileReference(ref.getScriptFileURL()) {
56             @Override
57             public Optional<String> getScriptType() {
58                 assert super.getScriptType().get().equalsIgnoreCase("js");
59                 return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
60             }
61         });
62     }
63
64     @Override
65     protected boolean watchSubDirectories() {
66         return false;
67     }
68 }