예제 #1
0
  /** Asynchronous close of watcher causes blocked threads to wakeup */
  static void testWakeup(Path dir) throws IOException {
    System.out.println("-- Wakeup Tests --");
    final WatchService watcher = FileSystems.getDefault().newWatchService();
    Runnable r =
        new Runnable() {
          public void run() {
            try {
              Thread.sleep(5000);
              System.out.println("close WatchService...");
              watcher.close();
            } catch (InterruptedException x) {
              x.printStackTrace();
            } catch (IOException x) {
              x.printStackTrace();
            }
          }
        };

    // start thread to close watch service after delay
    new Thread(r).start();

    try {
      System.out.println("take...");
      watcher.take();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
      System.out.println("ClosedWatchServiceException thrown");
    }

    System.out.println("OKAY");
  }
예제 #2
0
 static void takeExpectedKey(WatchService watcher, WatchKey expected) {
   System.out.println("take events...");
   WatchKey key;
   try {
     key = watcher.take();
   } catch (InterruptedException x) {
     // not expected
     throw new RuntimeException(x);
   }
   if (key != expected) throw new RuntimeException("removed unexpected key");
 }
예제 #3
0
  /** Обработчик всех событий помещенных в очередь */
  void processEvents() {
    for (; ; ) {

      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException x) {
        LOG.log(Level.SEVERE, x.getMessage());
        return;
      }

      Path dir = keys.get(key);
      if (dir == null) {
        LOG.log(Level.SEVERE, "Входной каталог не найден!");
        continue;
      }

      for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind kind = event.kind();

        // TODO - подумать над обработчиком события OVERFLOW
        if (kind == OVERFLOW) {
          continue;
        }

        WatchEvent<Path> ev = cast(event);
        Path name = ev.context();
        Path child = dir.resolve(name);

        // логируем событие
        if (kind == ENTRY_CREATE) {
          LOG.log(Level.FINEST, "{0}: {1}", new Object[] {event.kind().name(), child});
          Runnable worker = new WorkerThread(child);
          executor.execute(worker);
        }
      }

      boolean valid = key.reset();
      if (!valid) {
        keys.remove(key);
        if (keys.isEmpty()) {
          break;
        }
      }
    }
  }
예제 #4
0
  @Override
  public void run() {
    logger.debug("Register root " + startDir.toString());

    try {
      registerAll(startDir);
    } catch (IOException ex) {
      logger.error(ex.getMessage());
      return;
    }

    if (isInterrupted()) return;

    VOSync.debug("Sync local db with drive");

    DbPool.goSql(
        "Synching the local db with drive",
        "select NAME from FILES",
        new SqlWorker<Boolean>() {
          @Override
          public Boolean go(Connection conn, PreparedStatement stmt) throws SQLException {
            ResultSet resSet = stmt.executeQuery();
            while (resSet.next()) {
              try {
                String fileName = resSet.getString(1);
                Path filePath =
                    FileSystems.getDefault().getPath(startDir.toString(), fileName.substring(1));
                if (!filePath.toFile().exists()) {
                  logger.debug(
                      "Deleting file " + fileName + " existing in DB and not present on disk");
                  api.delete(fileName);
                  MetaHandler.delete(fileName);
                }
              } catch (DropboxException ex) {
              }
            }
            resSet.close();
            return true;
          }
        });

    if (isInterrupted()) return;

    VOSync.debug("Sync storage");

    syncStorage();

    logger.debug("Start watching");

    while (!isInterrupted()) {
      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException x) {
        return;
      }

      Path dir = keys.get(key);
      if (dir == null) {
        System.err.println("WatchKey " + key.toString() + " not recognized!");
        continue;
      }

      for (WatchEvent<?> event : key.pollEvents()) {
        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);
        Path relativeDir = startDir.relativize(child);
        String fileRelPath = "/" + fixPath(relativeDir.toString());

        // print out event
        logger.debug(event.kind().name() + ":" + child + " " + name + " " + key);

        try {
          if (Files.exists(child, new LinkOption[] {}) && Files.isHidden(child)) {
            logger.error(
                "Skipping hidden file " + child.getFileName()); // skip OS generated catalog files
          } else {
            if (event.kind() == ENTRY_CREATE) {
              if (Files.isRegularFile(child, NOFOLLOW_LINKS)) { // file modified
                uploadFile(fileRelPath, child);
              } else if (Files.isDirectory(child, NOFOLLOW_LINKS)) { // directory contents changed
                registerAll(child);
              }
            } else if (event.kind() == ENTRY_DELETE) {
              logger.debug("Deleting " + fileRelPath);
              api.delete(fileRelPath);
              MetaHandler.delete(fileRelPath);
              logger.debug("Deleted!");
            } else if (event.kind() == ENTRY_MODIFY) {
              if (Files.isRegularFile(child, NOFOLLOW_LINKS)) { // file modified
                uploadFile(fileRelPath, child);
              } else if (Files.isDirectory(child, NOFOLLOW_LINKS)) { // directory contents changed
                // logger.debug("Renewing dir: "+relativeDir.toString());
                // TODO update folder date
                // MetaHandler.setFile(fileRelPath, child, rev);
              }
            }
          }
        } catch (IOException ex) {
          ex.printStackTrace();
          logger.error(ex.getMessage());
        } catch (DropboxException ex) {
          ex.printStackTrace();
          logger.error(ex.getMessage());
        }
      }

      boolean valid = key.reset();

      if (!valid) keys.remove(key);
    }
  }
