Search in sources :

Example 56 with TypeLiteral

use of com.google.inject.TypeLiteral in project divide by HiddenStage.

the class BackendModule method configure.

@Override
protected final void configure() {
    logConfiguration();
    if (config == null)
        throw new IllegalStateException("Config can not be null");
    // ORDER MATTER
    bind(type).toInstance(config);
    bind(Config.class).toInstance(config);
    bind(Backend.class).in(Singleton.class);
    bind(Storage.class).toInstance(new XmlStorage(new File(config.fileSavePath + "storage.xml"), Storage.MODE_WORLD_WRITEABLE));
    bind(AccountStorage.class).toInstance(new XmlAccoutStorage(new File(config.fileSavePath + "accounts.xml")));
    bind(new TypeLiteral<DAO<BackendObject, BackendObject>>() {
    }).to(new TypeLiteral<LocalStorageIBoxDb<BackendObject, BackendObject>>() {
    }).in(Singleton.class);
    // ugly but required, doesnt allow binding to itself
    if (AuthManager.class.equals(getAuthManagerClass())) {
        bind(AuthManager.class).in(Singleton.class);
    } else {
        bind(AuthManager.class).to(getAuthManagerClass()).in(Singleton.class);
    }
    if (DataManager.class.equals(getDataManagerClass())) {
        bind(DataManager.class).in(Singleton.class);
    } else {
        bind(DataManager.class).to(getDataManagerClass()).in(Singleton.class);
    }
    if (ObjectManager.class.equals(getObjectManagerClass())) {
        bind(ObjectManager.class).in(Singleton.class);
    } else {
        bind(ObjectManager.class).to(getObjectManagerClass()).in(Singleton.class);
    }
    requestStaticInjection(Backend.class);
    requestStaticInjection(BackendUser.class);
    requestStaticInjection(BackendServices.class);
    additionalConfig(config);
}
Also used : AuthManager(io.divide.client.auth.AuthManager) DataManager(io.divide.client.data.DataManager) XmlStorage(io.divide.shared.file.XmlStorage) XmlAccoutStorage(io.divide.client.auth.credentials.XmlAccoutStorage) AccountStorage(io.divide.client.auth.AccountStorage) ObjectManager(io.divide.client.data.ObjectManager) XmlAccoutStorage(io.divide.client.auth.credentials.XmlAccoutStorage) Storage(io.divide.shared.file.Storage) AccountStorage(io.divide.client.auth.AccountStorage) XmlStorage(io.divide.shared.file.XmlStorage) DAO(io.divide.shared.server.DAO) TypeLiteral(com.google.inject.TypeLiteral) File(java.io.File)

Example 57 with TypeLiteral

use of com.google.inject.TypeLiteral in project gerrit by GerritCodeReview.

the class InMemoryTestingDatabaseModule method configure.

