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.

BatchIsolatedLauncher.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SonarQube Runner - Batch
  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.batch;
  21. import ch.qos.logback.classic.LoggerContext;
  22. import ch.qos.logback.classic.joran.JoranConfigurator;
  23. import ch.qos.logback.core.joran.spi.JoranException;
  24. import com.google.common.annotations.VisibleForTesting;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. import org.slf4j.LoggerFactory;
  31. import org.sonar.api.utils.SonarException;
  32. import org.sonar.batch.bootstrapper.Batch;
  33. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  34. /**
  35. * This class is executed within the classloader provided by the server. It contains the installed plugins and
  36. * the same version of sonar-batch as the server.
  37. */
  38. public class BatchIsolatedLauncher implements IsolatedLauncher {
  39. private static final String WARN = "WARN";
  40. private static final String DEBUG = "DEBUG";
  41. private static final String FALSE = "false";
  42. private Batch batch = null;
  43. @Override
  44. public void start(Properties globalProperties, List<Object> extensions) {
  45. batch = createBatch(globalProperties, extensions);
  46. batch.start();
  47. }
  48. @Override
  49. public void stop() {
  50. batch.stop();
  51. }
  52. @Override
  53. public void execute(Properties properties) {
  54. batch.executeTask((Map) properties);
  55. }
  56. Batch createBatch(Properties properties, List<Object> extensions) {
  57. initLogging(properties);
  58. EnvironmentInformation env = new EnvironmentInformation(properties.getProperty("sonarRunner.app"), properties.getProperty("sonarRunner.appVersion"));
  59. return Batch.builder()
  60. .setEnvironment(env)
  61. .addComponents(extensions)
  62. .setBootstrapProperties((Map) properties)
  63. .build();
  64. }
  65. private void initLogging(Properties props) {
  66. LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  67. JoranConfigurator jc = new JoranConfigurator();
  68. jc.setContext(context);
  69. context.reset();
  70. try (InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml")) {
  71. System.setProperty("ROOT_LOGGER_LEVEL", isDebug(props) ? DEBUG : "INFO");
  72. context.putProperty("SQL_LOGGER_LEVEL", getSqlLevel(props));
  73. context.putProperty("SQL_RESULTS_LOGGER_LEVEL", getSqlResultsLevel(props));
  74. jc.doConfigure(input);
  75. } catch (JoranException e) {
  76. throw new SonarException("can not initialize logging", e);
  77. } catch (IOException e1) {
  78. throw new SonarException("couldn't close resource", e1);
  79. }
  80. }
  81. @VisibleForTesting
  82. protected boolean isDebug(Properties props) {
  83. return Boolean.parseBoolean(props.getProperty("sonar.verbose", FALSE));
  84. }
  85. @VisibleForTesting
  86. protected static String getSqlLevel(Properties props) {
  87. boolean showSql = "true".equals(props.getProperty("sonar.showSql", FALSE));
  88. return showSql ? DEBUG : WARN;
  89. }
  90. @VisibleForTesting
  91. protected static String getSqlResultsLevel(Properties props) {
  92. boolean showSql = "true".equals(props.getProperty("sonar.showSqlResults", FALSE));
  93. return showSql ? DEBUG : WARN;
  94. }
  95. }