]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2497 Add batch properties to include/exclude plugins to load
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 3 Jun 2011 14:24:11 +0000 (16:24 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 3 Jun 2011 14:24:11 +0000 (16:24 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java

index 47270c198ab3df3551d7b4ed4ddbf2f3d200ce14..c6b3289fd618f0fc68e4f7322ca0cd3add336064 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.Plugin;
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
@@ -37,10 +43,7 @@ import org.sonar.core.plugin.JpaPluginFile;
 import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 public class BatchPluginRepository implements PluginRepository {
 
@@ -49,15 +52,31 @@ public class BatchPluginRepository implements PluginRepository {
   private JpaPluginDao dao;
   private ArtifactDownloader artifactDownloader;
   private Map<String, Plugin> pluginsByKey;
+  private String[] whiteList = null;
+  private String[] blackList = null;
 
-  public BatchPluginRepository(JpaPluginDao dao, ArtifactDownloader artifactDownloader) {
+  public BatchPluginRepository(JpaPluginDao dao, ArtifactDownloader artifactDownloader, Configuration configuration) {
     this.dao = dao;
     this.artifactDownloader = artifactDownloader;
+    if (configuration.getString(CoreProperties.INCLUDE_PLUGINS)!=null) {
+      whiteList = configuration.getStringArray(CoreProperties.INCLUDE_PLUGINS);
+    }
+    if (configuration.getString(CoreProperties.EXCLUDE_PLUGINS)!=null) {
+      blackList = configuration.getStringArray(CoreProperties.EXCLUDE_PLUGINS);
+    }
 //  TODO reactivate somewhere else:  LOG.info("Execution environment: {} {}", environment.getKey(), environment.getVersion());
   }
 
   public void start() {
-    doStart(dao.getPlugins());
+    doStart(filter(dao.getPlugins()));
+  }
+
+  List<JpaPlugin> filter(List<JpaPlugin> plugins) {
+    return ImmutableList.copyOf(Iterables.filter(plugins, new Predicate<JpaPlugin>() {
+      public boolean apply(JpaPlugin p) {
+        return isAccepted(p.getKey()) && (StringUtils.isBlank(p.getBasePlugin()) || isAccepted(p.getBasePlugin()));
+      }
+    }));
   }
 
   public void doStart(List<JpaPlugin> basePlugins) {
@@ -153,4 +172,14 @@ public class BatchPluginRepository implements PluginRepository {
     }
     return new Property[0];
   }
+
+  boolean isAccepted(String pluginKey) {
+    if (whiteList!=null) {
+      return ArrayUtils.contains(whiteList, pluginKey);
+    }
+    if (blackList!=null) {
+      return !ArrayUtils.contains(blackList, pluginKey);
+    }
+    return true;
+  }
 }
index 0c617bf427149087166709e66aef19383ab183a2..af8dbec719847c83b5ec2b9faba74eca55f04602 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
+import com.google.common.collect.Lists;
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
 import org.codehaus.plexus.util.FileUtils;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
+import org.hamcrest.Matchers;
 import org.junit.Test;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.Plugin;
 import org.sonar.core.plugin.JpaPlugin;
+import org.sonar.core.plugin.JpaPluginDao;
 import org.sonar.core.plugin.JpaPluginFile;
 
 import java.util.Arrays;
+import java.util.List;
 
+import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.not;
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNull.nullValue;
@@ -46,7 +53,7 @@ public class BatchPluginRepositoryTest {
     ArtifactDownloader extensionDownloader = mock(ArtifactDownloader.class);
     when(extensionDownloader.downloadExtension(any(JpaPluginFile.class))).thenReturn(
         FileUtils.toFile(getClass().getResource("/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar")));
-    BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader);
+    BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader, new PropertiesConfiguration());
 
     JpaPlugin plugin = new JpaPlugin("artifactsize");
     plugin.setPluginClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin");
@@ -67,7 +74,7 @@ public class BatchPluginRepositoryTest {
     ArtifactDownloader extensionDownloader = mock(ArtifactDownloader.class);
     prepareDownloader(extensionDownloader, "artifactsize", "/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar");
     prepareDownloader(extensionDownloader, "clirr", "/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/sonar-clirr-plugin-1.1.jar");
-    BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader);
+    BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader, new PropertiesConfiguration());
 
     JpaPlugin pluginBase = new JpaPlugin("artifactsize");
     pluginBase.setPluginClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin");
@@ -96,4 +103,58 @@ public class BatchPluginRepositoryTest {
       }
     }))).thenReturn(FileUtils.toFile(getClass().getResource(filename)));
   }
+
+  @Test
+  public void shouldAlwaysAcceptIfNoWhiteListAndBlackList() {
+    BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), new PropertiesConfiguration());
+    assertThat(repository.isAccepted("pmd"), Matchers.is(true));
+  }
+
+  @Test
+  public void whiteListShouldTakePrecedenceOverBlackList() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.INCLUDE_PLUGINS, "checkstyle,pmd,findbugs");
+    conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "cobertura,pmd");
+    BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf);
+
+    assertThat(repository.isAccepted("pmd"), Matchers.is(true));
+  }
+
+  @Test
+  public void shouldCheckWhitelist() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.INCLUDE_PLUGINS, "checkstyle,pmd,findbugs");
+    BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf);
+
+    assertThat(repository.isAccepted("checkstyle"), Matchers.is(true));
+    assertThat(repository.isAccepted("pmd"), Matchers.is(true));
+    assertThat(repository.isAccepted("cobertura"), Matchers.is(false));
+  }
+
+  @Test
+  public void shouldCheckBlackListIfNoWhiteList() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "checkstyle,pmd,findbugs");
+    BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf);
+
+    assertThat(repository.isAccepted("checkstyle"), Matchers.is(false));
+    assertThat(repository.isAccepted("pmd"), Matchers.is(false));
+    assertThat(repository.isAccepted("cobertura"), Matchers.is(true));
+  }
+
+  @Test
+  public void shouldExcludePluginDependents() {
+    JpaPlugin pmd = new JpaPlugin("pmd");
+    JpaPlugin checkstyle = new JpaPlugin("checkstyle");
+    JpaPlugin checkstyleExtension = new JpaPlugin("checkstyle-ext");
+    checkstyleExtension.setBasePlugin("checkstyle");
+
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "checkstyle");
+    BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf);
+
+    List<JpaPlugin> filteredPlugins = repository.filter(Arrays.asList(checkstyle, checkstyleExtension, pmd));
+    assertThat(filteredPlugins.size(), Matchers.is(1));
+    assertThat(filteredPlugins, hasItem(pmd));
+  }
 }
index a5461b5f86d04001e86154d6a2ad20287c307210..49fa07f4c954200d5cd3a64deb11c88ff965b3a3 100644 (file)
@@ -103,6 +103,8 @@ public interface CoreProperties {
   String SERVER_STARTTIME = "sonar.core.startTime"; // format is yyyy-MM-dd'T'HH:mm:ssZ
   String SKIP_TENDENCIES_PROPERTY = "sonar.skipTendencies";
   boolean SKIP_TENDENCIES_DEFAULT_VALUE = false;
+  String INCLUDE_PLUGINS = "sonar.includePlugins";
+  String EXCLUDE_PLUGINS = "sonar.excludePlugins";
 
   /* CPD */
   String CPD_PLUGIN = "cpd";