com.google.inject.multibindings.OptionalBinder Java Examples

The following examples show how to use com.google.inject.multibindings.OptionalBinder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClusterModule.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure() {
   // TODO move Clock to a more generic module
   bind(Clock.class).toInstance(Clock.systemUTC());
   bind(ClusterService.class).asEagerSingleton();

   switch (clusterServiceDao) {
      default:
         logger.warn("unknown cluster dao {}: using default instead", clusterServiceDao);
         // fall through
      case "default":
      case "cassandra":
         bind(ClusterServiceDao.class).to(CassandraClusterServiceDao.class);
         break;
      case "zookeeper":
         logger.info("using zookeeper for cluster registration");
         bind(ClusterServiceDao.class).to(ZookeeperClusterServiceDao.class);
         break;
   }
   OptionalBinder.newOptionalBinder(binder(), new TypeLiteral<Set<ClusterServiceListener>>() {});
}
 
Example #2
Source File: SpectatorModuleTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void injectedRegistryAddedToGlobal() {
  final ManualClock clock = new ManualClock();
  Injector injector = Guice.createInjector(
      new AbstractModule() {
        @Override protected void configure() {
          OptionalBinder.newOptionalBinder(binder(), Clock.class)
              .setBinding()
              .toInstance(clock);
        }
      },
      new SpectatorModule());
  Registry registry = injector.getInstance(Registry.class);
  Spectator.globalRegistry().counter("test").increment();
  clock.setWallTime(5000);
  Assertions.assertEquals(1, registry.counter("test").count());
}
 
Example #3
Source File: SpectatorModule.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Override protected void configure() {
  // Servo is all based on static classes. The context for servo needs to be set as early
  // as possible to avoid missing metrics because the context has not yet been set.
  SpectatorContext.setRegistry(Spectator.globalRegistry());
  bind(Plugin.class).asEagerSingleton();
  bind(StaticManager.class).asEagerSingleton();
  bind(Config.class)
      .annotatedWith(Names.named("spectator"))
      .toProvider(ConfigProvider.class);
  bind(AtlasConfig.class).to(AtlasConfiguration.class);
  OptionalBinder.newOptionalBinder(binder(), ExtendedRegistry.class)
      .setDefault()
      .toInstance(Spectator.registry());
  OptionalBinder.newOptionalBinder(binder(), Clock.class)
      .setDefault()
      .toInstance(Clock.SYSTEM);
  OptionalBinder.newOptionalBinder(binder(), Registry.class)
      .setDefault()
      .to(AtlasRegistry.class)
      .in(Scopes.SINGLETON);
}
 
Example #4
Source File: SessionModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindModel(Session.class, SessionDoc.Partial.class, model -> {
        model.bindService().to(SessionService.class);
    });

    OptionalBinder.newOptionalBinder(publicBinder(), SessionService.class);
}
 
Example #5
Source File: CryptoModule.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    // Truststore
    OptionalBinder.newOptionalBinder(binder(), Key.get(KeyStore.class, Names.named(TRUSTSTORE)));
    if (trustStore != null) {
        bind(KeyStore.class).annotatedWith(Names.named(TRUSTSTORE)).toInstance(trustStore);
    }

    // Keystore(s)
    for (Entry<String, KeyStore> keyStoreEntry : this.keyStores.entrySet()) {
        bind(Key.get(KeyStore.class, Names.named(keyStoreEntry.getKey()))).toInstance(keyStoreEntry.getValue());
    }

    // Encryption service(s)
    for (Entry<Key<EncryptionService>, EncryptionService> entry : this.encryptionServices.entrySet()) {
        bind(entry.getKey()).toInstance(entry.getValue());
    }

    // Hashing service
    bind(HashingService.class).to(PBKDF2HashingService.class);

    // SSL context
    OptionalBinder.newOptionalBinder(binder(), SSLContext.class);
    if (sslContext != null) {
        bind(SSLContext.class).toInstance(sslContext);
    }

    // Bind custom X509KeyManager if any
    if (keyManagerClass != null) {
        bind(X509KeyManager.class).to(keyManagerClass);
    } else {
        bind(X509KeyManager.class).toProvider(Providers.of(null));
    }

    // KeyManager adapters should be injectable
    keyManagerAdapters.forEach(this::requestInjection);
}
 
