Search in sources :

Example 1 with FactoryModule

use of com.google.gerrit.extensions.config.FactoryModule in project gerrit by GerritCodeReview.

the class SchemaUpdaterTest method update.

@Test
public void update() throws OrmException, FileNotFoundException, IOException {
    db.create();
    final Path site = Paths.get(UUID.randomUUID().toString());
    final SitePaths paths = new SitePaths(site);
    SchemaUpdater u = Guice.createInjector(new FactoryModule() {

        @Override
        protected void configure() {
            TypeLiteral<SchemaFactory<ReviewDb>> schemaFactory = new TypeLiteral<SchemaFactory<ReviewDb>>() {
            };
            bind(schemaFactory).to(NotesMigrationSchemaFactory.class);
            bind(Key.get(schemaFactory, ReviewDbFactory.class)).toInstance(db);
            bind(SitePaths.class).toInstance(paths);
            Config cfg = new Config();
            cfg.setString("user", null, "name", "Gerrit Code Review");
            cfg.setString("user", null, "email", "gerrit@localhost");
            //
            bind(Config.class).annotatedWith(//
            GerritServerConfig.class).toInstance(cfg);
            //
            bind(PersonIdent.class).annotatedWith(//
            GerritPersonIdent.class).toProvider(GerritPersonIdentProvider.class);
            bind(AllProjectsName.class).toInstance(new AllProjectsName("All-Projects"));
            bind(AllUsersName.class).toInstance(new AllUsersName("All-Users"));
            bind(GitRepositoryManager.class).toInstance(new InMemoryRepositoryManager());
            //
            bind(String.class).annotatedWith(//
            AnonymousCowardName.class).toProvider(AnonymousCowardNameProvider.class);
            bind(DataSourceType.class).to(InMemoryH2Type.class);
            bind(SystemGroupBackend.class);
            install(new ConfigNotesMigration.Module());
        }
    }).getInstance(SchemaUpdater.class);
    for (SchemaVersion s = u.getLatestSchemaVersion(); s.getVersionNbr() > 1; s = s.getPrior()) {
        try {
            assertThat(s.getPrior().getVersionNbr()).named("schema %s has prior version %s. Not true that", s.getVersionNbr(), s.getPrior().getVersionNbr()).isEqualTo(s.getVersionNbr() - 1);
        } catch (ProvisionException e) {
            // version.
            break;
        }
    }
    u.update(new UpdateUI() {

        @Override
        public void message(String msg) {
        }

        @Override
        public boolean yesno(boolean def, String msg) {
            return def;
        }

        @Override
        public boolean isBatch() {
            return true;
        }

        @Override
        public void pruneSchema(StatementExecutor e, List<String> pruneList) throws OrmException {
            for (String sql : pruneList) {
                e.execute(sql);
            }
        }
    });
    db.assertSchemaVersion();
    final SystemConfig sc = db.getSystemConfig();
    assertThat(sc.sitePath).isEqualTo(paths.site_path.toAbsolutePath().toString());
}
Also used : SystemConfig(com.google.gerrit.reviewdb.client.SystemConfig) InMemoryRepositoryManager(com.google.gerrit.testutil.InMemoryRepositoryManager) SystemConfig(com.google.gerrit.reviewdb.client.SystemConfig) Config(org.eclipse.jgit.lib.Config) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) SitePaths(com.google.gerrit.server.config.SitePaths) StatementExecutor(com.google.gwtorm.server.StatementExecutor) ProvisionException(com.google.inject.ProvisionException) TypeLiteral(com.google.inject.TypeLiteral) OrmException(com.google.gwtorm.server.OrmException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) Path(java.nio.file.Path) SchemaFactory(com.google.gwtorm.server.SchemaFactory) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) AnonymousCowardName(com.google.gerrit.server.config.AnonymousCowardName) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ConfigNotesMigration(com.google.gerrit.server.notedb.ConfigNotesMigration) AllUsersName(com.google.gerrit.server.config.AllUsersName) Test(org.junit.Test)

Example 2 with FactoryModule

