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 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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. public class ProjectBuildersExecutor {
  31. private static final Logger LOG = Loggers.get(ProjectBuildersExecutor.class);
  32. private final GlobalConfiguration globalConfig;
  33. private final ProjectBuilder[] projectBuilders;
  34. public ProjectBuildersExecutor(GlobalConfiguration globalConfig, ProjectBuilder... projectBuilders) {
  35. this.globalConfig = globalConfig;
  36. this.projectBuilders = projectBuilders;
  37. }
  38. public ProjectBuildersExecutor(GlobalConfiguration globalConfig) {
  39. this(globalConfig, new ProjectBuilder[0]);
  40. }
  41. public void execute(ProjectReactor reactor) {
  42. if (projectBuilders.length > 0) {
  43. Profiler profiler = Profiler.create(LOG).startInfo("Execute project builders");
  44. ProjectBuilderContext context = new ProjectBuilderContext(reactor, globalConfig);
  45. for (ProjectBuilder projectBuilder : projectBuilders) {
  46. try {
  47. LOG.debug("Execute project builder: {}", projectBuilder.getClass().getName());
  48. projectBuilder.build(context);
  49. } catch (Exception e) {
  50. throw MessageException.of("Failed to execute project builder: " + getDescription(projectBuilder), e);
  51. }
  52. }
  53. profiler.stopInfo();
  54. }
  55. }
  56. private static String getDescription(ProjectBuilder projectBuilder) {
  57. Method toString;
  58. try {
  59. toString = projectBuilder.getClass().getMethod("toString");
  60. } catch (Exception e) {
  61. // should never happen as every class has toString
  62. return projectBuilder.toString();
  63. }
  64. if (toString.getDeclaringClass() != Object.class) {
  65. return projectBuilder.toString();
  66. } else {
  67. return projectBuilder.getClass().getName();
  68. }
  69. }
  70. }