* fix NPE
This commit is contained in:
parent
6b2659f2a4
commit
92a433a77f
|
@ -1,7 +1,5 @@
|
||||||
|
|
||||||
package net.sourceforge.filebot.cli;
|
package net.sourceforge.filebot.cli;
|
||||||
|
|
||||||
|
|
||||||
import static java.nio.file.StandardWatchEventKinds.*;
|
import static java.nio.file.StandardWatchEventKinds.*;
|
||||||
import static java.util.Collections.*;
|
import static java.util.Collections.*;
|
||||||
import static net.sourceforge.tuned.FileUtilities.*;
|
import static net.sourceforge.tuned.FileUtilities.*;
|
||||||
|
@ -29,7 +27,6 @@ import java.util.logging.Logger;
|
||||||
import net.sourceforge.tuned.DefaultThreadFactory;
|
import net.sourceforge.tuned.DefaultThreadFactory;
|
||||||
import net.sourceforge.tuned.Timer;
|
import net.sourceforge.tuned.Timer;
|
||||||
|
|
||||||
|
|
||||||
public abstract class FolderWatchService implements Closeable {
|
public abstract class FolderWatchService implements Closeable {
|
||||||
|
|
||||||
private final Collection<File> commitSet = new HashSet<File>();
|
private final Collection<File> commitSet = new HashSet<File>();
|
||||||
|
@ -51,17 +48,14 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
|
|
||||||
private final boolean watchTree;
|
private final boolean watchTree;
|
||||||
|
|
||||||
|
|
||||||
public FolderWatchService(boolean watchTree) {
|
public FolderWatchService(boolean watchTree) {
|
||||||
this.watchTree = watchTree;
|
this.watchTree = watchTree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void setCommitPerFolder(boolean enabled) {
|
public synchronized void setCommitPerFolder(boolean enabled) {
|
||||||
this.commitPerFolder = enabled;
|
this.commitPerFolder = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void setCommitDelay(long commitDelay) {
|
public synchronized void setCommitDelay(long commitDelay) {
|
||||||
if (commitDelay < 0)
|
if (commitDelay < 0)
|
||||||
throw new IllegalArgumentException("Delay must not be negativ");
|
throw new IllegalArgumentException("Delay must not be negativ");
|
||||||
|
@ -70,12 +64,10 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
resetCommitTimer();
|
resetCommitTimer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void resetCommitTimer() {
|
public synchronized void resetCommitTimer() {
|
||||||
commitTimer.set(commitDelay, TimeUnit.MILLISECONDS, false);
|
commitTimer.set(commitDelay, TimeUnit.MILLISECONDS, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void commit() {
|
public synchronized void commit() {
|
||||||
final SortedSet<File> files = new TreeSet<File>();
|
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 abstract void processCommitSet(File[] files, File dir);
|
||||||
|
|
||||||
|
|
||||||
public synchronized void watchFolder(File folder) throws IOException {
|
public synchronized void watchFolder(File folder) throws IOException {
|
||||||
if (!watchTree) {
|
if (!watchTree) {
|
||||||
startWatch(folder);
|
startWatch(folder);
|
||||||
|
@ -121,16 +111,17 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
watchFolderTree(folder);
|
watchFolderTree(folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void watchFolderTree(File root) throws IOException {
|
private void watchFolderTree(File root) throws IOException {
|
||||||
for (File it : root.listFiles(FOLDERS)) {
|
File[] folders = root.listFiles(FOLDERS);
|
||||||
watchFolderTree(it);
|
if (folders != null) {
|
||||||
|
for (File it : folders) {
|
||||||
|
watchFolderTree(it);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startWatch(root);
|
startWatch(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private synchronized void startWatch(File node) throws IOException {
|
private synchronized void startWatch(File node) throws IOException {
|
||||||
if (!node.isDirectory()) {
|
if (!node.isDirectory()) {
|
||||||
throw new IllegalArgumentException("Must be a folder: " + node);
|
throw new IllegalArgumentException("Must be a folder: " + node);
|
||||||
|
@ -146,7 +137,6 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void created(File file) {
|
protected void created(File file) {
|
||||||
synchronized (commitSet) {
|
synchronized (commitSet) {
|
||||||
|
@ -167,7 +157,6 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void modified(File file) {
|
protected void modified(File file) {
|
||||||
synchronized (commitSet) {
|
synchronized (commitSet) {
|
||||||
|
@ -177,7 +166,6 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void deleted(File file) {
|
protected void deleted(File file) {
|
||||||
synchronized (commitSet) {
|
synchronized (commitSet) {
|
||||||
|
@ -187,7 +175,6 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void close() throws IOException {
|
public synchronized void close() throws IOException {
|
||||||
commitTimer.cancel();
|
commitTimer.cancel();
|
||||||
|
@ -195,20 +182,17 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
watchers.shutdownNow();
|
watchers.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private abstract static class FolderWatcher implements Runnable, Closeable {
|
private abstract static class FolderWatcher implements Runnable, Closeable {
|
||||||
|
|
||||||
private final Path node;
|
private final Path node;
|
||||||
private final WatchService watchService;
|
private final WatchService watchService;
|
||||||
|
|
||||||
|
|
||||||
public FolderWatcher(File node) throws IOException {
|
public FolderWatcher(File node) throws IOException {
|
||||||
this.node = node.toPath();
|
this.node = node.toPath();
|
||||||
this.watchService = this.node.getFileSystem().newWatchService();
|
this.watchService = this.node.getFileSystem().newWatchService();
|
||||||
this.node.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
|
this.node.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
@ -220,7 +204,6 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void watch() throws IOException, InterruptedException {
|
public void watch() throws IOException, InterruptedException {
|
||||||
try {
|
try {
|
||||||
boolean valid = true;
|
boolean valid = true;
|
||||||
|
@ -234,12 +217,10 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public File getAbsoluteFile(WatchEvent event) {
|
public File getAbsoluteFile(WatchEvent event) {
|
||||||
return node.resolve(event.context().toString()).toFile();
|
return node.resolve(event.context().toString()).toFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void processEvents(List<WatchEvent<?>> list) {
|
protected void processEvents(List<WatchEvent<?>> list) {
|
||||||
for (WatchEvent event : list) {
|
for (WatchEvent event : list) {
|
||||||
if (event.kind() == ENTRY_CREATE) {
|
if (event.kind() == ENTRY_CREATE) {
|
||||||
|
@ -252,16 +233,12 @@ public abstract class FolderWatchService implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected abstract void created(File file);
|
protected abstract void created(File file);
|
||||||
|
|
||||||
|
|
||||||
protected abstract void modified(File file);
|
protected abstract void modified(File file);
|
||||||
|
|
||||||
|
|
||||||
protected abstract void deleted(File file);
|
protected abstract void deleted(File file);
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
watchService.close();
|
watchService.close();
|
||||||
|
|
Loading…
Reference in New Issue