@Override
protected void configure() {
    bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(cfg);
    // TODO(dborowitz): Use jimfs.
    Path p = Paths.get(cfg.getString("gerrit", null, "tempSiteDir"));
    bind(Path.class).annotatedWith(SitePath.class).toInstance(p);
    makeSiteDirs(p);
    bind(GitRepositoryManager.class).to(InMemoryRepositoryManager.class);
    bind(InMemoryRepositoryManager.class).in(SINGLETON);
    bind(MetricMaker.class).to(DisabledMetricMaker.class);
    bind(DataSourceType.class).to(InMemoryH2Type.class);
    bind(NotesMigration.class).to(TestNotesMigration.class);
    TypeLiteral<SchemaFactory<ReviewDb>> schemaFactory = new TypeLiteral<SchemaFactory<ReviewDb>>() {
    };
    bind(schemaFactory).to(NotesMigrationSchemaFactory.class);
    bind(Key.get(schemaFactory, ReviewDbFactory.class)).to(InMemoryDatabase.class);
    bind(InMemoryDatabase.class).in(SINGLETON);
    bind(ChangeBundleReader.class).to(GwtormChangeBundleReader.class);
    listener().to(CreateDatabase.class);
    bind(SitePaths.class);
    bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class).in(SINGLETON);
    install(new SchemaModule());
    bind(SchemaVersion.class).to(SchemaVersion.C);
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker) MetricMaker(com.google.gerrit.metrics.MetricMaker) NotesMigrationSchemaFactory(com.google.gerrit.server.schema.NotesMigrationSchemaFactory) SchemaFactory(com.google.gwtorm.server.SchemaFactory) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) SchemaModule(com.google.gerrit.server.schema.SchemaModule) SchemaVersion(com.google.gerrit.server.schema.SchemaVersion) InMemoryRepositoryManager(com.google.gerrit.testutil.InMemoryRepositoryManager) InMemoryDatabase(com.google.gerrit.testutil.InMemoryDatabase) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) TrackingFootersProvider(com.google.gerrit.server.config.TrackingFootersProvider) TestNotesMigration(com.google.gerrit.testutil.TestNotesMigration) NotesMigration(com.google.gerrit.server.notedb.NotesMigration) GwtormChangeBundleReader(com.google.gerrit.server.notedb.GwtormChangeBundleReader) ChangeBundleReader(com.google.gerrit.server.notedb.ChangeBundleReader) TypeLiteral(com.google.inject.TypeLiteral) DataSourceType(com.google.gerrit.server.schema.DataSourceType) SitePath(com.google.gerrit.server.config.SitePath) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 58 with TypeLiteral

use of com.google.inject.TypeLiteral in project cdap by caskdata.

the class DatasetServiceTestBase method initializeAndStartService.

