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.

Batch.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.batch.bootstrapper;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import javax.annotation.Nullable;
  28. import org.sonar.api.utils.MessageException;
  29. import org.sonar.scanner.bootstrap.SpringGlobalContainer;
  30. /**
  31. * Entry point for SonarQube Scanner API 2.1+.
  32. *
  33. * @since 2.14
  34. */
  35. public final class Batch {
  36. private LoggingConfiguration loggingConfig;
  37. private List<Object> components;
  38. private Map<String, String> globalProperties = new HashMap<>();
  39. private Batch(Builder builder) {
  40. components = new ArrayList<>();
  41. components.addAll(builder.components);
  42. if (builder.environment != null) {
  43. components.add(builder.environment);
  44. }
  45. if (builder.globalProperties != null) {
  46. globalProperties.putAll(builder.globalProperties);
  47. }
  48. if (builder.isEnableLoggingConfiguration()) {
  49. loggingConfig = new LoggingConfiguration(builder.environment).setProperties(globalProperties);
  50. if (builder.logOutput != null) {
  51. loggingConfig.setLogOutput(builder.logOutput);
  52. }
  53. }
  54. }
  55. public LoggingConfiguration getLoggingConfiguration() {
  56. return loggingConfig;
  57. }
  58. public synchronized Batch execute() {
  59. return doExecute(this.globalProperties, this.components);
  60. }
  61. public synchronized Batch doExecute(Map<String, String> scannerProperties, List<Object> components) {
  62. configureLogging();
  63. try {
  64. SpringGlobalContainer.create(scannerProperties, components).execute();
  65. } catch (RuntimeException e) {
  66. throw handleException(e);
  67. }
  68. return this;
  69. }
  70. private RuntimeException handleException(RuntimeException t) {
  71. if (loggingConfig.isVerbose()) {
  72. return t;
  73. }
  74. Throwable y = t;
  75. do {
  76. if (y instanceof MessageException) {
  77. return (MessageException) y;
  78. }
  79. y = y.getCause();
  80. } while (y != null);
  81. return t;
  82. }
  83. private void configureLogging() {
  84. if (loggingConfig != null) {
  85. loggingConfig.setProperties(globalProperties);
  86. LoggingConfigurator.apply(loggingConfig);
  87. }
  88. }
  89. public static Builder builder() {
  90. return new Builder();
  91. }
  92. public static final class Builder {
  93. private Map<String, String> globalProperties;
  94. private EnvironmentInformation environment;
  95. private List<Object> components = new ArrayList<>();
  96. private boolean enableLoggingConfiguration = true;
  97. private LogOutput logOutput;
  98. private Builder() {
  99. }
  100. public Builder setEnvironment(EnvironmentInformation env) {
  101. this.environment = env;
  102. return this;
  103. }
  104. public Builder setComponents(List<Object> l) {
  105. this.components = l;
  106. return this;
  107. }
  108. public Builder setLogOutput(@Nullable LogOutput logOutput) {
  109. this.logOutput = logOutput;
  110. return this;
  111. }
  112. public Builder setGlobalProperties(Map<String, String> globalProperties) {
  113. this.globalProperties = globalProperties;
  114. return this;
  115. }
  116. public Builder addComponents(Object... components) {
  117. Collections.addAll(this.components, components);
  118. return this;
  119. }
  120. public Builder addComponent(Object component) {
  121. this.components.add(component);
  122. return this;
  123. }
  124. public boolean isEnableLoggingConfiguration() {
  125. return enableLoggingConfiguration;
  126. }
  127. /**
  128. * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its
  129. * own implementation of SLF4J.
  130. */
  131. public Builder setEnableLoggingConfiguration(boolean b) {
  132. this.enableLoggingConfiguration = b;
  133. return this;
  134. }
  135. public Batch build() {
  136. if (components == null) {
  137. throw new IllegalStateException("Batch components are not set");
  138. }
  139. return new Batch(this);
  140. }
  141. }
  142. }