mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-02-05 00:23:52 +01:00
AbstractFileTransformationService: stop watchService properly (#965)
* AbstractFileTransformationService: stop watchService properly Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
parent
8509a94cfd
commit
a11385101b
@ -57,6 +57,7 @@ public abstract class AbstractFileTransformationService<T> implements Transforma
|
|||||||
private @Nullable WatchService watchService = null;
|
private @Nullable WatchService watchService = null;
|
||||||
|
|
||||||
protected final Map<String, T> cachedFiles = new ConcurrentHashMap<>();
|
protected final Map<String, T> cachedFiles = new ConcurrentHashMap<>();
|
||||||
|
private final Map<WatchKey, Path> registeredKeys = new ConcurrentHashMap<>();
|
||||||
protected final List<String> watchedDirectories = new ArrayList<String>();
|
protected final List<String> watchedDirectories = new ArrayList<String>();
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(AbstractFileTransformationService.class);
|
private final Logger logger = LoggerFactory.getLogger(AbstractFileTransformationService.class);
|
||||||
@ -98,6 +99,7 @@ public abstract class AbstractFileTransformationService<T> implements Transforma
|
|||||||
|
|
||||||
protected void deactivate() {
|
protected void deactivate() {
|
||||||
localeProviderTracker.close();
|
localeProviderTracker.close();
|
||||||
|
stopWatchService();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Locale getLocale() {
|
protected Locale getLocale() {
|
||||||
@ -187,9 +189,10 @@ public abstract class AbstractFileTransformationService<T> implements Transforma
|
|||||||
String watchedDirectory = getSourcePath() + subDirectory;
|
String watchedDirectory = getSourcePath() + subDirectory;
|
||||||
Path transformFilePath = Paths.get(watchedDirectory);
|
Path transformFilePath = Paths.get(watchedDirectory);
|
||||||
try {
|
try {
|
||||||
transformFilePath.register(watchService, ENTRY_DELETE, ENTRY_MODIFY);
|
WatchKey registrationKey = transformFilePath.register(watchService, ENTRY_DELETE, ENTRY_MODIFY);
|
||||||
logger.debug("Watching directory {}", transformFilePath);
|
logger.debug("Watching directory {}", transformFilePath);
|
||||||
watchedDirectories.add(subDirectory);
|
watchedDirectories.add(subDirectory);
|
||||||
|
registeredKeys.put(registrationKey, transformFilePath);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warn("Unable to watch transformation directory : {}", watchedDirectory);
|
logger.warn("Unable to watch transformation directory : {}", watchedDirectory);
|
||||||
cachedFiles.clear();
|
cachedFiles.clear();
|
||||||
@ -225,6 +228,23 @@ public abstract class AbstractFileTransformationService<T> 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
|
* Returns the name of the localized transformation file
|
||||||
* if it actually exists, keeps the original in the other case
|
* if it actually exists, keeps the original in the other case
|
||||||
|
Loading…
Reference in New Issue
Block a user