summaryrefslogtreecommitdiffstats
path: root/sonar-server/src
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-06 17:22:57 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-06 17:22:57 +0000
commitaef2286bf00aca219862aa62ef5dcac4bb9d2e9a (patch)
tree185ed172e4fd630b1b14c07b072e049fee6c4494 /sonar-server/src
parent4592819b97fd656bd857e319b6c26c008e2f50f2 (diff)
downloadsonarqube-aef2286bf00aca219862aa62ef5dcac4bb9d2e9a.tar.gz
sonarqube-aef2286bf00aca219862aa62ef5dcac4bb9d2e9a.zip
SONAR-1229 Export/Import a given Sonar quality profile
Diffstat (limited to 'sonar-server/src')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java10
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/Platform.java20
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileExporters.java87
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java (renamed from sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleProfileBridge.java)10
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridges.java62
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleRepositories.java (renamed from sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridge.java)42
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/ProfileBackuper.java51
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java101
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/RulesConsole.java (renamed from sonar-server/src/main/java/org/sonar/server/rules/RuleRepositories.java)21
-rw-r--r--sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java6
-rw-r--r--sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java12
-rw-r--r--sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java32
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb8
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb35
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb21
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb36
-rw-r--r--sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java55
17 files changed, 381 insertions, 228 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
index 8ec95ce9d56..2041e4c03bd 100644
--- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
+++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
@@ -54,16 +54,6 @@ public class ProfilesManager extends BaseDao {
getSession().commit();
}
- public String exportProfile(String pluginKey, int profileId) {
- for (RulesRepository rulesRepository : rulesManager.getRulesRepositories()) {
- Plugin plugin = plugins.getPluginByExtension(rulesRepository);
- if (rulesRepository instanceof ConfigurationExportable && pluginKey.equals(plugin.getKey())) {
- RulesProfile profile = rulesDao.getProfileById(profileId);
- return ((ConfigurationExportable) rulesRepository).exportConfiguration(profile);
- }
- }
- return null;
- }
public void importProfile(String pluginKey, int profileId, String configuration) {
for (RulesRepository rulesRepository : rulesManager.getRulesRepositories()) {
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 1d236458e31..1a5313f91c1 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
@@ -28,8 +28,6 @@ import org.sonar.api.Plugins;
import org.sonar.api.database.configuration.DatabaseConfiguration;
import org.sonar.api.platform.Environment;
import org.sonar.api.platform.Server;
-import org.sonar.api.profiles.XMLProfileExporter;
-import org.sonar.api.profiles.XMLProfileImporter;
import org.sonar.api.resources.Languages;
import org.sonar.api.rules.DefaultRulesManager;
import org.sonar.api.utils.HttpDownloader;
@@ -51,10 +49,7 @@ import org.sonar.server.database.JndiDatabaseConnector;
import org.sonar.server.filters.FilterExecutor;
import org.sonar.server.mavendeployer.MavenRepository;
import org.sonar.server.plugins.*;
-import org.sonar.server.rules.DeprecatedRuleBridges;
-import org.sonar.server.rules.DeprecatedRuleProfileBridge;
-import org.sonar.server.rules.ProfileBackuper;
-import org.sonar.server.rules.RuleRepositories;
+import org.sonar.server.rules.*;
import org.sonar.server.startup.*;
import org.sonar.server.ui.AuthenticatorFactory;
import org.sonar.server.ui.CodeColorizers;
@@ -179,12 +174,12 @@ public final class Platform {
servicesContainer.as(Characteristics.CACHE).addComponent(AuthenticatorFactory.class);
servicesContainer.as(Characteristics.CACHE).addComponent(ServerLifecycleNotifier.class);
servicesContainer.as(Characteristics.CACHE).addComponent(DefaultRuleProvider.class);
- servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedRuleBridges.class);
- servicesContainer.as(Characteristics.CACHE).addComponent(RuleRepositories.class);
- servicesContainer.as(Characteristics.CACHE).addComponent(XMLProfileExporter.create());
- servicesContainer.as(Characteristics.CACHE).addComponent(XMLProfileImporter.create());
- servicesContainer.as(Characteristics.CACHE).addComponent(ProfileBackuper.class);
-
+ 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(ProfilesConsole.class);
+ servicesContainer.as(Characteristics.CACHE).addComponent(RulesConsole.class);
+
servicesContainer.start();
}
@@ -195,7 +190,6 @@ public final class Platform {
startupContainer.as(Characteristics.CACHE).addComponent(GwtPublisher.class);
startupContainer.as(Characteristics.CACHE).addComponent(RegisterMetrics.class);
startupContainer.as(Characteristics.CACHE).addComponent(RegisterRules.class);
- startupContainer.as(Characteristics.CACHE).addComponent(DeprecatedRuleProfileBridge.class);
startupContainer.as(Characteristics.CACHE).addComponent(RegisterProvidedProfiles.class);
startupContainer.as(Characteristics.CACHE).addComponent(ActivateDefaultProfiles.class);
startupContainer.as(Characteristics.CACHE).addComponent(JdbcDriverDeployer.class);
diff --git a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileExporters.java b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileExporters.java
new file mode 100644
index 00000000000..e5fc8865416
--- /dev/null
+++ b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfileExporters.java
@@ -0,0 +1,87 @@
+/*
+ * 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.sonar.api.Plugin;
+import org.sonar.api.Plugins;
+import org.sonar.api.ServerComponent;
+import org.sonar.api.profiles.ProfileExporter;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.rules.ConfigurationExportable;
+import org.sonar.api.rules.RulesRepository;
+import org.sonar.api.utils.SonarException;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class DeprecatedProfileExporters implements ServerComponent {
+
+ private Plugins plugins;
+ private RulesRepository[] deprecatedRepositories;
+
+ public DeprecatedProfileExporters(Plugins plugins, RulesRepository[] deprecatedRepositories) {
+ this.deprecatedRepositories = deprecatedRepositories;
+ this.plugins = plugins;
+ }
+
+ public DeprecatedProfileExporters(Plugins plugins) {
+ this.deprecatedRepositories = new RulesRepository[0];
+ this.plugins = plugins;
+ }
+
+ public List<ProfileExporter> create() {
+ List<ProfileExporter> result = new ArrayList<ProfileExporter>();
+ for (RulesRepository repo : deprecatedRepositories) {
+ if (repo instanceof ConfigurationExportable) {
+ result.add(new DeprecatedProfileExporter(getPlugin(repo), (ConfigurationExportable)repo));
+ }
+ }
+ return result;
+ }
+
+ private Plugin getPlugin(RulesRepository repository) {
+ return plugins.getPluginByExtension(repository);
+ }
+}
+
+class DeprecatedProfileExporter extends ProfileExporter {
+ private ConfigurationExportable repository;
+
+ protected DeprecatedProfileExporter(Plugin plugin, ConfigurationExportable repository) {
+ super(plugin.getKey(), plugin.getName());
+ this.repository = repository;
+ setMimeType("application/xml");
+ }
+
+
+ @Override
+ public void exportProfile(RulesProfile profile, Writer writer) {
+ String xml = repository.exportConfiguration(profile);
+ if (xml != null) {
+ try {
+ writer.append(xml);
+ } catch (IOException e) {
+ throw new SonarException("Can not export profile", e);
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleProfileBridge.java b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java
index 377afe2fc97..bb3db460eff 100644
--- a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleProfileBridge.java
+++ b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java
@@ -36,35 +36,35 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-public final class DeprecatedRuleProfileBridge {
+public final class DeprecatedProfiles {
private RulesRepository[] deprecatedRepositories;
private Plugins plugins;
private CheckProfile[] deprecatedCheckProfiles;
private CheckProfileProvider[] deprecatedCheckProfileProviders;
- public DeprecatedRuleProfileBridge(Plugins plugins, RulesRepository[] r, CheckProfile[] deprecatedCheckProfiles, CheckProfileProvider[] deprecatedCheckProfileProviders) {
+ public DeprecatedProfiles(Plugins plugins, RulesRepository[] r, CheckProfile[] deprecatedCheckProfiles, CheckProfileProvider[] deprecatedCheckProfileProviders) {
this.deprecatedRepositories = r;
this.plugins = plugins;
this.deprecatedCheckProfiles = deprecatedCheckProfiles;
this.deprecatedCheckProfileProviders = deprecatedCheckProfileProviders;
}
- public DeprecatedRuleProfileBridge(Plugins plugins, RulesRepository[] r, CheckProfile[] deprecatedCheckProfiles) {
+ public DeprecatedProfiles(Plugins plugins, RulesRepository[] r, CheckProfile[] deprecatedCheckProfiles) {
this.deprecatedRepositories = r;
this.plugins = plugins;
this.deprecatedCheckProfiles = deprecatedCheckProfiles;
this.deprecatedCheckProfileProviders = new CheckProfileProvider[0];
}
- public DeprecatedRuleProfileBridge(Plugins plugins, RulesRepository[] r, CheckProfileProvider[] deprecatedCheckProfileProviders) {
+ public DeprecatedProfiles(Plugins plugins, RulesRepository[] r, CheckProfileProvider[] deprecatedCheckProfileProviders) {
this.deprecatedRepositories = r;
this.plugins = plugins;
this.deprecatedCheckProfiles = new CheckProfile[0];
this.deprecatedCheckProfileProviders = deprecatedCheckProfileProviders;
}
- public DeprecatedRuleProfileBridge(Plugins plugins, RulesRepository[] r) {
+ public DeprecatedProfiles(Plugins plugins, RulesRepository[] r) {
this.deprecatedRepositories = r;
this.plugins = plugins;
this.deprecatedCheckProfiles = new CheckProfile[0];
diff --git a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridges.java b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridges.java
deleted file mode 100644
index e746abf64b7..00000000000
--- a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridges.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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.sonar.api.Plugin;
-import org.sonar.api.Plugins;
-import org.sonar.api.rules.RulesRepository;
-import org.sonar.server.platform.DefaultServerFileSystem;
-import org.sonar.server.rules.DeprecatedRuleBridge;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public final class DeprecatedRuleBridges {
-
- private RulesRepository<?>[] repositories;
- private DefaultServerFileSystem fileSystem;
- private Plugins plugins;
-
- public DeprecatedRuleBridges(DefaultServerFileSystem fileSystem, Plugins plugins) {
- this.fileSystem = fileSystem;
- this.plugins = plugins;
- this.repositories = new RulesRepository[0];
- }
-
- public DeprecatedRuleBridges(DefaultServerFileSystem fileSystem, Plugins plugins, RulesRepository[] repositories) {
- this.fileSystem = fileSystem;
- this.plugins = plugins;
- this.repositories = repositories;
- }
-
- public List<DeprecatedRuleBridge> createBridges() {
- List<DeprecatedRuleBridge> bridges = new ArrayList<DeprecatedRuleBridge>();
- for (RulesRepository repository : repositories) {
- Plugin plugin = getPlugin(repository);
- bridges.add(new DeprecatedRuleBridge(plugin.getKey(), plugin.getName(), repository, fileSystem));
- }
- return bridges;
- }
-
- private Plugin getPlugin(RulesRepository repository) {
- return plugins.getPluginByExtension(repository);
- }
-
-}
diff --git a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridge.java b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleRepositories.java
index d92184e9805..1794d2dfd27 100644
--- a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridge.java
+++ b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleRepositories.java
@@ -21,6 +21,8 @@ package org.sonar.server.rules;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.CharEncoding;
+import org.sonar.api.Plugin;
+import org.sonar.api.Plugins;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleParam;
import org.sonar.api.rules.RuleRepository;
@@ -33,15 +35,45 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-/**
- * Bridge for the deprecated extension "org.sonar.api.rules.RulesRepository"
- */
-public final class DeprecatedRuleBridge extends RuleRepository {
+public final class DeprecatedRuleRepositories {
+
+ private RulesRepository<?>[] repositories;
+ private DefaultServerFileSystem fileSystem;
+ private Plugins plugins;
+
+ public DeprecatedRuleRepositories(DefaultServerFileSystem fileSystem, Plugins plugins) {
+ this.fileSystem = fileSystem;
+ this.plugins = plugins;
+ this.repositories = new RulesRepository[0];
+ }
+
+ public DeprecatedRuleRepositories(DefaultServerFileSystem fileSystem, Plugins plugins, RulesRepository[] repositories) {
+ this.fileSystem = fileSystem;
+ this.plugins = plugins;
+ this.repositories = repositories;
+ }
+
+ public List<DeprecatedRuleRepository> create() {
+ List<DeprecatedRuleRepository> repositories = new ArrayList<DeprecatedRuleRepository>();
+ for (RulesRepository repository : this.repositories) {
+ Plugin plugin = getPlugin(repository);
+ repositories.add(new DeprecatedRuleRepository(plugin.getKey(), plugin.getName(), repository, fileSystem));
+ }
+ return repositories;
+ }
+
+ private Plugin getPlugin(RulesRepository repository) {
+ return plugins.getPluginByExtension(repository);
+ }
+}
+
+
+class DeprecatedRuleRepository extends RuleRepository {
private RulesRepository deprecatedRepository;
private DefaultServerFileSystem fileSystem;
- public DeprecatedRuleBridge(String repositoryKey, String repositoryName, RulesRepository deprecatedRepository, DefaultServerFileSystem fileSystem) {
+ public DeprecatedRuleRepository(String repositoryKey, String repositoryName, RulesRepository deprecatedRepository, DefaultServerFileSystem fileSystem) {
super(repositoryKey, deprecatedRepository.getLanguage().getKey());
this.deprecatedRepository = deprecatedRepository;
this.fileSystem = fileSystem;
diff --git a/sonar-server/src/main/java/org/sonar/server/rules/ProfileBackuper.java b/sonar-server/src/main/java/org/sonar/server/rules/ProfileBackuper.java
deleted file mode 100644
index ddfe6c3c681..00000000000
--- a/sonar-server/src/main/java/org/sonar/server/rules/ProfileBackuper.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.sonar.api.ServerComponent;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.profiles.XMLProfileExporter;
-import org.sonar.jpa.session.DatabaseSessionFactory;
-
-import java.io.StringWriter;
-import java.io.Writer;
-
-public final class ProfileBackuper implements ServerComponent {
-
- private DatabaseSessionFactory sessionFactory;
- private XMLProfileExporter exporter;
-
- public ProfileBackuper(DatabaseSessionFactory sessionFactory, XMLProfileExporter exporter) {
- this.sessionFactory = sessionFactory;
- this.exporter = exporter;
- }
-
- public String exportProfile(int profileId) {
- DatabaseSession session = sessionFactory.getSession();
- RulesProfile profile = session.getSingleResult(RulesProfile.class, "id", profileId);
- if (profile != null) {
- Writer writer = new StringWriter();
- exporter.exportProfile(profile, writer);
- return writer.toString();
- }
- return null;
- }
-}
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
new file mode 100644
index 00000000000..7e92a8078db
--- /dev/null
+++ b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java
@@ -0,0 +1,101 @@
+/*
+ * 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.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.ServerComponent;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.profiles.ProfileExporter;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.profiles.XMLProfileExporter;
+import org.sonar.jpa.session.DatabaseSessionFactory;
+
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public final class ProfilesConsole implements ServerComponent {
+
+ private DatabaseSessionFactory sessionFactory;
+ private List<ProfileExporter> profileExporters = new ArrayList<ProfileExporter>();
+
+
+ public ProfilesConsole(DatabaseSessionFactory sessionFactory,
+ ProfileExporter[] exporters, DeprecatedProfileExporters deprecatedExporters) {
+ this.sessionFactory = sessionFactory;
+ initProfileExporters(exporters, deprecatedExporters);
+ }
+
+ private void initProfileExporters(ProfileExporter[] exporters, DeprecatedProfileExporters deprecatedExporters) {
+ this.profileExporters.addAll(Arrays.asList(exporters));
+ for (ProfileExporter exporter : deprecatedExporters.create()) {
+ this.profileExporters.add(exporter);
+ }
+ }
+
+ public String backupProfile(int profileId) {
+ RulesProfile profile = loadProfile(profileId);
+ if (profile != null) {
+ Writer writer = new StringWriter();
+ XMLProfileExporter.create().exportProfile(profile, writer);
+ return writer.toString();
+ }
+ return null;
+ }
+
+ private RulesProfile loadProfile(int profileId) {
+ DatabaseSession session = sessionFactory.getSession();
+ RulesProfile profile = session.getSingleResult(RulesProfile.class, "id", profileId);
+ return profile;
+ }
+
+ public List<ProfileExporter> getProfileExportersForLanguage(String language) {
+ List<ProfileExporter> result = new ArrayList<ProfileExporter>();
+ for (ProfileExporter exporter : profileExporters) {
+ if (exporter.getSupportedLanguages() == null || exporter.getSupportedLanguages().length == 0 || ArrayUtils.contains(exporter.getSupportedLanguages(), language)) {
+ result.add(exporter);
+ }
+ }
+ return result;
+ }
+
+ public String exportProfile(int profileId, String exporterKey) {
+ RulesProfile profile = loadProfile(profileId);
+ if (profile != null) {
+ ProfileExporter exporter = getProfileExporter(exporterKey);
+ Writer writer = new StringWriter();
+ exporter.exportProfile(profile, writer);
+ return writer.toString();
+ }
+ return null;
+ }
+
+ public ProfileExporter getProfileExporter(String exporterKey) {
+ for (ProfileExporter exporter : profileExporters) {
+ if (StringUtils.equals(exporterKey, exporter.getKey())) {
+ return exporter;
+ }
+ }
+ return null;
+ }
+}
diff --git a/sonar-server/src/main/java/org/sonar/server/rules/RuleRepositories.java b/sonar-server/src/main/java/org/sonar/server/rules/RulesConsole.java
index 30ab4e6ed87..c7505386ce6 100644
--- a/sonar-server/src/main/java/org/sonar/server/rules/RuleRepositories.java
+++ b/sonar-server/src/main/java/org/sonar/server/rules/RulesConsole.java
@@ -21,6 +21,7 @@ package org.sonar.server.rules;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
import org.sonar.api.rules.RuleRepository;
@@ -28,19 +29,20 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-public final class RuleRepositories implements ServerComponent {
+public final class RulesConsole implements ServerComponent {
private List<RuleRepository> repositories = new ArrayList<RuleRepository>();
private ListMultimap<String, RuleRepository> repositoriesByLanguage = ArrayListMultimap.create();
- public RuleRepositories(RuleRepository[] repositories) {
- this(repositories, null);
+
+ public RulesConsole(RuleRepository[] repositories, DeprecatedRuleRepositories deprecatedRuleRepositories) {
+ initRepositories(repositories, deprecatedRuleRepositories);
}
- public RuleRepositories(RuleRepository[] repositories, DeprecatedRuleBridges deprecatedBridge) {
+ private void initRepositories(RuleRepository[] repositories, DeprecatedRuleRepositories deprecatedBridge) {
this.repositories.addAll(Arrays.asList(repositories));
if (deprecatedBridge != null) {
- this.repositories.addAll(deprecatedBridge.createBridges());
+ this.repositories.addAll(deprecatedBridge.create());
}
for (RuleRepository repository : this.repositories) {
repositoriesByLanguage.put(repository.getLanguage(), repository);
@@ -50,4 +52,13 @@ public final class RuleRepositories implements ServerComponent {
public List<RuleRepository> getRepositoriesByLanguage(String language) {
return repositoriesByLanguage.get(language);
}
+
+ public RuleRepository getRepository(String key) {
+ for (RuleRepository repository : repositories) {
+ if (StringUtils.equals(key, repository.getKey())) {
+ return repository;
+ }
+ }
+ return null;
+ }
}
diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java
index 6b857e7bb49..12c3aa18c1b 100644
--- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java
+++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java
@@ -31,7 +31,7 @@ import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleProvider;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.jpa.session.DatabaseSessionFactory;
-import org.sonar.server.rules.DeprecatedRuleProfileBridge;
+import org.sonar.server.rules.DeprecatedProfiles;
import java.util.ArrayList;
import java.util.Arrays;
@@ -47,7 +47,7 @@ public final class RegisterProvidedProfiles {
private List<ProfileDefinition> definitions = new ArrayList<ProfileDefinition>();
public RegisterProvidedProfiles(RuleProvider ruleProvider, DatabaseSessionFactory sessionFactory,
- DeprecatedRuleProfileBridge deprecatedBridge, RegisterRules registerRulesBefore,
+ DeprecatedProfiles deprecatedBridge, RegisterRules registerRulesBefore,
ProfileDefinition[] definitions) {
this.ruleProvider = ruleProvider;
this.sessionFactory = sessionFactory;
@@ -58,7 +58,7 @@ public final class RegisterProvidedProfiles {
}
public RegisterProvidedProfiles(RuleProvider ruleProvider, DatabaseSessionFactory sessionFactory,
- DeprecatedRuleProfileBridge deprecatedBridge, RegisterRules registerRulesBefore) {
+ DeprecatedProfiles deprecatedBridge, RegisterRules registerRulesBefore) {
this.ruleProvider = ruleProvider;
this.sessionFactory = sessionFactory;
if (deprecatedBridge != null) {
diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
index fba87355d79..ae9fbd01aca 100644
--- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
+++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
@@ -25,7 +25,7 @@ import org.sonar.api.rules.*;
import org.sonar.api.utils.Logs;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.jpa.session.DatabaseSessionFactory;
-import org.sonar.server.rules.DeprecatedRuleBridges;
+import org.sonar.server.rules.DeprecatedRuleRepositories;
import java.util.*;
@@ -34,16 +34,16 @@ public final class RegisterRules {
private DatabaseSessionFactory sessionFactory;
private List<RuleRepository> repositories = new ArrayList<RuleRepository>();
- public RegisterRules(DatabaseSessionFactory sessionFactory, DeprecatedRuleBridges bridges, RuleRepository[] repos) {
+ public RegisterRules(DatabaseSessionFactory sessionFactory, DeprecatedRuleRepositories repositories, RuleRepository[] repos) {
this.sessionFactory = sessionFactory;
this.repositories.addAll(Arrays.asList(repos));
- if (bridges != null) {
- this.repositories.addAll(bridges.createBridges());
+ if (repositories != null) {
+ this.repositories.addAll(repositories.create());
}
}
- public RegisterRules(DatabaseSessionFactory sessionFactory, DeprecatedRuleBridges bridges) {
- this(sessionFactory, bridges, new RuleRepository[0]);
+ public RegisterRules(DatabaseSessionFactory sessionFactory, DeprecatedRuleRepositories repositories) {
+ this(sessionFactory, repositories, new RuleRepository[0]);
}
public void start() {
diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
index 07408ae83c7..9654bd04bbf 100644
--- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
+++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
@@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory;
import org.sonar.api.Plugin;
import org.sonar.api.Plugins;
import org.sonar.api.Property;
+import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.rules.DefaultRulesManager;
@@ -43,8 +44,8 @@ import org.sonar.server.platform.Platform;
import org.sonar.server.plugins.PluginDownloader;
import org.sonar.server.plugins.UpdateFinder;
import org.sonar.server.plugins.UpdateFinderFactory;
-import org.sonar.server.rules.ProfileBackuper;
-import org.sonar.server.rules.RuleRepositories;
+import org.sonar.server.rules.ProfilesConsole;
+import org.sonar.server.rules.RulesConsole;
import org.sonar.updatecenter.common.Version;
import java.util.Collection;
@@ -122,24 +123,28 @@ public class JRubyFacade {
return getContainer().getComponent(Plugins.class).getPlugins();
}
- public List<Plugin> getPluginsWithConfigurationExportable(Language language) {
- return getRulesManager().getExportablePlugins(language);
- }
-
public List<Plugin> getPluginsWithConfigurationImportable(Language language) {
return getRulesManager().getImportablePlugins(language);
}
public List<RuleRepository> getRuleRepositoriesByLanguage(String languageKey) {
- return getContainer().getComponent(RuleRepositories.class).getRepositoriesByLanguage(languageKey);
+ return getContainer().getComponent(RulesConsole.class).getRepositoriesByLanguage(languageKey);
+ }
+
+ public String backupProfile(int profileId) {
+ return getContainer().getComponent(ProfilesConsole.class).backupProfile(profileId);
}
- public String exportProfile(int profileId) {
- return getContainer().getComponent(ProfileBackuper.class).exportProfile(profileId);
+ public List<ProfileExporter> getProfileExportersForLanguage(String language) {
+ return getContainer().getComponent(ProfilesConsole.class).getProfileExportersForLanguage(language);
}
- public String exportConfiguration(String pluginKey, long profileId) {
- return getProfilesManager().exportProfile(pluginKey, (int) profileId);
+ public String exportProfile(int profileId, String exporterKey) {
+ return getContainer().getComponent(ProfilesConsole.class).exportProfile(profileId, exporterKey);
+ }
+
+ public String getProfileExporterMimeType(String exporterKey) {
+ return getContainer().getComponent(ProfilesConsole.class).getProfileExporter(exporterKey).getMimeType();
}
public void importConfiguration(String pluginKey, long profileId, String configuration) {
@@ -154,11 +159,6 @@ public class JRubyFacade {
getProfilesManager().deleteProfile((int) profileId);
}
- public Map<String, Long> getRulesCountByCategory(String languageKey) {
- Language language = getContainer().getComponent(Languages.class).get(languageKey);
- return getRulesManager().countRulesByCategory(language);
- }
-
public List<Footer> getWebFooters() {
return getContainer().getComponents(Footer.class);
}
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 e510f703d9c..033a2e87a4c 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
@@ -27,14 +27,6 @@ class ProfilesController < ApplicationController
def index
@profiles = Profile.find(:all, :order => 'name')
- @rules_count_by_language = {}
- java_facade.getLanguages().collect { |language| language.getKey() }.each do |language|
- @rules_count_by_language[language] = 0
- java_facade.getRulesCountByCategory(language).each do |category, nb_rules|
- @rules_count_by_language[language] += nb_rules
- end
- end
-
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @profiles }
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb
index c9ae845b911..580ed989693 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb
@@ -78,19 +78,6 @@ class RulesConfigurationController < ApplicationController
end
- def export
- name = CGI::unescape(params[:name])
- language = params[:language]
- plugin_key = params[:plugin]
- if (name != 'active')
- profile = RulesProfile.find_by_name_and_language(name, language)
- else
- profile = RulesProfile.find_active_profile_by_language(language)
- end
- result = java_facade.exportConfiguration(plugin_key, profile.id)
- send_data(result, :type => 'text/xml', :disposition => 'inline')
- end
-
#
#
@@ -272,11 +259,31 @@ class RulesConfigurationController < ApplicationController
#
def backup
profile = RulesProfile.find(params[:id])
- xml = java_facade.exportProfile(profile.id)
+ xml = java_facade.backupProfile(profile.id)
send_data(xml, :type => 'text/xml', :disposition => "attachment; filename=#{profile.name}_#{profile.language}.xml")
end
+
+ #
+ #
+ # GET /rules_configuration/export?name=<profile name>&language=<language>&format<exporter key>
+ #
+ #
+ def export
+ name = CGI::unescape(params[:name])
+ language = params[:language]
+ if (name != 'active')
+ profile = RulesProfile.find_by_name_and_language(name, language)
+ else
+ profile = RulesProfile.find_active_profile_by_language(language)
+ end
+ exporter_key = params[:format]
+ result = java_facade.exportProfile(profile.id, exporter_key)
+ send_data(result, :type => java_facade.getProfileExporterMimeType(exporter_key), :disposition => 'inline')
+ end
+
+
def update_param
is_admin=true # security has already been checked by controller filters
profile = RulesProfile.find(params[:profile_id].to_i)
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb
index 9d25ec46a7d..308bc2a239f 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb
@@ -22,21 +22,16 @@ module ProfilesHelper
controller.java_facade.getLanguages()
end
- def exportable_plugins_by_language
- hash = {}
- controller.java_facade.getLanguages().each do |language|
- hash[language.getKey()] = controller.java_facade.getPluginsWithConfigurationExportable(language)
- end
- hash
- end
-
def projects_tooltip(profile)
- return nil if profile.projects.empty?
- html='<ul>'
- profile.projects.each do |project|
- html+="<li style='white-space: nowrap'>#{escape_javascript project.name}</li>"
+ html=nil
+ if profile.projects.size>0
+ html='<ul>'
+ profile.projects.each do |project|
+ html+="<li style='white-space: nowrap'>#{escape_javascript project.name}</li>"
+ end
+ html+='</ul>'
+ html
end
- html+='</ul>'
html
end
end \ No newline at end of file
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb
index 20f5deec785..498b872b59c 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb
@@ -1,13 +1,16 @@
-<% languages.each do |language| %>
+<%
+ languages.each do |language|
+ exporters=controller.java_facade.getProfileExportersForLanguage(language.getKey())
+%>
-<table class="data2 width100" id="profiles_<%= language.getKey() -%>">
+<table class="data2 width100 marginbottom10" id="profiles_<%= language.getKey() -%>">
<thead>
<tr>
<th><h2><%= language.getName() %></h2></th>
- <th class="right"><%= @rules_count_by_language[language.getKey()] %> coding rules</th>
- <th class="right">Export XML</th>
+ <th class="right"></th>
+ <th class="right">Export</th>
<th class="right">Alerts</th>
- <th class="right">Default</th>
+ <th class="right">Default</th>
<th class="right">Projects</th>
<th width="1%"> </th>
<th width="1%"> </th>
@@ -21,24 +24,24 @@
<td align="right">
<% activated_rules=profile.active_rules_by_category_and_level(nil, nil).size %>
<a id="rules_<%= u profile.key -%>" href="<%= url_for :controller => 'rules_configuration', :action => 'index', :id => profile.id -%>">
- <span id="activated_rules_<%= u profile.key -%>"><%= activated_rules %> activated rules</span>
+ <span id="activated_rules_<%= u profile.key -%>"><%= activated_rules %> rules</span>
</a>
</td>
+
<td align="right">
- <% exportable_plugins=exportable_plugins_by_language[profile.language] || []
- exportable_plugins.each do |plugin| %>
- <%= link_to plugin.getName(),
+ <% exporters.each do |exporter| %>
+ <%= link_to exporter.getName(),
{:controller => 'rules_configuration', :action => 'export', :language => profile.language,
- :name => url_encode(profile.name), :plugin => plugin.getKey(), :format => 'xml'},
- :id => "export_" + plugin.getKey().to_s + "_" + u(profile.key) %>
+ :name => url_encode(profile.name), :format => exporter.getKey()},
+ :id => "export_" + exporter.getKey().to_s + "_" + u(profile.key) %>
<% end %>
</td>
- <td align="right"><%= link_to pluralize(profile.alerts.size, 'alert'), profile_alerts_path(profile), :id => "alerts_#{u profile.key}" %></td>
+ <td align="right"><%= link_to pluralize(profile.alerts.size, 'alert'), profile_alerts_path(profile), :id => "alerts_#{u profile.key}" %></td>
- <td align="right">
+ <td align="right">
<% if (!profile.default_profile? && is_admin?) %>
- <%= button_to 'set as default', { :action => 'set_as_default', :id => profile.id }, :class => 'action',
+ <%= button_to 'Set as default', { :action => 'set_as_default', :id => profile.id }, :class => 'action',
:id => "activate_#{u profile.key}",
:confirm => "Are you sure that you want to set the profile '#{profile.name}' as default ?",
:method => :post %>
@@ -70,14 +73,14 @@
<% if (is_admin?) %>
<% form_tag(:action => 'copy', :id => profile.id) do -%>
<%= hidden_field_tag 'copy_' + profile.id.to_s %>
- <input type="button" name="button_copy" id="copy_<%= u profile.key %>" value="copy" onClick='var name=prompt("Name for the new profile"); if (name!=null) {$("copy_<%= profile.id %>").value=name; submit();} else {return false;}'>
+ <input type="button" name="button_copy" id="copy_<%= u profile.key %>" value="Copy" onClick='var name=prompt("Name for the new profile"); if (name!=null) {$("copy_<%= profile.id %>").value=name; submit();} else {return false;}'>
<% end
end %>
</td>
<td>
<% if (!(profile.provided?) && !(profile.default_profile?) && is_admin?) %>
- <%= button_to "delete", { :action => 'destroy', :id => profile.id }, :class => 'action',
+ <%= button_to "Delete", { :action => 'destroy', :id => profile.id }, :class => 'action',
:id => "delete_#{u profile.key}",
:confirm => "Are you sure that you want to delete the profile '#{profile.name}' ?",
:method => :delete %>
@@ -86,7 +89,6 @@
</tr>
<% end %>
</table>
-<br/>
<% end %>
<% if is_admin? %>
diff --git a/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java
new file mode 100644
index 00000000000..1d94991ce8e
--- /dev/null
+++ b/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.junit.Test;
+import org.sonar.api.rules.RulePriority;
+import org.sonar.server.rules.DeprecatedProfiles;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class DeprecatedProfilesTest {
+ @Test
+ public void testCreate() {
+ DeprecatedProfiles.DefaultProfileDefinition def = DeprecatedProfiles.DefaultProfileDefinition.create("sonar way", "java");
+ assertThat(def.getName(), is("sonar way"));
+ assertThat(def.getLanguage(), is("java"));
+ }
+
+ @Test
+ public void testActivateRule() {
+ DeprecatedProfiles.DefaultProfileDefinition def = DeprecatedProfiles.DefaultProfileDefinition.create("sonar way", "java");
+ def.activateRule("checkstyle", "IllegalRegexp", RulePriority.BLOCKER);
+ def.activateRule("pmd", "NullPointer", RulePriority.INFO);
+
+ assertThat(def.getRules().size(), is(2));
+ assertThat(def.getRulesByRepositoryKey("checkstyle").size(), is(1));
+ assertThat(def.getRulesByRepositoryKey("checkstyle").get(0).getPriority(), is(RulePriority.BLOCKER));
+ }
+
+ @Test
+ public void priorityIsOptional() {
+ DeprecatedProfiles.DefaultProfileDefinition def = DeprecatedProfiles.DefaultProfileDefinition.create("sonar way", "java");
+ def.activateRule("checkstyle", "IllegalRegexp", null);
+ assertThat(def.getRules().get(0).getPriority(), nullValue());
+ }
+}