]> git.basschouten.com Git - openhab-addons.git/blob
817e64a8ddb3dd87dedb7c0c235a6e758c038fba
[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.ScriptDependencyTracker;
24 import org.openhab.core.automation.module.script.ScriptEngineManager;
25 import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
26 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
27 import org.openhab.core.service.ReadyService;
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  */
37 @Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
38 public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
39     private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
40
41     private final String ignorePath;
42
43     @Activate
44     public JSScriptFileWatcher(final @Reference ScriptEngineManager manager,
45             final @Reference ReadyService readyService) {
46         super(manager, readyService, FILE_DIRECTORY);
47
48         ignorePath = pathToWatch + File.separator + "node_modules";
49     }
50
51     @Override
52     protected void processWatchEvent(@Nullable WatchEvent<?> event, WatchEvent.@Nullable Kind<?> kind,
53             @Nullable Path path) {
54         if (Objects.nonNull(path)) {
55             if (!path.startsWith(ignorePath)) {
56                 super.processWatchEvent(event, kind, path);
57             }
58         }
59     }
60
61     @Override
62     protected boolean createAndLoad(ScriptFileReference ref) {
63         return super.createAndLoad(new ScriptFileReference(ref.getScriptFileURL()) {
64             @Override
65             public Optional<String> getScriptType() {
66                 assert super.getScriptType().get().equalsIgnoreCase("js");
67                 return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
68             }
69         });
70     }
71
72     @Override
73     protected boolean watchSubDirectories() {
74         return false;
75     }
76 }