Example #6
Source File: MultibindingsModule.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
protected void configure() {
    final Multibinder<Plugin> pluginMultibinder = Multibinder.newSetBinder(binder(), Plugin.class);
    pluginMultibinder.addBinding().to(MyPlugin.class);
    pluginMultibinder.addBinding().toInstance(new MyPlugin2());

    final MapBinder<String, KeyedPlugin> keyedPluginMapBinder =
            MapBinder.newMapBinder(binder(), String.class, KeyedPlugin.class);
    keyedPluginMapBinder.addBinding("foo").to(MyKeyedPlugin.class);
    keyedPluginMapBinder.addBinding("bar").toInstance(new MyKeyedPlugin2());
    
    OptionalBinder.newOptionalBinder(binder(), OptService.class).setDefault().to(DefImpl.class);
    install(new OverideModule());
}
 
Example #7
Source File: TestModule.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override protected void configure() {
  final Registry registry = new DefaultRegistry();
  OptionalBinder.newOptionalBinder(binder(), ExtendedRegistry.class)
      .setBinding()
      .toInstance(new ExtendedRegistry(registry));
  OptionalBinder.newOptionalBinder(binder(), Registry.class)
      .setBinding()
      .toInstance(registry);
}
 
Example #8
Source File: PlannerCompilerModule.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    OptionalBinder.newOptionalBinder(binder(), Key.get(PlanProgramCompileOptions.class))
            .setDefault().toInstance(PlanProgramCompileOptions.DEFAULT_OPTIONS);
    bind(ProgramCompiler.class).to(PlanProgramCompiler.class);
    install(new PlanScopedModule());
    install(new ASMClassSourceModule());
}
 
Example #9
Source File: MapModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindModel(MapDoc.class, model -> {
        model.bindService().to(MapService.class);
    });

    OptionalBinder.newOptionalBinder(publicBinder(), MapService.class);
}
 
Example #10
Source File: ServerModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindAndExpose(ServerStore.class);

    bindModel(Server.class, ServerDoc.Partial.class, model -> {
        model.bindStore().to(ServerStore.class);
        model.bindService().to(ServerService.class);
    });

    OptionalBinder.newOptionalBinder(publicBinder(), ServerService.class);
}
 
Example #11
Source File: WhisperModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindModel(Whisper.class, WhisperDoc.Partial.class, model -> {
        model.bindService().to(WhisperService.class);
    });

    OptionalBinder.newOptionalBinder(publicBinder(), WhisperService.class)
                  .setDefault().to(NullWhisperService.class);

}
 
Example #12
Source File: EngagementModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindModel(EngagementDoc.class);

    OptionalBinder.newOptionalBinder(publicBinder(), EngagementService.class)
                  .setDefault().to(LocalEngagementService.class);
}
 
Example #13
Source File: UserModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindModel(User.class, UserDoc.Partial.class, model -> {
        model.bindService().to(UserService.class);
    });

    OptionalBinder.newOptionalBinder(publicBinder(), UserService.class);
}
 
Example #14
Source File: ThriftMetastoreModule.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected void setup(Binder binder)
{
    OptionalBinder.newOptionalBinder(binder, ThriftMetastoreClientFactory.class)
            .setDefault().to(DefaultThriftMetastoreClientFactory.class).in(Scopes.SINGLETON);
    binder.bind(MetastoreLocator.class).to(StaticMetastoreLocator.class).in(Scopes.SINGLETON);
    configBinder(binder).bindConfig(StaticMetastoreConfig.class);
    configBinder(binder).bindConfig(ThriftMetastoreConfig.class);

    binder.bind(ThriftMetastore.class).to(ThriftHiveMetastore.class).in(Scopes.SINGLETON);
    newExporter(binder).export(ThriftMetastore.class)
            .as(generator -> generator.generatedNameOf(ThriftHiveMetastore.class));

    if (buildConfigObject(HiveConfig.class).getRecordingPath() != null) {
        binder.bind(HiveMetastore.class)
                .annotatedWith(ForRecordingHiveMetastore.class)
                .to(BridgingHiveMetastore.class)
                .in(Scopes.SINGLETON);
        binder.bind(HiveMetastore.class)
                .annotatedWith(ForCachingHiveMetastore.class)
                .to(RecordingHiveMetastore.class)
                .in(Scopes.SINGLETON);
        binder.bind(RecordingHiveMetastore.class).in(Scopes.SINGLETON);
        newExporter(binder).export(RecordingHiveMetastore.class).withGeneratedName();

        Multibinder<Procedure> procedures = newSetBinder(binder, Procedure.class);
        procedures.addBinding().toProvider(WriteHiveMetastoreRecordingProcedure.class).in(Scopes.SINGLETON);
    }
    else {
        binder.bind(HiveMetastore.class)
                .annotatedWith(ForCachingHiveMetastore.class)
                .to(BridgingHiveMetastore.class)
                .in(Scopes.SINGLETON);
    }

    binder.install(new CachingHiveMetastoreModule());

    install(new ThriftMetastoreAuthenticationModule());
}
 
