Commit a3fdc34e authored by Rina Sergeeva's avatar Rina Sergeeva

merge with master

parents df44e0b8 7fb78cff
......@@ -59,6 +59,12 @@
</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
......@@ -143,13 +143,13 @@ public class FxAdminController extends FxController {
}
File root = new File(dirpathNameField.getText());
Timer timer = new Timer("Label Files Found Timer");
/*Timer timer = new Timer("Label Files Found Timer");*/
if (root.exists()) {
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) {
......@@ -167,7 +167,8 @@ public class FxAdminController extends FxController {
List<String> filesItems = files.stream().map(File::toString).collect(Collectors.toList());
ObservableList<String> items = FXCollections.observableArrayList(filesItems);
preparedFilesList.setItems(items);
timer.cancel();
//timer.cancel();
updateFilesFoundLabel(fileSearcher.getTotal());
});
});
......@@ -175,7 +176,7 @@ public class FxAdminController extends FxController {
fileSearcherThread.setDaemon(true);
fileSearcherThread.start();
timer.scheduleAtFixedRate(new TimerTask() {
/*timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> {
......@@ -183,7 +184,7 @@ public class FxAdminController extends FxController {
});
}
}, 0, 250);
updateFilesFoundLabel(fileSearcher.getTotal());*/
} else {
notificate("Invalid dirpath!", NotificationType.ERROR);
}
......
package application.util;
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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 boolean proceed = false;
private WatchService watcher;
private final Map<WatchKey, Path> keys;
private boolean trace = false;
private boolean finish = false;
private final boolean recursive;
private static FileMonitor instance;
static Logger logger = LogManager.getLogger(FileMonitor.class);
private FileMonitor() throws IOException {
@SuppressWarnings("unchecked")
private static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>) event;
}
public static synchronized FileMonitor getInstance() {
if (instance == null) {
try {
instance = new FileMonitor();
} catch (IOException e) {
System.out.println("Error while initializing monitor occured");
return null;
FileMonitor(FileSearcher fileSearcher, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap<WatchKey, Path>();
this.recursive = recursive;
this.fileSearcher = fileSearcher;
// 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) {
logger.info(String.format("register: %s\n", dir));
} else {
if (!dir.equals(prev)) {
logger.info(String.format("update: %s -> %s\n", prev, dir));
}
}
}
return instance;
keys.put(key, dir);
}
public void registerDir(File newDir) {
try {
proceed = true;
Path dir = Paths.get(newDir.getAbsolutePath());
keys.add(dir.register(watcher,
ENTRY_CREATE,
ENTRY_DELETE
/*ENTRY_MODIFY*/));
System.out.println("MONITOR ::: DIR REGISTERED" + newDir);
} catch (IOException x) {
System.err.println(x);
}
/**
* 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 stopMonitoring() {
proceed = false;
for (WatchKey key : keys) {
key.cancel();
}
keys.clear();
System.out.println("MONITOR ::: STOPPED");
public void finishMonitoring() {
this.finish = true;
}
@Override
public void run() {
System.out.println("MONITOR ::: STARTING");
fileSearcher = FileSearcher.getInstance();
processEvents();
}
private void processEvents() {
for (; ; ) {
while (proceed) {
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
......@@ -66,33 +94,62 @@ public class FileMonitor extends Thread {
return;
}
Path dir = keys.get(key);
if (dir == null) {
logger.error("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
WatchEvent.Kind kind = event.kind();
// TBD - provide example of how OVERFLOW event is handled
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) {
File file = filename.toAbsolutePath().toFile();
if (!file.isDirectory()) {
fileSearcher.addFile(filename.toAbsolutePath().toFile());
System.out.println("ADD FILE - " + filename.toAbsolutePath().toFile().toString());
// 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
logger.info(String.format("%s: %s\n", event.kind().name(), child));
if (event.kind() == ENTRY_CREATE)
fileSearcher.addFile(child.toAbsolutePath().toFile());
if (event.kind() == 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
}
}
}
if (kind == ENTRY_DELETE) {
File file = filename.toAbsolutePath().toFile();
if (!file.isDirectory()) {
fileSearcher.removeFile(filename.toAbsolutePath().toFile());
System.out.println("DELETE FILE - " + filename.toAbsolutePath().toFile().toString());
}
// 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;
import java.util.concurrent.atomic.AtomicInteger;
public class FileSearcher {
......@@ -10,48 +17,70 @@ public class FileSearcher {
private FileMonitor monitor;
private List<File> monitoredFiles = new ArrayList<>();
private int filesFound;
private AtomicInteger filesCounter = new AtomicInteger(0);
public List<File> prepareFiles(File root) {
filesFound = 0;
List<File> foundedFiles = null;
public List<File> prepareFiles(Path root) {
filesCounter.set(0);
List<File> foundedFiles = new ArrayList<>();
monitor = FileMonitor.getInstance();
try {
monitor = new FileMonitor(instance, true);
} catch (IOException e) {
System.out.println("ERROR : A problem encountered during creating MONITOR.");
}
if (monitor != null) {
monitor.setName("Monitor Thread");
monitor.registerDir(root);
monitor.start();
foundedFiles = scanDirectory(root);
System.out.println("Add monitored files");
foundedFiles.addAll(monitoredFiles);
monitor.stopMonitoring();
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());
// if(UnzipUtil.isZip(file.toFile())){
// UnzipUtil.unzipArchive(file.toFile());
filesCounter.incrementAndGet();
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("ERROR : A problem encountered during walking through directories.");
} finally {
monitor.finishMonitoring();
}
foundedFiles.addAll(monitoredFiles);
filesCounter.addAndGet(monitoredFiles.size());
}
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 if(UnzipUtil.isZip(file)){
UnzipUtil.unzipArchive(file);
} else
{
files.add(file);
filesFound++;
}
}
}
return files;
}
public static synchronized FileSearcher getInstance() {
if (instance == null) {
......@@ -72,7 +101,6 @@ public class FileSearcher {
}
public int getTotal() {
return filesFound;
return filesCounter.get();
}
}
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{HH:mm} %c{1} - %msg
rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
\ No newline at end of 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