private void loadExtensionsFromClassloaders( Map<String, DefaultCoreExtension> extensions, DefaultCoreExtensionRepository repository) { Set<URL> mavenURLs = ClasspathHelper.forPackage(MAVENPACKAGE); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setScanners(new ResourcesScanner()); configurationBuilder.setUrls(mavenURLs); configurationBuilder.filterInputsBy( new FilterBuilder.Include(FilterBuilder.prefix(MAVENPACKAGE))); Reflections reflections = new Reflections(configurationBuilder); Set<String> descriptors = reflections.getResources(Predicates.equalTo("pom.xml")); for (String descriptor : descriptors) { URL descriptorUrl = getClass().getClassLoader().getResource(descriptor); try { DefaultCoreExtension coreExtension = parseMavenPom(descriptorUrl, repository); extensions.put(coreExtension.getId().getId(), coreExtension); } catch (Exception e) { this.logger.warn("Failed to pase extension descriptor [{}]", descriptorUrl, e); } } // Try to find more guess(extensions, repository); }
public ClassDiscoveryImpl(String pkg) { this.pkg = pkg; configuration = new ConfigurationBuilder(); final Predicate<String> filter = new FilterBuilder.Include(FilterBuilder.prefix(pkg)); configuration.setUrls(ClasspathHelper.forPackage(pkg)); configuration.filterInputsBy(filter); configuration.setScanners(new TypeAnnotationsScanner().filterResultsBy(filter)); store = new Store(configuration); for (Scanner scanner : configuration.getScanners()) { scanner.setConfiguration(configuration); scanner.setStore(store.get(scanner)); } index(); }
@Override public void beforeStart(final Application application) { final Reflections reflections = new Reflections( new ConfigurationBuilder() .filterInputsBy(new FilterBuilder.Exclude(FilterBuilder.prefix("com.google"))) .addUrls(ClasspathHelper.forClassLoader(application.classloader())) .addScanners(new SubTypesScanner())); // automatic Guice module detection Set<Class<? extends AbstractModule>> guiceModules = reflections.getSubTypesOf(AbstractModule.class); for (Class<? extends Module> moduleClass : guiceModules) { try { if (!moduleClass.isAnonymousClass()) { modules.add(moduleClass.newInstance()); } } catch (InstantiationException e) { throw Throwables.propagate(e); } catch (IllegalAccessException e) { throw Throwables.propagate(e); } } modules.add( new AbstractModule() { @Override protected void configure() { bind(Application.class).toInstance(application); bind(Reflections.class).toInstance(reflections); Names.bindProperties( this.binder(), fromKeys( application.configuration().keys(), new Function<String, String>() { @Override public String apply(String key) { // remove after https://play.lighthouseapp.com/projects/82401/tickets/372 is // fixed if (key.contains("akka")) return null; return application.configuration().getString(key); } })); for (Class<? extends Controller> controllerClass : reflections.getSubTypesOf(Controller.class)) { requestStaticInjection(controllerClass); } // bind all services Multibinder<Service> serviceBinder = Multibinder.newSetBinder(binder(), Service.class); for (Class<? extends Service> serviceImplClass : reflections.getSubTypesOf(AbstractService.class)) { serviceBinder.addBinding().to(serviceImplClass).asEagerSingleton(); } for (Class<? extends Service> serviceImplClass : reflections.getSubTypesOf(AbstractIdleService.class)) { serviceBinder.addBinding().to(serviceImplClass).asEagerSingleton(); } for (Class<? extends Service> serviceImplClass : reflections.getSubTypesOf(AbstractExecutionThreadService.class)) { serviceBinder.addBinding().to(serviceImplClass).asEagerSingleton(); } // bind actor - todo use reflections for this // start/stop services after injection and on shutdown of the Play app bindListener( MoreMatchers.subclassesOf(Service.class), new TypeListener() { @Override public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { typeEncounter.register( new InjectionListener<I>() { @Override public void afterInjection(final I i) { onStartListeners.add( new OnStartListener() { @Override public void onApplicationStart( Application application, Injector injector) { Logger.info(String.format("Starting %s", i.toString())); ((Service) i).start(); onStopListeners.add( new OnStopListener() { @Override public void onApplicationStop(Application application) { Logger.info(String.format("Stopping %s", i.toString())); ((Service) i).stop(); } }); } }); } }); } }); } }); }