Search in sources :

Example 41 with Provider

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());
}
Also used : Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) Dependency(com.google.inject.spi.Dependency) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 42 with Provider

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;
}
Also used : NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) Provider(com.google.inject.Provider) DataSource(javax.sql.DataSource)

Example 43 with 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);
}
Also used : HoneycombConfiguration(com.nearinfinity.honeycomb.config.HoneycombConfiguration) AdapterType(com.nearinfinity.honeycomb.config.AdapterType) Provider(com.google.inject.Provider)

Example 44 with Provider

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;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NotContextException(javax.naming.NotContextException) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NameNotFoundException(javax.naming.NameNotFoundException) Provider(com.google.inject.Provider) LinkRef(javax.naming.LinkRef)

Example 45 with Provider

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>()));
}
Also used : AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Injector(com.google.inject.Injector) Key(com.google.inject.Key)

Aggregations

Provider (com.google.inject.Provider)133 Injector (com.google.inject.Injector)72 AbstractModule (com.google.inject.AbstractModule)63 Module (com.google.inject.Module)44 Key (com.google.inject.Key)17 TypeLiteral (com.google.inject.TypeLiteral)10 ArrayList (java.util.ArrayList)10 ProvisionException (com.google.inject.ProvisionException)9 Dependency (com.google.inject.spi.Dependency)9 Map (java.util.Map)8 Before (org.junit.Before)7 CurrentUser (com.google.gerrit.server.CurrentUser)6 RequestContext (com.google.gerrit.server.util.RequestContext)6 ThreadLocalRequestContext (com.google.gerrit.server.util.ThreadLocalRequestContext)6 InMemoryModule (com.google.gerrit.testutil.InMemoryModule)6 PrivateModule (com.google.inject.PrivateModule)6 Scope (com.google.inject.Scope)6 List (java.util.List)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Annotation (java.lang.annotation.Annotation)5