protected static void initializeAndStartService(CConfiguration cConf) throws Exception {
    // TODO: this whole method is a mess. Streamline it!
    injector = Guice.createInjector(new ConfigModule(cConf), new DiscoveryRuntimeModule().getInMemoryModules(), new NonCustomLocationUnitTestModule().getModule(), new NamespaceClientRuntimeModule().getInMemoryModules(), new SystemDatasetRuntimeModule().getInMemoryModules(), new TransactionInMemoryModule(), new AuthorizationTestModule(), new AuthorizationEnforcementModule().getInMemoryModules(), new AuthenticationContextModules().getMasterModule(), new AbstractModule() {

        @Override
        protected void configure() {
            bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class).in(Singleton.class);
            install(new FactoryModuleBuilder().implement(DatasetDefinitionRegistry.class, DefaultDatasetDefinitionRegistry.class).build(DatasetDefinitionRegistryFactory.class));
            // through the injector, we only need RemoteDatasetFramework in these tests
            bind(RemoteDatasetFramework.class);
            bind(OwnerStore.class).to(InMemoryOwnerStore.class);
            bind(OwnerAdmin.class).to(DefaultOwnerAdmin.class);
        }
    });
    AuthorizationEnforcer authEnforcer = injector.getInstance(AuthorizationEnforcer.class);
    AuthenticationContext authenticationContext = injector.getInstance(AuthenticationContext.class);
    DiscoveryService discoveryService = injector.getInstance(DiscoveryService.class);
    discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    dsFramework = injector.getInstance(RemoteDatasetFramework.class);
    // Tx Manager to support working with datasets
    txManager = injector.getInstance(TransactionManager.class);
    txManager.startAndWait();
    TransactionSystemClient txSystemClient = injector.getInstance(TransactionSystemClient.class);
    TransactionSystemClientService txSystemClientService = new DelegatingTransactionSystemClientService(txSystemClient);
    NamespacedLocationFactory namespacedLocationFactory = injector.getInstance(NamespacedLocationFactory.class);
    SystemDatasetInstantiatorFactory datasetInstantiatorFactory = new SystemDatasetInstantiatorFactory(locationFactory, dsFramework, cConf);
    // ok to pass null, since the impersonator won't actually be called, if kerberos security is not enabled
    Impersonator impersonator = new DefaultImpersonator(cConf, null);
    DatasetAdminService datasetAdminService = new DatasetAdminService(dsFramework, cConf, locationFactory, datasetInstantiatorFactory, new NoOpMetadataStore(), impersonator);
    ImmutableSet<HttpHandler> handlers = ImmutableSet.<HttpHandler>of(new DatasetAdminOpHTTPHandler(datasetAdminService));
    MetricsCollectionService metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
    opExecutorService = new DatasetOpExecutorService(cConf, discoveryService, metricsCollectionService, handlers);
    opExecutorService.startAndWait();
    Map<String, DatasetModule> defaultModules = injector.getInstance(Key.get(new TypeLiteral<Map<String, DatasetModule>>() {
    }, Constants.Dataset.Manager.DefaultDatasetModules.class));
    ImmutableMap<String, DatasetModule> modules = ImmutableMap.<String, DatasetModule>builder().putAll(defaultModules).putAll(DatasetMetaTableUtil.getModules()).build();
    registryFactory = injector.getInstance(DatasetDefinitionRegistryFactory.class);
    inMemoryDatasetFramework = new InMemoryDatasetFramework(registryFactory, modules);
    DiscoveryExploreClient exploreClient = new DiscoveryExploreClient(discoveryServiceClient, authenticationContext);
    ExploreFacade exploreFacade = new ExploreFacade(exploreClient, cConf);
    namespaceAdmin = injector.getInstance(NamespaceAdmin.class);
    namespaceAdmin.create(NamespaceMeta.DEFAULT);
    ownerAdmin = injector.getInstance(OwnerAdmin.class);
    NamespaceQueryAdmin namespaceQueryAdmin = injector.getInstance(NamespaceQueryAdmin.class);
    TransactionExecutorFactory txExecutorFactory = new DynamicTransactionExecutorFactory(txSystemClient);
    DatasetTypeManager typeManager = new DatasetTypeManager(cConf, locationFactory, txSystemClientService, txExecutorFactory, inMemoryDatasetFramework, impersonator);
    DatasetOpExecutor opExecutor = new InMemoryDatasetOpExecutor(dsFramework);
    DatasetInstanceManager instanceManager = new DatasetInstanceManager(txSystemClientService, txExecutorFactory, inMemoryDatasetFramework);
    PrivilegesManager privilegesManager = injector.getInstance(PrivilegesManager.class);
    DatasetTypeService typeService = new DatasetTypeService(typeManager, namespaceAdmin, namespacedLocationFactory, authEnforcer, privilegesManager, authenticationContext, cConf, impersonator, txSystemClientService, inMemoryDatasetFramework, txExecutorFactory, defaultModules);
    instanceService = new DatasetInstanceService(typeService, instanceManager, opExecutor, exploreFacade, namespaceQueryAdmin, ownerAdmin, authEnforcer, privilegesManager, authenticationContext);
    service = new DatasetService(cConf, discoveryService, discoveryServiceClient, metricsCollectionService, opExecutor, new HashSet<DatasetMetricsReporter>(), typeService, instanceService);
    // Start dataset service, wait for it to be discoverable
    service.startAndWait();
    waitForService(Constants.Service.DATASET_EXECUTOR);
    waitForService(Constants.Service.DATASET_MANAGER);
    // this usually happens while creating a namespace, however not doing that in data fabric tests
    Locations.mkdirsIfNotExists(namespacedLocationFactory.get(NamespaceId.DEFAULT));
}
Also used : RemoteDatasetFramework(co.cask.cdap.data2.datafabric.dataset.RemoteDatasetFramework) InMemoryDatasetOpExecutor(co.cask.cdap.data2.datafabric.dataset.service.executor.InMemoryDatasetOpExecutor) AuthenticationContext(co.cask.cdap.security.spi.authentication.AuthenticationContext) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) DatasetAdminOpHTTPHandler(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpHTTPHandler) TransactionInMemoryModule(org.apache.tephra.runtime.TransactionInMemoryModule) AuthorizationEnforcer(co.cask.cdap.security.spi.authorization.AuthorizationEnforcer) NamespacedLocationFactory(co.cask.cdap.common.namespace.NamespacedLocationFactory) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) ExploreFacade(co.cask.cdap.explore.client.ExploreFacade) DynamicTransactionExecutorFactory(co.cask.cdap.data.runtime.DynamicTransactionExecutorFactory) TransactionExecutorFactory(co.cask.cdap.data2.transaction.TransactionExecutorFactory) NoOpMetadataStore(co.cask.cdap.data2.metadata.store.NoOpMetadataStore) DatasetDefinitionRegistryFactory(co.cask.cdap.data2.dataset2.DatasetDefinitionRegistryFactory) SystemDatasetRuntimeModule(co.cask.cdap.data.runtime.SystemDatasetRuntimeModule) DiscoveryRuntimeModule(co.cask.cdap.common.guice.DiscoveryRuntimeModule) InMemoryDatasetFramework(co.cask.cdap.data2.dataset2.InMemoryDatasetFramework) HashSet(java.util.HashSet) HttpHandler(co.cask.http.HttpHandler) DatasetInstanceManager(co.cask.cdap.data2.datafabric.dataset.instance.DatasetInstanceManager) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) AuthenticationContextModules(co.cask.cdap.security.auth.context.AuthenticationContextModules) DefaultOwnerAdmin(co.cask.cdap.security.impersonation.DefaultOwnerAdmin) OwnerAdmin(co.cask.cdap.security.impersonation.OwnerAdmin) NamespaceAdmin(co.cask.cdap.common.namespace.NamespaceAdmin) PrivilegesManager(co.cask.cdap.security.spi.authorization.PrivilegesManager) NonCustomLocationUnitTestModule(co.cask.cdap.common.guice.NonCustomLocationUnitTestModule) DatasetTypeManager(co.cask.cdap.data2.datafabric.dataset.type.DatasetTypeManager) TransactionManager(org.apache.tephra.TransactionManager) AuthorizationEnforcementModule(co.cask.cdap.security.authorization.AuthorizationEnforcementModule) DelegatingTransactionSystemClientService(co.cask.cdap.data2.transaction.DelegatingTransactionSystemClientService) NamespaceClientRuntimeModule(co.cask.cdap.common.namespace.guice.NamespaceClientRuntimeModule) ConfigModule(co.cask.cdap.common.guice.ConfigModule) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) DynamicTransactionExecutorFactory(co.cask.cdap.data.runtime.DynamicTransactionExecutorFactory) DatasetModule(co.cask.cdap.api.dataset.module.DatasetModule) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) DiscoveryExploreClient(co.cask.cdap.explore.client.DiscoveryExploreClient) SystemDatasetInstantiatorFactory(co.cask.cdap.data.dataset.SystemDatasetInstantiatorFactory) TypeLiteral(com.google.inject.TypeLiteral) NamespaceQueryAdmin(co.cask.cdap.common.namespace.NamespaceQueryAdmin) TransactionSystemClientService(co.cask.cdap.data2.transaction.TransactionSystemClientService) DelegatingTransactionSystemClientService(co.cask.cdap.data2.transaction.DelegatingTransactionSystemClientService) DiscoveryService(org.apache.twill.discovery.DiscoveryService) DatasetAdminService(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminService) DatasetOpExecutor(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutor) InMemoryDatasetOpExecutor(co.cask.cdap.data2.datafabric.dataset.service.executor.InMemoryDatasetOpExecutor) AuthorizationTestModule(co.cask.cdap.security.authorization.AuthorizationTestModule) Impersonator(co.cask.cdap.security.impersonation.Impersonator) DefaultImpersonator(co.cask.cdap.security.impersonation.DefaultImpersonator) DefaultImpersonator(co.cask.cdap.security.impersonation.DefaultImpersonator) InMemoryOwnerStore(co.cask.cdap.security.impersonation.InMemoryOwnerStore) OwnerStore(co.cask.cdap.security.impersonation.OwnerStore) AbstractModule(com.google.inject.AbstractModule) DatasetOpExecutorService(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService)

