* 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,7 +27,6 @@ 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>();
@ -51,17 +48,14 @@ 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");
@ -70,12 +64,10 @@ public abstract class FolderWatchService implements Closeable {
resetCommitTimer();
}
public synchronized void resetCommitTimer() {
commitTimer.set(commitDelay, TimeUnit.MILLISECONDS, false);
}
public synchronized void commit() {
final SortedSet<File> files = new TreeSet<File>();
@ -108,10 +100,8 @@ 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);
@ -121,16 +111,17 @@ public abstract class FolderWatchService implements Closeable {
watchFolderTree(folder);
}
private void watchFolderTree(File root) throws IOException {
for (File it : root.listFiles(FOLDERS)) {
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);
@ -146,7 +137,6 @@ public abstract class FolderWatchService implements Closeable {
}
}
@Override
protected void created(File file) {
synchronized (commitSet) {
@ -167,7 +157,6 @@ public abstract class FolderWatchService implements Closeable {
}
}
@Override
protected void modified(File file) {
synchronized (commitSet) {
@ -177,7 +166,6 @@ public abstract class FolderWatchService implements Closeable {
}
}
@Override
protected void deleted(File file) {
synchronized (commitSet) {
@ -187,7 +175,6 @@ public abstract class FolderWatchService implements Closeable {
});
}
@Override
public synchronized void close() throws IOException {
commitTimer.cancel();
@ -195,20 +182,17 @@ public abstract class FolderWatchService implements Closeable {
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 {
@ -220,7 +204,6 @@ public abstract class FolderWatchService implements Closeable {
}
}
public void watch() throws IOException, InterruptedException {
try {
boolean valid = true;
@ -234,12 +217,10 @@ public abstract class FolderWatchService implements Closeable {
}
}
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) {
@ -252,16 +233,12 @@ 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();