Search in sources :

Example 16 with Provides

use of com.google.inject.Provides in project presto by prestodb.

the class RaptorModule method createDBI.

@ForMetadata
@Singleton
@Provides
public IDBI createDBI(@ForMetadata ConnectionFactory connectionFactory, TypeManager typeManager) {
    DBI dbi = new DBI(connectionFactory);
    dbi.registerMapper(new TableColumn.Mapper(typeManager));
    dbi.registerMapper(new Distribution.Mapper(typeManager));
    createTablesWithRetry(dbi);
    return dbi;
}
Also used : Distribution(com.facebook.presto.raptor.metadata.Distribution) IDBI(org.skife.jdbi.v2.IDBI) DBI(org.skife.jdbi.v2.DBI) TableColumn(com.facebook.presto.raptor.metadata.TableColumn) Singleton(javax.inject.Singleton) ForMetadata(com.facebook.presto.raptor.metadata.ForMetadata) Provides(com.google.inject.Provides)

Example 17 with Provides

use of com.google.inject.Provides in project presto by prestodb.

the class CassandraClientModule method createCassandraSession.

@Singleton
@Provides
public static CassandraSession createCassandraSession(CassandraConnectorId connectorId, CassandraClientConfig config, JsonCodec<List<ExtraColumnMetadata>> extraColumnMetadataCodec) {
    requireNonNull(config, "config is null");
    requireNonNull(extraColumnMetadataCodec, "extraColumnMetadataCodec is null");
    Cluster.Builder clusterBuilder = Cluster.builder().withProtocolVersion(ProtocolVersion.V3);
    List<String> contactPoints = requireNonNull(config.getContactPoints(), "contactPoints is null");
    checkArgument(!contactPoints.isEmpty(), "empty contactPoints");
    contactPoints.forEach(clusterBuilder::addContactPoint);
    clusterBuilder.withPort(config.getNativeProtocolPort());
    clusterBuilder.withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 10000));
    clusterBuilder.withRetryPolicy(config.getRetryPolicy().getPolicy());
    LoadBalancingPolicy loadPolicy = new RoundRobinPolicy();
    if (config.isUseDCAware()) {
        requireNonNull(config.getDcAwareLocalDC(), "DCAwarePolicy localDC is null");
        DCAwareRoundRobinPolicy.Builder builder = DCAwareRoundRobinPolicy.builder().withLocalDc(config.getDcAwareLocalDC());
        if (config.getDcAwareUsedHostsPerRemoteDc() > 0) {
            builder.withUsedHostsPerRemoteDc(config.getDcAwareUsedHostsPerRemoteDc());
            if (config.isDcAwareAllowRemoteDCsForLocal()) {
                builder.allowRemoteDCsForLocalConsistencyLevel();
            }
        }
        loadPolicy = builder.build();
    }
    if (config.isUseTokenAware()) {
        loadPolicy = new TokenAwarePolicy(loadPolicy, config.isTokenAwareShuffleReplicas());
    }
    if (config.isUseWhiteList()) {
        checkArgument(!config.getWhiteListAddresses().isEmpty(), "empty WhiteListAddresses");
        List<InetSocketAddress> whiteList = new ArrayList<>();
        for (String point : config.getWhiteListAddresses()) {
            whiteList.add(new InetSocketAddress(point, config.getNativeProtocolPort()));
        }
        loadPolicy = new WhiteListPolicy(loadPolicy, whiteList);
    }
    clusterBuilder.withLoadBalancingPolicy(loadPolicy);
    SocketOptions socketOptions = new SocketOptions();
    socketOptions.setReadTimeoutMillis(toIntExact(config.getClientReadTimeout().toMillis()));
    socketOptions.setConnectTimeoutMillis(toIntExact(config.getClientConnectTimeout().toMillis()));
    if (config.getClientSoLinger() != null) {
        socketOptions.setSoLinger(config.getClientSoLinger());
    }
    clusterBuilder.withSocketOptions(socketOptions);
    if (config.getUsername() != null && config.getPassword() != null) {
        clusterBuilder.withCredentials(config.getUsername(), config.getPassword());
    }
    QueryOptions options = new QueryOptions();
    options.setFetchSize(config.getFetchSize());
    options.setConsistencyLevel(config.getConsistencyLevel());
    clusterBuilder.withQueryOptions(options);
    if (config.getSpeculativeExecutionLimit() > 1) {
        clusterBuilder.withSpeculativeExecutionPolicy(new ConstantSpeculativeExecutionPolicy(// delay before a new execution is launched
        config.getSpeculativeExecutionDelay().toMillis(), // maximum number of executions
        config.getSpeculativeExecutionLimit()));
    }
    return new NativeCassandraSession(connectorId.toString(), extraColumnMetadataCodec, clusterBuilder.build(), config.getNoHostAvailableRetryTimeout());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketOptions(com.datastax.driver.core.SocketOptions) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) QueryOptions(com.datastax.driver.core.QueryOptions) ConstantSpeculativeExecutionPolicy(com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) WhiteListPolicy(com.datastax.driver.core.policies.WhiteListPolicy) ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 18 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class MiniGuiceTest method testSingletonsAreEager.

