Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Main.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SonarQube Runner - Distribution
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.sonar.runner.api.Runner;
  22. import java.io.BufferedReader;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.Properties;
  27. import org.sonar.runner.impl.Logs;
  28. /**
  29. * Arguments :
  30. * <ul>
  31. * <li>runner.home: optional path to runner home (root directory with sub-directories bin, lib and conf)</li>
  32. * <li>runner.settings: optional path to runner global settings, usually ${runner.home}/conf/sonar-runner.properties.
  33. * This property is used only if ${runner.home} is not defined</li>
  34. * <li>project.home: path to project root directory. If not set, then it's supposed to be the directory where the runner is executed</li>
  35. * <li>project.settings: optional path to project settings. Default value is ${project.home}/sonar-project.properties.</li>
  36. * </ul>
  37. *
  38. * @since 1.0
  39. */
  40. public class Main {
  41. private final Shutdown shutdown;
  42. private final Cli cli;
  43. private final Conf conf;
  44. private final RunnerFactory runnerFactory;
  45. private Runner<?> runner;
  46. private BufferedReader inputReader;
  47. Main(Shutdown shutdown, Cli cli, Conf conf, RunnerFactory runnerFactory) {
  48. this.shutdown = shutdown;
  49. this.cli = cli;
  50. this.conf = conf;
  51. this.runnerFactory = runnerFactory;
  52. }
  53. public static void main(String[] args) {
  54. Exit exit = new Exit();
  55. Shutdown shutdown = new Shutdown(exit);
  56. Cli cli = new Cli(exit).parse(args);
  57. cli.verify();
  58. Main main = new Main(shutdown, cli, new Conf(cli), new RunnerFactory());
  59. main.execute();
  60. }
  61. void execute() {
  62. Stats stats = new Stats().start();
  63. try {
  64. Properties p = conf.properties();
  65. init(p);
  66. runner.start();
  67. runAnalysis(stats, p);
  68. if(cli.isInteractive()) {
  69. while (waitForUser()) {
  70. stats = new Stats().start();
  71. runAnalysis(stats, p);
  72. }
  73. }
  74. } catch (Exception e) {
  75. displayExecutionResult(stats, "FAILURE");
  76. showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace());
  77. shutdown.exit(Exit.ERROR);
  78. }
  79. runner.stop();
  80. shutdown.exit(Exit.SUCCESS);
  81. }
  82. private void init(Properties p) throws IOException {
  83. SystemInfo.print();
  84. if (cli.isDisplayVersionOnly()) {
  85. shutdown.exit(Exit.SUCCESS);
  86. }
  87. if (cli.isDisplayStackTrace()) {
  88. Logs.info("Error stacktraces are turned on.");
  89. }
  90. runner = runnerFactory.create(p);
  91. }
  92. private void runAnalysis(Stats stats, Properties p) {
  93. runner.runAnalysis(p);
  94. displayExecutionResult(stats, "SUCCESS");
  95. }
  96. private boolean waitForUser() throws IOException {
  97. if (inputReader == null) {
  98. inputReader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
  99. }
  100. shutdown.signalReady(true);
  101. if(shutdown.shouldExit()) {
  102. //exit before displaying message
  103. return false;
  104. }
  105. Logs.info("<Press enter to restart analysis>");
  106. String line = inputReader.readLine();
  107. shutdown.signalReady(false);
  108. return line != null;
  109. }
  110. private static void displayExecutionResult(Stats stats, String resultMsg) {
  111. Logs.info("------------------------------------------------------------------------");
  112. Logs.info("EXECUTION " + resultMsg);
  113. Logs.info("------------------------------------------------------------------------");
  114. stats.stop();
  115. Logs.info("------------------------------------------------------------------------");
  116. }
  117. public void showError(String message, Throwable e, boolean showStackTrace) {
  118. if (showStackTrace) {
  119. Logs.error(message, e);
  120. if (!cli.isDebugMode()) {
  121. Logs.error("");
  122. suggestDebugMode();
  123. }
  124. } else {
  125. Logs.error(message);
  126. if (e != null) {
  127. Logs.error(e.getMessage());
  128. String previousMsg = "";
  129. for (Throwable cause = e.getCause(); cause != null
  130. && cause.getMessage() != null
  131. && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
  132. Logs.error("Caused by: " + cause.getMessage());
  133. previousMsg = cause.getMessage();
  134. }
  135. }
  136. Logs.error("");
  137. Logs.error("To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.");
  138. if (!cli.isDebugMode()) {
  139. suggestDebugMode();
  140. }
  141. }
  142. }
  143. private static void suggestDebugMode() {
  144. Logs.error("Re-run SonarQube Runner using the -X switch to enable full debug logging.");
  145. }
  146. }