Search in sources :

Example 11 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class PutConfig method setPluginConfigValues.

private void setPluginConfigValues(ProjectState projectState, ProjectConfig projectConfig, Map<String, Map<String, ConfigValue>> pluginConfigValues) throws BadRequestException {
    for (Entry<String, Map<String, ConfigValue>> e : pluginConfigValues.entrySet()) {
        String pluginName = e.getKey();
        PluginConfig cfg = projectConfig.getPluginConfig(pluginName);
        for (Entry<String, ConfigValue> v : e.getValue().entrySet()) {
            ProjectConfigEntry projectConfigEntry = pluginConfigEntries.get(pluginName, v.getKey());
            if (projectConfigEntry != null) {
                if (!isValidParameterName(v.getKey())) {
                    log.warn(String.format("Parameter name '%s' must match '^[a-zA-Z0-9]+[a-zA-Z0-9-]*$'", v.getKey()));
                    continue;
                }
                String oldValue = cfg.getString(v.getKey());
                String value = v.getValue().value;
                if (projectConfigEntry.getType() == ProjectConfigEntryType.ARRAY) {
                    List<String> l = Arrays.asList(cfg.getStringList(v.getKey()));
                    oldValue = Joiner.on("\n").join(l);
                    value = Joiner.on("\n").join(v.getValue().values);
                }
                if (Strings.emptyToNull(value) != null) {
                    if (!value.equals(oldValue)) {
                        validateProjectConfigEntryIsEditable(projectConfigEntry, projectState, v.getKey(), pluginName);
                        v.setValue(projectConfigEntry.preUpdate(v.getValue()));
                        value = v.getValue().value;
                        try {
                            switch(projectConfigEntry.getType()) {
                                case BOOLEAN:
                                    boolean newBooleanValue = Boolean.parseBoolean(value);
                                    cfg.setBoolean(v.getKey(), newBooleanValue);
                                    break;
                                case INT:
                                    int newIntValue = Integer.parseInt(value);
                                    cfg.setInt(v.getKey(), newIntValue);
                                    break;
                                case LONG:
                                    long newLongValue = Long.parseLong(value);
                                    cfg.setLong(v.getKey(), newLongValue);
                                    break;
                                case LIST:
                                    if (!projectConfigEntry.getPermittedValues().contains(value)) {
                                        throw new BadRequestException(String.format("The value '%s' is not permitted for parameter '%s' of plugin '" + pluginName + "'", value, v.getKey()));
                                    }
                                //$FALL-THROUGH$
                                case STRING:
                                    cfg.setString(v.getKey(), value);
                                    break;
                                case ARRAY:
                                    cfg.setStringList(v.getKey(), v.getValue().values);
                                    break;
                                default:
                                    log.warn(String.format("The type '%s' of parameter '%s' is not supported.", projectConfigEntry.getType().name(), v.getKey()));
                            }
                        } catch (NumberFormatException ex) {
                            throw new BadRequestException(String.format("The value '%s' of config parameter '%s' of plugin '%s' is invalid: %s", v.getValue(), v.getKey(), pluginName, ex.getMessage()));
                        }
                    }
                } else {
                    if (oldValue != null) {
                        validateProjectConfigEntryIsEditable(projectConfigEntry, projectState, v.getKey(), pluginName);
                        cfg.unset(v.getKey());
                    }
                }
            } else {
                throw new BadRequestException(String.format("The config parameter '%s' of plugin '%s' does not exist.", v.getKey(), pluginName));
            }
        }
    }
}
Also used : ConfigValue(com.google.gerrit.extensions.api.projects.ConfigValue) PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Example 12 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ProjectConfig method loadPluginSections.

private void loadPluginSections(Config rc) {
    pluginConfigs = new HashMap<>();
    for (String plugin : rc.getSubsections(PLUGIN)) {
        Config pluginConfig = new Config();
        pluginConfigs.put(plugin, pluginConfig);
        for (String name : rc.getNames(PLUGIN, plugin)) {
            String value = rc.getString(PLUGIN, plugin, name);
            String groupName = GroupReference.extractGroupName(value);
            if (groupName != null) {
                GroupReference ref = groupList.byName(groupName);
                if (ref == null) {
                    error(String.format("group \"%s\" not in %s", groupName, GroupList.FILE_NAME));
                }
                rc.setString(PLUGIN, plugin, name, value);
            }
            pluginConfig.setStringList(PLUGIN, plugin, name, Arrays.asList(rc.getStringList(PLUGIN, plugin, name)));
        }
    }
}
Also used : Config(org.eclipse.jgit.lib.Config) PluginConfig(com.google.gerrit.server.config.PluginConfig) NotifyConfig(com.google.gerrit.entities.NotifyConfig) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) StoredConfig(org.eclipse.jgit.lib.StoredConfig) GroupReference(com.google.gerrit.entities.GroupReference)

Example 13 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ConfigInfoCreator method getInheritedValue.

private static String getInheritedValue(ProjectState project, PluginConfigFactory cfgFactory, Extension<ProjectConfigEntry> e) {
    ProjectConfigEntry configEntry = e.getProvider().get();
    ProjectState parent = Iterables.getFirst(project.parents(), null);
    String inheritedValue = configEntry.getDefaultValue();
    if (parent != null) {
        PluginConfig parentCfgWithInheritance = cfgFactory.getFromProjectConfigWithInheritance(parent, e.getPluginName());
        inheritedValue = parentCfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue());
    }
    return inheritedValue;
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 14 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ConfigInfoCreator method getPluginConfig.

