Merge changes If7f8c086,I42a0032f,Id811f144

* changes:
  Bind PluginUser.Factory in PluginModule
  Expose the GerritRuntime to ServerPlugin
  Add GerritRuntime enum to describe the current running environment
This commit is contained in:
Maxime Guerreiro
2018-05-28 08:12:37 +00:00
committed by Gerrit Code Review
13 changed files with 96 additions and 31 deletions

View File

@@ -76,7 +76,6 @@ import com.google.gerrit.server.ChangeFinder;
import com.google.gerrit.server.CmdLineParserModule;
import com.google.gerrit.server.CreateGroupPermissionSyncer;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.Sequences;
import com.google.gerrit.server.account.AccountCacheImpl;
import com.google.gerrit.server.account.AccountControl;
@@ -262,7 +261,6 @@ public class GerritGlobalModule extends FactoryModule {
factory(MergedSender.Factory.class);
factory(MergeUtil.Factory.class);
factory(PatchScriptFactory.Factory.class);
factory(PluginUser.Factory.class);
factory(ProjectState.Factory.class);
factory(RegisterNewEmailSender.Factory.class);
factory(ReplacePatchSetSender.Factory.class);

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
/** Represents the current runtime environment in which Gerrit is running. */
public enum GerritRuntime {
/** Gerrit is running as a server, with all its features. */
DAEMON,
/** Gerrit is running from the command line, in batch mode (reindex, ...). */
BATCH
}

View File

@@ -152,7 +152,8 @@ public class JarPluginProvider implements ServerPluginProvider {
jarScanner,
description.dataDir,
pluginLoader,
pluginConfig.getString("metricsPrefix", null));
pluginConfig.getString("metricsPrefix", null),
description.gerritRuntime);
plugin.setCleanupHandle(new CleanupHandle(tmp, jarFile));
keep = true;
return plugin;

View File

