Make LifecycleModule inherit from FactoryModule

Cut down on a bunch of one-offs of install(new *Module() {...}) by
choosing a more appropriate base module class. This doesn't solve all
such cases, for example RpcServletModule must still inherit from
ServletModule, but it does clean up some code. (And who knows, fewer
modules may even mean faster startup time.)

Change-Id: Ie1aed05d0a64f7f11b121c9a637bb30d80e9a595
This commit is contained in:
Dave Borowitz 2013-11-11 13:48:24 -08:00
parent 49ef37b99b
commit ec6fca88a5
12 changed files with 33 additions and 100 deletions

View File

@ -34,7 +34,6 @@ import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.OrmRuntimeException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provides;
import com.google.inject.Singleton;
@ -46,7 +45,7 @@ import org.eclipse.jgit.lib.Config;
import java.io.File;
class InMemoryTestingDatabaseModule extends AbstractModule {
class InMemoryTestingDatabaseModule extends LifecycleModule {
private final Config cfg;
InMemoryTestingDatabaseModule(Config cfg) {
@ -71,12 +70,7 @@ class InMemoryTestingDatabaseModule extends AbstractModule {
bind(new TypeLiteral<SchemaFactory<ReviewDb>>() {})
.to(InMemoryDatabase.class);
install(new LifecycleModule() {
@Override
protected void configure() {
listener().to(CreateDatabase.class);
}
});
listener().to(CreateDatabase.class);
bind(SitePaths.class);
bind(TrackingFooters.class)

View File

@ -27,7 +27,6 @@ import com.google.gerrit.server.cache.MemoryCacheFactory;
import com.google.gerrit.server.cache.PersistentCacheFactory;
import com.google.gerrit.server.cache.h2.H2CacheImpl.ValueHolder;
import com.google.gerrit.server.config.ConfigUtil;
import com.google.gerrit.server.config.FactoryModule;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
@ -39,13 +38,7 @@ public class DefaultCacheFactory implements MemoryCacheFactory {
public static class Module extends LifecycleModule {
@Override
protected void configure() {
install(new FactoryModule() {
@Override
protected void configure() {
factory(ForwardingRemovalListener.Factory.class);
}
});
factory(ForwardingRemovalListener.Factory.class);
bind(DefaultCacheFactory.class);
bind(MemoryCacheFactory.class).to(DefaultCacheFactory.class);
bind(PersistentCacheFactory.class).to(H2CacheFactory.class);

View File

@ -31,7 +31,6 @@ import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.RemotePeer;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.FactoryModule;
import com.google.gerrit.server.config.GerritRequestModule;
import com.google.gerrit.server.contact.ContactStore;
import com.google.gerrit.server.contact.ContactStoreProvider;
@ -46,7 +45,7 @@ import com.google.inject.servlet.RequestScoped;
import java.net.SocketAddress;
public class WebModule extends FactoryModule {
public class WebModule extends LifecycleModule {
private final AuthConfig authConfig;
private final UrlModule.UrlConfig urlConfig;
private final boolean wantSSL;
@ -132,11 +131,6 @@ public class WebModule extends FactoryModule {
bind(SocketAddress.class).annotatedWith(RemotePeer.class).toProvider(
HttpRemotePeerProvider.class).in(RequestScoped.class);
install(new LifecycleModule() {
@Override
protected void configure() {
listener().toInstance(registerInParentInjectors());
}
});
listener().toInstance(registerInParentInjectors());
}
}

View File

@ -16,7 +16,6 @@ package com.google.gerrit.lucene;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.config.FactoryModule;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.index.ChangeSchemas;
import com.google.gerrit.server.index.IndexCollection;
@ -45,12 +44,7 @@ public class LuceneIndexModule extends LifecycleModule {
@Override
protected void configure() {
install(new FactoryModule() {
@Override
public void configure() {
factory(LuceneChangeIndex.Factory.class);
}
});
factory(LuceneChangeIndex.Factory.class);
install(new IndexModule(threads));
if (singleVersion == null && base == null) {
install(new MultiVersionModule());
@ -62,12 +56,7 @@ public class LuceneIndexModule extends LifecycleModule {
private class MultiVersionModule extends LifecycleModule {
@Override
public void configure() {
install(new FactoryModule() {
@Override
public void configure() {
factory(OnlineReindexer.Factory.class);
}
});
factory(OnlineReindexer.Factory.class);
listener().to(LuceneVersionManager.class);
}
}

View File

@ -15,9 +15,8 @@
package com.google.gerrit.pgm.http.jetty;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.inject.AbstractModule;
public class JettyModule extends AbstractModule {
public class JettyModule extends LifecycleModule {
private final JettyEnv env;
public JettyModule(final JettyEnv env) {
@ -28,11 +27,6 @@ public class JettyModule extends AbstractModule {
protected void configure() {
bind(JettyEnv.class).toInstance(env);
bind(JettyServer.class);
install(new LifecycleModule() {
@Override
protected void configure() {
listener().to(JettyServer.Lifecycle.class);
}
});
listener().to(JettyServer.Lifecycle.class);
}
}

View File

@ -1,7 +1,7 @@
package com.google.gerrit.lifecycle;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.inject.AbstractModule;
import com.google.gerrit.server.config.FactoryModule;
import com.google.inject.Singleton;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.internal.UniqueAnnotations;
@ -9,7 +9,7 @@ import com.google.inject.internal.UniqueAnnotations;
import java.lang.annotation.Annotation;
/** Module to support registering a unique LifecyleListener. */
public abstract class LifecycleModule extends AbstractModule {
public abstract class LifecycleModule extends FactoryModule {
/**
* Create a unique listener binding.
* <p>

View File

@ -19,7 +19,6 @@ import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@ -62,17 +61,11 @@ public class LocalDiskRepositoryManager implements GitRepositoryManager {
private static final String UNNAMED =
"Unnamed repository; edit this file to name it for gitweb.";
public static class Module extends AbstractModule {
public static class Module extends LifecycleModule {
@Override
protected void configure() {
bind(GitRepositoryManager.class).to(LocalDiskRepositoryManager.class);
install(new LifecycleModule() {
@Override
protected void configure() {
listener().to(LocalDiskRepositoryManager.Lifecycle.class);
}
});
listener().to(LocalDiskRepositoryManager.Lifecycle.class);
}
}

View File

@ -16,9 +16,8 @@ package com.google.gerrit.server.plugins;
import com.google.gerrit.extensions.systemstatus.ServerInformation;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.inject.AbstractModule;
public class PluginModule extends AbstractModule {
public class PluginModule extends LifecycleModule {
@Override
protected void configure() {
bind(ServerInformationImpl.class);
@ -28,11 +27,6 @@ public class PluginModule extends AbstractModule {
bind(PluginGuiceEnvironment.class);
bind(PluginLoader.class);
bind(CopyConfigModule.class);
install(new LifecycleModule() {
@Override
protected void configure() {
listener().to(PluginLoader.class);
}
});
listener().to(PluginLoader.class);
}
}

View File

@ -16,13 +16,13 @@ package com.google.gerrit.sshd;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.inject.AbstractModule;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.inject.binder.LinkedBindingBuilder;
import org.apache.sshd.server.Command;
/** Module to register commands in the SSH daemon. */
public abstract class CommandModule extends AbstractModule {
public abstract class CommandModule extends LifecycleModule {
/**
* Configure a command to be invoked by name.
*

View File

@ -21,7 +21,6 @@ import com.google.common.collect.Maps;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.PeerDaemonUser;
import com.google.gerrit.server.RemotePeer;
import com.google.gerrit.server.config.FactoryModule;
import com.google.gerrit.server.config.GerritRequestModule;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.git.AsyncReceiveCommits;
@ -47,7 +46,7 @@ import java.net.SocketAddress;
import java.util.Map;
/** Configures standard dependencies for {@link SshDaemon}. */
public class SshModule extends FactoryModule {
public class SshModule extends LifecycleModule {
private final Map<String, String> aliases;
@Inject
@ -87,25 +86,20 @@ public class SshModule extends FactoryModule {
install(new DefaultCommandModule());
install(new LifecycleModule() {
@Override
protected void configure() {
bind(ModuleGenerator.class).to(SshAutoRegisterModuleGenerator.class);
bind(SshPluginStarterCallback.class);
bind(StartPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(SshPluginStarterCallback.class);
bind(ModuleGenerator.class).to(SshAutoRegisterModuleGenerator.class);
bind(SshPluginStarterCallback.class);
bind(StartPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(SshPluginStarterCallback.class);
bind(ReloadPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(SshPluginStarterCallback.class);
bind(ReloadPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(SshPluginStarterCallback.class);
listener().toInstance(registerInParentInjectors());
listener().to(SshLog.class);
listener().to(SshDaemon.class);
listener().to(CommandFactoryProvider.class);
}
});
listener().toInstance(registerInParentInjectors());
listener().to(SshLog.class);
listener().to(SshDaemon.class);
listener().to(CommandFactoryProvider.class);
}
private void configureAliases() {

View File

@ -14,7 +14,6 @@
package com.google.gerrit.sshd.commands;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.sshd.CommandModule;
import com.google.gerrit.sshd.CommandName;
import com.google.gerrit.sshd.Commands;
@ -77,11 +76,6 @@ public class DefaultCommandModule extends CommandModule {
command("suexec").to(SuExec.class);
install(new LifecycleModule() {
@Override
protected void configure() {
listener().to(ShowCaches.StartupListener.class);
}
});
listener().to(ShowCaches.StartupListener.class);
}
}

View File

@ -196,16 +196,10 @@ public class WebAppInitializer extends GuiceServletContextListener
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 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(