Search in sources :

Example 1 with FileSystemWatchEvent

use of com.stnetix.ariaddna.localstoragemanager.event.FileSystemWatchEvent in project ariADDna by StnetixDevTeam.

the class Consumer method checkOtherEvents.

/**
 * find MOVE, CREATE, DELETE events
 * bufferQueue processing
 * wen we delete file the fileSystem generate ENTRY_DELETE and ENTRY_CREATE events but not in a strict order
 * therefore we take a event from bufferQueue and fid a pair, the remove these events from queue
 * if we did not find a pair
 */
private void checkOtherEvents() {
    // collection for prepared events
    List<Pair<Path, WatchEvent<?>>> doneList = new ArrayList<>();
    for (Pair<Path, WatchEvent<?>> p : bufferQueue) {
        if (p != null && !doneList.contains(p)) {
            @SuppressWarnings("unchecked") WatchEvent<Path> event = (WatchEvent<Path>) p.getValue();
            Path name = event.context();
            final boolean[] isMoveEvent = { false };
            WatchEvent.Kind<Path> from = event.kind();
            WatchEvent.Kind<Path> to = from.equals(ENTRY_DELETE) ? ENTRY_CREATE : ENTRY_DELETE;
            bufferQueue.forEach(e -> {
                if (!doneList.contains(e)) {
                    WatchEvent<Path> ev = (WatchEvent<Path>) e.getValue();
                    Path newName = ev.context();
                    if (ev.kind().equals(to) && newName.equals(name)) {
                        if (from.equals(ENTRY_DELETE)) {
                            LOGGER.debug("defined MOVE event from {} to {}", p.getKey().resolve(name), e.getKey().resolve(name));
                            service.runEvents(new FileSystemWatchEvent(FileSystemWatchEvent.Type.MOVE, p.getKey().resolve(name), e.getKey().resolve(name)));
                        } else {
                            LOGGER.debug("defined MOVE event from {} to {}", e.getKey().resolve(name), p.getKey().resolve(name));
                            service.runEvents(new FileSystemWatchEvent(FileSystemWatchEvent.Type.MOVE, e.getKey().resolve(name), p.getKey().resolve(name)));
                        }
                        bufferQueue.remove(e);
                        doneList.add(e);
                        isMoveEvent[0] = true;
                    }
                }
            });
            if (!isMoveEvent[0]) {
                FileSystemWatchEvent.Type eventType = from.equals(ENTRY_CREATE) ? FileSystemWatchEvent.Type.CREATE : FileSystemWatchEvent.Type.DELETE;
                if (from == ENTRY_CREATE) {
                    LOGGER.debug("defined CREATE event from {}", p.getKey().resolve(name));
                    try {
                        if (Files.isDirectory(p.getKey().resolve(name))) {
                            service.registerDirectories(p.getKey().resolve(name));
                        }
                    } catch (IOException x) {
                        LOGGER.trace("Cant register folder {}", x.getMessage());
                    }
                } else if (from == ENTRY_DELETE) {
                    LOGGER.debug("defined DELETE event from {}", p.getKey().resolve(name));
                }
                service.runEvents(new FileSystemWatchEvent(eventType, p.getKey().resolve(name)));
                isMoveEvent[0] = false;
            }
        }
        bufferQueue.remove(p);
        doneList.add(p);
    }
    // queue.clear();
    bufferQueue.clear();
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IOException(java.io.IOException) FileSystemWatchEvent(com.stnetix.ariaddna.localstoragemanager.event.FileSystemWatchEvent) WatchEvent(java.nio.file.WatchEvent) FileSystemWatchEvent(com.stnetix.ariaddna.localstoragemanager.event.FileSystemWatchEvent) Pair(javafx.util.Pair)

Aggregations

FileSystemWatchEvent (com.stnetix.ariaddna.localstoragemanager.event.FileSystemWatchEvent)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 WatchEvent (java.nio.file.WatchEvent)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Pair (javafx.util.Pair)1