public void testSingletonsAreEager() {
    final AtomicBoolean sInjected = new AtomicBoolean();
    R.injected = false;
    MiniGuice.inject(A.class, new Object() {

        @Provides
        F provideF(R r) {
            return new F();
        }

        @Provides
        @Singleton
        S provideS() {
            sInjected.set(true);
            return new S();
        }
    });
    assertTrue(R.injected);
    assertTrue(sInjected.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 19 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class ServletTest method testScopeExceptions.

public void testScopeExceptions() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            install(new ServletModule());
        }

        @Provides
        @RequestScoped
        String provideString() {
            return "foo";
        }

        @Provides
        @SessionScoped
        Integer provideInteger() {
            return 1;
        }

        @Provides
        @RequestScoped
        @Named("foo")
        String provideNamedString() {
            return "foo";
        }
    });
    try {
        injector.getInstance(String.class);
        fail();
    } catch (ProvisionException oose) {
        assertContains(oose.getMessage(), "Cannot access scoped [java.lang.String].");
    }
    try {
        injector.getInstance(Integer.class);
        fail();
    } catch (ProvisionException oose) {
        assertContains(oose.getMessage(), "Cannot access scoped [java.lang.Integer].");
    }
    Key<?> key = Key.get(String.class, Names.named("foo"));
    try {
        injector.getInstance(key);
        fail();
    } catch (ProvisionException oose) {
        assertContains(oose.getMessage(), "Cannot access scoped [" + Errors.convert(key) + "]");
    }
}
Also used : Named(com.google.inject.name.Named) ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 20 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class Struts2FactoryTest method testStruts2Factory.

public void testStruts2Factory() {
    Struts2Factory s2Factory = new Struts2Factory();
    TestListener testListener = new TestListener(new AbstractModule() {

        @Override
        protected void configure() {
        }

        @Provides
        @SuppressWarnings("unused")
        Date provideDate() {
            return TODAY;
        }
    });
    assertEquals(TODAY, testListener.getInjector().getInstance(Date.class));
    assertEquals(TODAY, s2Factory.buildBean(Date.class, null));
}
Also used : Provides(com.google.inject.Provides) Date(java.util.Date) AbstractModule(com.google.inject.AbstractModule)

Aggregations

Provides (com.google.inject.Provides)111 AbstractModule (com.google.inject.AbstractModule)26 Singleton (com.google.inject.Singleton)23 Singleton (javax.inject.Singleton)23 Injector (com.google.inject.Injector)22 LazySingleton (io.druid.guice.LazySingleton)12 Named (javax.inject.Named)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Named (com.google.inject.name.Named)8 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Inject (javax.inject.Inject)6 Properties (java.util.Properties)5 Test (org.junit.Test)5 Key (com.google.inject.Key)4 ExecutorService (java.util.concurrent.ExecutorService)4 ConfigModule (co.cask.cdap.common.guice.ConfigModule)3 DB (com.codecademy.eventhub.base.DB)3 Binder (com.google.inject.Binder)3