Class AbstractScriptFileWatcher
java.lang.Object
org.openhab.core.service.AbstractWatchService
org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher
- All Implemented Interfaces:
ScriptFileWatcher
,ScriptDependencyTracker.Listener
,ScriptEngineManager.FactoryChangeListener
,ReadyService.ReadyTracker
@NonNullByDefault
public abstract class AbstractScriptFileWatcher
extends AbstractWatchService
implements 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 & per-script start levels; made reusable, Jan N. Klug - Refactored dependency tracking to script engine factories
-
Field Summary
FieldsFields inherited from class org.openhab.core.service.AbstractWatchService
pathToWatch, watchQueueReader
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractScriptFileWatcher
(ScriptEngineManager manager, ReadyService readyService, StartLevelService startLevelService, String fileDirectory) -
Method Summary
Modifier and TypeMethodDescriptionvoid
activate()
Method to call on service activationvoid
Method to call on service deactivationvoid
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 WatchEvent.Kind<?> @Nullable []
getWatchEventKinds
(@Nullable Path subDir) Provides theWatchKey
s for the registration of the directory, which will be registered in the watch service.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.protected void
processWatchEvent
(@Nullable WatchEvent<?> event, @Nullable WatchEvent.Kind<?> kind, @Nullable Path path) Processes the given watch event.protected boolean
Determines whether the subdirectories of the source path (determined by theAbstractWatchService.getSourcePath()
) will be watched or not.Methods inherited from class org.openhab.core.service.AbstractWatchService
changeWatchDirectory, getSourcePath
-
Field Details
-
scheduler
-
-
Constructor Details
-
AbstractScriptFileWatcher
public AbstractScriptFileWatcher(ScriptEngineManager manager, ReadyService readyService, StartLevelService startLevelService, String fileDirectory)
-
-
Method Details
-
activate
public void activate()Description copied from class:AbstractWatchService
Method to call on service activation- Overrides:
activate
in classAbstractWatchService
-
getScheduler
Can be overridden by subclasses (e.g. for testing)- Returns:
- a
ScheduledExecutorService
-
deactivate
public void deactivate()Description copied from class:AbstractWatchService
Method to call on service deactivation- Overrides:
deactivate
in classAbstractWatchService
-
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
-
watchSubDirectories
protected boolean watchSubDirectories()Description copied from class:AbstractWatchService
Determines whether the subdirectories of the source path (determined by theAbstractWatchService.getSourcePath()
) will be watched or not.- Specified by:
watchSubDirectories
in classAbstractWatchService
- Returns:
true
if the subdirectories will be watched andfalse
if only the source path (determined by theAbstractWatchService.getSourcePath()
) will be watched
-
getWatchEventKinds
Description copied from class:AbstractWatchService
Provides theWatchKey
s for the registration of the directory, which will be registered in the watch service.- Specified by:
getWatchEventKinds
in classAbstractWatchService
- Parameters:
subDir
- the directory, which will be registered in the watch service- Returns:
- The array of
WatchKey
s for the registration ornull
if no registration has been done.
-
processWatchEvent
protected void processWatchEvent(@Nullable WatchEvent<?> event, @Nullable WatchEvent.Kind<?> kind, @Nullable Path path) Description copied from class:AbstractWatchService
Processes the given watch event. Note that the kind and the number of the events for the watched directory is a platform dependent (see the "Platform dependencies" sections ofWatchService
).- Specified by:
processWatchEvent
in classAbstractWatchService
- Parameters:
event
- the watch event to be handledkind
- the event's kindpath
- the path of the event (resolved to theAbstractWatchService.pathToWatch
)
-
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
-