Search in sources :

Example 1 with EagerSingleton

use of com.peterphi.std.guice.common.eagersingleton.annotations.EagerSingleton in project stdlib by petergeneric.

the class BindEagerSingletonRole method register.

@Override
public void register(final Stage stage, final ClassScannerFactory scannerFactory, final GuiceConfig config, final GuiceSetup setup, final List<Module> modules, final AtomicReference<Injector> injectorRef, final MetricRegistry metrics) {
    final ClassScanner scanner = scannerFactory.getInstance();
    if (scanner != null) {
        final List<Class<?>> classes = scanner.getAnnotatedClasses(EagerSingleton.class);
        final boolean isTestEnvironment = config.getBoolean(GuiceProperties.UNIT_TEST, false);
        Iterator<Class<?>> it = classes.iterator();
        while (it.hasNext()) {
            final Class<?> clazz = it.next();
            if (isTestEnvironment) {
                final EagerSingleton annotation = clazz.getAnnotation(EagerSingleton.class);
                // We're in a test environment but inTests isn't true, so we should ignore this annotation
                if (!annotation.inTests()) {
                    log.trace("Ignoring eager singleton with inTests=false: " + clazz);
                    it.remove();
                    continue;
                }
            }
            log.trace("Binding eager singleton: " + clazz);
        }
        if (!classes.isEmpty())
            modules.add(new BindEagerSingletonModule(classes));
    }
}
Also used : EagerSingleton(com.peterphi.std.guice.common.eagersingleton.annotations.EagerSingleton) ClassScanner(com.peterphi.std.guice.common.ClassScanner)

Aggregations

ClassScanner (com.peterphi.std.guice.common.ClassScanner)1 EagerSingleton (com.peterphi.std.guice.common.eagersingleton.annotations.EagerSingleton)1