Search in sources :

Example 1 with GerritServerConfigModule

use of com.google.gerrit.server.config.GerritServerConfigModule in project gerrit by GerritCodeReview.

the class SiteProgram method getDbType.

private String getDbType(Provider<DataSource> dsProvider) {
    String dbProductName;
    try (Connection conn = dsProvider.get().getConnection()) {
        dbProductName = conn.getMetaData().getDatabaseProductName().toLowerCase();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    List<Module> modules = new ArrayList<>();
    modules.add(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
        }
    });
    modules.add(new GerritServerConfigModule());
    modules.add(new DataSourceModule());
    Injector i = Guice.createInjector(modules);
    List<Binding<DataSourceType>> dsTypeBindings = i.findBindingsByType(new TypeLiteral<DataSourceType>() {
    });
    for (Binding<DataSourceType> binding : dsTypeBindings) {
        Annotation annotation = binding.getKey().getAnnotation();
        if (annotation instanceof Named) {
            if (((Named) annotation).value().toLowerCase().contains(dbProductName)) {
                return ((Named) annotation).value();
            }
        }
    }
    throw new IllegalStateException(String.format("Cannot guess database type from the database product name '%s'", dbProductName));
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) Binding(com.google.inject.Binding) Named(com.google.inject.name.Named) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) Injector(com.google.inject.Injector) DataSourceType(com.google.gerrit.server.schema.DataSourceType) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) Module(com.google.inject.Module) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule) DatabaseModule(com.google.gerrit.server.schema.DatabaseModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SchemaModule(com.google.gerrit.server.schema.SchemaModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) AbstractModule(com.google.inject.AbstractModule) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule)

Example 2 with GerritServerConfigModule

use of com.google.gerrit.server.config.GerritServerConfigModule 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 GerritServerConfigModule

use of com.google.gerrit.server.config.GerritServerConfigModule in project gerrit by GerritCodeReview.

the class BaseInit method createSiteInit.

private SiteInit createSiteInit() {
    final ConsoleUI ui = getConsoleUI();
    final Path sitePath = getSitePath();
    final List<Module> m = new ArrayList<>();
    final SecureStoreInitData secureStoreInitData = discoverSecureStoreClass();
    final String currentSecureStoreClassName = getConfiguredSecureStoreClass();
    if (secureStoreInitData != null && currentSecureStoreClassName != null && !currentSecureStoreClassName.equals(secureStoreInitData.className)) {
        String err = String.format("Different secure store was previously configured: %s. " + "Use SwitchSecureStore program to switch between implementations.", currentSecureStoreClassName);
        throw die(err);
    }
    m.add(new GerritServerConfigModule());
    m.add(new InitModule(standalone));
    m.add(new AbstractModule() {

        @Override
        protected void configure() {
            bind(ConsoleUI.class).toInstance(ui);
            bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
            List<String> plugins = MoreObjects.firstNonNull(getInstallPlugins(), new ArrayList<>());
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(InstallPlugins.class).toInstance(plugins);
            bind(new TypeLiteral<Boolean>() {
            }).annotatedWith(InstallAllPlugins.class).toInstance(installAllPlugins());
            bind(PluginsDistribution.class).toInstance(pluginsDistribution);
            String secureStoreClassName;
            if (secureStoreInitData != null) {
                secureStoreClassName = secureStoreInitData.className;
            } else {
                secureStoreClassName = currentSecureStoreClassName;
            }
            if (secureStoreClassName != null) {
                ui.message("Using secure store: %s\n", secureStoreClassName);
            }
            bind(SecureStoreInitData.class).toProvider(Providers.of(secureStoreInitData));
            bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(secureStoreClassName));
            bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(LibraryDownload.class).toInstance(getSkippedDownloads());
            bind(Boolean.class).annotatedWith(LibraryDownload.class).toInstance(skipAllDownloads());
            bind(MetricMaker.class).to(DisabledMetricMaker.class);
        }
    });
    try {
        return Guice.createInjector(PRODUCTION, m).getInstance(SiteInit.class);
    } catch (CreationException ce) {
        final Message first = ce.getErrorMessages().iterator().next();
        Throwable why = first.getCause();
        if (why instanceof Die) {
            throw (Die) why;
        }
        final StringBuilder buf = new StringBuilder(ce.getMessage());
        while (why != null) {
            buf.append("\n");
            buf.append(why.getMessage());
            why = why.getCause();
            if (why != null) {
                buf.append("\n  caused by ");
            }
        }
        throw die(buf.toString(), new RuntimeException("InitInjector failed", ce));
    }
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) Die(com.google.gerrit.common.Die) Message(com.google.inject.spi.Message) ArrayList(java.util.ArrayList) CreationException(com.google.inject.CreationException) SecureStore(com.google.gerrit.server.securestore.SecureStore) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) TypeLiteral(com.google.inject.TypeLiteral) List(java.util.List) ArrayList(java.util.ArrayList) Module(com.google.inject.Module) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) IndexModule(com.google.gerrit.server.index.IndexModule) AbstractModule(com.google.inject.AbstractModule) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker)

