1 package com.neuronrobotics.bowlerstudio.util;
35 import java.io.IOException;
36 import java.nio.file.*;
38 import static java.nio.file.StandardWatchEventKinds.*;
39 import java.nio.file.attribute.*;
42 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
54 private boolean run =
true;
60 private final Map<WatchKey, Path>
keys;
66 private ArrayList<IFileChangeListener>
listeners =
new ArrayList<IFileChangeListener>();
69 private static HashMap<String, FileChangeWatcher>
activeListener =
new HashMap<String, FileChangeWatcher>();
80 for (
int i = 0; i < array.length; i++) {
81 Object key = array[i];
90 ArrayList<IFileChangeListener> listeners2 =
new ArrayList<>();
92 for (
int i = 0; i < listeners2.size(); i++) {
117 System.err.println(
"Adding file to listening " +
fileToWatch.getAbsolutePath());
134 this.watcher = FileSystems.getDefault().newWatchService();
135 this.keys =
new HashMap<WatchKey, Path>();
138 System.out.format(
"Scanning %s ...\n", dir);
140 System.out.println(
"Done.");
146 setName(
"File Watcher Thread "+
fileToWatch.getName());
154 }
catch (Exception ex) {
155 ex.printStackTrace();
160 }
catch (InterruptedException e) {
208 @SuppressWarnings(
"unchecked")
209 static <T> WatchEvent<T> cast(WatchEvent<?> event) {
210 return (WatchEvent<T>) event;
221 private void register(Path dir)
throws IOException {
222 WatchKey key = dir.register(
watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
224 Path prev =
keys.get(key);
228 if (!dir.equals(prev)) {
247 Files.walkFileTree(start,
new SimpleFileVisitor<Path>() {
249 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
251 return FileVisitResult.CONTINUE;
265 }
catch (Exception x) {
271 Path dir =
keys.get(key);
273 System.err.println(
"WatchKey not recognized!!");
277 for (WatchEvent<?> event : key.pollEvents()) {
278 WatchEvent.Kind kind =
event.kind();
281 if (kind == OVERFLOW) {
286 WatchEvent<Path> ev = cast(event);
287 Path name = ev.context();
288 Path child = dir.resolve(name);
290 if (!child.toFile().getCanonicalPath().equals(
fileToWatch.getCanonicalPath())) {
295 System.err.println(
"File Changed: " +
getFileToWatch().getAbsolutePath());
296 for (
int i = 0; i <
listeners.size(); i++) {
298 listeners.get(i).onFileChange(child.toFile(), event);
302 }
catch (Exception e) {
310 boolean valid = key.reset();
315 if (
keys.isEmpty()) {
357 System.err.println(
"Closing watcher for "+
fileToWatch.getAbsolutePath());
359 }
catch (IOException e) {
static void close(File fileToWatch)
final WatchService watcher
void removeIFileChangeListener(IFileChangeListener l)
void addIFileChangeListener(IFileChangeListener l)
static void notifyOfDelete(File fileToWatch)
void registerAll(final Path start)
final Map< WatchKey, Path > keys
static FileChangeWatcher watch(File fileToWatch)
static HashMap< String, FileChangeWatcher > activeListener
void setFileToWatch(File fileToWatch)
FileChangeWatcher(File fileToWatch)
ArrayList< IFileChangeListener > listeners
void onFileDelete(File fileThatIsDeleted)