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
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractScriptFileWatcher
(WatchService watchService, ScriptEngineManager manager, ReadyService readyService, StartLevelService startLevelService, String fileDirectory, boolean watchSubDirectories) -
Method Summary
Modifier and TypeMethodDescriptionvoid
activate()
void
void
factoryAdded
(@Nullable String scriptType) Called by theScriptEngineManager
when a ScriptEngineFactory is addedvoid
factoryRemoved
(@Nullable String scriptType) Called by theScriptEngineManager
when a ScriptEngineFactory is removedprotected ScheduledExecutorService
Can be overridden by subclasses (e.g.getScriptType
(Path scriptFilePath) Get the scriptType (file-extension or MIME-type) for a given fileprotected int
getStartLevel
(Path scriptFilePath) Gets the individual start level for a given fileprotected Path
Get the base path that is used by thisScriptFileWatcher
CompletableFuture<@Nullable Void>
Returns aCompletableFuture<Void>
that completes when theScriptFileWatcher
has completed it's initial loading of files.void
onDependencyChange
(String scriptIdentifier) Called by the dependency tracker when a registered dependency changesvoid
onReadyMarkerAdded
(ReadyMarker readyMarker) Gets called when a newReadyMarker
was registered as being "ready".void
onReadyMarkerRemoved
(ReadyMarker readyMarker) Gets called when aReadyMarker
was unregistered.void
processWatchEvent
(WatchService.Kind kind, Path path) 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
Path
used
-
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:ScriptFileWatcher
Returns aCompletableFuture<Void>
that completes when theScriptFileWatcher
has completed it's initial loading of files.- Specified by:
ifInitialized
in 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_EXTENSIONS
are 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_RULEENGINE
is used.- Parameters:
scriptFilePath
- thePath
to the script- Returns:
- the start level for this script
-
processWatchEvent
Description copied from interface:WatchService.WatchEventListener
Notify Listener about watch event- Specified by:
processWatchEvent
in interfaceWatchService.WatchEventListener
- Parameters:
kind
- theWatchService.Kind
of this eventpath
- the relative path of the file associated with this event
-
onDependencyChange
Description copied from interface:ScriptDependencyTracker.Listener
Called by the dependency tracker when a registered dependency changes- Specified by:
onDependencyChange
in interfaceScriptDependencyTracker.Listener
- Parameters:
scriptIdentifier
- the identifier of the script whose dependency changed
-
onReadyMarkerAdded
Description copied from interface:ReadyService.ReadyTracker
Gets called when a newReadyMarker
was registered as being "ready".- Specified by:
onReadyMarkerAdded
in interfaceReadyService.ReadyTracker
-
onReadyMarkerRemoved
Description copied from interface:ReadyService.ReadyTracker
Gets called when aReadyMarker
was unregistered.- Specified by:
onReadyMarkerRemoved
in interfaceReadyService.ReadyTracker
-
factoryAdded
Description copied from interface:ScriptEngineManager.FactoryChangeListener
Called by theScriptEngineManager
when a ScriptEngineFactory is added- Specified by:
factoryAdded
in interfaceScriptEngineManager.FactoryChangeListener
- Parameters:
scriptType
- the script type supported by the newly added factory
-
factoryRemoved
Description copied from interface:ScriptEngineManager.FactoryChangeListener
Called by theScriptEngineManager
when a ScriptEngineFactory is removed- Specified by:
factoryRemoved
in interfaceScriptEngineManager.FactoryChangeListener
- Parameters:
scriptType
- the script type supported by the removed factory
-