Commit daf90e1b authored by Tatyana's avatar Tatyana 👽

FileMonitor & FileSearcher total refactoring

parent a4106066
......@@ -53,6 +53,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -149,7 +149,7 @@ public class FxAdminController extends FxController {
fileSearcher = FileSearcher.getInstance();
Thread fileSearcherThread = new Thread(() -> {
List<File> files = fileSearcher.prepareFiles(root);
List<File> files = fileSearcher.prepareFiles(root.toPath());
List<File> peFiles;
if (isPE) {
......
package application.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.*;
public class FileMonitor extends Thread {
private FileSearcher fileSearcher;
private WatchService watcher = FileSystems.getDefault().newWatchService();
private ArrayList<WatchKey> keys = new ArrayList<>();
private WatchService watcher;
private final Map<WatchKey, Path> keys;
private static FileMonitor instance;
private boolean trace = false;
private boolean finish = false;
private final boolean recursive;
private FileMonitor() throws IOException {
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>) event;
}
public static synchronized FileMonitor getInstance() {
FileMonitor(FileSearcher fileSearcher, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap<WatchKey, Path>();
this.recursive = recursive;
this.fileSearcher = fileSearcher;
/*if (recursive) {
System.out.format("Scanning %s ...\n", dir);
registerAll(dir);
System.out.println("Done.");
} else {
register(dir);
}*/
// enable trace after initial registration
this.trace = true;
}
/**
* Register the given directory with the WatchService
*/
public void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE/*, ENTRY_MODIFY*/);
if (trace) {
Path prev = keys.get(key);
if (prev == null) {
System.out.format("register: %s\n", dir);
} else {
if (!dir.equals(prev)) {
System.out.format("update: %s -> %s\n", prev, dir);
}
}
}
keys.put(key, dir);
}
/*public static synchronized FileMonitor getInstance() {
if (instance == null) {
try {
......@@ -28,31 +72,25 @@ public class FileMonitor extends Thread {
}
}
return instance;
}
}*/
/* public void registerDir(Path newDir) {
proceed = true;
public void registerDir(File newDir) {
try {
Path dir = Paths.get(newDir.getAbsolutePath());
keys.add(dir.register(watcher,
keys.add(newDir.register(watcher,
ENTRY_CREATE,
ENTRY_DELETE
/*ENTRY_MODIFY*/));
*//*ENTRY_MODIFY*//*));
} catch (IOException x) {
System.err.println(x);
}
}
public void stopMonitoring() {
for (WatchKey key : keys) {
key.cancel();
}
keys.clear();
}
}*/
public void run() {
/*public void run() {
fileSearcher = FileSearcher.getInstance();
while (keys.size() != 0) {
while (proceed) {
WatchKey key;
try {
key = watcher.take();
......@@ -71,16 +109,110 @@ public class FileMonitor extends Thread {
Path dir = (Path) key.watchable();
Path filename = dir.resolve(ev.context());
if (kind == ENTRY_CREATE) {
*//*if (kind == ENTRY_CREATE) {
fileSearcher.addFile(filename.toAbsolutePath().toFile());
System.out.println("CREATE - " + filename.toAbsolutePath().toFile().toString());
}
if (kind == ENTRY_DELETE) {
fileSearcher.removeFile(filename.toAbsolutePath().toFile());
System.out.println("DELETE - " + filename.toAbsolutePath().toFile().toString());
if (kind == ENTRY_DELETE) {*//*
fileSearcher.addFile(filename.toAbsolutePath().toFile());
System.out.println("MONITOR FOUNDED FILE - " + filename.toAbsolutePath().toFile().toString());
*//*}*//*
}
}
}*/
/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
private void registerAll(final Path start) throws IOException {
// register directory and sub-directories
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
register(dir);
return FileVisitResult.CONTINUE;
}
});
}
public void finishMonitoring() {
this.finish = true;
}
@Override
public void run() {
processEvents();
}
void processEvents() {
for (; ; ) {
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
// TBD - provide example of how OVERFLOW event is handled
if (kind == OVERFLOW) {
continue;
}
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
fileSearcher.addFile(child.toAbsolutePath().toFile());
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
if (event.kind().name().equals(ENTRY_CREATE))
fileSearcher.addFile(child.toAbsolutePath().toFile());
if (event.kind().name().equals(ENTRY_DELETE))
fileSearcher.removeFile(child.toAbsolutePath().toFile());
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (recursive && (kind == ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
if (finish) {
break;
}
}
}
}
package application.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
......@@ -12,42 +18,54 @@ public class FileSearcher {
private int filesFound;
public List<File> prepareFiles(File root) {
public List<File> prepareFiles(Path root) {
filesFound = 0;
List<File> foundedFiles = null;
List<File> foundedFiles = new ArrayList<>();
monitor = FileMonitor.getInstance();
try {
monitor = new FileMonitor(instance, true);
} catch (IOException e) {
System.out.println("Problem encountered during creating MONITOR.");
}
if (monitor != null) {
monitor.setName("Monitor Thread");
monitor.start();
monitor.registerDir(root);
foundedFiles = scanDirectory(root);
foundedFiles.addAll(monitoredFiles);
try {
Files.walkFileTree(root, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
monitor.register(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
foundedFiles.add(file.toFile());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
System.out.println("Problem encountered during walking.");
} finally {
monitor.finishMonitoring();
}
monitor.stopMonitoring();
foundedFiles.addAll(monitoredFiles);
}
return foundedFiles;
}
public List<File> scanDirectory(File root) {
List<File> files = new ArrayList<>();
File[] filesAndCatalogs = root.listFiles();
if (filesAndCatalogs != null) {
for (File file : filesAndCatalogs) {
if (file.isDirectory()) {
monitor.registerDir(file);
files.addAll(scanDirectory(file));
} else {
files.add(file);
filesFound++;
}
}
}
return files;
}
public static synchronized FileSearcher getInstance() {
if (instance == null) {
instance = new FileSearcher();
......@@ -69,5 +87,4 @@ public class FileSearcher {
public int getTotal() {
return filesFound;
}
}
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