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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.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. /**
  71. * @since 4.4
  72. * @deprecated since 6.6 use {@link #execute()}
  73. */
  74. @Deprecated
  75. public synchronized Batch start() {
  76. return this;
  77. }
  78. /**
  79. * @since 4.4
  80. * @deprecated since 6.6 use {@link #execute()}
  81. */
  82. @Deprecated
  83. public Batch executeTask(Map<String, String> analysisProperties, Object... components) {
  84. Map<String, String> mergedProps = new HashMap<>(this.globalProperties);
  85. mergedProps.putAll(analysisProperties);
  86. List<Object> mergedComponents = new ArrayList<>(this.components);
  87. mergedComponents.addAll(Arrays.asList(components));
  88. return doExecute(mergedProps, mergedComponents);
  89. }
  90. private RuntimeException handleException(RuntimeException t) {
  91. if (loggingConfig.isVerbose()) {
  92. return t;
  93. }
  94. Throwable y = t;
  95. do {
  96. if (y instanceof MessageException) {
  97. return (MessageException) y;
  98. }
  99. y = y.getCause();
  100. } while (y != null);
  101. return t;
  102. }
  103. /**
  104. * @since 4.4
  105. * @deprecated since 6.6 use {@link #execute()}
  106. */
  107. @Deprecated
  108. public synchronized void stop() {
  109. }
  110. private void configureLogging() {
  111. if (loggingConfig != null) {
  112. loggingConfig.setProperties(globalProperties);
  113. LoggingConfigurator.apply(loggingConfig);
  114. }
  115. }
  116. public static Builder builder() {
  117. return new Builder();
  118. }
  119. public static final class Builder {
  120. private Map<String, String> globalProperties;
  121. private EnvironmentInformation environment;
  122. private List<Object> components = new ArrayList<>();
  123. private boolean enableLoggingConfiguration = true;
  124. private LogOutput logOutput;
  125. private Builder() {
  126. }
  127. public Builder setEnvironment(EnvironmentInformation env) {
  128. this.environment = env;
  129. return this;
  130. }
  131. public Builder setComponents(List<Object> l) {
  132. this.components = l;
  133. return this;
  134. }
  135. public Builder setLogOutput(@Nullable LogOutput logOutput) {
  136. this.logOutput = logOutput;
  137. return this;
  138. }
  139. public Builder setGlobalProperties(Map<String, String> globalProperties) {
  140. this.globalProperties = globalProperties;
  141. return this;
  142. }
  143. /**
  144. * @deprecated since 6.6 use {@link #setGlobalProperties(Map)}
  145. */
  146. @Deprecated
  147. public Builder setBootstrapProperties(Map<String, String> bootstrapProperties) {
  148. this.globalProperties = bootstrapProperties;
  149. return this;
  150. }
  151. public Builder addComponents(Object... components) {
  152. Collections.addAll(this.components, components);
  153. return this;
  154. }
  155. public Builder addComponent(Object component) {
  156. this.components.add(component);
  157. return this;
  158. }
  159. public boolean isEnableLoggingConfiguration() {
  160. return enableLoggingConfiguration;
  161. }
  162. /**
  163. * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its
  164. * own implementation of SLF4J.
  165. */
  166. public Builder setEnableLoggingConfiguration(boolean b) {
  167. this.enableLoggingConfiguration = b;
  168. return this;
  169. }
  170. public Batch build() {
  171. if (components == null) {
  172. throw new IllegalStateException("Batch components are not set");
  173. }
  174. return new Batch(this);
  175. }
  176. }
  177. }