Search in sources :

Example 61 with Binder

use of com.google.inject.Binder in project druid by druid-io.

the class CacheMonitorTest method testInject.

@Test
public void testInject() throws Exception {
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {

        @Override
        public void configure(Binder binder) {
            JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
            binder.bind(Cache.class).toInstance(MapCache.create(0));
        }
    }));
    CacheMonitor monitor = injector.getInstance(CacheMonitor.class);
    Assert.assertNotNull(monitor.cache);
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) DruidNode(io.druid.server.DruidNode) Self(io.druid.guice.annotations.Self) Module(com.google.inject.Module) Test(org.junit.Test)

Example 62 with Binder

use of com.google.inject.Binder in project druid by druid-io.

the class HybridCacheTest method testInjection.

@Test
public void testInjection() throws Exception {
    final String prefix = "testInjectHybridCache";
    System.setProperty(prefix + ".type", "hybrid");
    System.setProperty(prefix + ".l1.type", "local");
    System.setProperty(prefix + ".l2.type", "memcached");
    System.setProperty(prefix + ".l2.hosts", "localhost:11711");
    final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bindConstant().annotatedWith(Names.named("serviceName")).to("hybridTest");
            binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
            binder.install(new CacheModule(prefix));
        }
    }));
    final CacheProvider cacheProvider = injector.getInstance(Key.get(CacheProvider.class, Global.class));
    Assert.assertNotNull(cacheProvider);
    Assert.assertEquals(HybridCacheProvider.class, cacheProvider.getClass());
    final Cache cache = cacheProvider.get();
    Assert.assertNotNull(cache);
    Assert.assertFalse(cache.isLocal());
    Assert.assertEquals(LocalCacheProvider.class, ((HybridCacheProvider) cacheProvider).level1.getClass());
    Assert.assertEquals(MemcachedCacheProvider.class, ((HybridCacheProvider) cacheProvider).level2.getClass());
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) CacheModule(io.druid.guice.CacheModule) Module(com.google.inject.Module) CacheModule(io.druid.guice.CacheModule) Global(io.druid.guice.annotations.Global) Test(org.junit.Test)

Example 63 with Binder

use of com.google.inject.Binder in project druid by druid-io.

the class InitializationTest method test05MakeInjectorWithModules.

@Test
public void test05MakeInjectorWithModules() throws Exception {
    Injector startupInjector = GuiceInjectors.makeStartupInjector();
    Injector injector = Initialization.makeInjectorWithModules(startupInjector, ImmutableList.<com.google.inject.Module>of(new com.google.inject.Module() {

        @Override
        public void configure(Binder binder) {
            JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
        }
    }));
    Assert.assertNotNull(injector);
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) DruidNode(io.druid.server.DruidNode) Self(io.druid.guice.annotations.Self) Module(com.fasterxml.jackson.databind.Module) Test(org.junit.Test)

Example 64 with Binder

use of com.google.inject.Binder in project guice by google.

the class FactoryProvider2 method getBindingFromNewInjector.

/**
   * Creates a child injector that binds the args, and returns the binding for the method's result.
   */
