BGPCEP-710: Create Network Topology Loader
[bgpcep.git] / config-loader / config-loader-impl / src / main / java / org / opendaylight / bgpcep / config / loader / impl / FileWatcherImpl.java
@@ -6,7 +6,7 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.protocol.bgp.config.loader.impl;
+package org.opendaylight.bgpcep.config.loader.impl;
 
 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
 import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
@@ -20,8 +20,8 @@ import java.nio.file.WatchService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public final class BGPFileWatcher implements FileWatcher, AutoCloseable {
-    private static final Logger LOG = LoggerFactory.getLogger(BGPFileWatcher.class);
+public final class FileWatcherImpl implements FileWatcher, AutoCloseable {
+    private static final Logger LOG = LoggerFactory.getLogger(FileWatcherImpl.class);
     private static final String INTERRUPTED = "InterruptedException";
     private static final String BGPCEP_CONFIG_FOLDER = "bgpcep";
     private static final String DEFAULT_APP_CONFIG_FILE_PATH = "etc" + File.separator + "opendaylight"
@@ -29,21 +29,24 @@ public final class BGPFileWatcher implements FileWatcher, AutoCloseable {
     private static final Path PATH = Paths.get(DEFAULT_APP_CONFIG_FILE_PATH);
     private final WatchService watchService;
 
-    public BGPFileWatcher() throws IOException {
+    public FileWatcherImpl() throws IOException {
+        this.watchService = FileSystems.getDefault().newWatchService();
         final File file = new File(DEFAULT_APP_CONFIG_FILE_PATH);
         if (!file.exists()) {
-            file.mkdirs();
+            if (file.mkdirs()) {
+                return;
+            }
         }
 
-        this.watchService = FileSystems.getDefault().newWatchService();
         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
             try {
-                BGPFileWatcher.this.watchService.close();
+                FileWatcherImpl.this.watchService.close();
             } catch (final IOException e) {
                 LOG.warn(INTERRUPTED, e);
             }
         }));
         PATH.register(this.watchService, OVERFLOW, ENTRY_CREATE);
+        LOG.info("File Watcher service initiated");
     }
 
     @Override
@@ -52,7 +55,7 @@ public final class BGPFileWatcher implements FileWatcher, AutoCloseable {
     }
 
     @Override
-    public WatchService getWatchService() {
+    public synchronized WatchService getWatchService() {
         return this.watchService;
     }