Commit 51990097 authored by Tatyana's avatar Tatyana 👽

Merge branch 'feature/file-monitor' into 'master'

Feature/file monitor

See merge request !8
parents 5289d72b 57c6d98d
......@@ -28,6 +28,7 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class FxAdminController extends FxController {
......@@ -185,14 +186,10 @@ public class FxAdminController extends FxController {
File root = new File(dirpathNameField.getText());
if (root.exists()) {
FileSearcher fileSearcher = new FileSearcher();
FileSearcher fileSearcher = FileSearcher.getInstance();
Thread fileSearcherThread = new Thread(() -> {
List<File> files = fileSearcher.scanDirectory(root);
//TODO maybe we can remove it
List<String> filesItems = new ArrayList<>();
for (File file : files) {
filesItems.add(file.toString());
}
List<File> files = fileSearcher.prepareFiles(root);
List<String> filesItems = files.stream().map(file -> file.toString()).collect(Collectors.toList());
ObservableList<String> items = FXCollections.observableArrayList(filesItems);
preparedFilesList.setItems(items);
});
......
package application.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
public class FileMonitor extends Thread {
private FileSearcher fileSearcher;
private WatchService watcher;
private boolean proceed = false;
public FileMonitor() {
try {
watcher = FileSystems.getDefault().newWatchService();
} catch (IOException e) {
}
}
public void registerDir(File newDir) {
proceed = true;
try {
Path dir = Paths.get(newDir.getAbsolutePath());
dir.register(watcher,
ENTRY_CREATE,
ENTRY_DELETE
/*ENTRY_MODIFY*/);
} catch (IOException x) {
System.err.println(x);
}
}
public void stopMonitoring() {
proceed = false;
}
public void run() {
fileSearcher = FileSearcher.getInstance();
while (proceed) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path dir = (Path) key.watchable();
Path filename = dir.resolve(ev.context());
if (kind == ENTRY_CREATE) {
fileSearcher.addFile(filename.toAbsolutePath().toFile());
}
if (kind == ENTRY_DELETE) {
fileSearcher.removeFile(filename.toAbsolutePath().toFile());
}
}
}
}
}
......@@ -6,17 +6,50 @@ import java.util.List;
public class FileSearcher {
private static FileSearcher instance;
private FileMonitor monitor = new FileMonitor();
private List<File> monitoredFiles = new ArrayList<>();
public List<File> prepareFiles(File root) {
monitor.start();
monitor.registerDir(root);
List<File> foundedFiles = scanDirectory(root);
foundedFiles.addAll(monitoredFiles);
monitor.stopMonitoring();
return foundedFiles;
}
public List<File> scanDirectory(File root) {
List<File> files = new ArrayList<>();
File[] filesAndCatalogs = root.listFiles();
assert filesAndCatalogs != null;
for (File file : filesAndCatalogs) {
if (file.isDirectory()) {
files.addAll(scanDirectory(file));
} else {
files.add(file);
if (filesAndCatalogs != null) {
for (File file : filesAndCatalogs) {
if (file.isDirectory()) {
monitor.registerDir(file);
files.addAll(scanDirectory(file));
} else {
files.add(file);
}
}
}
return files;
}
public static synchronized FileSearcher getInstance() {
if (instance == null) {
instance = new FileSearcher();
}
return instance;
}
private FileSearcher() {
}
public void addFile(File file) {
monitoredFiles.add(file);
}
public void removeFile(File file) {
monitoredFiles.remove(file);
}
}
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