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.

ProjectBuildersExecutor.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.scan;
  21. import java.lang.reflect.Method;
  22. import org.sonar.api.batch.bootstrap.ProjectBuilder;
  23. import org.sonar.api.batch.bootstrap.ProjectReactor;
  24. import org.sonar.api.batch.bootstrap.internal.ProjectBuilderContext;
  25. import org.sonar.api.utils.MessageException;
  26. import org.sonar.api.utils.log.Logger;
  27. import org.sonar.api.utils.log.Loggers;
  28. import org.sonar.api.utils.log.Profiler;
  29. import org.sonar.scanner.bootstrap.GlobalConfiguration;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. public class ProjectBuildersExecutor {
  32. private static final Logger LOG = Loggers.get(ProjectBuildersExecutor.class);
  33. private final GlobalConfiguration globalConfig;
  34. private final ProjectBuilder[] projectBuilders;
  35. @Autowired(required = false)
  36. public ProjectBuildersExecutor(GlobalConfiguration globalConfig, ProjectBuilder... projectBuilders) {
  37. this.globalConfig = globalConfig;
  38. this.projectBuilders = projectBuilders;
  39. }
  40. @Autowired(required = false)
  41. public ProjectBuildersExecutor(GlobalConfiguration globalConfig) {
  42. this(globalConfig, new ProjectBuilder[0]);
  43. }
  44. public void execute(ProjectReactor reactor) {
  45. if (projectBuilders.length > 0) {
  46. Profiler profiler = Profiler.create(LOG).startInfo("Execute project builders");
  47. ProjectBuilderContext context = new ProjectBuilderContext(reactor, globalConfig);
  48. for (ProjectBuilder projectBuilder : projectBuilders) {
  49. try {
  50. LOG.debug("Execute project builder: {}", projectBuilder.getClass().getName());
  51. projectBuilder.build(context);
  52. } catch (Exception e) {
  53. throw MessageException.of("Failed to execute project builder: " + getDescription(projectBuilder), e);
  54. }
  55. }
  56. profiler.stopInfo();
  57. }
  58. }
  59. private static String getDescription(ProjectBuilder projectBuilder) {
  60. Method toString;
  61. try {
  62. toString = projectBuilder.getClass().getMethod("toString");
  63. } catch (Exception e) {
  64. // should never happen as every class has toString
  65. return projectBuilder.toString();
  66. }
  67. if (toString.getDeclaringClass() != Object.class) {
  68. return projectBuilder.toString();
  69. } else {
  70. return projectBuilder.getClass().getName();
  71. }
  72. }
  73. }