Search in sources :

Example 1 with NinjaBaseModule

use of ninja.conf.NinjaBaseModule in project ninja by ninjaframework.

the class Bootstrap method configure.

protected void configure() throws Exception {
    // Base configuration of Ninja
    addModule(new NinjaBaseModule(ninjaProperties));
    // Main application module (conf.Module or com.example.conf.Module)
    String applicationModuleClassName = ninjaBaseDirectoryResolver.resolveApplicationClassName(APPLICATION_GUICE_MODULE_CONVENTION_LOCATION);
    AbstractModule applicationModule = null;
    if (SwissKnife.doesClassExist(applicationModuleClassName, this)) {
        Class<?> applicationModuleClass = Class.forName(applicationModuleClassName);
        // Tries to instantiate module by giving the NinjaProperties as constructor arg
        try {
            applicationModule = (AbstractModule) applicationModuleClass.getConstructor(NinjaProperties.class).newInstance(ninjaProperties);
        } catch (NoSuchMethodException e) {
            applicationModule = (AbstractModule) applicationModuleClass.getConstructor().newInstance();
        }
    }
    // Slipstream in the "classic" ninja configuration?
    if (applicationModule == null || !(applicationModule instanceof FrameworkModule)) {
        // Classic configuration of Ninja
        logger.info("Enabling Ninja classic configuration");
        addModule(new NinjaClassicModule(ninjaProperties));
    }
    if (applicationModule != null) {
        addModule(applicationModule);
    }
    // Ninja module
    String applicationNinjaClassName = ninjaBaseDirectoryResolver.resolveApplicationClassName(NINJA_CONVENTION_LOCATION);
    final Class<? extends Ninja> ninjaClass;
    if (SwissKnife.doesClassExist(applicationNinjaClassName, this)) {
        final Class<?> clazzPotentially = Class.forName(applicationNinjaClassName);
        if (Ninja.class.isAssignableFrom(clazzPotentially)) {
            ninjaClass = (Class<? extends Ninja>) clazzPotentially;
        } else {
            final String ERROR_MESSAGE = String.format("Found a class %s in your application's conf directory." + " This class does not implement Ninja interface %s. " + " Please implement the interface or remove the class.", applicationNinjaClassName, Ninja.class.getName());
            logger.error(ERROR_MESSAGE);
            throw new IllegalStateException(ERROR_MESSAGE);
        }
    } else {
        ninjaClass = NinjaDefault.class;
    }
    addModule(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Ninja.class).to(ninjaClass).in(Singleton.class);
        }
    });
}
Also used : NinjaProperties(ninja.utils.NinjaProperties) FrameworkModule(ninja.conf.FrameworkModule) AbstractModule(com.google.inject.AbstractModule) NinjaClassicModule(ninja.conf.NinjaClassicModule) Singleton(com.google.inject.Singleton) NinjaBaseModule(ninja.conf.NinjaBaseModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)1 Singleton (com.google.inject.Singleton)1 FrameworkModule (ninja.conf.FrameworkModule)1 NinjaBaseModule (ninja.conf.NinjaBaseModule)1 NinjaClassicModule (ninja.conf.NinjaClassicModule)1 NinjaProperties (ninja.utils.NinjaProperties)1