diff options
30 files changed, 541 insertions, 364 deletions
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java index 49b56a5446c..883013caf3a 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java @@ -27,6 +27,7 @@ import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.profiles.ProfileExporter; import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Java; import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.rules.RuleParam; @@ -42,8 +43,10 @@ public class CheckstyleProfileExporter extends ProfileExporter { private Configuration conf; public CheckstyleProfileExporter(Configuration conf) { + super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME); this.conf = conf; - setSupportedRepositories(CheckstyleConstants.REPOSITORY_KEY); + setSupportedLanguages(Java.KEY); + setMimeType("application/xml"); } /** diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java index dce362a38a6..26b917b9329 100644 --- a/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java @@ -137,53 +137,6 @@ public class DefaultRulesManager extends RulesManager { return Collections.emptyList(); } - /** - * Gets count of rules by categories defined for a given language - * - * @param language the language - * @return a Map with the category as key and the count as value - */ - public Map<String, Long> countRulesByCategory(Language language) { - return countRulesByCategory(language, rulesDao); - } - - protected Map<String, Long> countRulesByCategory(Language language, RulesDao rulesDao) { - Map<String, Long> countByCategory = new HashMap<String, Long>(); - List<Plugin> result = getPlugins(language); - if (!CollectionUtils.isEmpty(result)) { - List<String> keys = getPluginKeys(getPlugins(language)); - for (RulesCategory rulesCategory : rulesDao.getCategories()) { - Long rulesCount = rulesDao.countRules(keys, rulesCategory.getName()); - countByCategory.put(rulesCategory.getName(), rulesCount); - } - } - return countByCategory; - } - - private List<String> getPluginKeys(List<Plugin> plugins) { - ArrayList<String> keys = new ArrayList<String>(); - for (Plugin plugin : plugins) { - keys.add(plugin.getKey()); - } - return keys; - } - - /** - * Get the list of rules plugin that implement a mechanism of export for a given language - * - * @param language the language - * @return the list of plugins - */ - public List<Plugin> getExportablePlugins(Language language) { - List<Plugin> targets = new ArrayList<Plugin>(); - List<RulesRepository<?>> rulesRepositories = getRulesRepositories(language); - for (RulesRepository<?> repository : rulesRepositories) { - if (repository instanceof ConfigurationExportable) { - targets.add(plugins.getPluginByExtension(repository)); - } - } - return targets; - } /** * Get the list of rules plugin that implement a mechanism of import for a given language diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java index 62aa2a3a077..7aec32d575e 100644 --- a/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java +++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java @@ -38,24 +38,6 @@ import static org.mockito.Mockito.when; public class DefaultRulesManagerTest {
@Test
- public void shouldReturnsZeroRulesByCategoryWhenNoPluginsForALanguage() {
- DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins();
-
- Language language = mock(Language.class);
- Map<String, Long> result = rulesManager.countRulesByCategory(language);
- assertThat(result.size(), is(0));
- }
-
- @Test
- public void shouldReturnsZeroExportablePluginsWhenLanguageHasNoRulesPlugin() {
- DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins();
-
- Language language = mock(Language.class);
- List<Plugin> result = rulesManager.getExportablePlugins(language);
- assertThat(result.size(), is(0));
- }
-
- @Test
public void shouldReturnsZeroImportablePluginsWhenLanguageHasNoRulesPlugin() {
DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins();
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileImporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileDefinition.java index 83eec7a46a2..b071a24df8e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileImporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/AnnotationProfileDefinition.java @@ -20,32 +20,27 @@ package org.sonar.api.profiles; import org.sonar.api.rules.RulePriority; -import org.sonar.api.utils.ValidationMessages; import org.sonar.check.AnnotationIntrospector; import org.sonar.check.BelongsToProfile; -import java.io.Reader; import java.util.Collection; /** * @since 2.3 */ -public final class AnnotationProfileImporter extends ProfileImporter { +public abstract class AnnotationProfileDefinition extends ProfileDefinition { private String repositoryKey; private Collection<Class> annotatedClasses; - AnnotationProfileImporter(String repositoryKey, Collection<Class> annotatedClasses) { + protected AnnotationProfileDefinition(String repositoryKey, String profileName, String language, Collection<Class> annotatedClasses) { + super(profileName, language); this.repositoryKey = repositoryKey; this.annotatedClasses = annotatedClasses; } - public static AnnotationProfileImporter create(String repositoryKey, Collection<Class> annotatedClasses) { - return new AnnotationProfileImporter(repositoryKey, annotatedClasses); - } - @Override - public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { + public ProfilePrototype createPrototype() { ProfilePrototype profile = ProfilePrototype.create(); if (annotatedClasses != null) { for (Class aClass : annotatedClasses) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java index fb945bde59f..c722e5f7c5e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileDefinition.java @@ -32,8 +32,8 @@ public abstract class ProfileDefinition implements ServerExtension { protected ProfileDefinition() { } - protected ProfileDefinition(String name, String language) { - this.name = name; + protected ProfileDefinition(String profileName, String language) { + this.name = profileName; this.language = language; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileExporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileExporter.java index e439a4635ae..58e9a6b0b0e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileExporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileExporter.java @@ -19,6 +19,7 @@ */ package org.sonar.api.profiles; +import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; import org.sonar.api.ServerExtension; @@ -29,18 +30,76 @@ import java.io.Writer; */ public abstract class ProfileExporter implements BatchExtension, ServerExtension { - private String[] supportedRepositories = new String[0]; + private String[] supportedLanguages = new String[0]; + private String key; + private String name; + private String mimeType = "plain/text"; + + protected ProfileExporter(String key, String name) { + this.key = key; + this.name = name; + } public abstract void exportProfile(RulesProfile profile, Writer writer); - protected final void setSupportedRepositories(String... repositoryKeys) { - supportedRepositories = (repositoryKeys != null ? repositoryKeys : new String[0]); + public final String getKey() { + return key; + } + + public final ProfileExporter setKey(String s) { + this.key = s; + return this; + } + + public final String getName() { + return name; + } + + public final ProfileExporter setName(String s) { + this.name = s; + return this; + } + + protected final ProfileExporter setSupportedLanguages(String... languages) { + supportedLanguages = (languages != null ? languages : new String[0]); + return this; + } + + public final String getMimeType() { + return mimeType; + } + + public final ProfileExporter setMimeType(String s) { + if (StringUtils.isNotBlank(s)) { + this.mimeType = s; + } + return this; } /** - * @return if empty, then all repositories are supported. + * @return if empty, then any languages are supported. */ - public final String[] getSupportedRepositories() { - return supportedRepositories; + public final String[] getSupportedLanguages() { + return supportedLanguages; + } + + @Override + public final boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ProfileExporter that = (ProfileExporter) o; + if (key != null ? !key.equals(that.key) : that.key != null) { + return false; + } + return true; + } + + @Override + public final int hashCode() { + return key != null ? key.hashCode() : 0; } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileImporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileImporter.java index 3df8020f539..8cceb8be6ed 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileImporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/ProfileImporter.java @@ -29,18 +29,64 @@ import java.io.Reader; */ public abstract class ProfileImporter implements ServerExtension { - private String[] supportedRepositories = new String[0]; + private String[] supportedLanguages = new String[0]; + private String key; + private String name; + + protected ProfileImporter(String key, String name) { + this.key = key; + this.name = name; + } public abstract ProfilePrototype importProfile(Reader reader, ValidationMessages messages); - protected final void setSupportedRepositories(String... repositoryKeys) { - supportedRepositories = (repositoryKeys != null ? repositoryKeys : new String[0]); + public final String getKey() { + return key; + } + + public final ProfileImporter setKey(String s) { + this.key = s; + return this; + } + + public final String getName() { + return name; + } + + public final ProfileImporter setName(String s) { + this.name = s; + return this; + } + + protected final ProfileImporter setSupportedLanguages(String... languages) { + supportedLanguages = (languages != null ? languages : new String[0]); + return this; } /** - * @return if empty, then all repositories are supported. + * @return if empty, then any languages are supported. */ - public final String[] getSupportedRepositories() { - return supportedRepositories; + public final String[] getSupportedLanguages() { + return supportedLanguages; + } + + @Override + public final boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ProfileImporter that = (ProfileImporter) o; + if (key != null ? !key.equals(that.key) : that.key != null) { + return false; + } + return true; + } + + @Override + public final int hashCode() { + return key != null ? key.hashCode() : 0; } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileExporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileExporter.java index de76bd3aba6..a045499060c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileExporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileExporter.java @@ -31,17 +31,12 @@ import java.io.Writer; /** * @since 2.3 */ -public final class XMLProfileExporter extends ProfileExporter { - - private XMLProfileExporter() { - // support all repositories - } +public final class XMLProfileExporter { public static XMLProfileExporter create() { return new XMLProfileExporter(); } - @Override public void exportProfile(RulesProfile profile, Writer writer) { try { appendHeader(writer); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java index d0b9c4be653..fe5633c055f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java @@ -36,7 +36,7 @@ import java.util.List; /** * @since 2.3 */ -public final class XMLProfileImporter extends ProfileImporter { +public final class XMLProfileImporter { private XMLProfileImporter() { // support all repositories @@ -46,7 +46,6 @@ public final class XMLProfileImporter extends ProfileImporter { return new XMLProfileImporter(); } - @Override public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { ProfilePrototype profile = ProfilePrototype.create(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/RulesManager.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/RulesManager.java index 1f87fabe09e..976fa2f6f35 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/RulesManager.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/RulesManager.java @@ -65,22 +65,6 @@ public abstract class RulesManager { public abstract List<Plugin> getPlugins(Language language); /** - * Gets count of rules by categories defined for a given language - * - * @param language the language - * @return a Map with the category as key and the count as value - */ - public abstract Map<String, Long> countRulesByCategory(Language language); - - - /** - * Get the list of rules plugin that implement a mechanism of export for a given language - * - * @param language the language - * @return the list of plugins - */ - public abstract List<Plugin> getExportablePlugins(Language language); - /** * Get the list of rules plugin that implement a mechanism of import for a given language * * @param language the language diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java index 308ed354d06..995a4aeb626 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileImporterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java @@ -28,18 +28,18 @@ import org.sonar.check.IsoCategory; import org.sonar.check.Priority; import java.util.Arrays; +import java.util.Collection; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; -public class AnnotationProfileImporterTest { +public class AnnotationProfileDefinitionTest { @Test public void importProfile() { - AnnotationProfileImporter importer = AnnotationProfileImporter.create("checkstyle", Arrays.<Class>asList(FakeRule.class)); - ValidationMessages validation = ValidationMessages.create(); - ProfilePrototype profile = importer.importProfile(null, validation); - assertThat(profile.getRule("checkstyle", "fake").getPriority(), is(RulePriority.BLOCKER)); + ProfileDefinition definition = new FakeDefinition(); + ProfilePrototype profile = definition.createPrototype(); + assertThat(profile.getRule("squid", "fake").getPriority(), is(RulePriority.BLOCKER)); } } @@ -48,3 +48,11 @@ public class AnnotationProfileImporterTest { class FakeRule { } + + +class FakeDefinition extends AnnotationProfileDefinition { + + public FakeDefinition() { + super("squid", "sonar way", "java", Arrays.<Class>asList(FakeRule.class)); + } +}
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileExporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileExporterTest.java index 3dd63aa7481..7db9a3f6897 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileExporterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileExporterTest.java @@ -30,29 +30,29 @@ public class ProfileExporterTest { @Test public void testSupportedRepositories() { - ProfileExporter exporter = new ProfileExporter() { + ProfileExporter exporter = new ProfileExporter("all", "All") { @Override public void exportProfile(RulesProfile profile, Writer writer) { } }; - exporter.setSupportedRepositories("checkstyle", "pmd"); + exporter.setSupportedLanguages("java", "php"); - assertThat(exporter.getSupportedRepositories().length, is(2)); - assertThat(exporter.getSupportedRepositories()[0], is("checkstyle")); - assertThat(exporter.getSupportedRepositories()[1], is("pmd")); + assertThat(exporter.getSupportedLanguages().length, is(2)); + assertThat(exporter.getSupportedLanguages()[0], is("java")); + assertThat(exporter.getSupportedLanguages()[1], is("php")); } @Test public void supportAllRepositories() { - ProfileExporter exporter = new ProfileExporter() { + ProfileExporter exporter = new ProfileExporter("all", "All") { @Override public void exportProfile(RulesProfile profile, Writer writer) { } }; - assertThat(exporter.getSupportedRepositories().length, is(0)); + assertThat(exporter.getSupportedLanguages().length, is(0)); - exporter.setSupportedRepositories(null); - assertThat(exporter.getSupportedRepositories().length, is(0)); + exporter.setSupportedLanguages(null); + assertThat(exporter.getSupportedLanguages().length, is(0)); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java index 48d1388639e..13e5eb7ea5c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java @@ -31,31 +31,31 @@ public class ProfileImporterTest { @Test public void testSupportedRepositories() { - ProfileImporter inmporter = new ProfileImporter() { + ProfileImporter inmporter = new ProfileImporter("all", "All") { @Override public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { return null; } }; - inmporter.setSupportedRepositories("checkstyle", "pmd"); + inmporter.setSupportedLanguages("java", "php"); - assertThat(inmporter.getSupportedRepositories().length, is(2)); - assertThat(inmporter.getSupportedRepositories()[0], is("checkstyle")); - assertThat(inmporter.getSupportedRepositories()[1], is("pmd")); + assertThat(inmporter.getSupportedLanguages().length, is(2)); + assertThat(inmporter.getSupportedLanguages()[0], is("java")); + assertThat(inmporter.getSupportedLanguages()[1], is("php")); } @Test public void supportAllRepositories() { - ProfileImporter importer = new ProfileImporter() { + ProfileImporter importer = new ProfileImporter("all", "All") { @Override public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { return null; } }; - assertThat(importer.getSupportedRepositories().length, is(0)); + assertThat(importer.getSupportedLanguages().length, is(0)); - importer.setSupportedRepositories(null); - assertThat(importer.getSupportedRepositories().length, is(0)); + importer.setSupportedLanguages(null); + assertThat(importer.getSupportedLanguages().length, is(0)); } } 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()); + } +} |