Search in sources :

Example 6 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)) {
                log.info(String.format("plugin %s: including %s", name, classes));
                urls.add(classes.toUri().toURL());
            }
        }
        urls.add(tmp.toUri().toURL());
        ClassLoader pluginLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), PluginLoader.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));
        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) URLClassLoader(java.net.URLClassLoader)

Example 7 with PluginConfig

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

the class ProjectConfig method getPluginConfig.

public PluginConfig getPluginConfig(String pluginName) {
    Config pluginConfig = pluginConfigs.get(pluginName);
    if (pluginConfig == null) {
        pluginConfig = new Config();
        pluginConfigs.put(pluginName, pluginConfig);
    }
    return new PluginConfig(pluginName, pluginConfig, this);
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) Config(org.eclipse.jgit.lib.Config) PluginConfig(com.google.gerrit.server.config.PluginConfig)

Example 8 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)

Aggregations

PluginConfig (com.google.gerrit.server.config.PluginConfig)8 ProjectConfigEntry (com.google.gerrit.server.config.ProjectConfigEntry)4 Config (org.eclipse.jgit.lib.Config)3 GroupReference (com.google.gerrit.common.data.GroupReference)2 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 Map (java.util.Map)2 ConfigValue (com.google.gerrit.extensions.api.projects.ConfigValue)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 Change (com.google.gerrit.reviewdb.client.Change)1 Project (com.google.gerrit.reviewdb.client.Project)1 CommitValidationException (com.google.gerrit.server.git.validators.CommitValidationException)1 RefOperationValidationException (com.google.gerrit.server.git.validators.RefOperationValidationException)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)1 UpdateException (com.google.gerrit.server.update.UpdateException)1 OrmException (com.google.gwtorm.server.OrmException)1 IOException (java.io.IOException)1