List of usage examples for java.nio.file WatchEvent kind
Kind<T> kind();
From source file:SecurityWatch.java
public void watchVideoCamera(Path path) throws IOException, InterruptedException { watchService = FileSystems.getDefault().newWatchService(); register(path, StandardWatchEventKinds.ENTRY_CREATE); OUTERMOST: while (true) { final WatchKey key = watchService.poll(); if (key == null) { System.out.println("The video camera is jammed - security watch system is canceled!"); break; } else {//from ww w. j a v a 2s. c o m for (WatchEvent<?> watchEvent : key.pollEvents()) { final Kind<?> kind = watchEvent.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) { continue; } if (kind == StandardWatchEventKinds.ENTRY_CREATE) { //get the filename for the event final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; final Path filename = watchEventPath.context(); final Path child = path.resolve(filename); if (Files.probeContentType(child).equals("image/jpeg")) { //print it out the video capture time SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); System.out.println("Video capture successfully at: " + dateFormat.format(new Date())); } else { System.out.println("The video camera capture format failed! This could be a virus!"); break OUTERMOST; } } } boolean valid = key.reset(); if (!valid) { break; } } } watchService.close(); }
From source file:org.siphon.db2js.jshttp.ServerUnitManager.java
public void onFileChanged(WatchEvent<Path> ev, Path file) { Kind<Path> kind = ev.kind(); String filename = file.toString(); if (kind == StandardWatchEventKinds.ENTRY_DELETE) { if (contexts.containsKey(filename)) { if (logger.isDebugEnabled()) { logger.debug(filename + " dropped"); }/* www.j ava2s . c o m*/ contexts.remove(filename); } } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { if (contexts.containsKey(filename)) { if (logger.isDebugEnabled()) { logger.debug(filename + " changed"); } contexts.remove(filename); } } }
From source file:com.xiaomi.linden.common.util.FileChangeWatcher.java
@Override public void run() { try {/* w ww .j a v a 2s. co m*/ watcher = FileSystems.getDefault().newWatchService(); Path path = new File(absolutePath).toPath().getParent(); String fileWatched = FilenameUtils.getName(absolutePath); path.register(watcher, new WatchEvent.Kind[] { StandardWatchEventKinds.ENTRY_MODIFY }, SensitivityWatchEventModifier.HIGH); LOGGER.info("File watcher start to watch {}", absolutePath); while (isAlive()) { try { Thread.sleep(interval); WatchKey key = watcher.poll(1000l, TimeUnit.MILLISECONDS); if (key == null) { continue; } List<WatchEvent<?>> events = key.pollEvents(); for (WatchEvent<?> event : events) { if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { String file = event.context().toString(); if (fileWatched.equals(file)) { doOnChange(); } } } if (!key.reset()) { LOGGER.info("File watcher key not valid."); } } catch (InterruptedException e) { LOGGER.error("File watcher thread exit!"); break; } } } catch (Throwable e) { LOGGER.error("File watcher error {}", Throwables.getStackTraceAsString(e)); } }
From source file:org.balloon_project.overflight.task.importer.ImporterFileListener.java
@Override public void run() { // TODO initial import start // initial import of existing file logger.info("Scanning for files to import"); File importDir = new File(configuration.getDatabaseImportDirectory()); if (importDir.exists() && importDir.isDirectory()) { for (File file : importDir.listFiles()) { if (file.isFile() && file.getPath().endsWith(IndexingTask.N_TRIPLES_EXTENSION)) { logger.info("File event: Adding " + file.toString() + " to importer queue"); importer.startImporting(file); }//from ww w. ja v a2 s.c o m } } // starting file watch service for future files try { String path = configuration.getDatabaseImportDirectory(); logger.info("Starting import file listener for path " + path); Path tmpPath = Paths.get(path); WatchService watchService = FileSystems.getDefault().newWatchService(); tmpPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); for (;;) { WatchKey key = watchService.take(); for (WatchEvent event : key.pollEvents()) { if (event.kind().name() == "OVERFLOW") { continue; } else { WatchEvent<Path> ev = (WatchEvent<Path>) event; Path filename = ev.context(); logger.info("File event: Adding " + filename.toString() + " to importer queue"); importer.startImporting(tmpPath.resolve(filename).toFile()); } } // Reset the key -- this step is critical if you want to // receive further watch events. If the key is no longer valid, // the directory is inaccessible so exit the loop. boolean valid = key.reset(); if (!valid) { break; } } } catch (IOException | InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { logger.debug("Stopping import file listener"); } }
From source file:org.siphon.d2js.jshttp.ServerUnitManager.java
public void onFileChanged(WatchEvent<Path> ev, Path file) { Kind<Path> kind = ev.kind(); String filename = file.toString(); try {//from w w w.j a va 2 s .c o m if (kind == StandardWatchEventKinds.ENTRY_DELETE) { if (allD2js.containsKey(filename)) { if (logger.isDebugEnabled()) { logger.debug(filename + " dropped"); } ScriptObjectMirror d2js = allD2js.get(filename); if (d2js.containsKey("releaseD2js")) { d2js.callMember("releaseD2js"); } allD2js.remove(filename); //TODO call releaseD2js? } } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { if (allD2js.containsKey(filename)) { if (logger.isDebugEnabled()) { logger.debug(filename + " changed"); } ScriptObjectMirror d2js = allD2js.get(filename); if (d2js.containsKey("releaseD2js")) { d2js.callMember("releaseD2js"); } allD2js.remove(filename); } } } catch (Exception e) { logger.error("file synchronize failed on " + filename + " changed ", e); } }
From source file:cz.muni.fi.pb138.cvmanager.service.PDFgenerator.java
/** * By external calling pdflatex function of laTex generates pdf curriculum vitae document from .tex file * @param username name of user whose is the CV * @return language to export (sk/en)//from w ww .j a va 2 s . c om * @throws IOException * @throws InterruptedException * @throws NullPointerException */ public InputStream latexToPdf(String username) throws IOException, InterruptedException, NullPointerException { ProcessBuilder pb = new ProcessBuilder("pdflatex", username + "_cv.tex", "--output-directory="); File file = new File("cvxml/"); pb.directory(file); Process p = pb.start(); WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir = Paths.get("cvxml/"); dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY); while (true) { // wait for a key to be available for 10 seconds WatchKey key = watcher.poll(10000L, TimeUnit.MILLISECONDS); if (key == null) { break; } for (WatchEvent<?> event : key.pollEvents()) { // get event type WatchEvent.Kind<?> kind = event.kind(); // get file name @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event; Path fileName = ev.context(); System.out.println(kind.name() + ": " + fileName); } boolean valid = key.reset(); if (!valid) { break; } } System.out.println("end of cycle"); File pdf = new File("cvxml/" + username + "_cv.pdf"); return new FileInputStream(pdf); }
From source file:org.wso2.carbon.uuf.renderablecreator.html.internal.io.HtmlRenderableUpdater.java
private void run() { while (!isWatchServiceStopped) { WatchKey watchKey;/*from w ww . j a v a2 s .c o m*/ try { 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; } } }
From source file:ch.cyberduck.core.local.FileWatcher.java
private void callback(final Local folder, final WatchEvent<?> event, final FileWatcherListener l) { final WatchEvent.Kind<?> kind = event.kind(); if (log.isInfoEnabled()) { log.info(String.format("Process file system event %s for %s", kind.name(), event.context())); }// w w w.j a v a2s .com if (ENTRY_MODIFY == kind) { l.fileWritten(this.normalize(folder, event.context().toString())); } else if (ENTRY_DELETE == kind) { l.fileDeleted(this.normalize(folder, event.context().toString())); } else if (ENTRY_CREATE == kind) { l.fileCreated(this.normalize(folder, event.context().toString())); } else { log.debug(String.format("Ignored file system event %s for %s", kind.name(), event.context())); } }
From source file:bjerne.gallery.service.impl.GalleryRootDirConfigJob.java
private void watchForChanges() { Path dir = configFile.getParentFile().toPath(); try {//from w ww . j a v a 2 s .com WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); for (;;) { try { key = watcher.take(); } catch (InterruptedException | ClosedWatchServiceException e) { LOG.info("Interrupted during watcher.take(). Exiting watch loop."); return; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); if (kind == OVERFLOW) { continue; } @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event; Path filename = ev.context(); Path child = dir.resolve(filename); if (child.equals(configFile.toPath())) { LOG.debug("File was changed."); updateConfigFromFile(); } } boolean valid = key.reset(); if (!valid) { break; } } } catch (IOException ioe) { LOG.error("Exception in filewatcher loop. Exiting.", ioe); } }
From source file:com.reactive.hzdfs.dll.JarModuleLoader.java
private Set<File> filesFromEvents() throws InterruptedException { WatchKey key = watcher.take(); Set<File> files = new LinkedHashSet<File>(); if (key != null && key.isValid()) { for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { Path item = (Path) event.context(); File file = new File( ((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName()); if (log.isDebugEnabled()) { log.debug("Watch Event: " + event.kind() + ": " + file); }//from w ww. ja va 2 s . c om if (isJarFile(file)) { files.add(file); } else log.warn("[JAR Loader] Ignoring file:- " + file); } } key.reset(); } return files; }