use of com.google.inject.Provider in project guice by google.
the class MultibinderTest method testMultibindingProviderDependencies.
public void testMultibindingProviderDependencies() {
final Annotation setAnn = Names.named("foo");
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
multibinder.addBinding().toInstance("a");
multibinder.addBinding().toInstance("b");
}
});
HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
});
HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
});
// sanity check the size
assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
Set<Dependency<?>> expected = Sets.newHashSet();
for (Dependency<?> dep : setBinding.getDependencies()) {
Key key = dep.getKey();
Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
expected.add(providerDependency);
}
assertEquals(expected, providerBinding.getDependencies());
}
use of com.google.inject.Provider in project platformlayer by platformlayer.
the class GuiceDataSourceProvider method bindJndi.
public static Provider<? extends DataSource> bindJndi(final String key) {
Provider<DataSource> provider = new Provider<DataSource>() {
@Override
public DataSource get() {
try {
InitialContext context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup(key);
return dataSource;
} catch (NamingException e) {
throw new IllegalStateException("Error getting JDNI connection: " + key, e);
}
}
};
return provider;
}
use of com.google.inject.Provider in project honeycomb by altamiracorp.
the class StoreFactoryTest method createFactory.
private StoreFactory createFactory() {
HoneycombConfiguration configurationHolder = new HoneycombConfiguration(adapterConfigs, "hbase");
Map<AdapterType, Provider<Store>> map = Maps.newHashMap();
map.put(AdapterType.HBASE, storeProvider);
when(storeProvider.get()).thenReturn(store);
return new StoreFactory(map, configurationHolder);
}
use of com.google.inject.Provider in project camel by apache.
the class JndiContext method lookup.
public Object lookup(String name) throws NamingException {
if (name.length() == 0) {
return this;
}
Object result = treeBindings.get(name);
if (result == null) {
result = bindings.get(name);
}
if (result == null) {
int pos = name.indexOf(':');
if (pos > 0) {
String scheme = name.substring(0, pos);
Context ctx = NamingManager.getURLContext(scheme, environment);
if (ctx == null) {
throw new NamingException("scheme " + scheme + " not recognized");
}
return ctx.lookup(name);
} else {
// Split out the first name of the path
// and look for it in the bindings map.
CompositeName path = new CompositeName(name);
if (path.size() == 0) {
return this;
} else {
String first = path.get(0);
Object value = bindings.get(first);
if (value == null) {
throw new NameNotFoundException(name);
} else if (value instanceof Context && path.size() > 1) {
Context subContext = (Context) value;
value = subContext.lookup(path.getSuffix(1));
}
return value;
}
}
}
if (result instanceof Provider) {
Provider provider = (Provider) result;
result = provider.get();
}
if (result instanceof LinkRef) {
LinkRef ref = (LinkRef) result;
result = lookup(ref.getLinkName());
}
if (result instanceof Reference) {
try {
result = NamingManager.getObjectInstance(result, null, null, this.environment);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
}
}
if (result instanceof JndiContext) {
String prefix = getNameInNamespace();
if (prefix.length() > 0) {
prefix = prefix + SEPARATOR;
}
result = new JndiContext((JndiContext) result, environment, prefix + name);
}
return result;
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class SpiBindingsTest method testExtensionSpi.
public void testExtensionSpi() {
final AtomicBoolean visiting = new AtomicBoolean(false);
final Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(String.class).toProvider(new ProviderWithExtensionVisitor<String>() {
public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) {
assertSame(this, binding.getUserSuppliedProvider());
// to process the binding as normal
if (visiting.get()) {
assertTrue("visitor: " + visitor, visitor instanceof FailingSpiTargetVisitor);
return (V) "visited";
} else {
return visitor.visit(binding);
}
}
public String get() {
return "FooBar";
}
});
}
});
visiting.set(true);
// Check for Provider<String> binding -- that is still a ProviderBinding.
Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
};
Binding<Provider<String>> providerBinding = injector.getBinding(providerOfStringKey);
assertEquals(providerOfStringKey, providerBinding.getKey());
checkBindingSource(providerBinding);
assertTrue("binding: " + providerBinding, providerBinding instanceof ProviderBinding);
providerBinding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {
@Override
public Void visit(ProviderBinding<? extends Provider<String>> binding) {
assertEquals(Key.get(String.class), binding.getProvidedKey());
return null;
}
});
// Check for String binding -- that one is ProviderInstanceBinding, and gets hooked
Binding<String> binding = injector.getBinding(String.class);
assertEquals(Key.get(String.class), binding.getKey());
checkBindingSource(binding);
assertTrue(binding instanceof ProviderInstanceBinding);
assertEquals("visited", binding.acceptTargetVisitor(new FailingSpiTargetVisitor<String>()));
}
Aggregations