2 * Copyright (c) 2010-2023 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.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.automation.module.script.ScriptDependencyTracker;
21 import org.openhab.core.automation.module.script.ScriptEngineManager;
22 import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
23 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
24 import org.openhab.core.service.ReadyService;
25 import org.openhab.core.service.StartLevelService;
26 import org.openhab.core.service.WatchService;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
32 * Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
34 * @author Jonathan Gilbert - Initial contribution
35 * @author Jan N. Klug - Refactored to new WatchService
37 @Component(immediate = true, service = { ScriptFileWatcher.class, ScriptDependencyTracker.Listener.class })
39 public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
40 private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
43 public JSScriptFileWatcher(final @Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService,
44 final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService,
45 final @Reference StartLevelService startLevelService) {
46 super(watchService, manager, readyService, startLevelService, FILE_DIRECTORY, true);
50 protected Optional<String> getScriptType(Path scriptFilePath) {
51 String scriptType = super.getScriptType(scriptFilePath).orElse(null);
52 if (!scriptFilePath.startsWith(getWatchPath().resolve("node_modules")) && ("js".equals(scriptType))) {
53 return Optional.of(scriptType);
55 return Optional.empty();