Example 59 with TypeLiteral

use of com.google.inject.TypeLiteral in project druid by druid-io.

the class ConditionalMultibindTest method testMultiConditionalBind_typeLiteral.

@Test
public void testMultiConditionalBind_typeLiteral() {
    props.setProperty("animal.type", "pets");
    final Set<Animal> set1 = ImmutableSet.<Animal>of(new Dog(), new Tiger());
    final Set<Animal> set2 = ImmutableSet.<Animal>of(new Cat(), new Fish());
    final Set<Animal> set3 = ImmutableSet.<Animal>of(new Cat());
    final Set<Animal> union = new HashSet<>();
    union.addAll(set1);
    union.addAll(set2);
    final Zoo<Animal> zoo1 = new Zoo<>(set1);
    final Zoo<Animal> zoo2 = new Zoo<>();
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, new TypeLiteral<Set<Animal>>() {
            }).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), set1).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), set2);
            ConditionalMultibind.create(props, binder, new TypeLiteral<Zoo<Animal>>() {
            }).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), zoo1);
        }
    }, new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, new TypeLiteral<Set<Animal>>() {
            }).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), set3);
            ConditionalMultibind.create(props, binder, new TypeLiteral<Set<Animal>>() {
            }, SanDiego.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), union);
            ConditionalMultibind.create(props, binder, new TypeLiteral<Zoo<Animal>>() {
            }).addBinding(new TypeLiteral<Zoo<Animal>>() {
            });
        }
    });
    Set<Set<Animal>> actualAnimalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Set<Animal>>>() {
    }));
    Assert.assertEquals(3, actualAnimalSet.size());
    Assert.assertEquals(ImmutableSet.of(set1, set2, set3), actualAnimalSet);
    actualAnimalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Set<Animal>>>() {
    }, SanDiego.class));
    Assert.assertEquals(1, actualAnimalSet.size());
    Assert.assertEquals(ImmutableSet.of(union), actualAnimalSet);
    final Set<Zoo<Animal>> actualZooSet = injector.getInstance(Key.get(new TypeLiteral<Set<Zoo<Animal>>>() {
    }));
    Assert.assertEquals(2, actualZooSet.size());
    Assert.assertEquals(ImmutableSet.of(zoo1, zoo2), actualZooSet);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 60 with TypeLiteral