예제 #5
0
  // void processEvents() {
  public void run() {
    System.out.println("WatchDir Thread INFO: priority=" + Thread.currentThread().getPriority());
    for (; ; ) {
      // wait for key to be signalled
      System.out.println("WatchDir INFO: restarting loop...acquiring new key");
      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;
      }

      for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind kind = event.kind();
        // TBD - provide example of how OVERFLOW event is handled
        if (kind == OVERFLOW) {
          System.out.println("Encountered OVERFLOW Event - " + event);
          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);

        // print out event
        System.out.format("[WatchDir] %s: %s\n", event.kind().name(), child);

        // if directory is created, and watching recursively, then
        // register it and its sub-directories
        if (recursive && (kind == ENTRY_CREATE)) {
          try {
            if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
              registerAll(child);
            }
          } catch (IOException x) {
            // ignore to keep sample readbale
          }
        }

        long t = System.currentTimeMillis();
        if (!Folder.dontWatch.contains(Folder.getInternalPath(child))) {
          Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
          System.out.println(
              "WatchDir#"
                  + key
                  + " INFO: path="
                  + child
                  + ", internal="
                  + Folder.getInternalPath(child)
                  + " is NOT in don't watch list. Forwarding it to other peers. @"
                  + Main.timeToString(t)); // DEBUG
          forwardToItopic(kind, child);
        } else {
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
          System.out.println(
              "WatchDir#"
                  + key
                  + " INFO: path="
                  + child
                  + ", internal="
                  + Folder.getInternalPath(child)
                  + " IS in the don't watch list. NOT forwarding. @"
                  + Main.timeToString(t)); // DEBUG
          // try{
          //     Thread.sleep(minDelayBtwnWatchEvents);
          // } catch(InterruptedException ex) {
          //     System.err.println("Exception:"+ex+" while trying to sleep WatchDir thread");
          //     ex.printStackTrace();
          // }
        }
      }

      // 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;
        }
      }
    }
  }
예제 #6
0
파일: WatchDir.java 프로젝트: raynmune/fmd
  /** Process all events for keys queued to the watcher */
  void processEvents() {
    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;
      }

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

        // print out event
        System.out.format("%s: %s\n", event.kind().name(), child);
        // Printing manager
        if (event.kind().name().equals("ENTRY_MODIFY")
            && (child.endsWith("baseFerremundoPointer.csv")
                || child.endsWith("baseFerremundoPointer2.csv"))) {
          File file = new File(child.toString());
          file.renameTo(new File("/home/dios/FERREMUNDO/BD/baseFerremundoPointer_.csv"));
          InvoicePrintingManager manager = new InvoicePrintingManager();
          manager.manage();
        }
        // if directory is created, and watching recursively, then
        // register it and its sub-directories
        if (recursive && (kind == ENTRY_CREATE)) {
          try {
            if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
              registerAll(child);
            }
          } catch (IOException x) {
            // ignore to keep sample readbale
          }
        }
      }

      // 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;
        }
      }
    }
  }
예제 #7
0
  /** Simple test to check exceptions and other cases */
  @SuppressWarnings("unchecked")
  static void testExceptions(Path dir) throws IOException {
    System.out.println("-- Exceptions and other simple tests --");

    WatchService watcher = FileSystems.getDefault().newWatchService();
    try {

      // Poll tests

      WatchKey key;
      System.out.println("poll...");
      key = watcher.poll();
      if (key != null) throw new RuntimeException("no keys registered");

      System.out.println("poll with timeout...");
      try {
        long start = System.currentTimeMillis();
        key = watcher.poll(3000, TimeUnit.MILLISECONDS);
        if (key != null) throw new RuntimeException("no keys registered");
        long waited = System.currentTimeMillis() - start;
        if (waited < 2900) throw new RuntimeException("poll was too short");
      } catch (InterruptedException x) {
        throw new RuntimeException(x);
      }

      // IllegalArgumentException
      System.out.println("IllegalArgumentException tests...");
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {});
        throw new RuntimeException("IllegalArgumentException not thrown");
      } catch (IllegalArgumentException x) {
      }
      try {
        // OVERFLOW is ignored so this is equivalent to the empty set
        dir.register(watcher, new WatchEvent.Kind<?>[] {OVERFLOW});
        throw new RuntimeException("IllegalArgumentException not thrown");
      } catch (IllegalArgumentException x) {
      }

      // UnsupportedOperationException
      try {
        dir.register(
            watcher,
            new WatchEvent.Kind<?>[] {
              new WatchEvent.Kind<Object>() {
                @Override
                public String name() {
                  return "custom";
                }

                @Override
                public Class<Object> type() {
                  return Object.class;
                }
              }
            });
      } catch (UnsupportedOperationException x) {
      }
      try {
        dir.register(
            watcher,
            new WatchEvent.Kind<?>[] {ENTRY_CREATE},
            new WatchEvent.Modifier() {
              @Override
              public String name() {
                return "custom";
              }
            });
        throw new RuntimeException("UnsupportedOperationException not thrown");
      } catch (UnsupportedOperationException x) {
      }

      // NullPointerException
      System.out.println("NullPointerException tests...");
      try {
        dir.register(null, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {null});
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE}, (WatchEvent.Modifier) null);
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
    } finally {
      watcher.close();
    }

    // -- ClosedWatchServiceException --

    System.out.println("ClosedWatchServiceException tests...");

    try {
      watcher.poll();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (ClosedWatchServiceException x) {
    }

    // assume that poll throws exception immediately
    long start = System.currentTimeMillis();
    try {
      watcher.poll(10000, TimeUnit.MILLISECONDS);
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
      long waited = System.currentTimeMillis() - start;
      if (waited > 5000) throw new RuntimeException("poll was too long");
    }

    try {
      watcher.take();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
    }

    try {
      dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (ClosedWatchServiceException x) {
    }

    System.out.println("OKAY");
  }