Commit 97cfad71 authored by Tatyana's avatar Tatyana 👽

Monitor fixes #1

parents 53bd0c30 51990097
......@@ -3,30 +3,42 @@ package application.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import static java.nio.file.StandardWatchEventKinds.*;
public class FileMonitor extends Thread {
private FileSearcher fileSearcher;
private WatchService watcher;
private WatchService watcher = FileSystems.getDefault().newWatchService();
private boolean proceed = false;
private ArrayList<WatchKey> keys = new ArrayList<>();
public FileMonitor() {
private static FileMonitor instance;
private FileMonitor() throws IOException {
}
public static synchronized FileMonitor getInstance() {
if (instance == null) {
try {
watcher = FileSystems.getDefault().newWatchService();
instance = new FileMonitor();
} catch (IOException e) {
System.out.println("Error while initializing monitor occured");
return null;
}
}
return instance;
}
public void registerDir(File newDir) {
proceed = true;
try {
Path dir = Paths.get(newDir.getAbsolutePath());
dir.register(watcher,
keys.add(dir.register(watcher,
ENTRY_CREATE,
ENTRY_DELETE
/*ENTRY_MODIFY*/);
/*ENTRY_MODIFY*/));
} catch (IOException x) {
System.err.println(x);
}
......@@ -34,6 +46,10 @@ public class FileMonitor extends Thread {
public void stopMonitoring() {
proceed = false;
for (WatchKey key : keys) {
key.cancel();
}
keys.clear();
}
public void run() {
......
......@@ -14,12 +14,20 @@ public class FileSearcher {
public List<File> prepareFiles(File root) {
filesFound = 0;
monitor = new FileMonitor();
List<File> foundedFiles = null;
monitor = FileMonitor.getInstance();
if (monitor != null) {
monitor.setName("Monitor Thread");
monitor.start();
monitor.registerDir(root);
List<File> foundedFiles = scanDirectory(root);
foundedFiles = scanDirectory(root);
foundedFiles.addAll(monitoredFiles);
monitor.stopMonitoring();
}
return foundedFiles;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment