]> git.basschouten.com Git - openhab-addons.git/blob
2285c26903dfb1c5f3fbd45cf0c84c04f8659f2a
[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.Objects;
19 import java.util.Optional;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
23 import org.openhab.core.automation.module.script.ScriptEngineManager;
24 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
25 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
26 import org.openhab.core.service.ReadyService;
27
28 /**
29  * Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
30  *
31  * @author Jonathan Gilbert - Initial contribution
32  */
33 public class JSScriptFileWatcher extends ScriptFileWatcher {
34     private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
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 (Objects.nonNull(path)) {
49             if (!path.startsWith(ignorePath)) {
50                 super.processWatchEvent(event, kind, path);
51             }
52         }
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
66     @Override
67     protected boolean watchSubDirectories() {
68         return false;
69     }
70 }