use of com.google.inject.TypeLiteral in project druid by druid-io.

the class ConditionalMultibindTest method testMultiConditionalBind_multiple_modules.

@Test
public void testMultiConditionalBind_multiple_modules() {
    props.setProperty("animal.type", "pets");
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
        }
    }, new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("not_match"), Tiger.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Fish.class);
        }
    });
    Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>() {
    }));
    Assert.assertEquals(3, animalSet.size());
    Assert.assertEquals(animalSet, ImmutableSet.<Animal>of(new Cat(), new Dog(), new Fish()));
}
Also used : Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Test(org.junit.Test)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)112 AbstractModule (com.google.inject.AbstractModule)42 Injector (com.google.inject.Injector)37 Module (com.google.inject.Module)15 Map (java.util.Map)14 Key (com.google.inject.Key)12 Provider (com.google.inject.Provider)12 ParameterizedType (java.lang.reflect.ParameterizedType)12 ImmutableSet (com.google.common.collect.ImmutableSet)10 Binding (com.google.inject.Binding)10 Annotation (java.lang.annotation.Annotation)10 HashMap (java.util.HashMap)10 Set (java.util.Set)10 Binder (com.google.inject.Binder)9 InjectionPoint (com.google.inject.spi.InjectionPoint)9 Method (java.lang.reflect.Method)9 Type (java.lang.reflect.Type)9 HashSet (java.util.HashSet)9 ProvisionException (com.google.inject.ProvisionException)7 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)7