From 258ba741701561a5f6988183fd8d92a86b3434bd Mon Sep 17 00:00:00 2001 From: Vitalii Parfonov Date: Thu, 18 May 2017 12:09:54 +0300 Subject: [PATCH] One more fix for avoid NPE (#5121) Signed-off-by: Vitalii Parfonov --- .../api/vfs/watcher/FileWatcherService.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/watcher/FileWatcherService.java b/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/watcher/FileWatcherService.java index 93a6f3ab440..33ca6848c7b 100644 --- a/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/watcher/FileWatcherService.java +++ b/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/watcher/FileWatcherService.java @@ -169,7 +169,7 @@ void stop() { } } - boolean isStopped(){ + boolean isStopped() { return executor.isShutdown(); } @@ -186,21 +186,23 @@ boolean isStopped(){ * directory */ public void register(Path dir) { + if (!Files.exists(dir)) { + LOG.debug("Trying to register directory '{}' but it does not exist", dir); + return; + } LOG.debug("Registering directory '{}'", dir); if (keys.values().contains(dir)) { int previous = registrations.get(dir); LOG.debug("Directory is already being watched, increasing watch counter, previous value: {}", previous); registrations.put(dir, previous + 1); } else { - if (Files.exists(dir)) { - try { - LOG.debug("Starting watching directory '{}'", dir); - WatchKey watchKey = dir.register(service, eventKinds, eventModifiers); - keys.put(watchKey, dir); - registrations.put(dir, 1); - } catch (IOException e) { - LOG.error("Can't register dir {} in file watch service", dir, e); - } + try { + LOG.debug("Starting watching directory '{}'", dir); + WatchKey watchKey = dir.register(service, eventKinds, eventModifiers); + keys.put(watchKey, dir); + registrations.put(dir, 1); + } catch (IOException e) { + LOG.error("Can't register dir {} in file watch service", dir, e); } } } @@ -210,9 +212,9 @@ public void register(Path dir) { * method decreases by one registration counter that corresponds to * directory specified by the argument. If registration counter comes to * zero directory watching is totally cancelled. - * + *

* If this method is called for not existing directory nothing happens. - * + *

* If this method is called for not registered directory nothing happens. * * @param dir @@ -324,8 +326,9 @@ private void run() { private void resetAndRemove(WatchKey watchKey, Path dir) { if (!watchKey.reset()) { - registrations.remove(dir); - + if (dir != null) { + registrations.remove(dir); + } keys.remove(watchKey); } }