Example 4 with GerritServerConfigModule

use of com.google.gerrit.server.config.GerritServerConfigModule in project gerrit by GerritCodeReview.

the class SiteProgram method createDbInjector.

/**
 * Provides database connectivity and site path.
 */
protected Injector createDbInjector(boolean enableMetrics) {
    List<Module> modules = new ArrayList<>();
    Module sitePathModule = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
            bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(getConfiguredSecureStoreClass()));
        }
    };
    modules.add(sitePathModule);
    if (enableMetrics) {
        modules.add(new DropWizardMetricMaker.ApiModule());
    } else {
        modules.add(new AbstractModule() {

            @Override
            protected void configure() {
                bind(MetricMaker.class).to(DisabledMetricMaker.class);
            }
        });
    }
    modules.add(new LifecycleModule() {

        @Override
        protected void configure() {
            listener().to(SystemReaderInstaller.class);
        }
    });
    Module configModule = new GerritServerConfigModule();
    modules.add(configModule);
    modules.add(new AbstractModule() {

        @Override
        protected void configure() {
            bind(GerritRuntime.class).toInstance(getGerritRuntime());
        }
    });
    Injector cfgInjector = Guice.createInjector(sitePathModule, configModule);
    modules.add(new SchemaModule());
    modules.add(cfgInjector.getInstance(GitRepositoryManagerModule.class));
    // The only implementation of experiments is available in all programs that can use
    // gerrit.config
    modules.add(new ConfigExperimentFeaturesModule());
    try {
        return Guice.createInjector(PRODUCTION, ModuleOverloader.override(modules, LibModuleLoader.loadModules(cfgInjector, LibModuleType.DB_MODULE_TYPE)));
    } catch (CreationException ce) {
        Message first = ce.getErrorMessages().iterator().next();
        Throwable why = first.getCause();
        StringBuilder buf = new StringBuilder();
        if (why != null) {
            buf.append(why.getMessage());
            why = why.getCause();
        } else {
            buf.append(first.getMessage());
        }
        while (why != null) {
            buf.append("\n  caused by ");
            buf.append(why.toString());
            why = why.getCause();
        }
        throw die(buf.toString(), new RuntimeException("DbInjector failed", ce));
    }
}
Also used : SchemaModule(com.google.gerrit.server.schema.SchemaModule) Message(com.google.inject.spi.Message) GerritServerConfigModule.getSecureStoreClassName(com.google.gerrit.server.config.GerritServerConfigModule.getSecureStoreClassName) SecureStoreClassName(com.google.gerrit.server.securestore.SecureStoreClassName) ArrayList(java.util.ArrayList) CreationException(com.google.inject.CreationException) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) Injector(com.google.inject.Injector) ConfigExperimentFeaturesModule(com.google.gerrit.server.experiments.ConfigExperimentFeatures.ConfigExperimentFeaturesModule) DropWizardMetricMaker(com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker) SitePath(com.google.gerrit.server.config.SitePath) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) Module(com.google.inject.Module) ConfigExperimentFeaturesModule(com.google.gerrit.server.experiments.ConfigExperimentFeatures.ConfigExperimentFeaturesModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SchemaModule(com.google.gerrit.server.schema.SchemaModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) AbstractModule(com.google.inject.AbstractModule) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SystemReaderInstaller(com.google.gerrit.server.git.SystemReaderInstaller)

Example 5 with GerritServerConfigModule

use of com.google.gerrit.server.config.GerritServerConfigModule in project gerrit by GerritCodeReview.

the class WebAppInitializer method createDbInjector.

private Injector createDbInjector() {
    final List<Module> modules = new ArrayList<>();
    AbstractModule secureStore = createSecureStoreModule();
    modules.add(secureStore);
    if (sitePath != null) {
        Module sitePathModule = new AbstractModule() {

            @Override
            protected void configure() {
                bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
            }
        };
        modules.add(sitePathModule);
        Module configModule = new GerritServerConfigModule();
        modules.add(configModule);
        Injector cfgInjector = Guice.createInjector(sitePathModule, configModule, secureStore);
        Config cfg = cfgInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
        String dbType = cfg.getString("database", null, "type");
        final DataSourceType dst = Guice.createInjector(new DataSourceModule(), configModule, sitePathModule, secureStore).getInstance(Key.get(DataSourceType.class, Names.named(dbType.toLowerCase())));
        modules.add(new LifecycleModule() {

            @Override
            protected void configure() {
                bind(DataSourceType.class).toInstance(dst);
                bind(DataSourceProvider.Context.class).toInstance(DataSourceProvider.Context.MULTI_USER);
                bind(Key.get(DataSource.class, Names.named("ReviewDb"))).toProvider(DataSourceProvider.class).in(SINGLETON);
                listener().to(DataSourceProvider.class);
            }
        });
    } else {
        modules.add(new LifecycleModule() {

            @Override
            protected void configure() {
                bind(Key.get(DataSource.class, Names.named("ReviewDb"))).toProvider(ReviewDbDataSourceProvider.class).in(SINGLETON);
                listener().to(ReviewDbDataSourceProvider.class);
            }
        });
        // If we didn't get the site path from the system property
        // we need to get it from the database, as that's our old
        // method of locating the site path on disk.
        //
        modules.add(new AbstractModule() {

            @Override
            protected void configure() {
                bind(Path.class).annotatedWith(SitePath.class).toProvider(SitePathFromSystemConfigProvider.class).in(SINGLETON);
            }
        });
        modules.add(new GerritServerConfigModule());
    }
    modules.add(new DatabaseModule());
    modules.add(new ConfigNotesMigration.Module());
    modules.add(new DropWizardMetricMaker.ApiModule());
    return Guice.createInjector(PRODUCTION, modules);
}
Also used : GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Config(org.eclipse.jgit.lib.Config) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) AuthConfig(com.google.gerrit.server.config.AuthConfig) DownloadConfig(com.google.gerrit.server.config.DownloadConfig) FilterConfig(javax.servlet.FilterConfig) ArrayList(java.util.ArrayList) DataSourceProvider(com.google.gerrit.server.schema.DataSourceProvider) DatabaseModule(com.google.gerrit.server.schema.DatabaseModule) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) Injector(com.google.inject.Injector) DataSourceType(com.google.gerrit.server.schema.DataSourceType) ConfigNotesMigration(com.google.gerrit.server.notedb.ConfigNotesMigration) DropWizardMetricMaker(com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker) SitePath(com.google.gerrit.server.config.SitePath) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) AuthConfigModule(com.google.gerrit.server.config.AuthConfigModule) Module(com.google.inject.Module) CanonicalWebUrlModule(com.google.gerrit.server.config.CanonicalWebUrlModule) GerritGlobalModule(com.google.gerrit.server.config.GerritGlobalModule) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule) DatabaseModule(com.google.gerrit.server.schema.DatabaseModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) LuceneIndexModule(com.google.gerrit.lucene.LuceneIndexModule) DefaultPermissionBackendModule(com.google.gerrit.server.project.DefaultPermissionBackendModule) OAuthModule(com.google.gerrit.httpd.auth.oauth.OAuthModule) SchemaModule(com.google.gerrit.server.schema.SchemaModule) MimeUtil2Module(com.google.gerrit.server.mime.MimeUtil2Module) GarbageCollectionModule(com.google.gerrit.server.git.GarbageCollectionModule) SshModule(com.google.gerrit.sshd.SshModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) SshAddressesModule(com.google.gerrit.server.ssh.SshAddressesModule) HttpPluginModule(com.google.gerrit.httpd.plugins.HttpPluginModule) DefaultCommandModule(com.google.gerrit.sshd.commands.DefaultCommandModule) PluginRestApiModule(com.google.gerrit.server.plugins.PluginRestApiModule) ReceiveCommitsExecutorModule(com.google.gerrit.server.git.ReceiveCommitsExecutorModule) ElasticIndexModule(com.google.gerrit.elasticsearch.ElasticIndexModule) IndexModule(com.google.gerrit.server.index.IndexModule) StaticModule(com.google.gerrit.httpd.raw.StaticModule) RestCacheAdminModule(com.google.gerrit.server.config.RestCacheAdminModule) IndexCommandsModule(com.google.gerrit.sshd.commands.IndexCommandsModule) DiffExecutorModule(com.google.gerrit.server.patch.DiffExecutorModule) NoSshModule(com.google.gerrit.server.ssh.NoSshModule) GpgModule(com.google.gerrit.gpg.GpgModule) OpenIdModule(com.google.gerrit.httpd.auth.openid.OpenIdModule) SshHostKeyModule(com.google.gerrit.sshd.SshHostKeyModule) AbstractModule(com.google.inject.AbstractModule) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule)

