use of com.google.inject.Binding in project gerrit by GerritCodeReview.
the class SiteProgram method getDbType.
private String getDbType(Provider<DataSource> dsProvider) {
String dbProductName;
try (Connection conn = dsProvider.get().getConnection()) {
dbProductName = conn.getMetaData().getDatabaseProductName().toLowerCase();
} catch (SQLException e) {
throw new RuntimeException(e);
}
List<Module> modules = new ArrayList<>();
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
}
});
modules.add(new GerritServerConfigModule());
modules.add(new DataSourceModule());
Injector i = Guice.createInjector(modules);
List<Binding<DataSourceType>> dsTypeBindings = i.findBindingsByType(new TypeLiteral<DataSourceType>() {
});
for (Binding<DataSourceType> binding : dsTypeBindings) {
Annotation annotation = binding.getKey().getAnnotation();
if (annotation instanceof Named) {
if (((Named) annotation).value().toLowerCase().contains(dbProductName)) {
return ((Named) annotation).value();
}
}
}
throw new IllegalStateException(String.format("Cannot guess database type from the database product name '%s'", dbProductName));
}
use of com.google.inject.Binding in project xtext-core by eclipse.
the class WrappingInjectorProvider method createInjector.
protected Injector createInjector() {
Injector delegateInjector = delegate.getInjector();
final Map<Key<?>, Binding<?>> bindings = delegateInjector.getBindings();
Injector injector = Guice.createInjector(Modules.override(new Module() {
@Override
public void configure(Binder binder) {
for (Binding<?> binding : bindings.values()) {
Type typeLiteral = binding.getKey().getTypeLiteral().getType();
if (!Injector.class.equals(typeLiteral) && !Logger.class.equals(typeLiteral)) {
binding.applyTo(binder);
}
}
}
}).with(new Module() {
@Override
public void configure(Binder binder) {
binder.bind(IParser.class).toInstance(new TestDataProvider());
}
}));
return injector;
}
use of com.google.inject.Binding in project camel by apache.
the class JndiBindings method bindInjectorAndBindings.
/**
* Binds the given injector and its binding providers to the given JNDI
* context using <a
* href="http://code.google.com/p/camel-extra/wiki/GuiceJndi">this mapping
* mechanism</a>.
* <p/>
* This will expose all of the bindings providers to JNDI along with any
* bindings which are annotated with {@link JndiBind} or {@link @Named} to
* the given JNDI context.
*
* @param context
* the context to export objects to
* @param injector
* the injector used to find the bindings
*/
public static void bindInjectorAndBindings(Context context, Injector injector, Properties jndiNames) throws NamingException {
// lets find all the exported bindings
Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
for (Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Binding<?> binding = entry.getValue();
Annotation annotation = key.getAnnotation();
Type type = key.getTypeLiteral().getType();
JndiBind jndiBind = null;
if (type instanceof Class) {
Class<?> aClass = (Class<?>) type;
jndiBind = aClass.getAnnotation(JndiBind.class);
}
if (annotation instanceof JndiBind) {
jndiBind = (JndiBind) annotation;
}
String jndiName = null;
if (jndiBind != null) {
jndiName = jndiBind.value();
}
if (jndiName == null) {
if (annotation instanceof Named) {
Named named = (Named) annotation;
String name = named.value();
jndiName = type.toString() + "/" + name;
} else if (type instanceof Class<?>) {
Class<?> aClass = (Class<?>) type;
if (annotation == null) {
jndiName = aClass.getName();
} else {
jndiName = aClass.getName() + annotation;
}
}
}
if (jndiName != null) {
Object value = binding.getProvider();
if (value != null) {
context.bind(jndiName, value);
}
}
}
for (Entry<Object, Object> entry : jndiNames.entrySet()) {
String jndiName = entry.getKey().toString();
String expression = entry.getValue().toString();
Provider<?> provider = getProviderForExpression(injector, expression);
if (provider != null) {
context.bind(jndiName, provider);
}
}
}
use of com.google.inject.Binding in project camel by apache.
the class Injectors method close.
/**
* Closes objects within the given scope using the currently registered
* {@link Closer} implementations
*/
public static void close(Injector injector, Class<? extends Annotation> scopeAnnotationToClose, CloseErrors errors) throws CloseFailedException {
Set<Closer> closers = getInstancesOf(injector, Closer.class);
Closer closer = CompositeCloser.newInstance(closers);
if (closer == null) {
return;
}
Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
for (Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Binding<?> binding = entry.getValue();
closeBinding(key, binding, scopeAnnotationToClose, closer, errors);
}
tryCloseJitBindings(closer, injector, scopeAnnotationToClose, errors);
errors.throwIfNecessary();
}
use of com.google.inject.Binding in project che by eclipse.
the class ConfigurationProperties method getProperties.
public Map<String, String> getProperties(String namePattern) {
final Pattern pattern = Pattern.compile(namePattern);
final Map<String, String> result = new HashMap<>();
for (Map.Entry<Key<?>, Binding<?>> keyBindingEntry : injectorProvider.get().getAllBindings().entrySet()) {
final Key<?> key = keyBindingEntry.getKey();
final Annotation annotation = key.getAnnotation();
if (annotation instanceof com.google.inject.name.Named && key.getTypeLiteral().getRawType() == String.class) {
final String name = ((com.google.inject.name.Named) annotation).value();
if (name != null && pattern.matcher(name).matches()) {
final String value = (String) keyBindingEntry.getValue().getProvider().get();
result.put(name, value);
}
}
}
return result;
}
Aggregations