From 10969b185d9c8800cd5697993fd2b7d67e6d422a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20=C5=BDivkov?= Date: Thu, 6 Nov 2014 14:46:53 +0100 Subject: [PATCH] Inject InitStep members again before calling postRun. InitStep instances are created before a site is initialized and the set of injectable objects is quite small at that time. This also limits what could be done from the postRun method, which is invoked after site is initialized. After the site is initialized a sysInjector is already available and we could make use of it in order to perform optional injection and provide the InitStep.postRun with additional useful objects. This way the postRun can perform many more things than before. To make use of the optional injection in an InitStep one would write something like: public class MyInitStep implements InitStep { private final ConsoleUI ui; @Inject MyInitStep(ConsoleUI ui) { this.ui = ui; } @Override public void run() throws Exception { ... } @Inject(optional = true) void setGitRepositoryManager(GitRepositoryManager git) { gitManager = git; } @Override public void postRun() throws Exception { // make use of gitManager } } Change-Id: Idfd04747f45db8c7e0aee892175245ccd280d5e0 --- .../java/com/google/gerrit/pgm/BaseInit.java | 25 +++++++++++-------- .../google/gerrit/pgm/init/InitPlugins.java | 9 +++++++ .../gerrit/pgm/init/SitePathInitializer.java | 3 ++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/BaseInit.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/BaseInit.java index 76adadcc5d..5a8cb4fc8b 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/BaseInit.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/BaseInit.java @@ -70,6 +70,8 @@ public class BaseInit extends SiteProgram { protected final PluginsDistribution pluginsDistribution; private final List pluginsToInstall; + private Injector sysInjector; + protected BaseInit(PluginsDistribution pluginsDistribution, List pluginsToInstall) { this.standalone = true; @@ -111,7 +113,7 @@ public class BaseInit extends SiteProgram { run = createSiteRun(init); run.upgradeSchema(); - init.initializer.postRun(); + init.initializer.postRun(createSysInjector(init)); } catch (Exception failure) { if (init.flags.deleteOnFailure) { recursiveDelete(getSitePath()); @@ -316,15 +318,18 @@ public class BaseInit extends SiteProgram { } private Injector createSysInjector(final SiteInit init) { - final List modules = new ArrayList(); - modules.add(new AbstractModule() { - @Override - protected void configure() { - bind(ConsoleUI.class).toInstance(init.ui); - bind(InitFlags.class).toInstance(init.flags); - } - }); - return createDbInjector(SINGLE_USER).createChildInjector(modules); + if (sysInjector == null) { + final List modules = new ArrayList(); + modules.add(new AbstractModule() { + @Override + protected void configure() { + bind(ConsoleUI.class).toInstance(init.ui); + bind(InitFlags.class).toInstance(init.flags); + } + }); + sysInjector = createDbInjector(SINGLE_USER).createChildInjector(modules); + } + return sysInjector; } private static void recursiveDelete(File path) { diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitPlugins.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitPlugins.java index e5f6f561ed..add5c8d368 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitPlugins.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitPlugins.java @@ -19,6 +19,7 @@ import com.google.gerrit.pgm.util.ConsoleUI; import com.google.gerrit.server.config.SitePaths; import com.google.gerrit.server.plugins.PluginLoader; import com.google.inject.Inject; +import com.google.inject.Injector; import com.google.inject.Singleton; import java.io.File; @@ -80,6 +81,8 @@ public class InitPlugins implements InitStep { private final InitPluginStepsLoader pluginLoader; private final PluginsDistribution pluginsDistribution; + private Injector postRunInjector; + @Inject InitPlugins(final ConsoleUI ui, final SitePaths site, InitFlags initFlags, InitPluginStepsLoader pluginLoader, @@ -104,6 +107,11 @@ public class InitPlugins implements InitStep { postInitPlugins(); } + @Inject(optional = true) + void setPostRunInjector(Injector injector) { + postRunInjector = injector; + } + private void installPlugins() throws IOException { List plugins = listPlugins(site, pluginsDistribution); for (PluginData plugin : plugins) { @@ -155,6 +163,7 @@ public class InitPlugins implements InitStep { private void postInitPlugins() throws Exception { for (InitStep initStep : pluginLoader.getInitSteps()) { + postRunInjector.injectMembers(initStep); initStep.postRun(); } } diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/SitePathInitializer.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/SitePathInitializer.java index 82daf8140c..d980c09164 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/SitePathInitializer.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/SitePathInitializer.java @@ -108,12 +108,13 @@ public class SitePathInitializer { } } - public void postRun() throws Exception { + public void postRun(Injector injector) throws Exception { for (InitStep step : steps) { if (step instanceof InitPlugins && flags.skipPlugins) { continue; } + injector.injectMembers(step); step.postRun(); } }