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.

ModuleSensorsExecutor.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.scanner.sensor;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.stream.Collectors;
  24. import java.util.stream.Stream;
  25. import org.sonar.api.batch.fs.internal.SensorStrategy;
  26. import org.sonar.api.utils.log.Logger;
  27. import org.sonar.api.utils.log.Loggers;
  28. import org.sonar.core.util.logs.Profiler;
  29. import org.sonar.scanner.bootstrap.ScannerPluginRepository;
  30. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  31. import org.sonar.scanner.fs.InputModuleHierarchy;
  32. public class ModuleSensorsExecutor {
  33. private static final Logger LOG = Loggers.get(ModuleSensorsExecutor.class);
  34. private static final Profiler profiler = Profiler.create(LOG);
  35. private final ModuleSensorExtensionDictionary selector;
  36. private final SensorStrategy strategy;
  37. private final ScannerPluginRepository pluginRepo;
  38. private final boolean isRoot;
  39. public ModuleSensorsExecutor(ModuleSensorExtensionDictionary selector, DefaultInputModule module, InputModuleHierarchy hierarchy,
  40. SensorStrategy strategy, ScannerPluginRepository pluginRepo) {
  41. this.selector = selector;
  42. this.strategy = strategy;
  43. this.pluginRepo = pluginRepo;
  44. this.isRoot = hierarchy.isRoot(module);
  45. }
  46. public void execute() {
  47. Collection<ModuleSensorWrapper> moduleSensors = new ArrayList<>();
  48. withModuleStrategy(() -> moduleSensors.addAll(selector.selectSensors(false)));
  49. Collection<ModuleSensorWrapper> deprecatedGlobalSensors = new ArrayList<>();
  50. if (isRoot) {
  51. deprecatedGlobalSensors.addAll(selector.selectSensors(true));
  52. }
  53. printSensors(moduleSensors, deprecatedGlobalSensors);
  54. withModuleStrategy(() -> execute(moduleSensors));
  55. if (isRoot) {
  56. execute(deprecatedGlobalSensors);
  57. }
  58. }
  59. private void printSensors(Collection<ModuleSensorWrapper> moduleSensors, Collection<ModuleSensorWrapper> globalSensors) {
  60. String sensors = Stream
  61. .concat(moduleSensors.stream(), globalSensors.stream())
  62. .map(Object::toString)
  63. .collect(Collectors.joining(" -> "));
  64. LOG.debug("Sensors : {}", sensors);
  65. }
  66. private void withModuleStrategy(Runnable r) {
  67. boolean orig = strategy.isGlobal();
  68. strategy.setGlobal(false);
  69. r.run();
  70. strategy.setGlobal(orig);
  71. }
  72. private void execute(Collection<ModuleSensorWrapper> sensors) {
  73. for (ModuleSensorWrapper sensor : sensors) {
  74. String sensorName = getSensorName(sensor);
  75. profiler.startInfo("Sensor " + sensorName);
  76. sensor.analyse();
  77. profiler.stopInfo();
  78. }
  79. }
  80. private String getSensorName(ModuleSensorWrapper sensor) {
  81. ClassLoader cl = getSensorClassLoader(sensor);
  82. String pluginKey = pluginRepo.getPluginKey(cl);
  83. if (pluginKey != null) {
  84. return sensor.toString() + " [" + pluginKey + "]";
  85. }
  86. return sensor.toString();
  87. }
  88. private static ClassLoader getSensorClassLoader(ModuleSensorWrapper sensor) {
  89. return sensor.wrappedSensor().getClass().getClassLoader();
  90. }
  91. }