diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 13:01:22 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 13:01:22 +0000 |
commit | 42c3d47753e93dd92d2caf214df1f4db01d3b27b (patch) | |
tree | a6e18b0f6d5eb939026b3d5e1363bab1f9c353a2 /sonar-server | |
parent | ddcc6272a62c245618d7797712062c96ff7ad3d4 (diff) | |
download | sonarqube-42c3d47753e93dd92d2caf214df1f4db01d3b27b.tar.gz sonarqube-42c3d47753e93dd92d2caf214df1f4db01d3b27b.zip |
backward compatibility of ConfigurationImportable rule repositories
Diffstat (limited to 'sonar-server')
4 files changed, 123 insertions, 15 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index aec1073f6b5..5bbb113f777 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -177,6 +177,7 @@ public final class Platform { servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedRuleRepositories.class); servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedProfiles.class); servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedProfileExporters.class); + servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedProfileImporters.class); servicesContainer.as(Characteristics.CACHE).addComponent(ProfilesConsole.class); servicesContainer.as(Characteristics.CACHE).addComponent(RulesConsole.class); diff --git a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileImporters.java b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileImporters.java new file mode 100644 index 00000000000..c55ec758f1c --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileImporters.java @@ -0,0 +1,93 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.rules; + +import org.apache.commons.io.IOUtils; +import org.sonar.api.Plugin; +import org.sonar.api.Plugins; +import org.sonar.api.profiles.ProfileImporter; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.*; +import org.sonar.api.utils.SonarException; +import org.sonar.api.utils.ValidationMessages; + +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +public class DeprecatedProfileImporters { + + private Plugins plugins; + private RuleFinder ruleFinder; + private RulesRepository[] deprecatedRepositories; + + public DeprecatedProfileImporters(Plugins plugins, RuleFinder ruleFinder, RulesRepository[] deprecatedRepositories) { + this.deprecatedRepositories = deprecatedRepositories; + this.plugins = plugins; + this.ruleFinder = ruleFinder; + } + + public DeprecatedProfileImporters(Plugins plugins, RuleFinder ruleFinder) { + this.deprecatedRepositories = new RulesRepository[0]; + this.plugins = plugins; + this.ruleFinder = ruleFinder; + } + + public List<ProfileImporter> create() { + List<ProfileImporter> result = new ArrayList<ProfileImporter>(); + for (RulesRepository repo : deprecatedRepositories) { + if (repo instanceof ConfigurationImportable) { + result.add(new DeprecatedProfileImporter(getPlugin(repo), ruleFinder, repo)); + } + } + return result; + } + + private Plugin getPlugin(RulesRepository repository) { + return plugins.getPluginByExtension(repository); + } +} + +class DeprecatedProfileImporter extends ProfileImporter { + private RulesRepository importableRepository; + private RuleFinder ruleFinder; + + protected DeprecatedProfileImporter(Plugin plugin, RuleFinder ruleFinder, RulesRepository importableRepository) { + super(plugin.getKey(), plugin.getName()); + this.importableRepository = importableRepository; + this.ruleFinder = ruleFinder; + setSupportedLanguages(importableRepository.getLanguage().getKey()); + } + + @Override + public RulesProfile importProfile(Reader reader, ValidationMessages messages) { + List<Rule> rules = new ArrayList<Rule>(ruleFinder.findAll(RuleQuery.create().withRepositoryKey(getKey()))); + try { + RulesProfile profile = RulesProfile.create(getKey(), getName()); + List<ActiveRule> activeRules = ((ConfigurationImportable) importableRepository).importConfiguration(IOUtils.toString(reader), rules); + profile.setActiveRules(activeRules); + return profile; + + } catch (IOException e) { + throw new SonarException("Fail to load the profile definition", e); + } + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java index e538f7c80cf..64d525939bc 100644 --- a/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java +++ b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java @@ -24,6 +24,8 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; import org.sonar.api.database.DatabaseSession; import org.sonar.api.profiles.*; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.rules.RuleFinder; import org.sonar.api.utils.ValidationMessages; import org.sonar.jpa.session.DatabaseSessionFactory; @@ -39,22 +41,29 @@ public final class ProfilesConsole implements ServerComponent { private DatabaseSessionFactory sessionFactory; private RuleFinder ruleFinder; - private List<ProfileExporter> profileExporters = new ArrayList<ProfileExporter>(); - private List<ProfileImporter> profileImporters = new ArrayList<ProfileImporter>(); + private List<ProfileExporter> exporters = new ArrayList<ProfileExporter>(); + private List<ProfileImporter> importers = new ArrayList<ProfileImporter>(); public ProfilesConsole(DatabaseSessionFactory sessionFactory, RuleFinder ruleFinder, ProfileExporter[] exporters, DeprecatedProfileExporters deprecatedExporters, - ProfileImporter[] importers) { + ProfileImporter[] importers, DeprecatedProfileImporters deprecatedImporters) { this.ruleFinder = ruleFinder; this.sessionFactory = sessionFactory; initProfileExporters(exporters, deprecatedExporters); - this.profileImporters.addAll(Arrays.asList(importers)); + initProfileImporters(importers, deprecatedImporters); } private void initProfileExporters(ProfileExporter[] exporters, DeprecatedProfileExporters deprecatedExporters) { - this.profileExporters.addAll(Arrays.asList(exporters)); + this.exporters.addAll(Arrays.asList(exporters)); for (ProfileExporter exporter : deprecatedExporters.create()) { - this.profileExporters.add(exporter); + this.exporters.add(exporter); + } + } + + private void initProfileImporters(ProfileImporter[] importers, DeprecatedProfileImporters deprecatedImporters) { + this.importers.addAll(Arrays.asList(importers)); + for (ProfileImporter importer : deprecatedImporters.create()) { + this.importers.add(importer); } } @@ -91,7 +100,7 @@ public final class ProfilesConsole implements ServerComponent { public List<ProfileExporter> getProfileExportersForLanguage(String language) { List<ProfileExporter> result = new ArrayList<ProfileExporter>(); - for (ProfileExporter exporter : profileExporters) { + for (ProfileExporter exporter : exporters) { if (exporter.getSupportedLanguages() == null || exporter.getSupportedLanguages().length == 0 || ArrayUtils.contains(exporter.getSupportedLanguages(), language)) { result.add(exporter); } @@ -101,7 +110,7 @@ public final class ProfilesConsole implements ServerComponent { public List<ProfileImporter> getProfileImportersForLanguage(String language) { List<ProfileImporter> result = new ArrayList<ProfileImporter>(); - for (ProfileImporter importer : profileImporters) { + for (ProfileImporter importer : importers) { if (importer.getSupportedLanguages() == null || importer.getSupportedLanguages().length == 0 || ArrayUtils.contains(importer.getSupportedLanguages(), language)) { result.add(importer); } @@ -122,24 +131,29 @@ public final class ProfilesConsole implements ServerComponent { } /** - * Important : the ruby controller has already removed existing profile with same name/language. + * Important : the ruby controller has already create the profile */ public ValidationMessages importProfile(String profileName, String language, String importerKey, String profileDefinition) { ValidationMessages messages = ValidationMessages.create(); ProfileImporter importer = getProfileImporter(importerKey); RulesProfile profile = importer.importProfile(new StringReader(profileDefinition), messages); if (!messages.hasErrors()) { - profile.setName(profileName); - profile.setLanguage(language); DatabaseSession session = sessionFactory.getSession(); - session.saveWithoutFlush(profile); + RulesProfile persistedProfile = session.getSingleResult(RulesProfile.class, "name", profileName, "language", language); + for (ActiveRule activeRule : profile.getActiveRules()) { + ActiveRule persistedActiveRule = persistedProfile.activateRule(activeRule.getRule(), activeRule.getPriority()); + for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) { + persistedActiveRule.setParameter(activeRuleParam.getKey(), activeRuleParam.getValue()); + } + } + session.saveWithoutFlush(persistedProfile); session.commit(); } return messages; } public ProfileExporter getProfileExporter(String exporterKey) { - for (ProfileExporter exporter : profileExporters) { + for (ProfileExporter exporter : exporters) { if (StringUtils.equals(exporterKey, exporter.getKey())) { return exporter; } @@ -148,7 +162,7 @@ public final class ProfilesConsole implements ServerComponent { } public ProfileImporter getProfileImporter(String exporterKey) { - for (ProfileImporter importer : profileImporters) { + for (ProfileImporter importer : importers) { if (StringUtils.equals(exporterKey, importer.getKey())) { return importer; } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb index 50e9f6b27e2..0e097d5ef7f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb @@ -65,7 +65,7 @@ class ProfilesController < ApplicationController if ok && params[:backup] params[:backup].each_pair do |importer_key, file| if !file.blank? && ok - messages = java_facade.importProfile(profile.id, importer_key, read_file_param(file)) + messages = java_facade.importProfile(profile_name, language, importer_key, read_file_param(file)) flash_validation_messages(messages) ok &= !messages.hasErrors() end |