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));
}
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);
}
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;
}
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));
}
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);
}
}
Aggregations