Aggregations

GerritServerConfigModule (com.google.gerrit.server.config.GerritServerConfigModule)9 SitePath (com.google.gerrit.server.config.SitePath)9 Module (com.google.inject.Module)9 ArrayList (java.util.ArrayList)9 AbstractModule (com.google.inject.AbstractModule)8 SchemaModule (com.google.gerrit.server.schema.SchemaModule)6 Path (java.nio.file.Path)6 LifecycleModule (com.google.gerrit.lifecycle.LifecycleModule)5 GitRepositoryManagerModule (com.google.gerrit.server.git.GitRepositoryManagerModule)5 DisabledMetricMaker (com.google.gerrit.metrics.DisabledMetricMaker)4 DropWizardMetricMaker (com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker)4 Injector (com.google.inject.Injector)4 IndexModule (com.google.gerrit.server.index.IndexModule)3 DataSourceModule (com.google.gerrit.server.schema.DataSourceModule)3 DataSourceType (com.google.gerrit.server.schema.DataSourceType)3 DatabaseModule (com.google.gerrit.server.schema.DatabaseModule)3 GpgModule (com.google.gerrit.gpg.GpgModule)2 OAuthModule (com.google.gerrit.httpd.auth.oauth.OAuthModule)2 OpenIdModule (com.google.gerrit.httpd.auth.openid.OpenIdModule)2 HttpPluginModule (com.google.gerrit.httpd.plugins.HttpPluginModule)2