Skip to content

Instantly share code, notes, and snippets.

@almilo
Created March 18, 2014 15:24
Show Gist options
  • Save almilo/9622245 to your computer and use it in GitHub Desktop.
Save almilo/9622245 to your computer and use it in GitHub Desktop.
New Java 7 NIO.2 Watch API example.
package com.edorasware.java7.nio2;
import com.sun.nio.file.SensitivityWatchEventModifier;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
public class WatchApp {
public static final Map<WatchEvent.Kind<Path>, String> EVENT_KINDS_AND_MESSAGES = new HashMap<>();
public static final WatchEvent.Kind[] EVENT_KINDS;
static {
EVENT_KINDS_AND_MESSAGES.put(StandardWatchEventKinds.ENTRY_CREATE, "Created: '%s'.\n");
EVENT_KINDS_AND_MESSAGES.put(StandardWatchEventKinds.ENTRY_DELETE, "Deleted: '%s'.\n");
EVENT_KINDS_AND_MESSAGES.put(StandardWatchEventKinds.ENTRY_MODIFY, "Modified: '%s'.\n");
EVENT_KINDS = getKeysAsArray(EVENT_KINDS_AND_MESSAGES);
}
private static WatchEvent.Kind[] getKeysAsArray(Map<WatchEvent.Kind<Path>, String> map) {
// WTF??
return map.keySet().toArray(new WatchEvent.Kind[EVENT_KINDS_AND_MESSAGES.keySet().size()]);
}
public static void main(String[] args) throws IOException {
if (args.length < 1) {
throw new IllegalArgumentException("Error: directory to watch expected as CLI argument.");
}
Path watchedDirectory = Paths.get(args[0]);
new WatchApp().watch(watchedDirectory);
}
private void watch(Watchable watchable) throws IOException {
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
//watchable.register(watchService, EVENT_KINDS); // Standard behavior
watchable.register(watchService, EVENT_KINDS, SensitivityWatchEventModifier.HIGH); // Extended behavior with non-portable Event Modifier to reduce the delay
System.out.format("Watching: '%s' for changes.\n", watchable);
//while (processWatchKey(watchService.poll(500, TimeUnit.MILLISECONDS), EVENT_KINDS_AND_MESSAGES)) { // Polling call
while (processWatchKey(watchService.take(), EVENT_KINDS_AND_MESSAGES)) { // Blocking call
System.out.print(".");
}
} catch (InterruptedException e) {
System.out.println("Watcher service interrupted.");
}
}
private boolean processWatchKey(WatchKey watchKey, Map<WatchEvent.Kind<Path>, String> messages) {
if (watchKey == null) {
return true;
}
for (WatchEvent event : watchKey.pollEvents()) {
Object context = event.context();
String message = messages.get(event.kind());
if (message != null) {
System.out.format(message, context.toString());
String jsFileStringPath = getJSFileStringPath(context);
if (jsFileStringPath != null) {
System.out.format("JS file: '%s'.\n", jsFileStringPath);
}
} else {
System.out.format("No message for event of kind: '%s'.\n", event.kind());
}
}
return watchKey.reset();
}
private static String getJSFileStringPath(Object context) {
if (Path.class.isInstance(context)) {
Path path = Path.class.cast(context);
String fileName = path.getFileName().toString();
//if (path.toFile().isFile() && fileName.endsWith(".js")) { // This should return true in the case of a regular file with .js extension??
if (fileName.endsWith(".js")) {
return fileName;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment