Example usage for java.nio.file WatchKey watchable

List of usage examples for java.nio.file WatchKey watchable

Introduction

In this page you can find the example usage for java.nio.file WatchKey watchable.

Prototype

Watchable watchable();

Source Link

Document

Returns the object for which this watch key was created.

Usage

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.io.RenderableUpdater.java

private void run() {
    while (!isWatchServiceStopped) {
        WatchKey watchKey;
        try {/*  w  ww.  j  av a  2s . com*/
            watchKey = watcher.take();
        } catch (ClosedWatchServiceException e) {
            log.debug("File watch service is closed.");
            return;
        } catch (InterruptedException e) {
            log.debug("File watch service interrupted.");
            return;
        }

        for (WatchEvent<?> event : watchKey.pollEvents()) {
            if (event.kind() != StandardWatchEventKinds.ENTRY_MODIFY) {
                continue; // We only watch file modify events.
            }

            Path updatedDirectory = (Path) watchKey.watchable();
            @SuppressWarnings("unchecked")
            Path updatedFileName = ((WatchEvent<Path>) event).context();
            MutableHbsRenderable mutableRenderable = watchingRenderables.get(updatedFileName);
            if (mutableRenderable != null) {
                // Updated file is a MutableHbsRenderable
                Path updatedFileAbsolutePath = updatedDirectory.resolve(updatedFileName);
                try {
                    String content = readFileContent(updatedFileAbsolutePath);
                    mutableRenderable.reload(new StringTemplateSource(mutableRenderable.getPath(), content));
                    log.info("Handlebars template '" + updatedFileAbsolutePath + "' reloaded successfully.");
                } catch (UUFException e) {
                    log.error("An error occurred while reloading Handlebars template '"
                            + updatedFileAbsolutePath + "'.", e);
                }
            } else {
                MutableExecutable mutableExecutable = watchingExecutables.get(updatedFileName);
                if (mutableExecutable != null) {
                    // Updated file is a MutableExecutable
                    Path updatedFileAbsolutePath = updatedDirectory.resolve(updatedFileName);
                    try {
                        mutableExecutable.reload(readFileContent(updatedFileAbsolutePath));
                        log.info("JavaScript file '" + updatedFileAbsolutePath + "' reloaded successfully.");
                    } catch (UUFException e) {
                        log.error("An error occurred while reloading JavaScript file '"
                                + updatedFileAbsolutePath + "'.", e);
                    }
                }
            }
        }

        boolean valid = watchKey.reset();
        if (!valid) {
            // Watch key cannot not be reset because watch service is already closed.
            break;
        }
    }
}

From source file:org.wso2.carbon.uuf.renderablecreator.html.internal.io.HtmlRenderableUpdater.java

private void run() {
    while (!isWatchServiceStopped) {
        WatchKey watchKey;
        try {//from w w  w .java2s  .c o  m
            watchKey = watchService.take();
        } catch (ClosedWatchServiceException e) {
            log.debug("File watch service is closed.");
            return;
        } catch (InterruptedException e) {
            log.debug("File watch service interrupted.");
            return;
        }

        for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
            if (watchEvent.kind() != StandardWatchEventKinds.ENTRY_MODIFY) {
                continue;
            }

            Path updatedDirectory = (Path) watchKey.watchable();
            @SuppressWarnings("unchecked")
            Path updatedFileName = ((WatchEvent<Path>) watchEvent).context();
            // Updating the changed html file
            Path updatedFileAbsolutePath = updatedDirectory.resolve(updatedFileName);
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(updatedFileAbsolutePath.getParent())) {
                for (Path entry : stream) {
                    if (Files.isDirectory(entry)) {
                        continue;
                    }

                    MutableHtmlRenderable mutableHtmlRenderable = watchingRenderables.get(entry);
                    if (mutableHtmlRenderable != null) {
                        try {
                            String content = new String(Files.readAllBytes(entry), StandardCharsets.UTF_8);
                            mutableHtmlRenderable.setHtml(content);
                            log.info("HTML template '" + entry + "' reloaded successfully.");
                        } catch (IOException e) {
                            log.error("An error occurred while reloading HTML template '" + entry + "'.", e);
                        }
                    }
                }
            } catch (IOException e) {
                log.error("An error occurred while reloading HTML template '" + updatedFileAbsolutePath + "'.",
                        e);
            }
        }

        boolean valid = watchKey.reset();
        if (!valid) {
            // Watch key cannot not be reset because watch service is already closed.
            break;
        }
    }
}