Example usage for java.nio.file WatchEvent.Kind toString

List of usage examples for java.nio.file WatchEvent.Kind toString

Introduction

In this page you can find the example usage for java.nio.file WatchEvent.Kind toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.dia.kafka.isatools.producer.DirWatcher.java

/**
 * Process all events for keys queued to the watcher
 * @param isatProd/*from   w w  w.ja  v  a2 s .  c  o  m*/
 */
void processEvents(ISAToolsKafkaProducer isatProd) {
    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;
        }

        List<JSONObject> jsonParsedResults = new ArrayList<JSONObject>();

        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);

            // If an inner file has been modify then, recreate the entry
            if (kind == ENTRY_MODIFY || kind == ENTRY_CREATE) {
                File fileCheck = child.getParent().toFile();
                if (child.toFile().isDirectory()) {
                    fileCheck = child.toFile();
                }

                System.out.format("[%s] %s : %s\n", this.getClass().getSimpleName(), kind.toString(),
                        fileCheck.getAbsolutePath());
                List<String> folderFiles = ISAToolsKafkaProducer.getFolderFiles(fileCheck);
                List<JSONObject> jsonObjects = ISAToolsKafkaProducer.doTikaRequest(folderFiles);
                if (!jsonObjects.isEmpty()) {
                    //                        jsonParsedResults.addAll(jsonObjects);
                    isatProd.sendISAToolsUpdates(jsonObjects);
                }
            }

            // TODO this event has still to be specified for documents
            if (kind == ENTRY_DELETE) {
                System.err.println(String.format("Delete event not supported %s", child.toAbsolutePath()));
            }

            // if directory is created, and watching recursively, then
            // register it and its sub-directories
            if (kind == ENTRY_CREATE) {
                try {
                    if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                        registerAll(child);
                    }
                } catch (IOException x) {
                    // ignore to keep sample readbale
                    System.err.format("IOException when creating %s \n", child.toAbsolutePath());
                }
            }
        }

        // 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;
            }
        }
    }
}