/** * collect saved Reflections resources from all urls that contains the given packagePrefix and * matches the given resourceNameFilter and de-serializes them using the default serializer {@link * XmlSerializer} or using the optionally supplied optionalSerializer * * <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 * * @param optionalSerializer - optionally supply one serializer instance. if not specified or * null, {@link XmlSerializer} will be used */ public static Reflections collect( final String packagePrefix, final Predicate<String> resourceNameFilter, @Nullable Serializer... optionalSerializer) { Serializer serializer = optionalSerializer != null && optionalSerializer.length == 1 ? optionalSerializer[0] : new XmlSerializer(); Collection<URL> urls = ClasspathHelper.forPackage(packagePrefix); if (urls.isEmpty()) return null; long start = System.currentTimeMillis(); final Reflections reflections = new Reflections(); Iterable<Vfs.File> files = Vfs.findFiles(urls, packagePrefix, resourceNameFilter); for (final Vfs.File file : files) { InputStream inputStream = null; try { inputStream = file.openInputStream(); reflections.merge(serializer.read(inputStream)); } catch (IOException e) { throw new ReflectionsException("could not merge " + file, e); } finally { close(inputStream); } } if (log != null) { Store store = reflections.getStore(); 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 collect %d url%s, producing %d keys and %d values [%s]", System.currentTimeMillis() - start, urls.size(), urls.size() > 1 ? "s" : "", keys, values, Joiner.on(", ").join(urls))); } return reflections; }
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; // gracefully shutdown the parallel scanner executor service. if (executorService != null) { executorService.shutdown(); } 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()) : "")); } }