Search in sources :

Example 91 with Provides

use of com.google.inject.Provides in project ninja by ninjaframework.

the class LifecycleSupportTest method providedSingletonStartableShouldBeStarted.

@Test
public void providedSingletonStartableShouldBeStarted() {
    Injector injector = createInjector(new AbstractModule() {

        @Override
        protected void configure() {
        }

        @Provides
        @Singleton
        public MockSingletonService provide() {
            return new MockSingletonService();
        }
    });
    start(injector);
    assertThat(MockSingletonService.started, equalTo(1));
}
Also used : Injector(com.google.inject.Injector) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 92 with Provides

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

the class MongoClientModule method createMongoSession.

@Singleton
@Provides
public static MongoSession createMongoSession(TypeManager typeManager, MongoClientConfig config) {
    requireNonNull(config, "config is null");
    MongoClientOptions.Builder options = MongoClientOptions.builder();
    options.connectionsPerHost(config.getConnectionsPerHost()).connectTimeout(config.getConnectionTimeout()).socketTimeout(config.getSocketTimeout()).socketKeepAlive(config.getSocketKeepAlive()).sslEnabled(config.getSslEnabled()).maxWaitTime(config.getMaxWaitTime()).minConnectionsPerHost(config.getMinConnectionsPerHost()).readPreference(config.getReadPreference().getReadPreference()).writeConcern(config.getWriteConcern().getWriteConcern());
    if (config.getRequiredReplicaSetName() != null) {
        options.requiredReplicaSetName(config.getRequiredReplicaSetName());
    }
    MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build());
    return new MongoSession(typeManager, client, config);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientOptions(com.mongodb.MongoClientOptions) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 93 with Provides

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

the class ProviderMethodsModule method getProviderMethods.

public List<ProviderMethod<?>> getProviderMethods(Binder binder) {
    List<ProviderMethod<?>> result = Lists.newArrayList();
    Multimap<Signature, Method> methodsBySignature = HashMultimap.create();
    filter.reset();
    Class<?> c = delegate.getClass();
    while (filter.isWorthScanningForMethods(Provides.class.getName(), c)) {
        for (Method method : filter.getAllMethods(Provides.class.getName(), c)) {
            // increasing visibility of a subclass).
            if (((method.getModifiers() & (Modifier.PRIVATE | Modifier.STATIC)) == 0) && !method.isBridge() && !method.isSynthetic()) {
                methodsBySignature.put(new Signature(method), method);
            }
            if (isProvider(method)) {
                result.add(createProviderMethod(binder, method));
            }
        }
        c = c.getSuperclass();
    }
    // assuming that every method is an override, in general it should be very quick.
    for (ProviderMethod<?> provider : result) {
        Method method = provider.getMethod();
        for (Method matchingSignature : methodsBySignature.get(new Signature(method))) {
            // overridding it.
            if (matchingSignature.getDeclaringClass().isAssignableFrom(method.getDeclaringClass())) {
                continue;
            }
            // now we know matching signature is in a subtype of method.getDeclaringClass()
            if (overrides(matchingSignature, method)) {
                binder.addError("Overriding @Provides methods is not allowed." + "\n\t@Provides method: %s\n\toverridden by: %s", method, matchingSignature);
                break;
            }
        }
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) Provides(com.google.inject.Provides)

Example 94 with Provides

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

the class MarkupTemplateModule method provideTemplateEngine.

@SuppressWarnings("UnusedDeclaration")
@Provides
@Singleton
MarkupTemplateEngine provideTemplateEngine(ServerConfig serverConfig, Config config) {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    TemplateConfiguration effectiveConfiguration = new TemplateConfiguration(config);
    // not copied by constructor
    effectiveConfiguration.setCacheTemplates(config.isCacheTemplates());
    Path templatesDir = serverConfig.getBaseDir().file(config.getTemplatesDirectory());
    return new MarkupTemplateEngine(parent, effectiveConfiguration, new CachingTemplateResolver(templatesDir));
}
Also used : Path(java.nio.file.Path) CachingTemplateResolver(ratpack.groovy.template.internal.CachingTemplateResolver) TemplateConfiguration(groovy.text.markup.TemplateConfiguration) MarkupTemplateEngine(groovy.text.markup.MarkupTemplateEngine) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 95 with Provides

use of com.google.inject.Provides in project keywhiz by square.

the class CryptoModule method derivationProvider.

/**
   * Sometimes a different provider is preferable for key derivation. In particular, when an HSM
   * holds the base derivation key.
   *
   * @return the security provider to use strictly for key derivation.
   */
@Provides
@Derivation
@Singleton
Provider derivationProvider() {
    try {
        Provider provider = (Provider) Class.forName(derivationProviderClass).newInstance();
        if (Security.getProvider(provider.getName()) == null) {
            logger.debug("Registering new crypto provider {}", provider.getName());
            Security.addProvider(provider);
        }
        return provider;
    } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
        logger.error("Error instantiating derivation provider: {}", e.getMessage(), e);
        throw Throwables.propagate(e);
    }
}
Also used : Provider(java.security.Provider) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

Provides (com.google.inject.Provides)126 Singleton (javax.inject.Singleton)36 AbstractModule (com.google.inject.AbstractModule)26 Singleton (com.google.inject.Singleton)24 Injector (com.google.inject.Injector)22 Named (javax.inject.Named)17 LazySingleton (io.druid.guice.LazySingleton)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Named (com.google.inject.name.Named)8 IOException (java.io.IOException)7 File (java.io.File)6 ArrayList (java.util.ArrayList)6 Inject (javax.inject.Inject)6 Properties (java.util.Properties)5 Client (javax.ws.rs.client.Client)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 Binder (com.google.inject.Binder)3