Skip to content

Commit

Permalink
Ignore relative library directories for scripts (#2408)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Gilbert <jpg@trillica.com>
  • Loading branch information
jpg0 authored Jun 25, 2021
1 parent 2d71afe commit 241a4f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,32 @@
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Tracks dependencies between scripts and reloads dependees
* Tracks dependencies between scripts and reloads dependees. Can be used by script engine providers to watch library
* files.
*
* @author Jonathan Gilbert - Initial contribution
*/
@Component(immediate = true, service = DependencyTracker.class)
public class DependencyTracker {
public abstract class DependencyTracker {

private final Logger logger = LoggerFactory.getLogger(DependencyTracker.class);

private final Set<DependencyChangeListener> dependencyChangeListeners = ConcurrentHashMap.newKeySet();

private final BidiSetBag<String, String> scriptToLibs = new BidiSetBag<>();
private final ScriptLibraryWatcher scriptLibraryWatcher = new ScriptLibraryWatcher() {
private final ScriptLibraryWatcher scriptLibraryWatcher = new ScriptLibraryWatcher(getLibPath()) {
@Override
void updateFile(String libraryPath) {
Set<String> 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());
Expand All @@ -55,12 +52,12 @@ void updateFile(String libraryPath) {
}
};

@Activate
abstract String getLibPath();

public void activate() {
scriptLibraryWatcher.activate();
}

@Deactivate
public void deactivate() {
scriptLibraryWatcher.deactivate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@
*
* @author Simon Merschjohann - Initial contribution
* @author Kai Kreuzer - improved logging and removed thread pool
* @author Jonathan Gilbert - added dependency tracking & per-script start levels
* @author Jonathan Gilbert - added dependency tracking, per-script start levels & ignore lib dirs
*/
@Component(immediate = true)
public class ScriptFileWatcher extends AbstractWatchService
implements ReadyService.ReadyTracker, DependencyTracker.DependencyChangeListener {

private static final Set<String> KNOWN_LIB_NAMES = Set.of("node_modules");
private static final String FILE_DIRECTORY = "automation" + File.separator + "jsr223";
private static final long RECHECK_INTERVAL = 20;

Expand Down Expand Up @@ -151,9 +152,22 @@ protected boolean watchSubDirectories() {

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.file.Path;
import java.nio.file.WatchEvent;

import org.openhab.core.OpenHAB;
import org.openhab.core.service.AbstractWatchService;

/**
Expand All @@ -28,11 +27,8 @@
*/
abstract class ScriptLibraryWatcher extends AbstractWatchService {

public static final String LIB_PATH = String.join(File.separator, OpenHAB.getConfigFolder(), "automation", "lib",
"javascript");

ScriptLibraryWatcher() {
super(LIB_PATH);
ScriptLibraryWatcher(String libPath) {
super(libPath);
}

@Override
Expand Down

0 comments on commit 241a4f6

Please sign in to comment.