public Binding<?> getBindingFromNewInjector(final Method method, final Object[] args, final AssistData data) {
    checkState(injector != null, "Factories.create() factories cannot be used until they're initialized by Guice.");
    final Key<?> returnType = data.returnType;
    // We ignore any pre-existing binding annotation.
    final Key<?> returnKey = Key.get(returnType.getTypeLiteral(), RETURN_ANNOTATION);
    Module assistedModule = new AbstractModule() {

        @Override
        @SuppressWarnings({ "unchecked", "rawtypes" })
        protected // raw keys are necessary for the args array and return value
        void configure() {
            Binder binder = binder().withSource(method);
            int p = 0;
            if (!data.optimized) {
                for (Key<?> paramKey : data.paramTypes) {
                    // Wrap in a Provider to cover null, and to prevent Guice from injecting the parameter
                    binder.bind((Key) paramKey).toProvider(Providers.of(args[p++]));
                }
            } else {
                for (Key<?> paramKey : data.paramTypes) {
                    // Bind to our ThreadLocalProviders.
                    binder.bind((Key) paramKey).toProvider(data.providers.get(p++));
                }
            }
            Constructor constructor = data.constructor;
            // message for the user.
            if (constructor != null) {
                binder.bind(returnKey).toConstructor(constructor, (TypeLiteral) data.implementationType).in(// make sure we erase any scope on the implementation type
                Scopes.NO_SCOPE);
            }
        }
    };
    Injector forCreate = injector.createChildInjector(assistedModule);
    Binding<?> binding = forCreate.getBinding(returnKey);
    // If we have providers cached in data, cache the binding for future optimizations.
    if (data.optimized) {
        data.cachedBinding = binding;
    }
    return binding;
}
Also used : Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Constructor(java.lang.reflect.Constructor) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) InjectionPoint(com.google.inject.spi.InjectionPoint) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 65 with Binder

use of com.google.inject.Binder in project hadoop by apache.

the class TestNMAppsPage method testNMAppsPage.

@Test
public void testNMAppsPage() {
    Configuration conf = new Configuration();
    final NMContext nmcontext = new NMContext(new NMContainerTokenSecretManager(conf), new NMTokenSecretManagerInNM(), null, new ApplicationACLsManager(conf), new NMNullStateStoreService(), false, conf);
    Injector injector = WebAppTests.createMockInjector(NMContext.class, nmcontext, new Module() {

        @Override
        public void configure(Binder binder) {
            NodeManager nm = TestNMAppsPage.mocknm(nmcontext);
            binder.bind(NodeManager.class).toInstance(nm);
            binder.bind(Context.class).toInstance(nmcontext);
        }
    });
    ApplicationBlock instance = injector.getInstance(ApplicationBlock.class);
    instance.set(YarnWebParams.APPLICATION_ID, applicationid);
    instance.render();
}
Also used : Binder(com.google.inject.Binder) NodeManager(org.apache.hadoop.yarn.server.nodemanager.NodeManager) ApplicationACLsManager(org.apache.hadoop.yarn.server.security.ApplicationACLsManager) Configuration(org.apache.hadoop.conf.Configuration) NMContext(org.apache.hadoop.yarn.server.nodemanager.NodeManager.NMContext) Injector(com.google.inject.Injector) NMContainerTokenSecretManager(org.apache.hadoop.yarn.server.nodemanager.security.NMContainerTokenSecretManager) NMTokenSecretManagerInNM(org.apache.hadoop.yarn.server.nodemanager.security.NMTokenSecretManagerInNM) Module(com.google.inject.Module) NMNullStateStoreService(org.apache.hadoop.yarn.server.nodemanager.recovery.NMNullStateStoreService) ApplicationBlock(org.apache.hadoop.yarn.server.nodemanager.webapp.ApplicationPage.ApplicationBlock) Test(org.junit.Test)

Aggregations

Binder (com.google.inject.Binder)67 Module (com.google.inject.Module)64 Injector (com.google.inject.Injector)49 Test (org.junit.Test)40 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 Self (io.druid.guice.annotations.Self)12 DruidNode (io.druid.server.DruidNode)12 IOException (java.io.IOException)10 TypeLiteral (com.google.inject.TypeLiteral)9 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)9 Properties (java.util.Properties)8 RMContext (org.apache.hadoop.yarn.server.resourcemanager.RMContext)7 ResourceManager (org.apache.hadoop.yarn.server.resourcemanager.ResourceManager)7 ImmutableList (com.google.common.collect.ImmutableList)5 Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)5 List (java.util.List)5 Provides (com.google.inject.Provides)4 LifecycleModule (io.druid.guice.LifecycleModule)4 HashSet (java.util.HashSet)4 AnnotationIntrospectorPair (com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair)3