OSDN Git Service

ディレクトリ処理中にエラーが発生しても例外をスローせず継続するようにする
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / filewatch / FileWatch.java
1 package yukihane.inqubus.filewatch;
2
3 import java.io.IOException;
4 import java.nio.file.DirectoryStream;
5 import java.nio.file.FileSystems;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.StandardWatchEventKinds;
9 import java.nio.file.WatchEvent;
10 import java.nio.file.WatchEvent.Kind;
11 import java.nio.file.WatchKey;
12 import java.nio.file.WatchService;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Set;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18
19 /**
20  *
21  * @author yuki
22  */
23 public final class FileWatch implements Runnable {
24
25     private static final Logger logger = Logger.getLogger(FileWatch.class.getName());
26     private final Set<Path> files;
27     private final Set<Path> directories;
28
29     public FileWatch(Collection<Path> dirs) {
30         FileDir res = new FileDir();
31         getFileDir(dirs, res);
32
33         this.files = new HashSet<>(res.files);
34         this.directories = new HashSet<>(res.dirs);
35     }
36
37     public Set<Path> getFiles() {
38         synchronized (files) {
39             return new HashSet<>(files);
40         }
41     }
42
43     private void getFileDir(Iterable<Path> paths, FileDir result) {
44         for (Path p : paths) {
45             getFileDir(p, result);
46         }
47     }
48
49     private void getFileDir(Path path, FileDir result) {
50         try {
51             if (Files.isDirectory(path)) {
52                 result.dirs.add(path);
53                 DirectoryStream<Path> dir = Files.newDirectoryStream(path);
54                 getFileDir(dir, result);
55             } else if (Files.isRegularFile(path)) {
56                 result.files.add(path);
57             }
58         } catch (IOException ex) {
59             logger.log(Level.WARNING, "パスの処理中にエラー: {0}", new Object[]{path});
60         }
61     }
62
63     private static class FileDir {
64
65         private final Set<Path> files = new HashSet<>();
66         private final Set<Path> dirs = new HashSet<>();
67     }
68
69     @Override
70     public void run() {
71         try {
72             final WatchService ws = FileSystems.getDefault().newWatchService();
73             for (Path p : directories) {
74                 p.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
75             }
76
77             while (true) {
78                 final WatchKey wk = ws.take();
79                 for (final WatchEvent<?> event : wk.pollEvents()) {
80                     final Kind<?> kind = event.kind();
81                     if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) {
82                         final Path p = (Path) event.context();
83                         if (Files.isRegularFile(p)) {
84                             synchronized (files) {
85                                 files.add(p);
86                             }
87                         }
88                     } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) {
89                         final Path p = (Path) event.context();
90                         if (Files.isRegularFile(p)) {
91                             synchronized (files) {
92                                 files.remove(p);
93                             }
94                         }
95                     }
96                 }
97
98                 if (!wk.reset()) {
99                     System.out.println("No longer valid");
100                     wk.cancel();
101                     ws.close();
102                     break;
103                 }
104
105             }
106         } catch (InterruptedException ex) {
107             ex.printStackTrace();
108         } catch (IOException ex) {
109             ex.printStackTrace();
110         }
111     }
112 }