*/
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;
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 {
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) {
}
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;
+ }
}
*/
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;
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");
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");
}
}))).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));
+ }
}