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());
}
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());
}
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);
}
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);
}
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()));
}
Aggregations