OSDN Git Service

ディレクトリ内のファイル追加/削除監視サービス
authoryukihane <yukihane.feather@gmail.com>
Thu, 1 Sep 2011 01:46:48 +0000 (10:46 +0900)
committeryukihane <yukihane.feather@gmail.com>
Thu, 1 Sep 2011 01:46:48 +0000 (10:46 +0900)
frontend/src/yukihane/inqubus/filewatch/FileWatch.java [new file with mode: 0644]

diff --git a/frontend/src/yukihane/inqubus/filewatch/FileWatch.java b/frontend/src/yukihane/inqubus/filewatch/FileWatch.java
new file mode 100644 (file)
index 0000000..7c100fa
--- /dev/null
@@ -0,0 +1,108 @@
+package yukihane.inqubus.filewatch;
+
+import java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardWatchEventKinds;
+import java.nio.file.WatchEvent;
+import java.nio.file.WatchEvent.Kind;
+import java.nio.file.WatchKey;
+import java.nio.file.WatchService;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ *
+ * @author yuki
+ */
+public final class FileWatch implements Runnable {
+
+    private final Set<Path> files;
+    private final Set<Path> directories;
+
+    public FileWatch(Collection<Path> dirs) throws IOException {
+        FileDir res = new FileDir();
+        getFileDir(dirs, res);
+
+        this.files = new HashSet<>(res.files);
+        this.directories = new HashSet<>(res.dirs);
+    }
+
+    public Set<Path> getFiles() {
+        synchronized (files) {
+            return new HashSet<>(files);
+        }
+    }
+
+    private void getFileDir(Collection<Path> paths, FileDir res) throws IOException {
+        for (Path p : paths) {
+            getFileDir(p, res);
+        }
+    }
+
+    private void getFileDir(Path p, FileDir res) throws IOException {
+
+        if (Files.isDirectory(p)) {
+            res.dirs.add(p);
+            DirectoryStream<Path> dir = Files.newDirectoryStream(p);
+            for (Path pp : dir) {
+                getFileDir(pp, res);
+            }
+        } else if (Files.isRegularFile(p)) {
+            res.files.add(p);
+        }
+    }
+
+    private static class FileDir {
+
+        private final Set<Path> files = new HashSet<>();
+        private final Set<Path> dirs = new HashSet<>();
+    }
+
+    @Override
+    public void run() {
+        try {
+            final WatchService ws = FileSystems.getDefault().newWatchService();
+            for (Path p : directories) {
+                p.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
+            }
+
+            while (true) {
+                final WatchKey wk = ws.take();
+                for (final WatchEvent<?> event : wk.pollEvents()) {
+                    final Kind<?> kind = event.kind();
+                    if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) {
+                        final Path p = (Path) event.context();
+                        if (Files.isRegularFile(p)) {
+                            synchronized (files) {
+                                files.add(p);
+                            }
+                        }
+                    } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) {
+                        final Path p = (Path) event.context();
+                        if (Files.isRegularFile(p)) {
+                            synchronized (files) {
+                                files.remove(p);
+                            }
+                        }
+                    }
+                }
+
+                if (!wk.reset()) {
+                    System.out.println("No longer valid");
+                    wk.cancel();
+                    ws.close();
+                    break;
+                }
+
+            }
+        } catch (InterruptedException ex) {
+            ex.printStackTrace();
+        } catch (IOException ex) {
+            ex.printStackTrace();
+        }
+    }
+}