* fix NPE

This commit is contained in:
Reinhard Pointner 2014-03-29 02:24:37 +00:00
parent 6b2659f2a4
commit 92a433a77f
1 changed files with 46 additions and 69 deletions

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot.cli;
import static java.nio.file.StandardWatchEventKinds.*;
import static java.util.Collections.*;
import static net.sourceforge.tuned.FileUtilities.*;
@ -29,18 +27,17 @@ import java.util.logging.Logger;
import net.sourceforge.tuned.DefaultThreadFactory;
import net.sourceforge.tuned.Timer;
public abstract class FolderWatchService implements Closeable {
private final Collection<File> commitSet = new HashSet<File>();
private final ExecutorService processor = Executors.newSingleThreadExecutor();
private final ExecutorService watchers = Executors.newCachedThreadPool(new DefaultThreadFactory("FolderWatchService"));
private long commitDelay = 500; // 0.5 s
private boolean commitPerFolder = true;
private final Timer commitTimer = new Timer() {
@Override
public void run() {
synchronized (processor) {
@ -48,37 +45,32 @@ public abstract class FolderWatchService implements Closeable {
}
}
};
private final boolean watchTree;
public FolderWatchService(boolean watchTree) {
this.watchTree = watchTree;
}
public synchronized void setCommitPerFolder(boolean enabled) {
this.commitPerFolder = enabled;
}
public synchronized void setCommitDelay(long commitDelay) {
if (commitDelay < 0)
throw new IllegalArgumentException("Delay must not be negativ");
this.commitDelay = commitDelay;
resetCommitTimer();
}
public synchronized void resetCommitTimer() {
commitTimer.set(commitDelay, TimeUnit.MILLISECONDS, false);
}
public synchronized void commit() {
final SortedSet<File> files = new TreeSet<File>();
synchronized (commitSet) {
for (File it : commitSet) {
if (it.isFile()) {
@ -87,16 +79,16 @@ public abstract class FolderWatchService implements Closeable {
}
commitSet.clear();
}
if (files.isEmpty()) {
return;
}
// divide into commit batches per folder if required
final Map<File, ? extends Collection<File>> commitBatches = commitPerFolder ? mapByFolder(files) : singletonMap((File) null, files);
processor.submit(new Runnable() {
@Override
public void run() {
synchronized (processor) {
@ -107,37 +99,36 @@ public abstract class FolderWatchService implements Closeable {
}
});
}
public abstract void processCommitSet(File[] files, File dir);
public synchronized void watchFolder(File folder) throws IOException {
if (!watchTree) {
startWatch(folder);
return;
}
watchFolderTree(folder);
}
private void watchFolderTree(File root) throws IOException {
for (File it : root.listFiles(FOLDERS)) {
watchFolderTree(it);
File[] folders = root.listFiles(FOLDERS);
if (folders != null) {
for (File it : folders) {
watchFolderTree(it);
}
}
startWatch(root);
}
private synchronized void startWatch(File node) throws IOException {
if (!node.isDirectory()) {
throw new IllegalArgumentException("Must be a folder: " + node);
}
watchers.submit(new FolderWatcher(node) {
@Override
protected void processEvents(List<WatchEvent<?>> events) {
synchronized (commitSet) {
@ -145,8 +136,7 @@ public abstract class FolderWatchService implements Closeable {
super.processEvents(events);
}
}
@Override
protected void created(File file) {
synchronized (commitSet) {
@ -154,7 +144,7 @@ public abstract class FolderWatchService implements Closeable {
commitSet.add(file);
return;
}
// start watching newly created folder
if (watchTree) {
try {
@ -166,8 +156,7 @@ public abstract class FolderWatchService implements Closeable {
}
}
}
@Override
protected void modified(File file) {
synchronized (commitSet) {
@ -176,8 +165,7 @@ public abstract class FolderWatchService implements Closeable {
}
}
}
@Override
protected void deleted(File file) {
synchronized (commitSet) {
@ -186,29 +174,25 @@ public abstract class FolderWatchService implements Closeable {
}
});
}
@Override
public synchronized void close() throws IOException {
commitTimer.cancel();
processor.shutdownNow();
watchers.shutdownNow();
}
private abstract static class FolderWatcher implements Runnable, Closeable {
private final Path node;
private final WatchService watchService;
public FolderWatcher(File node) throws IOException {
this.node = node.toPath();
this.watchService = this.node.getFileSystem().newWatchService();
this.node.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
}
@Override
public void run() {
try {
@ -219,8 +203,7 @@ public abstract class FolderWatchService implements Closeable {
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage(), e);
}
}
public void watch() throws IOException, InterruptedException {
try {
boolean valid = true;
@ -233,13 +216,11 @@ public abstract class FolderWatchService implements Closeable {
this.close();
}
}
public File getAbsoluteFile(WatchEvent event) {
return node.resolve(event.context().toString()).toFile();
}
protected void processEvents(List<WatchEvent<?>> list) {
for (WatchEvent event : list) {
if (event.kind() == ENTRY_CREATE) {
@ -251,21 +232,17 @@ public abstract class FolderWatchService implements Closeable {
}
}
}
protected abstract void created(File file);
protected abstract void modified(File file);
protected abstract void deleted(File file);
@Override
public void close() throws IOException {
watchService.close();
}
}
}