From 6a6e201ac7bc12180f161e305046f43da1e3f385 Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Fri, 25 Jun 2021 11:11:18 +0200 Subject: [PATCH] Revert "Ignore relative library directories for scripts (#2408)" (#2414) This reverts commit 241a4f6ebf6bc12382ddb477b8627a2a64cd1bb7. --- .../internal/loader/DependencyTracker.java | 17 ++++++++++------- .../internal/loader/ScriptFileWatcher.java | 16 +--------------- .../internal/loader/ScriptLibraryWatcher.java | 8 ++++++-- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/DependencyTracker.java b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/DependencyTracker.java index ead42520a..d93cf15de 100644 --- a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/DependencyTracker.java +++ b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/DependencyTracker.java @@ -17,6 +17,9 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.openhab.core.automation.module.script.rulesupport.internal.loader.collection.BidiSetBag; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; @@ -24,25 +27,25 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Tracks dependencies between scripts and reloads dependees. Can be used by script engine providers to watch library - * files. + * Tracks dependencies between scripts and reloads dependees * * @author Jonathan Gilbert - Initial contribution */ -public abstract class DependencyTracker { +@Component(immediate = true, service = DependencyTracker.class) +public class DependencyTracker { private final Logger logger = LoggerFactory.getLogger(DependencyTracker.class); private final Set dependencyChangeListeners = ConcurrentHashMap.newKeySet(); private final BidiSetBag scriptToLibs = new BidiSetBag<>(); - private final ScriptLibraryWatcher scriptLibraryWatcher = new ScriptLibraryWatcher(getLibPath()) { + private final ScriptLibraryWatcher scriptLibraryWatcher = new ScriptLibraryWatcher() { @Override void updateFile(String libraryPath) { Set scripts; synchronized (scriptToLibs) { scripts = new HashSet<>(scriptToLibs.getKeys(libraryPath)); // take a copy as it will change as we - // reimport + // reimport } DependencyTracker.this.logger.debug("Library {} changed; reimporting {} scripts...", libraryPath, scripts.size()); @@ -52,12 +55,12 @@ public abstract class DependencyTracker { } }; - abstract String getLibPath(); - + @Activate public void activate() { scriptLibraryWatcher.activate(); } + @Deactivate public void deactivate() { scriptLibraryWatcher.deactivate(); } diff --git a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcher.java b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcher.java index 2c6d68033..f4a3e7f02 100644 --- a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcher.java +++ b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcher.java @@ -59,13 +59,12 @@ import org.slf4j.LoggerFactory; * * @author Simon Merschjohann - Initial contribution * @author Kai Kreuzer - improved logging and removed thread pool - * @author Jonathan Gilbert - added dependency tracking, per-script start levels & ignore lib dirs + * @author Jonathan Gilbert - added dependency tracking & per-script start levels */ @Component(immediate = true) public class ScriptFileWatcher extends AbstractWatchService implements ReadyService.ReadyTracker, DependencyTracker.DependencyChangeListener { - private static final Set KNOWN_LIB_NAMES = Set.of("node_modules"); private static final String FILE_DIRECTORY = "automation" + File.separator + "jsr223"; private static final long RECHECK_INTERVAL = 20; @@ -152,22 +151,9 @@ public class ScriptFileWatcher extends AbstractWatchService @Override protected Kind[] getWatchEventKinds(Path subDir) { - if (isLibDirectory(subDir)) { - return null; // don't watch libraries - } - return new Kind[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY }; } - private Boolean isLibDirectory(Path subDir) { - for (Path segment : subDir) { - if (KNOWN_LIB_NAMES.contains(segment.toString())) { - return true; - } - } - return false; - } - @Override protected void processWatchEvent(WatchEvent event, Kind kind, Path path) { File file = path.toFile(); diff --git a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptLibraryWatcher.java b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptLibraryWatcher.java index f30d14dc0..2f8ba5f0e 100644 --- a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptLibraryWatcher.java +++ b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptLibraryWatcher.java @@ -18,6 +18,7 @@ import java.io.File; import java.nio.file.Path; import java.nio.file.WatchEvent; +import org.openhab.core.OpenHAB; import org.openhab.core.service.AbstractWatchService; /** @@ -27,8 +28,11 @@ import org.openhab.core.service.AbstractWatchService; */ abstract class ScriptLibraryWatcher extends AbstractWatchService { - ScriptLibraryWatcher(String libPath) { - super(libPath); + public static final String LIB_PATH = String.join(File.separator, OpenHAB.getConfigFolder(), "automation", "lib", + "javascript"); + + ScriptLibraryWatcher() { + super(LIB_PATH); } @Override