List of usage examples for java.nio.file WatchKey pollEvents
List<WatchEvent<?>> pollEvents();
From source file:net.mindengine.dashserver.compiler.GlobalAssetsFileWatcher.java
@Override public void run() { copyAllAssets();/* w ww .ja v a2 s. c o m*/ Path path = Paths.get(this.assetsFolderPath); FileSystem fileSystem = path.getFileSystem(); try (WatchService service = fileSystem.newWatchService()) { path.register(service, ENTRY_MODIFY, ENTRY_CREATE); while (true) { WatchKey watchKey = service.take(); for (WatchEvent<?> watchEvent : watchKey.pollEvents()) { Path watchEventPath = (Path) watchEvent.context(); String fileName = watchEventPath.toString(); copyFileAsset(fileName); } if (!watchKey.reset()) { break; } } } catch (Exception e) { e.printStackTrace(); } }
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)/* www.j a v a 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:com.xiaomi.linden.common.util.FileChangeWatcher.java
@Override public void run() { try {// w w w. jav a 2 s . 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: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 w w w .j a va 2 s . 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:io.mangoo.build.Watcher.java
@SuppressWarnings("all") private void handleEvents(WatchKey watchKey, Path path) { for (WatchEvent<?> watchEvent : watchKey.pollEvents()) { WatchEvent.Kind<?> watchEventKind = watchEvent.kind(); if (OVERFLOW.equals(watchEventKind)) { continue; }/* ww w . j a va2 s. c o m*/ WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent; Path name = ev.context(); Path child = path.resolve(name); if (ENTRY_MODIFY.equals(watchEventKind) && !child.toFile().isDirectory()) { handleNewOrModifiedFile(child); } if (ENTRY_CREATE.equals(watchEventKind)) { if (!child.toFile().isDirectory()) { handleNewOrModifiedFile(child); } try { if (Files.isDirectory(child, NOFOLLOW_LINKS)) { registerAll(child); } } catch (IOException e) { LOG.error("Something fishy happened. Unable to register new dir for watching", e); } } } }
From source file:com.gwac.job.FileTransferServiceImpl.java
public void transFile() { System.out.println("123"); try {//from ww w. j a v a2s .com System.out.println("123"); watcher = FileSystems.getDefault().newWatchService(); Path dir = Paths.get("E:/TestData/gwacTest"); dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY); System.out.println("Watch Service registered for dir: " + dir.getFileName()); isSuccess = true; } catch (IOException ex) { isSuccess = false; ex.printStackTrace(); } if (isBeiJingServer || !isSuccess) { return; } if (running == true) { log.debug("start job fileTransferJob..."); running = false; } else { log.warn("job fileTransferJob is running, jump this scheduler."); return; } try { WatchKey key = watcher.poll(); if (key != null) { for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); WatchEvent<Path> ev = (WatchEvent<Path>) event; Path fileName = ev.context(); System.out.println(kind.name() + ": " + fileName); if (kind == ENTRY_MODIFY) { System.out.println("My source file has changed!!!"); } } } boolean valid = key.reset(); if (!valid) { return; } } catch (Exception ex) { } if (running == false) { running = true; log.debug("job fileTransferJob is done."); } }
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(11, TimeUnit.SECONDS); if (key == null) { System.out.println("The video camera is jammed - security watch system is canceled!"); break; } else {/* w ww . jav a 2 s . 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:de.elomagic.carafile.client.CaraCloud.java
/** * Starts synchronization of the given base path with the registry. * <p/>//from w ww . j av a 2 s .co m * This method will block till call method {@link CaraCloud#stop() } * * @throws IOException Thrown when an I/O error occurs * @throws InterruptedException Thrown when method stop was called or application will terminate * @throws GeneralSecurityException */ public void start() throws IOException, InterruptedException, GeneralSecurityException { if (client == null) { throw new IllegalStateException("Attribute \"client\" must be set."); } if (basePath == null) { throw new IllegalStateException("Attribute \"basePath\" must be set."); } if (!Files.exists(basePath)) { throw new IllegalStateException("Path \"" + basePath + "\" must exists."); } if (!Files.isDirectory(basePath)) { throw new IllegalStateException("Path \"" + basePath + "\" must be a directory/folder."); } watcher = FileSystems.getDefault().newWatchService(); registerDefaultWatch(basePath); while (!Thread.interrupted()) { WatchKey key = watcher.take(); for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { Path path = basePath.resolve(event.context().toString()); createFile(path); } else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { } else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { Path path = basePath.resolve(event.context().toString()); deleteFile(path); } else { LOG.error("Unsupported kind: " + event.kind() + ", path: " + event.context()); } } key.reset(); } }
From source file:org.eclipse.smarthome.core.transform.AbstractFileTransformationService.java
/** * Ensures that a modified or deleted cached files does not stay in the cache */// w w w .j av a2 s .c om private void processFolderEvents() { WatchKey key = watchService.poll(); if (key != null) { for (WatchEvent<?> e : key.pollEvents()) { if (e.kind() == OVERFLOW) { continue; } // Context for directory entry event is the file name of entry @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) e; Path path = ev.context(); logger.debug("Refreshing transformation file '{}'", path); for (String fileEntry : cachedFiles.keySet()) { if (fileEntry.endsWith(path.toString())) { cachedFiles.remove(fileEntry); } } } key.reset(); } }
From source file:jsonbrowse.JsonBrowse.java
@Override public void run() { try {// www .j av a 2s . c o m WatchKey key = watcher.take(); while (key != null) { if (watching) { for (WatchEvent event : key.pollEvents()) { if (event.context() instanceof Path) { Path path = (Path) (event.context()); if (path.getFileName().equals(jsonFilePath.getFileName())) { if (path.toFile().length() > 0) updateModel(); } } } } key.reset(); key = watcher.take(); } } catch (InterruptedException | IOException ex) { Logger.getLogger(JsonBrowse.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Stopping thread."); }