Search in sources :

Example 11 with AbstractModule

use of com.google.inject.AbstractModule in project ninja by ninjaframework.

the class ControllerMethodInvokerTest method create.

private ControllerMethodInvoker create(String methodName, final Object... toBind) {
    Method method = null;
    for (Method m : MockController.class.getMethods()) {
        if (m.getName().equals(methodName)) {
            method = m;
            break;
        }
    }
    return ControllerMethodInvoker.build(method, method, Guice.createInjector(new AbstractModule() {

        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        protected void configure() {
            Multibinder<ParamParser> parsersBinder = Multibinder.newSetBinder(binder(), ParamParser.class);
            bind(NinjaProperties.class).toInstance(ninjaProperties);
            for (Object o : toBind) {
                if (o instanceof Class && ParamParser.class.isAssignableFrom((Class) o)) {
                    parsersBinder.addBinding().to((Class<? extends ParamParser>) o);
                } else {
                    bind((Class<Object>) o.getClass()).toInstance(o);
                }
            }
        }
    }), ninjaProperties);
}
Also used : NinjaProperties(ninja.utils.NinjaProperties) Method(java.lang.reflect.Method) AbstractModule(com.google.inject.AbstractModule)

Example 12 with AbstractModule

use of com.google.inject.AbstractModule in project ninja by ninjaframework.

the class ControllerMethodInvoker method instantiateComponent.

private static <T> T instantiateComponent(Class<? extends T> argumentExtractor, final Annotation annotation, final Class<?> paramType, Injector injector) {
    // Noarg constructor
    Constructor noarg = getNoArgConstructor(argumentExtractor);
    if (noarg != null) {
        try {
            return (T) noarg.newInstance();
        } catch (Exception e) {
            throw new RoutingException(e);
        }
    }
    // Simple case, just takes the annotation
    Constructor simple = getSingleArgConstructor(argumentExtractor, annotation.annotationType());
    if (simple != null) {
        try {
            return (T) simple.newInstance(annotation);
        } catch (Exception e) {
            throw new RoutingException(e);
        }
    }
    // Simple case, just takes the parsed class
    Constructor simpleClass = getSingleArgConstructor(argumentExtractor, Class.class);
    if (simpleClass != null) {
        try {
            return (T) simpleClass.newInstance(paramType);
        } catch (Exception e) {
            throw new RoutingException(e);
        }
    }
    // Complex case, use Guice.  Create a child injector with the annotation in it.
    return injector.createChildInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind((Class<Annotation>) annotation.annotationType()).toInstance(annotation);
            bind(ArgumentClassHolder.class).toInstance(new ArgumentClassHolder(paramType));
        }
    }).getInstance(argumentExtractor);
}
Also used : RoutingException(ninja.RoutingException) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) RoutingException(ninja.RoutingException) BadRequestException(ninja.exceptions.BadRequestException) AbstractModule(com.google.inject.AbstractModule)

Example 13 with AbstractModule

use of com.google.inject.AbstractModule in project ninja by ninjaframework.

the class BodyParserEnginePostTest method setUp.

@Before
public void setUp() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(NinjaProperties.class).toInstance(new NinjaPropertiesImpl(NinjaMode.test));
            Multibinder<ParamParser> parsersBinder = Multibinder.newSetBinder(binder(), ParamParser.class);
            parsersBinder.addBinding().to(NeedingInjectionParamParser.class);
        }
    });
    validation = new ValidationImpl();
    Mockito.when(this.context.getValidation()).thenReturn(this.validation);
    bodyParserEnginePost = injector.getInstance(BodyParserEnginePost.class);
}
Also used : NinjaPropertiesImpl(ninja.utils.NinjaPropertiesImpl) ParamParser(ninja.params.ParamParser) NeedingInjectionParamParser(ninja.params.ControllerMethodInvokerTest.NeedingInjectionParamParser) Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) NeedingInjectionParamParser(ninja.params.ControllerMethodInvokerTest.NeedingInjectionParamParser) AbstractModule(com.google.inject.AbstractModule) ValidationImpl(ninja.validation.ValidationImpl) Before(org.junit.Before)

Example 14 with AbstractModule

use of com.google.inject.AbstractModule in project ninja by ninjaframework.

the class LifecycleSupportTest method serviceShouldBeStartedIfExplicitlyBoundAsSingleton.

@Test
public void serviceShouldBeStartedIfExplicitlyBoundAsSingleton() {
    Injector injector = createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(MockService.class).toInstance(new MockService());
        }
    });
    start(injector);
    assertThat(MockService.started, equalTo(1));
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 15 with AbstractModule

use of com.google.inject.AbstractModule in project ninja by ninjaframework.

the class LifecycleSupportTest method serviceShouldNotBeStartedIfExplicitlyBoundAndNotSingleton.

@Test
public void serviceShouldNotBeStartedIfExplicitlyBoundAndNotSingleton() {
    Injector injector = createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(MockService.class);
        }
    });
    start(injector);
    assertThat(MockService.started, equalTo(0));
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Aggregations

AbstractModule (com.google.inject.AbstractModule)840 Injector (com.google.inject.Injector)522 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 TypeLiteral (com.google.inject.TypeLiteral)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35