Search in sources :

Example 51 with Binding

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));
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) Binding(com.google.inject.Binding) Named(com.google.inject.name.Named) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) Injector(com.google.inject.Injector) DataSourceType(com.google.gerrit.server.schema.DataSourceType) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) Module(com.google.inject.Module) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule) DatabaseModule(com.google.gerrit.server.schema.DatabaseModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SchemaModule(com.google.gerrit.server.schema.SchemaModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) AbstractModule(com.google.inject.AbstractModule) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule)

Example 52 with Binding

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;
}
Also used : Binding(com.google.inject.Binding) Binder(com.google.inject.Binder) Type(java.lang.reflect.Type) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Logger(java.util.logging.Logger) Key(com.google.inject.Key) IParser(org.eclipse.xtext.parser.IParser)

Example 53 with Binding

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);
        }
    }
}
Also used : Binding(com.google.inject.Binding) Named(com.google.inject.name.Named) Annotation(java.lang.annotation.Annotation) Entry(java.util.Map.Entry) Type(java.lang.reflect.Type) Key(com.google.inject.Key)

Example 54 with Binding

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();
}
Also used : Closer(org.apache.camel.guice.support.Closer) CompositeCloser(org.apache.camel.guice.support.CompositeCloser) Binding(com.google.inject.Binding) Entry(java.util.Map.Entry) Key(com.google.inject.Key)

Example 55 with Binding

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;
}
Also used : Binding(com.google.inject.Binding) Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.google.inject.Key)

Aggregations

Binding (com.google.inject.Binding)92 Injector (com.google.inject.Injector)58 Key (com.google.inject.Key)36 AbstractModule (com.google.inject.AbstractModule)33 InstanceBinding (com.google.inject.spi.InstanceBinding)23 Map (java.util.Map)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Module (com.google.inject.Module)18 Element (com.google.inject.spi.Element)18 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)17 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)16 HttpServlet (javax.servlet.http.HttpServlet)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)12 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)12 ServletContext (javax.servlet.ServletContext)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 TypeLiteral (com.google.inject.TypeLiteral)10 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)10