diff --git a/bundles/org.openhab.core.transform/src/main/java/org/eclipse/smarthome/core/transform/AbstractFileTransformationService.java b/bundles/org.openhab.core.transform/src/main/java/org/eclipse/smarthome/core/transform/AbstractFileTransformationService.java index 64e7f165b..e0ca6eb0f 100644 --- a/bundles/org.openhab.core.transform/src/main/java/org/eclipse/smarthome/core/transform/AbstractFileTransformationService.java +++ b/bundles/org.openhab.core.transform/src/main/java/org/eclipse/smarthome/core/transform/AbstractFileTransformationService.java @@ -57,6 +57,7 @@ public abstract class AbstractFileTransformationService implements Transforma private @Nullable WatchService watchService = null; protected final Map cachedFiles = new ConcurrentHashMap<>(); + private final Map registeredKeys = new ConcurrentHashMap<>(); protected final List watchedDirectories = new ArrayList(); private final Logger logger = LoggerFactory.getLogger(AbstractFileTransformationService.class); @@ -98,6 +99,7 @@ public abstract class AbstractFileTransformationService implements Transforma protected void deactivate() { localeProviderTracker.close(); + stopWatchService(); } protected Locale getLocale() { @@ -187,9 +189,10 @@ public abstract class AbstractFileTransformationService implements Transforma String watchedDirectory = getSourcePath() + subDirectory; Path transformFilePath = Paths.get(watchedDirectory); try { - transformFilePath.register(watchService, ENTRY_DELETE, ENTRY_MODIFY); + WatchKey registrationKey = transformFilePath.register(watchService, ENTRY_DELETE, ENTRY_MODIFY); logger.debug("Watching directory {}", transformFilePath); watchedDirectories.add(subDirectory); + registeredKeys.put(registrationKey, transformFilePath); } catch (IOException e) { logger.warn("Unable to watch transformation directory : {}", watchedDirectory); cachedFiles.clear(); @@ -225,6 +228,23 @@ public abstract class AbstractFileTransformationService implements Transforma } } + private synchronized void stopWatchService() { + if (watchService != null) { + for (WatchKey watchKey : registeredKeys.keySet()) { + watchKey.cancel(); + } + registeredKeys.clear(); + watchedDirectories.clear(); + try { + watchService.close(); + } catch (IOException e) { + logger.warn("Cannot deactivate transformation directory watcher", e); + } + watchService = null; + cachedFiles.clear(); + } + } + /** * Returns the name of the localized transformation file * if it actually exists, keeps the original in the other case