public WebApp(String id, Set<String> owners, Set<String> hostnames, Set<String> uriContexts, AppMode mode, Router router, PojoDispatcher dispatcher, Classes classes, Config config) { this.id = id; this.router = U.or(router, new HttpRouter()); this.dispatcher = U.or(dispatcher, new WebPojoDispatcher(classes)); this.owners = U.safe(owners); this.hostnames = U.safe(hostnames); this.uriContexts = U.safe(uriContexts); this.mode = U.or(mode, AppMode.DEVELOPMENT); this.classes = U.or(classes, new Classes()); this.config = U.or(config, new Config()); }
private static synchronized List<Class<?>> scanClasses(String packageName, String nameRegex, Predicate<Class<?>> filter, Class<? extends Annotation> annotated, ClassLoader classLoader) { packageName = U.or(packageName, ""); List<?> cacheKey = null; WebApp app = Ctxs.ctx().app(); Classes appClasses = app.getClasses(); Map<List<?>, List<Class<?>>> cache = appClasses.getCache(); cacheKey = U.list(packageName, nameRegex, filter, annotated, classLoader); List<Class<?>> cachedClasses = cache.get(cacheKey); if (cachedClasses != null) { return cachedClasses; } long startingAt = U.time(); Log.info("Filtering " + appClasses.size() + " classes", "annotated", annotated, "package", packageName, "name", nameRegex); Pattern regex = nameRegex != null ? Pattern.compile(nameRegex) : null; List<Class<?>> classes = filterClasses(appClasses, packageName, regex, filter, annotated); cache.put(cacheKey, classes); long timeMs = U.time() - startingAt; Log.info("Finished classpath scan", "time", timeMs + "ms", "classes", classes); return classes; }
@SuppressWarnings("unchecked") public RootWebApp() { super("root", Collections.EMPTY_SET, Collections.EMPTY_SET, U.set("/"), Conf.dev() ? AppMode.DEVELOPMENT : AppMode.PRODUCTION, null, null, Classes.from(Scan.getClasses()), new Config(Conf.root().toMap())); }
private static List<Class<?>> filterClasses(Classes appClasses, String packageName, Pattern regex, Predicate<Class<?>> filter, Class<? extends Annotation> annotated) { List<Class<?>> matching = U.list(); for (Entry<String, Class<?>> e : appClasses.entrySet()) { Class<?> cls = e.getValue(); String pkg = cls.getPackage() != null ? cls.getPackage().getName() : ""; if (U.isEmpty(packageName) || pkg.startsWith(packageName + ".") || pkg.equals(packageName)) { if (ClasspathUtil.classMatches(cls, filter, annotated, regex)) { matching.add(cls); } } } return matching; }