Example #15
Source File: ModelBinder.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private ModelBinder(ProtectedBinder protectedBinder, TypeLiteral<M> M, TypeLiteral<P> P) {
    this.binder = Binders.wrap(protectedBinder.publicBinder());
    this.M = M;
    this.P = P;

    this.metas = Multibinder.newSetBinder(binder, ModelMeta.class);
    this.serviceBinder = OptionalBinder.newOptionalBinder(binder, ModelService(M, P));
    this.queryServiceBinder = OptionalBinder.newOptionalBinder(binder, QueryService(M));
    this.updateServiceBinder = OptionalBinder.newOptionalBinder(binder, UpdateService(P));
    this.storeBinder = OptionalBinder.newOptionalBinder(binder, ModelStore(M));

    binder.install(new OneTime());
    binder.install(new PerModel());
}
 
Example #16
Source File: TournamentModelManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void configure() {
    bindAndExpose(TournamentStore.class);

    bindModel(team.Team.class, team.Partial.class);

    bindModel(Tournament.class, model -> {
        model.bindStore().to(TournamentStore.class);
        model.queryService().setBinding().to(TournamentService.class);
    });

    OptionalBinder.newOptionalBinder(publicBinder(), TournamentService.class)
                  .setDefault().to(NullTournamentService.class);
}
 
Example #17
Source File: HttpHealthCheckModule.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
   bind(HttpHealthCheckServer.class)
      .asEagerSingleton();
   expose(HttpHealthCheckServer.class);
   
   OptionalBinder.newOptionalBinder(binder(), ClusterService.class);
   
   Multibinder<HttpResource> resourceBinder =
         Multibinder
            .newSetBinder(binder(), HttpResource.class, Names.named(HealthCheckServerConfig.NAME_HEALTHCHECK_RESOURCES));
   resourceBinder.addBinding().to(CheckPage.class);
   resourceBinder.addBinding().to(StatusPage.class);
}
 
Example #18
Source File: ModelBinder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public OptionalBinder<UpdateService<P>> updateService() {
    return updateServiceBinder;
}
 
Example #19
Source File: ModelBinder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public OptionalBinder<QueryService<M>> queryService() {
    return queryServiceBinder;
}
 
Example #20
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
default <T> OptionalBinder<T> forOptional(Key<T> key) {
    return OptionalBinder.newOptionalBinder(forwardedBinder(), key);
}
 
Example #21
Source File: ClusteredPartitionModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
   bind(Partitioner.class).to(DynamicPartitioner.class);
   bindSetOf(ClusterServiceListener.class).addBinding().to(DynamicPartitioner.class);
   OptionalBinder.newOptionalBinder(binder(), new TypeLiteral<Set<PartitionListener>>() {});
}
 
Example #22
Source File: MultibindingsModule.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
@Override
protected void configure() {
    OptionalBinder.newOptionalBinder(binder(), OptService.class).setBinding().to(ActualImpl.class);
}
 
Example #23
Source File: SimplePartitionModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
   bind(Partitioner.class).to(SimplePartitioner.class);
   OptionalBinder.newOptionalBinder(binder(), new TypeLiteral<Set<PartitionListener>>() {});
}
 
Example #24
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
default <T> OptionalBinder<T> forOptional(Class<T> type) { return forOptional(Key.get(type)); } 
Example #25
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
default <T> OptionalBinder<T> forOptional(TypeLiteral<T> type) { return forOptional(Key.get(type)); }