mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-11 05:41:52 +01:00
This reverts commit 241a4f6ebf
.
This commit is contained in:
parent
241a4f6ebf
commit
6a6e201ac7
@ -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<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());
|
||||
@ -52,12 +55,12 @@ public abstract class DependencyTracker {
|
||||
}
|
||||
};
|
||||
|
||||
abstract String getLibPath();
|
||||
|
||||
@Activate
|
||||
public void activate() {
|
||||
scriptLibraryWatcher.activate();
|
||||
}
|
||||
|
||||
@Deactivate
|
||||
public void deactivate() {
|
||||
scriptLibraryWatcher.deactivate();
|
||||
}
|
||||
|
@ -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<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;
|
||||
|
||||
@ -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();
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user