Search in sources :

Example 21 with ProvisionException

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

the class IdentifiedUser method guessHost.

private String guessHost() {
    String host = null;
    SocketAddress remotePeer = null;
    try {
        remotePeer = remotePeerProvider.get();
    } catch (OutOfScopeException | ProvisionException e) {
    // Leave null.
    }
    if (remotePeer instanceof InetSocketAddress) {
        InetSocketAddress sa = (InetSocketAddress) remotePeer;
        InetAddress in = sa.getAddress();
        host = in != null ? getHost(in) : sa.getHostName();
    }
    if (Strings.isNullOrEmpty(host)) {
        return "unknown";
    }
    return host;
}
Also used : ProvisionException(com.google.inject.ProvisionException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) OutOfScopeException(com.google.inject.OutOfScopeException) InetAddress(java.net.InetAddress)

Example 22 with ProvisionException

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

the class AbstractVersionManager method fail.

private ProvisionException fail(Throwable t) {
    ProvisionException e = new ProvisionException("Error scanning indexes");
    e.initCause(t);
    return e;
}
Also used : ProvisionException(com.google.inject.ProvisionException)

Example 23 with ProvisionException

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

the class DataSourceProvider method intercept.

private DataSource intercept(String interceptor, DataSource ds) {
    if (interceptor == null) {
        return ds;
    }
    try {
        Constructor<?> c = Class.forName(interceptor).getConstructor();
        DataSourceInterceptor datasourceInterceptor = (DataSourceInterceptor) c.newInstance();
        return datasourceInterceptor.intercept("reviewDb", ds);
    } catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new ProvisionException("Cannot intercept datasource", e);
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) DataSourceInterceptor(com.google.gerrit.extensions.persistence.DataSourceInterceptor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 24 with ProvisionException

use of com.google.inject.ProvisionException 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 25 with ProvisionException

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

the class PerThreadReviewDbModule method configure.

@Override
protected void configure() {
    final List<ReviewDb> dbs = Collections.synchronizedList(new ArrayList<ReviewDb>());
    final ThreadLocal<ReviewDb> localDb = new ThreadLocal<>();
    bind(ReviewDb.class).toProvider(new Provider<ReviewDb>() {

        @Override
        public ReviewDb get() {
            ReviewDb db = localDb.get();
            if (db == null) {
                try {
                    db = schema.open();
                    dbs.add(db);
                    localDb.set(db);
                } catch (OrmException e) {
                    throw new ProvisionException("unable to open ReviewDb", e);
                }
            }
            return db;
        }
    });
    listener().toInstance(new LifecycleListener() {

        @Override
        public void start() {
        // Do nothing.
        }

        @Override
        public void stop() {
            for (ReviewDb db : dbs) {
                db.close();
            }
        }
    });
}
Also used : ProvisionException(com.google.inject.ProvisionException) OrmException(com.google.gwtorm.server.OrmException) LifecycleListener(com.google.gerrit.extensions.events.LifecycleListener) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

ProvisionException (com.google.inject.ProvisionException)57 Injector (com.google.inject.Injector)22 AbstractModule (com.google.inject.AbstractModule)20 Module (com.google.inject.Module)11 Provider (com.google.inject.Provider)9 OutOfScopeException (com.google.inject.OutOfScopeException)6 TypeLiteral (com.google.inject.TypeLiteral)6 Key (com.google.inject.Key)5 Message (com.google.inject.spi.Message)5 ImmutableList (com.google.common.collect.ImmutableList)4 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)4 OrmException (com.google.gwtorm.server.OrmException)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Provides (com.google.inject.Provides)3 Dependency (com.google.inject.spi.Dependency)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 Path (java.nio.file.Path)3