private static Map<String, Map<String, ConfigParameterInfo>> getPluginConfig(ProjectState project, DynamicMap<ProjectConfigEntry> pluginConfigEntries, PluginConfigFactory cfgFactory, AllProjectsName allProjects) {
    TreeMap<String, Map<String, ConfigParameterInfo>> pluginConfig = new TreeMap<>();
    for (Extension<ProjectConfigEntry> e : pluginConfigEntries) {
        ProjectConfigEntry configEntry = e.getProvider().get();
        PluginConfig cfg = cfgFactory.getFromProjectConfig(project, e.getPluginName());
        String configuredValue = cfg.getString(e.getExportName());
        ConfigParameterInfo p = new ConfigParameterInfo();
        p.displayName = configEntry.getDisplayName();
        p.description = configEntry.getDescription();
        p.warning = configEntry.getWarning(project);
        p.type = configEntry.getType();
        p.permittedValues = configEntry.getPermittedValues();
        p.editable = configEntry.isEditable(project) ? true : null;
        if (configEntry.isInheritable() && !allProjects.equals(project.getNameKey())) {
            PluginConfig cfgWithInheritance = cfgFactory.getFromProjectConfigWithInheritance(project, e.getPluginName());
            p.inheritable = true;
            p.value = configEntry.onRead(project, cfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue()));
            p.configuredValue = configuredValue;
            p.inheritedValue = getInheritedValue(project, cfgFactory, e);
        } else {
            if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
                p.values = configEntry.onRead(project, Arrays.asList(cfg.getStringList(e.getExportName())));
            } else {
                p.value = configEntry.onRead(project, configuredValue != null ? configuredValue : configEntry.getDefaultValue());
            }
        }
        Map<String, ConfigParameterInfo> pc = pluginConfig.get(e.getPluginName());
        if (pc == null) {
            pc = new TreeMap<>();
            pluginConfig.put(e.getPluginName(), pc);
        }
        pc.put(e.getExportName(), p);
    }
    return !pluginConfig.isEmpty() ? pluginConfig : null;
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) ConfigParameterInfo(com.google.gerrit.extensions.api.projects.ConfigInfo.ConfigParameterInfo)

Example 15 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class JarPluginProvider method loadJarPlugin.

private ServerPlugin loadJarPlugin(String name, Path srcJar, FileSnapshot snapshot, Path tmp, PluginDescription description) throws IOException, InvalidPluginException, MalformedURLException {
    JarFile jarFile = new JarFile(tmp.toFile());
    boolean keep = false;
    try {
        Manifest manifest = jarFile.getManifest();
        Plugin.ApiType type = Plugin.getApiType(manifest);
        List<URL> urls = new ArrayList<>(2);
        String overlay = System.getProperty("gerrit.plugin-classes");
        if (overlay != null) {
            Path classes = Paths.get(overlay).resolve(name).resolve("main");
            if (Files.isDirectory(classes)) {
                logger.atInfo().log("plugin %s: including %s", name, classes);
                urls.add(classes.toUri().toURL());
            }
        }
        urls.add(tmp.toUri().toURL());
        ClassLoader pluginLoader = URLClassLoader.newInstance(urls.toArray(new URL[urls.size()]), PluginUtil.parentFor(type));
        JarScanner jarScanner = createJarScanner(tmp);
        PluginConfig pluginConfig = configFactory.getFromGerritConfig(name);
        ServerPlugin plugin = new ServerPlugin(name, description.canonicalUrl, description.user, srcJar, snapshot, jarScanner, description.dataDir, pluginLoader, pluginConfig.getString("metricsPrefix", null), description.gerritRuntime);
        plugin.setCleanupHandle(new CleanupHandle(tmp, jarFile));
        keep = true;
        return plugin;
    } finally {
        if (!keep) {
            jarFile.close();
        }
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) URL(java.net.URL) PluginConfig(com.google.gerrit.server.config.PluginConfig) URLClassLoader(java.net.URLClassLoader)

Aggregations

PluginConfig (com.google.gerrit.server.config.PluginConfig)19 ProjectConfigEntry (com.google.gerrit.server.config.ProjectConfigEntry)7 Config (org.eclipse.jgit.lib.Config)6 Test (org.junit.Test)5 GroupReference (com.google.gerrit.entities.GroupReference)4 Map (java.util.Map)4 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)3 CachedProjectConfig (com.google.gerrit.entities.CachedProjectConfig)3 NotifyConfig (com.google.gerrit.entities.NotifyConfig)3 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)3 LinkedHashMap (java.util.LinkedHashMap)3 StoredConfig (org.eclipse.jgit.lib.StoredConfig)3 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 GroupReference (com.google.gerrit.common.data.GroupReference)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 TreeMap (java.util.TreeMap)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ConfigParameterInfo (com.google.gerrit.extensions.api.projects.ConfigInfo.ConfigParameterInfo)1 ConfigValue (com.google.gerrit.extensions.api.projects.ConfigValue)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1