Пример #1
0
  @Override
  public void run() {
    while (running) {
      try {
        WatchKey key = watcher.take();
        sleep(100);
        for (WatchEvent<?> event : key.pollEvents()) {
          Kind<?> kind = event.kind();
          String dateiname = event.context().toString();
          int linie;
          LineReader lr;
          switch (kind.name()) {
            case "ENTRY_CREATE":
              // HTreff<X>PN<Y>.ctl
              // HTreff<X>PN<Y>.nrt
              // HTreff<X>MN<Y>.ctl
              // HTreff<X>MN<Y>.nrt
              if (!dateiname.startsWith("HTreff")) break;

              int endIndex = dateiname.indexOf('N') - 1;
              linie = Integer.parseUnsignedInt(dateiname.substring(6, endIndex));
              lr = getLineReaderByNumber(linie);
              if (lr != null) {
                File datei = new File(dateiname);
                if (dateiname.endsWith(".ctl")) {
                  FileInputStream reader = null;
                  try {
                    reader = new FileInputStream(dateiname);
                    List<String> lines = IOUtils.readLines(reader, Charset.forName("UTF-8"));
                    if (lines.size() > 0) lr.addTreffer(new Hit(lines.get(0)));
                  } catch (IOException e) {
                    e.printStackTrace();
                  } finally {
                    if (reader != null) IOUtils.closeQuietly(reader);
                  }
                }
                if (!datei.delete()) {
                  System.out.println(dateiname + " konnte nicht gelöscht werden.");
                }
              }
              break;
            case "ENTRY_DELETE":
              // HServ<X>.ctl
              if (dateiname.startsWith("HServ") && dateiname.endsWith(".ctl")) {
                linie = Integer.parseUnsignedInt(dateiname.substring(5, dateiname.length() - 4));
                lr = getLineReaderByNumber(linie);
                if (lr != null) lr.reenable();
              }
              break;
            default: // OVERFLOW
              continue;
          }
        }
        key.reset();
      } catch (InterruptedException | ClosedWatchServiceException e) {
        if (running) {
          e.printStackTrace();
        }
      }
    }
  }
Пример #2
0
 private LineReader getLineReaderByNumber(int nummer) {
   for (LineReader lr : lineReader) {
     if (lr.getNummer() == nummer) return lr;
   }
   return null;
 }