Example #1
0
  /**
   * collect saved Reflections resources from all urls that contains the given packagePrefix and
   * matches the given resourceNameFilter and de-serializes them using the serializer configured in
   * the configuration
   *
   * <p>it is preferred to use a designated resource prefix (for example META-INF/reflections but
   * not just META-INF), so that relevant urls could be found much faster
   */
  public Reflections collect(
      final String packagePrefix,
      final Predicate<String> resourceNameFilter,
      final Serializer serializer) {
    for (final Vfs.File file :
        Vfs.findFiles(
            ClasspathHelper.forPackage(packagePrefix), packagePrefix, resourceNameFilter)) {
      InputStream inputStream = null;
      try {
        inputStream = file.openInputStream();
        merge(serializer.read(inputStream));
        log.info(
            "Reflections collected metadata from "
                + file
                + " using serializer "
                + serializer.getClass().getName());
      } catch (IOException e) {
        throw new ReflectionsException("could not merge " + file, e);
      } finally {
        Utils.close(inputStream);
      }
    }

    return this;
  }
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())
                : ""));
  }