Search in sources :

Example 51 with Binder

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

the class MetricsModuleTest method testSimpleInjection.

@Test
public void testSimpleInjection() {
    final 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));
        }
    }));
    final DataSourceTaskIdHolder dimensionIdHolder = new DataSourceTaskIdHolder();
    injector.injectMembers(dimensionIdHolder);
    Assert.assertNull(dimensionIdHolder.getDataSource());
    Assert.assertNull(dimensionIdHolder.getTaskId());
}
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 52 with Binder

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

the class GuiceInjectorsTest method testSanity.

@Test
public void testSanity() {
    Injector stageOne = GuiceInjectors.makeStartupInjector();
    Module module = stageOne.getInstance(DruidSecondaryModule.class);
    Injector stageTwo = Guice.createInjector(module, new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bind(String.class).toInstance("Expected String");
            JsonConfigProvider.bind(binder, "druid.emitter.", Emitter.class);
            binder.bind(CustomEmitter.class).toProvider(new CustomEmitterFactory());
        }
    });
    CustomEmitter customEmitter = stageTwo.getInstance(CustomEmitter.class);
    Assert.assertEquals("Expected String", customEmitter.getOtherValue());
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Test(org.junit.Test)

Example 53 with Binder

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

the class JettyServerModule method configureServlets.

@Override
protected void configureServlets() {
    Binder binder = binder();
    JsonConfigProvider.bind(binder, "druid.server.http", ServerConfig.class);
    binder.bind(GuiceContainer.class).to(DruidGuiceContainer.class);
    binder.bind(DruidGuiceContainer.class).in(Scopes.SINGLETON);
    binder.bind(CustomExceptionMapper.class).in(Singleton.class);
    serve("/*").with(DruidGuiceContainer.class);
    Jerseys.addResource(binder, StatusResource.class);
    binder.bind(StatusResource.class).in(LazySingleton.class);
    // Adding empty binding for ServletFilterHolders and Handlers so that injector returns an empty set if none
    // are provided by extensions.
    Multibinder.newSetBinder(binder, Handler.class);
    Multibinder.newSetBinder(binder, ServletFilterHolder.class);
    MetricsModule.register(binder, JettyMonitor.class);
}
Also used : Binder(com.google.inject.Binder) GuiceContainer(com.sun.jersey.guice.spi.container.servlet.GuiceContainer) StatusResource(io.druid.server.StatusResource)

Example 54 with Binder

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

the class ConditionalMultibindTest method testMultiConditionalBind_typeLiteral.

@Test
public void testMultiConditionalBind_typeLiteral() {
    props.setProperty("animal.type", "pets");
    final Set<Animal> set1 = ImmutableSet.<Animal>of(new Dog(), new Tiger());
    final Set<Animal> set2 = ImmutableSet.<Animal>of(new Cat(), new Fish());
    final Set<Animal> set3 = ImmutableSet.<Animal>of(new Cat());
    final Set<Animal> union = new HashSet<>();
    union.addAll(set1);
    union.addAll(set2);
    final Zoo<Animal> zoo1 = new Zoo<>(set1);
    final Zoo<Animal> zoo2 = new Zoo<>();
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, new TypeLiteral<Set<Animal>>() {
            }).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), set1).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), set2);
            ConditionalMultibind.create(props, binder, new TypeLiteral<Zoo<Animal>>() {
            }).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), zoo1);
        }
    }, new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, new TypeLiteral<Set<Animal>>() {
            }).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), set3);
            ConditionalMultibind.create(props, binder, new TypeLiteral<Set<Animal>>() {
            }, SanDiego.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), union);
            ConditionalMultibind.create(props, binder, new TypeLiteral<Zoo<Animal>>() {
            }).addBinding(new TypeLiteral<Zoo<Animal>>() {
            });
        }
    });
    Set<Set<Animal>> actualAnimalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Set<Animal>>>() {
    }));
    Assert.assertEquals(3, actualAnimalSet.size());
    Assert.assertEquals(ImmutableSet.of(set1, set2, set3), actualAnimalSet);
    actualAnimalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Set<Animal>>>() {
    }, SanDiego.class));
    Assert.assertEquals(1, actualAnimalSet.size());
    Assert.assertEquals(ImmutableSet.of(union), actualAnimalSet);
    final Set<Zoo<Animal>> actualZooSet = injector.getInstance(Key.get(new TypeLiteral<Set<Zoo<Animal>>>() {
    }));
    Assert.assertEquals(2, actualZooSet.size());
    Assert.assertEquals(ImmutableSet.of(zoo1, zoo2), actualZooSet);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 55 with Binder

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

the class ConditionalMultibindTest method testMultiConditionalBind_multiple_modules.

@Test
public void testMultiConditionalBind_multiple_modules() {
    props.setProperty("animal.type", "pets");
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
        }
    }, new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("not_match"), Tiger.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Fish.class);
        }
    });
    Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>() {
    }));
    Assert.assertEquals(3, animalSet.size());
    Assert.assertEquals(animalSet, ImmutableSet.<Animal>of(new Cat(), new Dog(), new Fish()));
}
Also used : Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Test(org.junit.Test)

Aggregations

Binder (com.google.inject.Binder)72 Module (com.google.inject.Module)65 Injector (com.google.inject.Injector)51 Test (org.junit.Test)41 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 Key (com.google.inject.Key)4 Provides (com.google.inject.Provides)4 LifecycleModule (io.druid.guice.LifecycleModule)4 HashSet (java.util.HashSet)4