You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BatchPluginPredicate.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.batch.bootstrap;
  21. import com.google.common.base.Joiner;
  22. import com.google.common.base.Predicate;
  23. import com.google.common.base.Splitter;
  24. import com.google.common.collect.Lists;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.sonar.api.BatchComponent;
  27. import org.sonar.api.CoreProperties;
  28. import org.sonar.api.config.Settings;
  29. import org.sonar.api.utils.log.Logger;
  30. import org.sonar.api.utils.log.Loggers;
  31. import javax.annotation.Nonnull;
  32. import java.text.MessageFormat;
  33. import java.util.Arrays;
  34. import java.util.List;
  35. import java.util.Set;
  36. import static com.google.common.collect.Lists.newArrayList;
  37. import static com.google.common.collect.Sets.newHashSet;
  38. /**
  39. * Filters the plugins to be enabled during analysis
  40. */
  41. public class BatchPluginPredicate implements Predicate<String>, BatchComponent {
  42. private static final Logger LOG = Loggers.get(BatchPluginPredicate.class);
  43. private static final String CORE_PLUGIN_KEY = "core";
  44. private static final String BUILDBREAKER_PLUGIN_KEY = "buildbreaker";
  45. private static final String PROPERTY_IS_DEPRECATED_MSG = "Property {0} is deprecated. Please use {1} instead.";
  46. private final Set<String> whites = newHashSet(), blacks = newHashSet();
  47. private final DefaultAnalysisMode mode;
  48. public BatchPluginPredicate(Settings settings, DefaultAnalysisMode mode) {
  49. this.mode = mode;
  50. if (settings.hasKey(CoreProperties.BATCH_INCLUDE_PLUGINS)) {
  51. whites.addAll(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_INCLUDE_PLUGINS)));
  52. }
  53. if (settings.hasKey(CoreProperties.BATCH_EXCLUDE_PLUGINS)) {
  54. blacks.addAll(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_EXCLUDE_PLUGINS)));
  55. }
  56. if (mode.isPreview()) {
  57. // These default values are not supported by Settings because the class CorePlugin
  58. // is not loaded yet.
  59. if (settings.hasKey(CoreProperties.DRY_RUN_INCLUDE_PLUGINS)) {
  60. LOG.warn(MessageFormat.format(PROPERTY_IS_DEPRECATED_MSG, CoreProperties.DRY_RUN_INCLUDE_PLUGINS, CoreProperties.PREVIEW_INCLUDE_PLUGINS));
  61. whites.addAll(propertyValues(settings,
  62. CoreProperties.DRY_RUN_INCLUDE_PLUGINS, CoreProperties.PREVIEW_INCLUDE_PLUGINS_DEFAULT_VALUE));
  63. } else {
  64. whites.addAll(propertyValues(settings,
  65. CoreProperties.PREVIEW_INCLUDE_PLUGINS, CoreProperties.PREVIEW_INCLUDE_PLUGINS_DEFAULT_VALUE));
  66. }
  67. if (settings.hasKey(CoreProperties.DRY_RUN_EXCLUDE_PLUGINS)) {
  68. LOG.warn(MessageFormat.format(PROPERTY_IS_DEPRECATED_MSG, CoreProperties.DRY_RUN_EXCLUDE_PLUGINS, CoreProperties.PREVIEW_EXCLUDE_PLUGINS));
  69. blacks.addAll(propertyValues(settings,
  70. CoreProperties.DRY_RUN_EXCLUDE_PLUGINS, CoreProperties.PREVIEW_EXCLUDE_PLUGINS_DEFAULT_VALUE));
  71. } else {
  72. blacks.addAll(propertyValues(settings,
  73. CoreProperties.PREVIEW_EXCLUDE_PLUGINS, CoreProperties.PREVIEW_EXCLUDE_PLUGINS_DEFAULT_VALUE));
  74. }
  75. }
  76. if (!whites.isEmpty()) {
  77. LOG.info("Include plugins: " + Joiner.on(", ").join(whites));
  78. }
  79. if (!blacks.isEmpty()) {
  80. LOG.info("Exclude plugins: " + Joiner.on(", ").join(blacks));
  81. }
  82. }
  83. @Override
  84. public boolean apply(@Nonnull String pluginKey) {
  85. if (CORE_PLUGIN_KEY.equals(pluginKey)) {
  86. return !mode.isMediumTest();
  87. }
  88. if (BUILDBREAKER_PLUGIN_KEY.equals(pluginKey) && mode.isPreview()) {
  89. LOG.info("Build Breaker plugin is no more supported in preview/incremental mode");
  90. return false;
  91. }
  92. // FIXME what happens if there are only white-listed plugins ?
  93. List<String> mergeList = newArrayList(blacks);
  94. mergeList.removeAll(whites);
  95. return mergeList.isEmpty() || !mergeList.contains(pluginKey);
  96. }
  97. Set<String> getWhites() {
  98. return whites;
  99. }
  100. Set<String> getBlacks() {
  101. return blacks;
  102. }
  103. static List<String> propertyValues(Settings settings, String key, String defaultValue) {
  104. String s = StringUtils.defaultIfEmpty(settings.getString(key), defaultValue);
  105. return Lists.newArrayList(Splitter.on(",").trimResults().split(s));
  106. }
  107. }