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
This commit is contained in:
Saša Živkov 2014-11-06 14:46:53 +01:00
parent 80ad102bdb
commit 10969b185d
3 changed files with 26 additions and 11 deletions

View File

@ -70,6 +70,8 @@ public class BaseInit extends SiteProgram {
protected final PluginsDistribution pluginsDistribution;
private final List<String> pluginsToInstall;
private Injector sysInjector;
protected BaseInit(PluginsDistribution pluginsDistribution,
List<String> 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<Module> modules = new ArrayList<Module>();
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<Module> modules = new ArrayList<Module>();
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) {

View File

@ -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<PluginData> 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();
}
}

View File

@ -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();
}
}