use of com.google.gerrit.extensions.config.FactoryModule in project gerrit by GerritCodeReview.

the class Passwd method getSysInjector.

private Injector getSysInjector() {
    List<Module> modules = new ArrayList<>();
    modules.add(new FactoryModule() {

        @Override
        protected void configure() {
            bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
            bind(ConsoleUI.class).toInstance(ConsoleUI.getInstance(password != null));
            factory(Section.Factory.class);
            bind(Boolean.class).annotatedWith(InstallAllPlugins.class).toInstance(Boolean.FALSE);
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(InstallPlugins.class).toInstance(new ArrayList<>());
            bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(getConfiguredSecureStoreClass()));
        }
    });
    modules.add(new GerritServerConfigModule());
    return Guice.createInjector(modules);
}
Also used : SitePath(com.google.gerrit.server.config.SitePath) Path(java.nio.file.Path) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) TypeLiteral(com.google.inject.TypeLiteral) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) ArrayList(java.util.ArrayList) Module(com.google.inject.Module) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) FactoryModule(com.google.gerrit.extensions.config.FactoryModule)

Example 3 with FactoryModule

use of com.google.gerrit.extensions.config.FactoryModule in project gerrit by GerritCodeReview.

the class Reindex method createSysInjector.

private Injector createSysInjector() {
    Map<String, Integer> versions = new HashMap<>();
    if (changesVersion != null) {
        versions.put(ChangeSchemaDefinitions.INSTANCE.getName(), changesVersion);
    }
    boolean replica = ReplicaUtil.isReplica(globalConfig);
    List<Module> modules = new ArrayList<>();
    Module indexModule;
    IndexType indexType = IndexModule.getIndexType(dbInjector);
    if (indexType.isLucene()) {
        indexModule = LuceneIndexModule.singleVersionWithExplicitVersions(versions, threads, replica, AutoFlush.DISABLED);
    } else if (indexType.isFake()) {
        // compile the component in.
        try {
            Class<?> clazz = Class.forName("com.google.gerrit.index.testing.FakeIndexModule");
            Method m = clazz.getMethod("singleVersionWithExplicitVersions", Map.class, int.class, boolean.class);
            indexModule = (Module) m.invoke(null, versions, threads, replica);
        } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException("can't create index", e);
        }
    } else {
        throw new IllegalStateException("unsupported index.type = " + indexType);
    }
    modules.add(indexModule);
    modules.add(new AbstractModule() {

        @Override
        protected void configure() {
            super.configure();
            OptionalBinder.newOptionalBinder(binder(), IsFirstInsertForEntry.class).setBinding().toInstance(IsFirstInsertForEntry.YES);
        }
    });
    modules.add(new BatchProgramModule(dbInjector));
    modules.add(new FactoryModule() {

        @Override
        protected void configure() {
            factory(ChangeResource.Factory.class);
        }
    });
    return dbInjector.createChildInjector(ModuleOverloader.override(modules, LibModuleLoader.loadReindexModules(cfgInjector, versions, threads, replica)));
}
Also used : HashMap(java.util.HashMap) IsFirstInsertForEntry(com.google.gerrit.server.index.options.IsFirstInsertForEntry) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) AbstractModule(com.google.inject.AbstractModule) BatchProgramModule(com.google.gerrit.pgm.util.BatchProgramModule) Module(com.google.inject.Module) LuceneIndexModule(com.google.gerrit.lucene.LuceneIndexModule) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) IndexModule(com.google.gerrit.server.index.IndexModule) BatchProgramModule(com.google.gerrit.pgm.util.BatchProgramModule) AbstractModule(com.google.inject.AbstractModule) IndexType(com.google.gerrit.index.IndexType) Map(java.util.Map) HashMap(java.util.HashMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Example 4 with FactoryModule

use of com.google.gerrit.extensions.config.FactoryModule in project gerrit by GerritCodeReview.

the class ChangeExternalIdCaseSensitivity method run.

@Override
public int run() throws Exception {
    mustHaveValidSite();
    ui = ConsoleUI.getInstance(batch);
    Injector dbInjector = createDbInjector();
    manager.add(dbInjector, dbInjector.createChildInjector(NoteDbSchemaVersionCheck.module()));
    dbInjector.createChildInjector(new FactoryModule() {

        @Override
        protected void configure() {
            bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
            install(new FactoryModuleBuilder().build(ExternalIdCaseSensitivityMigrator.Factory.class));
            factory(MetaDataUpdate.InternalFactory.class);
            DynamicMap.mapOf(binder(), ExternalIdUpsertPreprocessor.class);
            // The ChangeExternalIdCaseSensitivity program needs to access all external IDs only
            // once to update them. After the update they are not accessed again. Hence the
            // LocalUsernamesToLowerCase program doesn't benefit from caching external IDs and
            // the external ID cache can be disabled.
            install(DisabledExternalIdCache.module());
        }
    }).injectMembers(this);
    globalConfig = dbInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
    this.isUserNameCaseInsensitive = globalConfig.getBoolean("auth", "userNameCaseInsensitive", false);
    String message = "auth.userNameCaseInsensitive is set to %b. " + "External IDs will be migrated to be case %ssensitive. Continue?";
    if (!ui.yesno(true, message, isUserNameCaseInsensitive, isUserNameCaseInsensitive ? "" : "in")) {
        return 0;
    }
    Collection<ExternalId> todo = externalIds.all();
    monitor.beginTask("Converting external ID note names", todo.size());
    manager.start();
    try {
        migratorFactory.create(!isUserNameCaseInsensitive, dryrun).migrate(todo, () -> monitor.update(1));
    } finally {
        manager.stop();
        monitor.endTask();
    }
    int exitCode;
    if (!dryrun) {
        updateGerritConfig();
        exitCode = reindexAccounts();
    } else {
        exitCode = 0;
    }
    return exitCode;
}
Also used : Injector(com.google.inject.Injector) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 5 with FactoryModule

use of com.google.gerrit.extensions.config.FactoryModule in project gerrit by GerritCodeReview.

the class AbstractChangeNotesTest method setUpTestEnvironment.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Before
public void setUpTestEnvironment() throws Exception {
    setTimeForTesting();
    serverIdent = new PersonIdent("Gerrit Server", "noreply@gerrit.com", Date.from(TimeUtil.now()), TZ);
    project = Project.nameKey("test-project");
    repoManager = new InMemoryRepositoryManager();
    repo = repoManager.createRepository(project);
    tr = new TestRepository<>(repo);
    rw = tr.getRevWalk();
    accountCache = new FakeAccountCache();
    Account.Builder co = Account.builder(Account.id(1), TimeUtil.now());
    co.setFullName("Change Owner");
    co.setPreferredEmail("change@owner.com");
    accountCache.put(co.build());
    Account.Builder ou = Account.builder(Account.id(2), TimeUtil.now());
    ou.setFullName("Other Account");
    ou.setPreferredEmail("other@account.com");
    accountCache.put(ou.build());
    assertableFanOutExecutor = new AssertableExecutorService();
    injector = Guice.createInjector(new FactoryModule() {

        @Override
        public void configure() {
            install(new GitModule());
            install(new DefaultUrlFormatterModule());
            install(NoteDbModule.forTest());
            bind(AllUsersName.class).toProvider(AllUsersNameProvider.class);
            bind(String.class).annotatedWith(GerritServerId.class).toInstance("gerrit");
            bind(GitRepositoryManager.class).toInstance(repoManager);
            bind(ProjectCache.class).to(NullProjectCache.class);
            bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(testConfig);
            bind(String.class).annotatedWith(AnonymousCowardName.class).toProvider(AnonymousCowardNameProvider.class);
            bind(String.class).annotatedWith(CanonicalWebUrl.class).toInstance("http://localhost:8080/");
            bind(Boolean.class).annotatedWith(EnablePeerIPInReflogRecord.class).toInstance(Boolean.FALSE);
            bind(Realm.class).to(FakeRealm.class);
            bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON);
            bind(AccountCache.class).toInstance(accountCache);
            bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class).toInstance(serverIdent);
            bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
            bind(MetricMaker.class).to(DisabledMetricMaker.class);
            bind(ExecutorService.class).annotatedWith(FanOutExecutor.class).toInstance(assertableFanOutExecutor);
            bind(ServiceUserClassifier.class).to(ServiceUserClassifier.NoOp.class);
            bind(InternalChangeQuery.class).toProvider(() -> {
                throw new UnsupportedOperationException();
            });
            bind(PatchSetApprovalUuidGenerator.class).to(TestPatchSetApprovalUuidGenerator.class);
        }
    });
    injector.injectMembers(this);
    repoManager.createRepository(allUsers);
    changeOwner = userFactory.create(co.id());
    otherUser = userFactory.create(ou.id());
    otherUserId = otherUser.getAccountId();
    internalUser = new InternalUser();
}
Also used : MetricMaker(com.google.gerrit.metrics.MetricMaker) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker) Account(com.google.gerrit.entities.Account) InMemoryRepositoryManager(com.google.gerrit.testing.InMemoryRepositoryManager) AssertableExecutorService(com.google.gerrit.testing.AssertableExecutorService) InternalUser(com.google.gerrit.server.InternalUser) GerritServerId(com.google.gerrit.server.config.GerritServerId) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) FakeAccountCache(com.google.gerrit.testing.FakeAccountCache) SystemGroupBackend(com.google.gerrit.server.group.SystemGroupBackend) FakeRealm(com.google.gerrit.server.account.FakeRealm) Realm(com.google.gerrit.server.account.Realm) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) EnablePeerIPInReflogRecord(com.google.gerrit.server.config.EnablePeerIPInReflogRecord) AnonymousCowardName(com.google.gerrit.server.config.AnonymousCowardName) CanonicalWebUrl(com.google.gerrit.server.config.CanonicalWebUrl) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) GitModule(com.google.gerrit.server.git.GitModule) FanOutExecutor(com.google.gerrit.server.FanOutExecutor) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ServiceUserClassifier(com.google.gerrit.server.account.ServiceUserClassifier) DefaultUrlFormatterModule(com.google.gerrit.server.config.DefaultUrlFormatter.DefaultUrlFormatterModule) PatchSetApprovalUuidGenerator(com.google.gerrit.server.approval.PatchSetApprovalUuidGenerator) TestPatchSetApprovalUuidGenerator(com.google.gerrit.server.approval.testing.TestPatchSetApprovalUuidGenerator) FakeAccountCache(com.google.gerrit.testing.FakeAccountCache) AccountCache(com.google.gerrit.server.account.AccountCache) ProjectCache(com.google.gerrit.server.project.ProjectCache) NullProjectCache(com.google.gerrit.server.project.NullProjectCache) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) AllUsersName(com.google.gerrit.server.config.AllUsersName) Before(org.junit.Before)

Aggregations

FactoryModule (com.google.gerrit.extensions.config.FactoryModule)14 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)5 GerritServerConfig (com.google.gerrit.server.config.GerritServerConfig)4 GitReferenceUpdated (com.google.gerrit.server.extensions.events.GitReferenceUpdated)4 PersonIdent (org.eclipse.jgit.lib.PersonIdent)4 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)3 Repository (org.eclipse.jgit.lib.Repository)3 CommentValidator (com.google.gerrit.extensions.validators.CommentValidator)2 LuceneIndexModule (com.google.gerrit.lucene.LuceneIndexModule)2 DisabledMetricMaker (com.google.gerrit.metrics.DisabledMetricMaker)2 MetricMaker (com.google.gerrit.metrics.MetricMaker)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 InternalUser (com.google.gerrit.server.InternalUser)2 AccountCache (com.google.gerrit.server.account.AccountCache)2 FakeRealm (com.google.gerrit.server.account.FakeRealm)2 Realm (com.google.gerrit.server.account.Realm)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)2 AllUsersName (com.google.gerrit.server.config.AllUsersName)2 AnonymousCowardName (com.google.gerrit.server.config.AnonymousCowardName)2