Skip to content

Commit

Permalink
Revert "Ignore relative library directories for scripts (openhab#2408)…
Browse files Browse the repository at this point in the history
…" (openhab#2414)

This reverts commit 241a4f6.
  • Loading branch information
kaikreuzer authored Jun 25, 2021
1 parent 241a4f6 commit 6a6e201
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,35 @@
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. 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<DependencyChangeListener> dependencyChangeListeners = ConcurrentHashMap.newKeySet();

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

abstract String getLibPath();

@Activate
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,13 +59,12 @@
*
* @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<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 @@ -152,22 +151,9 @@ 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,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.WatchEvent;

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

/**
Expand All @@ -27,8 +28,11 @@
*/
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
Expand Down

0 comments on commit 6a6e201

Please sign in to comment.