From 3046b64911ef15e475492c2c20ae73e1a583c785 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 27 Jul 2017 06:37:00 +0100 Subject: [PATCH] Simplify InstallPlugin Create instances using a Provider, and add builder methods to set the parameters. This makes the implementation more consistent with other REST endpoints, and will make it easier to use from the plugin API. Change-Id: I5a4db8917c47c62dc5ce199ecad70c724e4e876c --- .../gerrit/server/plugins/InstallPlugin.java | 26 +++++++++++++------ .../server/plugins/PluginsCollection.java | 9 +++++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/InstallPlugin.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/InstallPlugin.java index 7f05860b91..61aed6a824 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/InstallPlugin.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/InstallPlugin.java @@ -24,6 +24,7 @@ import com.google.gerrit.extensions.restapi.Response; import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.TopLevelResource; import com.google.inject.Inject; +import com.google.inject.Provider; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; @@ -34,13 +35,23 @@ import java.util.zip.ZipException; @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) class InstallPlugin implements RestModifyView { private final PluginLoader loader; - private final String name; - private final boolean created; - InstallPlugin(PluginLoader loader, String name, boolean created) { + private String name; + private boolean created; + + @Inject + InstallPlugin(PluginLoader loader) { this.loader = loader; + } + + public InstallPlugin setName(String name) { this.name = name; + return this; + } + + public InstallPlugin setCreated(boolean created) { this.created = created; + return this; } @Override @@ -84,18 +95,17 @@ class InstallPlugin implements RestModifyView { - private final PluginLoader loader; + private final Provider install; @Inject - Overwrite(PluginLoader loader) { - this.loader = loader; + Overwrite(Provider install) { + this.install = install; } @Override public Response apply(PluginResource resource, InstallPluginInput input) throws BadRequestException, MethodNotAllowedException, IOException { - return new InstallPlugin(loader, resource.getName(), false) - .apply(TopLevelResource.INSTANCE, input); + return install.get().setName(resource.getName()).apply(TopLevelResource.INSTANCE, input); } } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginsCollection.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginsCollection.java index b8a9a9e0a4..314415e19f 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginsCollection.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginsCollection.java @@ -33,13 +33,18 @@ public class PluginsCollection private final DynamicMap> views; private final PluginLoader loader; private final Provider list; + private final Provider install; @Inject PluginsCollection( - DynamicMap> views, PluginLoader loader, Provider list) { + DynamicMap> views, + PluginLoader loader, + Provider list, + Provider install) { this.views = views; this.loader = loader; this.list = list; + this.install = install; } @Override @@ -64,7 +69,7 @@ public class PluginsCollection if (!loader.isRemoteAdminEnabled()) { throw new MethodNotAllowedException("remote installation is disabled"); } - return new InstallPlugin(loader, id.get(), true /* created */); + return install.get().setName(id.get()).setCreated(true); } @Override