use of com.google.gerrit.extensions.events.LifecycleListener 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();
}
}
});
}
use of com.google.gerrit.extensions.events.LifecycleListener in project gerrit by GerritCodeReview.
the class LifecycleManager method stop.
/** Stop all listeners, in the reverse order they were registered. */
public void stop() {
for (int i = handles.size() - 1; 0 <= i; i--) {
handles.get(i).remove();
}
handles.clear();
for (int i = startedIndex; 0 <= i; i--) {
LifecycleListener obj = listeners.get(i).get();
try {
obj.stop();
} catch (Throwable err) {
LoggerFactory.getLogger(obj.getClass()).warn("Failed to stop", err);
}
startedIndex = i - 1;
}
}
use of com.google.gerrit.extensions.events.LifecycleListener in project gerrit by GerritCodeReview.
the class PrivateInternals_DynamicTypes method registerInParentInjectors.
public static LifecycleListener registerInParentInjectors() {
return new LifecycleListener() {
private List<RegistrationHandle> handles;
@Inject
private Injector self;
@Override
public void start() {
handles = new ArrayList<>(4);
Injector parent = self.getParent();
while (parent != null) {
handles.addAll(attachSets(self, dynamicSetsOf(parent)));
handles.addAll(attachMaps(self, "gerrit", dynamicMapsOf(parent)));
parent = parent.getParent();
}
if (handles.isEmpty()) {
handles = null;
}
}
@Override
public void stop() {
remove(handles);
handles = null;
}
};
}
use of com.google.gerrit.extensions.events.LifecycleListener in project gerrit by GerritCodeReview.
the class SiteProgram method createDbInjector.
/** @return provides database connectivity and site path. */
protected Injector createDbInjector(final boolean enableMetrics, final DataSourceProvider.Context context) {
final Path sitePath = getSitePath();
final List<Module> modules = new ArrayList<>();
Module sitePathModule = new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
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() {
bind(DataSourceProvider.Context.class).toInstance(context);
if (dsProvider != null) {
bind(Key.get(DataSource.class, Names.named("ReviewDb"))).toProvider(dsProvider).in(SINGLETON);
if (LifecycleListener.class.isAssignableFrom(dsProvider.getClass())) {
listener().toInstance((LifecycleListener) dsProvider);
}
} else {
bind(Key.get(DataSource.class, Names.named("ReviewDb"))).toProvider(SiteLibraryBasedDataSourceProvider.class).in(SINGLETON);
listener().to(SiteLibraryBasedDataSourceProvider.class);
}
}
});
Module configModule = new GerritServerConfigModule();
modules.add(configModule);
Injector cfgInjector = Guice.createInjector(sitePathModule, configModule);
Config cfg = cfgInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
String dbType;
if (dsProvider != null) {
dbType = getDbType(dsProvider);
} else {
dbType = cfg.getString("database", null, "type");
}
if (dbType == null) {
throw new ProvisionException("database.type must be defined");
}
final DataSourceType dst = Guice.createInjector(new DataSourceModule(), configModule, sitePathModule).getInstance(Key.get(DataSourceType.class, Names.named(dbType.toLowerCase())));
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(DataSourceType.class).toInstance(dst);
}
});
modules.add(new DatabaseModule());
modules.add(new SchemaModule());
modules.add(cfgInjector.getInstance(GitRepositoryManagerModule.class));
modules.add(new ConfigNotesMigration.Module());
modules.addAll(LibModuleLoader.loadModules(cfgInjector));
try {
return Guice.createInjector(PRODUCTION, modules);
} catch (CreationException ce) {
final Message first = ce.getErrorMessages().iterator().next();
Throwable why = first.getCause();
if (why instanceof SQLException) {
throw die("Cannot connect to SQL database", why);
}
if (why instanceof OrmException && why.getCause() != null && "Unable to determine driver URL".equals(why.getMessage())) {
why = why.getCause();
if (isCannotCreatePoolException(why)) {
throw die("Cannot connect to SQL database", why.getCause());
}
throw die("Cannot connect to SQL database", why);
}
final 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));
}
}
use of com.google.gerrit.extensions.events.LifecycleListener in project gerrit by GerritCodeReview.
the class LifecycleManager method start.
/** Start all listeners, in the order they were registered. */
public void start() {
for (int i = startedIndex + 1; i < listeners.size(); i++) {
LifecycleListener listener = listeners.get(i).get();
startedIndex = i;
listener.start();
}
}
Aggregations