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