2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.jsscripting.internal.fs.watch;
16 import java.nio.file.Path;
17 import java.nio.file.WatchEvent;
18 import java.util.Objects;
19 import java.util.Optional;
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;
33 * Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
35 * @author Jonathan Gilbert - Initial contribution
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";
41 private final String ignorePath;
44 public JSScriptFileWatcher(final @Reference ScriptEngineManager manager,
45 final @Reference ReadyService readyService) {
46 super(manager, readyService, FILE_DIRECTORY);
48 ignorePath = pathToWatch + File.separator + "node_modules";
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);
62 protected boolean createAndLoad(ScriptFileReference ref) {
63 return super.createAndLoad(new ScriptFileReference(ref.getScriptFileURL()) {
65 public Optional<String> getScriptType() {
66 assert super.getScriptType().get().equalsIgnoreCase("js");
67 return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
73 protected boolean watchSubDirectories() {