Merge branch '1.3.x'
This commit is contained in:
commit
a3f4b3c704
|
|
@ -21,6 +21,7 @@ import java.io.FileFilter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -44,7 +45,7 @@ public class FileSystemWatcher {
|
||||||
|
|
||||||
private static final long DEFAULT_QUIET_PERIOD = 400;
|
private static final long DEFAULT_QUIET_PERIOD = 400;
|
||||||
|
|
||||||
private List<FileChangeListener> listeners = new ArrayList<FileChangeListener>();
|
private final List<FileChangeListener> listeners = new ArrayList<FileChangeListener>();
|
||||||
|
|
||||||
private final boolean daemon;
|
private final boolean daemon;
|
||||||
|
|
||||||
|
|
@ -52,14 +53,16 @@ public class FileSystemWatcher {
|
||||||
|
|
||||||
private final long quietPeriod;
|
private final long quietPeriod;
|
||||||
|
|
||||||
|
private final AtomicInteger remainingScans = new AtomicInteger(-1);
|
||||||
|
|
||||||
|
private final Map<File, FolderSnapshot> folders = new HashMap<File, FolderSnapshot>();
|
||||||
|
|
||||||
private Thread watchThread;
|
private Thread watchThread;
|
||||||
|
|
||||||
private AtomicInteger remainingScans = new AtomicInteger(-1);
|
|
||||||
|
|
||||||
private Map<File, FolderSnapshot> folders = new LinkedHashMap<File, FolderSnapshot>();
|
|
||||||
|
|
||||||
private FileFilter triggerFilter;
|
private FileFilter triggerFilter;
|
||||||
|
|
||||||
|
private final Object monitor = new Object();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link FileSystemWatcher} instance.
|
* Create a new {@link FileSystemWatcher} instance.
|
||||||
*/
|
*/
|
||||||
|
|
@ -89,11 +92,13 @@ public class FileSystemWatcher {
|
||||||
* {@link #start() started}.
|
* {@link #start() started}.
|
||||||
* @param fileChangeListener the listener to add
|
* @param fileChangeListener the listener to add
|
||||||
*/
|
*/
|
||||||
public synchronized void addListener(FileChangeListener fileChangeListener) {
|
public void addListener(FileChangeListener fileChangeListener) {
|
||||||
Assert.notNull(fileChangeListener, "FileChangeListener must not be null");
|
Assert.notNull(fileChangeListener, "FileChangeListener must not be null");
|
||||||
|
synchronized (this.monitor) {
|
||||||
checkNotStarted();
|
checkNotStarted();
|
||||||
this.listeners.add(fileChangeListener);
|
this.listeners.add(fileChangeListener);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add source folders to monitor. Cannot be called after the watcher has been
|
* Add source folders to monitor. Cannot be called after the watcher has been
|
||||||
|
|
@ -102,66 +107,63 @@ public class FileSystemWatcher {
|
||||||
*/
|
*/
|
||||||
public void addSourceFolders(Iterable<File> folders) {
|
public void addSourceFolders(Iterable<File> folders) {
|
||||||
Assert.notNull(folders, "Folders must not be null");
|
Assert.notNull(folders, "Folders must not be null");
|
||||||
|
synchronized (this.monitor) {
|
||||||
for (File folder : folders) {
|
for (File folder : folders) {
|
||||||
addSourceFolder(folder);
|
addSourceFolder(folder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a source folder to monitor. Cannot be called after the watcher has been
|
* Add a source folder to monitor. Cannot be called after the watcher has been
|
||||||
* {@link #start() started}.
|
* {@link #start() started}.
|
||||||
* @param folder the folder to monitor
|
* @param folder the folder to monitor
|
||||||
*/
|
*/
|
||||||
public synchronized void addSourceFolder(File folder) {
|
public void addSourceFolder(File folder) {
|
||||||
Assert.notNull(folder, "Folder must not be null");
|
Assert.notNull(folder, "Folder must not be null");
|
||||||
Assert.isTrue(folder.isDirectory(),
|
Assert.isTrue(folder.isDirectory(),
|
||||||
"Folder '" + folder + "' must exist and must" + " be a directory");
|
"Folder '" + folder + "' must exist and must" + " be a directory");
|
||||||
|
synchronized (this.monitor) {
|
||||||
checkNotStarted();
|
checkNotStarted();
|
||||||
this.folders.put(folder, null);
|
this.folders.put(folder, null);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set an optional {@link FileFilter} used to limit the files that trigger a change.
|
* Set an optional {@link FileFilter} used to limit the files that trigger a change.
|
||||||
* @param triggerFilter a trigger filter or null
|
* @param triggerFilter a trigger filter or null
|
||||||
*/
|
*/
|
||||||
public synchronized void setTriggerFilter(FileFilter triggerFilter) {
|
public void setTriggerFilter(FileFilter triggerFilter) {
|
||||||
|
synchronized (this.monitor) {
|
||||||
this.triggerFilter = triggerFilter;
|
this.triggerFilter = triggerFilter;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void checkNotStarted() {
|
private void checkNotStarted() {
|
||||||
|
synchronized (this.monitor) {
|
||||||
Assert.state(this.watchThread == null, "FileSystemWatcher already started");
|
Assert.state(this.watchThread == null, "FileSystemWatcher already started");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start monitoring the source folder for changes.
|
* Start monitoring the source folder for changes.
|
||||||
*/
|
*/
|
||||||
public synchronized void start() {
|
public void start() {
|
||||||
|
synchronized (this.monitor) {
|
||||||
saveInitialSnapshots();
|
saveInitialSnapshots();
|
||||||
if (this.watchThread == null) {
|
if (this.watchThread == null) {
|
||||||
this.watchThread = new Thread() {
|
Map<File, FolderSnapshot> localFolders = new HashMap<File, FolderSnapshot>();
|
||||||
@Override
|
localFolders.putAll(this.folders);
|
||||||
public void run() {
|
this.watchThread = new Thread(new Watcher(this.remainingScans,
|
||||||
int remaining = FileSystemWatcher.this.remainingScans.get();
|
new ArrayList<FileChangeListener>(this.listeners),
|
||||||
while (remaining > 0 || remaining == -1) {
|
this.triggerFilter, this.pollInterval, this.quietPeriod,
|
||||||
try {
|
localFolders));
|
||||||
if (remaining > 0) {
|
|
||||||
FileSystemWatcher.this.remainingScans.decrementAndGet();
|
|
||||||
}
|
|
||||||
scan();
|
|
||||||
}
|
|
||||||
catch (InterruptedException ex) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
remaining = FileSystemWatcher.this.remainingScans.get();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
this.watchThread.setName("File Watcher");
|
this.watchThread.setName("File Watcher");
|
||||||
this.watchThread.setDaemon(this.daemon);
|
this.watchThread.setDaemon(this.daemon);
|
||||||
this.remainingScans = new AtomicInteger(-1);
|
|
||||||
this.watchThread.start();
|
this.watchThread.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void saveInitialSnapshots() {
|
private void saveInitialSnapshots() {
|
||||||
for (File folder : this.folders.keySet()) {
|
for (File folder : this.folders.keySet()) {
|
||||||
|
|
@ -169,6 +171,80 @@ public class FileSystemWatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop monitoring the source folders.
|
||||||
|
*/
|
||||||
|
public void stop() {
|
||||||
|
stopAfter(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop monitoring the source folders.
|
||||||
|
* @param remainingScans the number of remaining scans
|
||||||
|
*/
|
||||||
|
void stopAfter(int remainingScans) {
|
||||||
|
synchronized (this.monitor) {
|
||||||
|
Thread thread = this.watchThread;
|
||||||
|
if (thread != null) {
|
||||||
|
this.remainingScans.set(remainingScans);
|
||||||
|
if (remainingScans <= 0) {
|
||||||
|
thread.interrupt();
|
||||||
|
}
|
||||||
|
if (Thread.currentThread() != thread) {
|
||||||
|
try {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
catch (InterruptedException ex) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.watchThread = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class Watcher implements Runnable {
|
||||||
|
|
||||||
|
private final AtomicInteger remainingScans;
|
||||||
|
|
||||||
|
private final List<FileChangeListener> listeners;
|
||||||
|
|
||||||
|
private final FileFilter triggerFilter;
|
||||||
|
|
||||||
|
private final long pollInterval;
|
||||||
|
|
||||||
|
private final long quietPeriod;
|
||||||
|
|
||||||
|
private Map<File, FolderSnapshot> folders;
|
||||||
|
|
||||||
|
private Watcher(AtomicInteger remainingScans, List<FileChangeListener> listeners,
|
||||||
|
FileFilter triggerFilter, long pollInterval, long quietPeriod,
|
||||||
|
Map<File, FolderSnapshot> folders) {
|
||||||
|
this.remainingScans = remainingScans;
|
||||||
|
this.listeners = listeners;
|
||||||
|
this.triggerFilter = triggerFilter;
|
||||||
|
this.pollInterval = pollInterval;
|
||||||
|
this.quietPeriod = quietPeriod;
|
||||||
|
this.folders = folders;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int remainingScans = this.remainingScans.get();
|
||||||
|
while (remainingScans > 0 || remainingScans == -1) {
|
||||||
|
try {
|
||||||
|
if (remainingScans > 0) {
|
||||||
|
this.remainingScans.decrementAndGet();
|
||||||
|
}
|
||||||
|
scan();
|
||||||
|
}
|
||||||
|
catch (InterruptedException ex) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
remainingScans = this.remainingScans.get();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private void scan() throws InterruptedException {
|
private void scan() throws InterruptedException {
|
||||||
Thread.sleep(this.pollInterval - this.quietPeriod);
|
Thread.sleep(this.pollInterval - this.quietPeriod);
|
||||||
Map<File, FolderSnapshot> previous;
|
Map<File, FolderSnapshot> previous;
|
||||||
|
|
@ -231,34 +307,6 @@ public class FileSystemWatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop monitoring the source folders.
|
|
||||||
*/
|
|
||||||
public synchronized void stop() {
|
|
||||||
stopAfter(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop monitoring the source folders.
|
|
||||||
* @param remainingScans the number of remaining scans
|
|
||||||
*/
|
|
||||||
synchronized void stopAfter(int remainingScans) {
|
|
||||||
Thread thread = this.watchThread;
|
|
||||||
if (thread != null) {
|
|
||||||
this.remainingScans.set(remainingScans);
|
|
||||||
if (remainingScans <= 0) {
|
|
||||||
thread.interrupt();
|
|
||||||
}
|
|
||||||
if (Thread.currentThread() != thread) {
|
|
||||||
try {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
catch (InterruptedException ex) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.watchThread = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue