Class AbstractScriptFileWatcher
java.lang.Object
org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher
- All Implemented Interfaces:
ScriptFileWatcher,ScriptDependencyTracker.Listener,ScriptEngineManager.FactoryChangeListener,ReadyService.ReadyTracker,WatchService.WatchEventListener
@NonNullByDefault
public abstract class AbstractScriptFileWatcher
extends Object
implements WatchService.WatchEventListener, ReadyService.ReadyTracker, ScriptDependencyTracker.Listener, ScriptEngineManager.FactoryChangeListener, ScriptFileWatcher
The
AbstractScriptFileWatcher is default implementation for watching a directory for files. If a new/modified
file is detected, the script is read and passed to the ScriptEngineManager. It needs to be sub-classed for
actual use.- Author:
- Simon Merschjohann - Initial contribution, Kai Kreuzer - improved logging and removed thread pool, Jonathan Gilbert - added dependency tracking and per-script start levels; made reusable, Jan N. Klug - Refactored dependency tracking to script engine factories
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractScriptFileWatcher(WatchService watchService, ScriptEngineManager manager, ReadyService readyService, StartLevelService startLevelService, String fileDirectory, boolean watchSubDirectories) -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate()voidvoidfactoryAdded(@Nullable String scriptType) Called by theScriptEngineManagerwhen a ScriptEngineFactory is addedvoidfactoryRemoved(@Nullable String scriptType) Called by theScriptEngineManagerwhen a ScriptEngineFactory is removedprotected ScheduledExecutorServiceCan be overridden by subclasses (e.g. for testing)getScriptType(Path scriptFilePath) Get the scriptType (file-extension or MIME-type) for a given fileprotected intgetStartLevel(Path scriptFilePath) Gets the individual start level for a given fileprotected PathGet the base path that is used by thisScriptFileWatcherCompletableFuture<@Nullable Void> Returns aCompletableFuture<Void>that completes when theScriptFileWatcherhas completed it's initial loading of files.voidonDependencyChange(String scriptIdentifier) Called by the dependency tracker when a registered dependency changesvoidonReadyMarkerAdded(ReadyMarker readyMarker) Gets called when a newReadyMarkerwas registered as being "ready".voidonReadyMarkerRemoved(ReadyMarker readyMarker) Gets called when aReadyMarkerwas unregistered.voidprocessWatchEvent(WatchService.Kind kind, Path fullPath) Notify Listener about watch event
-
Field Details
-
scheduler
-
-
Constructor Details
-
AbstractScriptFileWatcher
protected AbstractScriptFileWatcher(WatchService watchService, ScriptEngineManager manager, ReadyService readyService, StartLevelService startLevelService, String fileDirectory, boolean watchSubDirectories)
-
-
Method Details
-
getWatchPath
Get the base path that is used by thisScriptFileWatcher- Returns:
- the
Pathused
-
getScheduler
Can be overridden by subclasses (e.g. for testing)- Returns:
- a
ScheduledExecutorService
-
activate
public void activate() -
deactivate
public void deactivate() -
ifInitialized
Description copied from interface:ScriptFileWatcherReturns aCompletableFuture<Void>that completes when theScriptFileWatcherhas completed it's initial loading of files.- Specified by:
ifInitializedin interfaceScriptFileWatcher- Returns:
- the
CompletableFuture
-
getScriptType
Get the scriptType (file-extension or MIME-type) for a given file The scriptType is determined by the file extension. The extensions inEXCLUDED_FILE_EXTENSIONSare ignored. Implementations should override this method if they provide a MIME-type instead of the file extension. -
getStartLevel
Gets the individual start level for a given file The start level is derived from the name and location of the file bySTART_LEVEL_PATTERNS. If no match is found,StartLevelService.STARTLEVEL_RULEENGINEis used.- Parameters:
scriptFilePath- thePathto the script- Returns:
- the start level for this script
-
processWatchEvent
Description copied from interface:WatchService.WatchEventListenerNotify Listener about watch event- Specified by:
processWatchEventin interfaceWatchService.WatchEventListener- Parameters:
kind- theWatchService.Kindof this eventfullPath- the absolute path of the file associated with this event
-
onDependencyChange
Description copied from interface:ScriptDependencyTracker.ListenerCalled by the dependency tracker when a registered dependency changes- Specified by:
onDependencyChangein interfaceScriptDependencyTracker.Listener- Parameters:
scriptIdentifier- the identifier of the script whose dependency changed
-
onReadyMarkerAdded
Description copied from interface:ReadyService.ReadyTrackerGets called when a newReadyMarkerwas registered as being "ready".- Specified by:
onReadyMarkerAddedin interfaceReadyService.ReadyTracker- Parameters:
readyMarker-
-
onReadyMarkerRemoved
Description copied from interface:ReadyService.ReadyTrackerGets called when aReadyMarkerwas unregistered.- Specified by:
onReadyMarkerRemovedin interfaceReadyService.ReadyTracker- Parameters:
readyMarker-
-
factoryAdded
Description copied from interface:ScriptEngineManager.FactoryChangeListenerCalled by theScriptEngineManagerwhen a ScriptEngineFactory is added- Specified by:
factoryAddedin interfaceScriptEngineManager.FactoryChangeListener- Parameters:
scriptType- the script type supported by the newly added factory
-
factoryRemoved
Description copied from interface:ScriptEngineManager.FactoryChangeListenerCalled by theScriptEngineManagerwhen a ScriptEngineFactory is removed- Specified by:
factoryRemovedin interfaceScriptEngineManager.FactoryChangeListener- Parameters:
scriptType- the script type supported by the removed factory
-