Skip to content

Commit

Permalink
Align AbstractScriptDependencyTracker with AbstractScriptFileWatcher (o…
Browse files Browse the repository at this point in the history
…penhab#3372)

Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: c17481c
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent 4c88260 commit 5b7ee40
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static org.openhab.core.service.WatchService.Kind.MODIFY;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -50,9 +52,19 @@ public abstract class AbstractScriptDependencyTracker
private final BidiSetBag<String, String> scriptToLibs = new BidiSetBag<>();
private final WatchService watchService;

public AbstractScriptDependencyTracker(WatchService watchService, final String libraryPath) {
this.libraryPath = Path.of(libraryPath);
public AbstractScriptDependencyTracker(WatchService watchService, final String fileDirectory) {
this.watchService = watchService;
this.libraryPath = watchService.getWatchPath().resolve(fileDirectory);

if (!Files.exists(libraryPath)) {
try {
Files.createDirectories(libraryPath);
} catch (IOException e) {
logger.warn("Failed to create watched directory: {}", libraryPath);
}
} else if (!Files.isDirectory(libraryPath)) {
logger.warn("Trying to watch directory {}, however it is a file", libraryPath);
}

watchService.registerListener(this, this.libraryPath);
}
Expand All @@ -61,6 +73,10 @@ public void deactivate() {
watchService.unregisterListener(this);
}

public Path getLibraryPath() {
return libraryPath;
}

@Override
public void processWatchEvent(WatchService.Kind kind, Path path) {
File file = path.toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.OpenHAB;
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
import org.openhab.core.automation.module.script.ScriptEngineContainer;
import org.openhab.core.automation.module.script.ScriptEngineManager;
Expand Down Expand Up @@ -101,7 +100,7 @@ public AbstractScriptFileWatcher(final WatchService watchService, final ScriptEn
this.manager = manager;
this.readyService = readyService;
this.watchSubDirectories = watchSubDirectories;
this.watchPath = Path.of(OpenHAB.getConfigFolder()).resolve(fileDirectory);
this.watchPath = watchService.getWatchPath().resolve(fileDirectory);

manager.addFactoryChangeListener(this);
readyService.registerTracker(this, new ReadyMarkerFilter().withType(StartLevelService.STARTLEVEL_MARKER_TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
package org.openhab.core.automation.module.script.rulesupport.loader;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.*;

import java.nio.file.Path;

Expand Down Expand Up @@ -49,6 +46,7 @@ public class AbstractScriptDependencyTrackerTest {

@BeforeEach
public void setup() {
when(watchServiceMock.getWatchPath()).thenReturn(Path.of(""));
scriptDependencyTracker = new AbstractScriptDependencyTracker(watchServiceMock, WATCH_DIR) {
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class AbstractScriptFileWatcherTest extends JavaTest {
public void setUp() {
System.setProperty(CONFIG_DIR_PROG_ARGUMENT, tempScriptDir.toString());

when(watchServiceMock.getWatchPath()).thenReturn(tempScriptDir);

atomicInteger.set(0);
currentStartLevel = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,20 @@ private void closeWatcherAndUnregister() throws IOException {
}
}

@Override
public Path getWatchPath() {
Path basePath = this.basePath;
if (basePath == null) {
throw new IllegalStateException("Trying to access WatchService before initialization completed.");
}
return basePath;
}

@Override
public void registerListener(WatchEventListener watchEventListener, List<Path> paths, boolean withSubDirectories) {
Path basePath = this.basePath;
if (basePath == null) {
throw new IllegalStateException("Trying to register listener before initialization complete.");
throw new IllegalStateException("Trying to register listener before initialization completed.");
}
for (Path path : paths) {
Path absolutePath = path.isAbsolute() ? path : basePath.resolve(path).toAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ default void registerListener(WatchEventListener watchEventListener, Path path,
*/
void unregisterListener(WatchEventListener watchEventListener);

/**
* Get the base directory for this {@link WatchService}
*
* @return the {@link Path} that is the base path for all reported events
*/
Path getWatchPath();

@FunctionalInterface
interface WatchEventListener {
/**
Expand Down

0 comments on commit 5b7ee40

Please sign in to comment.