Example #1
0
  protected void scan() {
    if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
      if (log != null) log.warn("given scan urls are empty. set urls in the configuration");
      return;
    }

    if (log != null && log.isDebugEnabled()) {
      log.debug("going to scan these urls:\n" + Joiner.on("\n").join(configuration.getUrls()));
    }

    long time = System.currentTimeMillis();
    int scannedUrls = 0;
    ExecutorService executorService = configuration.getExecutorService();
    List<Future<?>> futures = Lists.newArrayList();

    for (final URL url : configuration.getUrls()) {
      try {
        if (executorService != null) {
          futures.add(
              executorService.submit(
                  new Runnable() {
                    public void run() {
                      if (log != null && log.isDebugEnabled())
                        log.debug("[" + Thread.currentThread().toString() + "] scanning " + url);
                      scan(url);
                    }
                  }));
        } else {
          scan(url);
        }
        scannedUrls++;
      } catch (ReflectionsException e) {
        if (log != null && log.isWarnEnabled())
          log.warn("could not create Vfs.Dir from url. ignoring the exception and continuing", e);
      }
    }

    // todo use CompletionService
    if (executorService != null) {
      for (Future future : futures) {
        try {
          future.get();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }

    time = System.currentTimeMillis() - time;

    if (log != null) {
      int keys = 0;
      int values = 0;
      for (String index : store.keySet()) {
        keys += store.get(index).keySet().size();
        values += store.get(index).size();
      }

      log.info(
          format(
              "Reflections took %d ms to scan %d urls, producing %d keys and %d values %s",
              time,
              scannedUrls,
              keys,
              values,
              executorService != null && executorService instanceof ThreadPoolExecutor
                  ? format(
                      "[using %d cores]",
                      ((ThreadPoolExecutor) executorService).getMaximumPoolSize())
                  : ""));
    }
  }
Example #2
0
  protected void scan() {
    if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
      //  log.error("given scan urls are empty. set urls in the configuration");
      return;
    } else {
      if (log.isDebugEnabled()) {
        StringBuilder urls = new StringBuilder();
        for (URL url : configuration.getUrls()) {
          urls.append("\t").append(url.toExternalForm()).append("\n");
        }
        log.debug("going to scan these urls:\n" + urls);
      }
    }

    long time = System.currentTimeMillis();

    ExecutorService executorService = configuration.getExecutorService();

    if (executorService == null) {
      for (URL url : configuration.getUrls()) {
        try {
          for (final Vfs.File file : Vfs.fromURL(url).getFiles()) {
            scan(file);
          }
        } catch (ReflectionsException e) {
          log.error("could not create Vfs.Dir from url. ignoring the exception and continuing", e);
        }
      }
    } else {
      // todo use CompletionService
      List<Future<?>> futures = Lists.newArrayList();
      try {
        for (URL url : configuration.getUrls()) {
          try {
            for (final Vfs.File file : Vfs.fromURL(url).getFiles()) {
              futures.add(
                  executorService.submit(
                      new Runnable() {
                        public void run() {
                          scan(file);
                        }
                      }));
            }
          } catch (ReflectionsException e) {
            log.error(
                "could not create Vfs.Dir from url. ignoring the exception and continuing", e);
          }
        }

        for (Future future : futures) {
          try {
            future.get();
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
      } finally {
        executorService.shutdown();
      }
    }

    time = System.currentTimeMillis() - time;

    Integer keys = store.getKeysCount();
    Integer values = store.getValuesCount();

    log.info(
        format(
            "Reflections took %d ms to scan %d urls, producing %d keys and %d values %s",
            time,
            configuration.getUrls().size(),
            keys,
            values,
            executorService != null && executorService instanceof ThreadPoolExecutor
                ? format(
                    "[using %d cores]", ((ThreadPoolExecutor) executorService).getMaximumPoolSize())
                : ""));
  }