@@ -33,6 +33,7 @@ import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.cache.PersistentCacheFactory;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.ConfigUtil;
import com.google.gerrit.server.config.GerritRuntime;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.plugins.ServerPluginProvider.PluginDescription;
@@ -89,6 +90,7 @@ public class PluginLoader implements LifecycleListener {
private final PersistentCacheFactory persistentCacheFactory;
private final boolean remoteAdmin;
private final UniversalServerPluginProvider serverPluginFactory;
private final GerritRuntime gerritRuntime;
@Inject
public PluginLoader(
@@ -100,7 +102,8 @@ public class PluginLoader implements LifecycleListener {
@GerritServerConfig Config cfg,
@CanonicalWebUrl Provider<String> provider,
PersistentCacheFactory cacheFactory,
UniversalServerPluginProvider pluginFactory) {
UniversalServerPluginProvider pluginFactory,
GerritRuntime gerritRuntime) {
pluginsDir = sitePaths.plugins_dir;
dataDir = sitePaths.data_dir;
tempDir = sitePaths.tmp_dir;
@@ -113,6 +116,7 @@ public class PluginLoader implements LifecycleListener {
serverPluginFactory = pluginFactory;
remoteAdmin = cfg.getBoolean("plugins", null, "allowRemoteAdmin", false);
this.gerritRuntime = gerritRuntime;
long checkFrequency =
ConfigUtil.getTimeUnit(
@@ -611,7 +615,8 @@ public class PluginLoader implements LifecycleListener {
new PluginDescription(
pluginUserFactory.create(name),
getPluginCanonicalWebUrl(name),
getPluginDataDir(name)));
getPluginDataDir(name),
gerritRuntime));
}
// Only one active plugin per plugin name can exist for each plugin name.

View File

@@ -17,10 +17,15 @@ package com.google.gerrit.server.plugins;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.systemstatus.ServerInformation;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.config.GerritRuntime;
public class PluginModule extends LifecycleModule {
@Override
protected void configure() {
requireBinding(GerritRuntime.class);
factory(PluginUser.Factory.class);
bind(ServerInformationImpl.class);
bind(ServerInformation.class).to(ServerInformationImpl.class);

View File

@@ -18,10 +18,13 @@ import static com.google.gerrit.server.plugins.PluginResource.PLUGIN_KIND;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.RestApiModule;
import com.google.gerrit.server.PluginUser;
import com.google.inject.Key;
public class PluginRestApiModule extends RestApiModule {
@Override
protected void configure() {
requireBinding(Key.get(PluginUser.Factory.class));
bind(PluginsCollection.class);
DynamicMap.mapOf(binder(), PLUGIN_KIND);
put(PLUGIN_KIND).to(InstallPlugin.Overwrite.class);

View File

@@ -21,6 +21,7 @@ import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.gerrit.extensions.registration.ReloadableRegistrationHandle;
import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.config.GerritRuntime;
import com.google.gerrit.server.util.RequestContext;
import com.google.inject.Guice;
import com.google.inject.Injector;
@@ -44,6 +45,7 @@ public class ServerPlugin extends Plugin {
private final String pluginCanonicalWebUrl;
private final ClassLoader classLoader;
private final String metricsPrefix;
private final GerritRuntime gerritRuntime;
protected Class<? extends Module> sysModule;
protected Class<? extends Module> sshModule;
protected Class<? extends Module> httpModule;
@@ -63,7 +65,8 @@ public class ServerPlugin extends Plugin {
PluginContentScanner scanner,
Path dataDir,
ClassLoader classLoader,
String metricsPrefix)
String metricsPrefix,
GerritRuntime gerritRuntime)
throws InvalidPluginException {
super(
name,
@@ -77,33 +80,12 @@ public class ServerPlugin extends Plugin {
this.classLoader = classLoader;
this.manifest = scanner == null ? null : getPluginManifest(scanner);
this.metricsPrefix = metricsPrefix;
this.gerritRuntime = gerritRuntime;
if (manifest != null) {
loadGuiceModules(manifest, classLoader);
}
}
public ServerPlugin(
String name,
String pluginCanonicalWebUrl,
PluginUser pluginUser,
Path srcJar,
FileSnapshot snapshot,
PluginContentScanner scanner,
Path dataDir,
ClassLoader classLoader)
throws InvalidPluginException {
this(
name,
pluginCanonicalWebUrl,
pluginUser,
srcJar,
snapshot,
scanner,
dataDir,
classLoader,
null);
}
private void loadGuiceModules(Manifest manifest, ClassLoader classLoader)
throws InvalidPluginException {
Attributes main = manifest.getMainAttributes();

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.plugins;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.config.GerritRuntime;
import java.nio.file.Path;
import org.eclipse.jgit.internal.storage.file.FileSnapshot;
@@ -36,6 +37,7 @@ public interface ServerPluginProvider {
public final PluginUser user;
public final String canonicalUrl;
public final Path dataDir;
final GerritRuntime gerritRuntime;
/**
* Creates a new PluginDescription for ServerPluginProvider.
@@ -43,11 +45,14 @@ public interface ServerPluginProvider {
* @param user Gerrit user for interacting with plugins
* @param canonicalUrl plugin root Web URL
* @param dataDir directory for plugin data
* @param gerritRuntime current Gerrit runtime (daemon, batch, ...)
*/
public PluginDescription(PluginUser user, String canonicalUrl, Path dataDir) {
public PluginDescription(
PluginUser user, String canonicalUrl, Path dataDir, GerritRuntime gerritRuntime) {
this.user = user;
this.canonicalUrl = canonicalUrl;
this.dataDir = dataDir;
this.gerritRuntime = gerritRuntime;
}
}

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.plugins;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.config.GerritRuntime;
import java.nio.file.Path;
public class TestServerPlugin extends ServerPlugin {
@@ -33,7 +34,17 @@ public class TestServerPlugin extends ServerPlugin {
String sshName,
Path dataDir)
throws InvalidPluginException {
super(name, pluginCanonicalWebUrl, user, null, null, null, dataDir, classloader);
super(
name,
pluginCanonicalWebUrl,
user,
null,
null,
null,
dataDir,
classloader,
null,
GerritRuntime.DAEMON);
this.classLoader = classloader;
this.sysName = sysName;
this.httpName = httpName;