Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.google.gerrit.server.config.PluginConfig Maven / Gradle / Ivy
package com.google.gerrit.server.config;
import static com.google.common.base.Preconditions.checkState;
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.entities.AccountGroup;
import com.google.gerrit.entities.CachedProjectConfig;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.server.project.ProjectConfig;
import com.google.gerrit.server.project.ProjectState;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
@AutoValue
public abstract class PluginConfig {
private static final String PLUGIN = "plugin" ;
protected abstract String pluginName () ;
protected abstract Config cfg () ;
protected abstract Optional projectConfig () ;
protected abstract ImmutableMap groupReferences () ;
public static PluginConfig create (
String pluginName, Config cfg, @Nullable CachedProjectConfig projectConfig) {
ImmutableMap.Builder groupReferences =
ImmutableMap.builder();
if (projectConfig != null ) {
groupReferences.putAll(projectConfig.getGroups());
}
return new AutoValue_PluginConfig(
pluginName, copyConfig(cfg), Optional.ofNullable(projectConfig), groupReferences.build());
}
public static PluginConfig createFromGerritConfig (String pluginName, Config cfg) {
return new AutoValue_PluginConfig(pluginName, cfg, Optional.empty(), ImmutableMap.of());
}
PluginConfig withInheritance (ProjectState.Factory projectStateFactory) {
checkState(projectConfig().isPresent(), "no project config provided" );
ProjectState state = projectStateFactory.create(projectConfig().get());
ProjectState parent = Iterables.getFirst(state.parents(), null );
if (parent == null ) {
return this ;
}
Map groupReferences = new HashMap<>();
groupReferences.putAll(groupReferences());
PluginConfig parentPluginConfig =
parent.getPluginConfig(pluginName()).withInheritance(projectStateFactory);
Set allNames = cfg().getNames(PLUGIN, pluginName());
Config newCfg = copyConfig(cfg());
for (String name : parentPluginConfig.cfg().getNames(PLUGIN, pluginName())) {
if (!allNames.contains(name)) {
List values =
Arrays.asList(parentPluginConfig.cfg().getStringList(PLUGIN, pluginName(), name));
for (String value : values) {
Optional groupRef =
parentPluginConfig
.projectConfig()
.get()
.getGroupByName(GroupReference.extractGroupName(value));
if (groupRef.isPresent()) {
groupReferences.putIfAbsent(groupRef.get().getUUID(), groupRef.get());
}
}
newCfg.setStringList(PLUGIN, pluginName(), name, values);
}
}
return new AutoValue_PluginConfig(
pluginName(), newCfg, projectConfig(), ImmutableMap.copyOf(groupReferences));
}
private static Config copyConfig (Config cfg) {
Config copiedCfg = new Config();
try {
copiedCfg.fromText(cfg.toText());
} catch (ConfigInvalidException e) {
throw new IllegalStateException(e);
}
return copiedCfg;
}
public String getString (String name) {
return cfg().getString(PLUGIN, pluginName(), name);
}
public String getString (String name, String defaultValue) {
if (defaultValue == null ) {
return cfg().getString(PLUGIN, pluginName(), name);
}
return MoreObjects.firstNonNull(cfg().getString(PLUGIN, pluginName(), name), defaultValue);
}
public String[] getStringList(String name) {
return cfg().getStringList(PLUGIN, pluginName(), name);
}
public int getInt (String name, int defaultValue) {
return cfg().getInt(PLUGIN, pluginName(), name, defaultValue);
}
public long getLong (String name, long defaultValue) {
return cfg().getLong(PLUGIN, pluginName(), name, defaultValue);
}
public boolean getBoolean (String name, boolean defaultValue) {
return cfg().getBoolean(PLUGIN, pluginName(), name, defaultValue);
}
public > T getEnum (String name, T defaultValue) {
return cfg().getEnum(PLUGIN, pluginName(), name, defaultValue);
}
public > T getEnum (T[] all, String name, T defaultValue) {
return cfg().getEnum(all, PLUGIN, pluginName(), name, defaultValue);
}
public Set getNames () {
return cfg().getNames(PLUGIN, pluginName(), true );
}
public Optional getGroupReference (String name) {
String exactName = GroupReference.extractGroupName(getString(name));
return groupReferences().values().stream().filter(g -> g.getName().equals(exactName)).findAny();
}
public static class Update {
private final String pluginName;
private Config cfg;
private final Optional projectConfig;
public Update (String pluginName, Config cfg, Optional projectConfig) {
this .pluginName = pluginName;
this .cfg = cfg;
this .projectConfig = projectConfig;
}
@VisibleForTesting
public static Update forTest (String pluginName, Config cfg) {
return new Update(pluginName, cfg, Optional.empty());
}
public PluginConfig asPluginConfig () {
return PluginConfig.create(
pluginName, cfg, projectConfig.map(ProjectConfig::getCacheable).orElse(null ));
}
public String getString (String name) {
return cfg.getString(PLUGIN, pluginName, name);
}
public String getString (String name, String defaultValue) {
if (defaultValue == null ) {
return cfg.getString(PLUGIN, pluginName, name);
}
return MoreObjects.firstNonNull(cfg.getString(PLUGIN, pluginName, name), defaultValue);
}
public String[] getStringList(String name) {
return cfg.getStringList(PLUGIN, pluginName, name);
}
public int getInt (String name, int defaultValue) {
return cfg.getInt(PLUGIN, pluginName, name, defaultValue);
}
public long getLong (String name, long defaultValue) {
return cfg.getLong(PLUGIN, pluginName, name, defaultValue);
}
public boolean getBoolean (String name, boolean defaultValue) {
return cfg.getBoolean(PLUGIN, pluginName, name, defaultValue);
}
public > T getEnum (String name, T defaultValue) {
return cfg.getEnum(PLUGIN, pluginName, name, defaultValue);
}
public > T getEnum (T[] all, String name, T defaultValue) {
return cfg.getEnum(all, PLUGIN, pluginName, name, defaultValue);
}
public Set getNames () {
return cfg.getNames(PLUGIN, pluginName, true );
}
public void setString (String name, String value) {
if (Strings.isNullOrEmpty(value)) {
cfg.unset(PLUGIN, pluginName, name);
} else {
cfg.setString(PLUGIN, pluginName, name, value);
}
}
public void setStringList (String name, List values) {
if (values == null || values.isEmpty()) {
cfg.unset(PLUGIN, pluginName, name);
} else {
cfg.setStringList(PLUGIN, pluginName, name, values);
}
}
public void setInt (String name, int value) {
cfg.setInt(PLUGIN, pluginName, name, value);
}
public void setLong (String name, long value) {
cfg.setLong(PLUGIN, pluginName, name, value);
}
public void setBoolean (String name, boolean value) {
cfg.setBoolean(PLUGIN, pluginName, name, value);
}
public > void setEnum (String name, T value) {
cfg.setEnum(PLUGIN, pluginName, name, value);
}
public void unset (String name) {
cfg.unset(PLUGIN, pluginName, name);
}
public void setGroupReference (String name, GroupReference value) {
checkState(projectConfig.isPresent(), "no project config provided" );
GroupReference groupRef = projectConfig.get().resolve(value);
setString(name, groupRef.toConfigValue());
}
}
}