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.

IsolatedLauncher.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Sonar 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 org.apache.commons.io.IOUtils;
  26. import org.slf4j.LoggerFactory;
  27. import org.sonar.api.batch.bootstrap.ProjectReactor;
  28. import org.sonar.api.utils.SonarException;
  29. import org.sonar.batch.bootstrapper.Batch;
  30. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  31. import java.io.InputStream;
  32. import java.util.List;
  33. import java.util.Properties;
  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 IsolatedLauncher {
  39. public void execute(Properties properties, List<Object> extensions) {
  40. String task = properties.getProperty("sonar.task");
  41. ProjectReactor projectReactor = null;
  42. if (task == null || "scan".equals(task)) {
  43. Properties propsClone = new Properties();
  44. propsClone.putAll(properties);
  45. projectReactor = new ProjectReactorBuilder(propsClone).build();
  46. }
  47. initLogging(properties);
  48. EnvironmentInformation env = new EnvironmentInformation(properties.getProperty("sonarRunner.userAgent"), properties.getProperty("sonarRunner.userAgentVersion"));
  49. Batch.Builder builder = Batch.builder()
  50. .setEnvironment(env)
  51. .addComponents(extensions);
  52. if (projectReactor != null) {
  53. builder.setProjectReactor(projectReactor);
  54. }
  55. builder.build().execute();
  56. }
  57. private void initLogging(Properties props) {
  58. LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  59. JoranConfigurator jc = new JoranConfigurator();
  60. jc.setContext(context);
  61. context.reset();
  62. InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
  63. System.setProperty("ROOT_LOGGER_LEVEL", isDebug(props) ? "DEBUG" : "INFO");
  64. context.putProperty("SQL_LOGGER_LEVEL", getSqlLevel(props));
  65. context.putProperty("SQL_RESULTS_LOGGER_LEVEL", getSqlResultsLevel(props));
  66. try {
  67. jc.doConfigure(input);
  68. } catch (JoranException e) {
  69. throw new SonarException("can not initialize logging", e);
  70. } finally {
  71. IOUtils.closeQuietly(input);
  72. }
  73. }
  74. @VisibleForTesting
  75. protected boolean isDebug(Properties props) {
  76. return Boolean.parseBoolean(props.getProperty("sonar.verbose", "false"));
  77. }
  78. @VisibleForTesting
  79. protected static String getSqlLevel(Properties props) {
  80. boolean showSql = "true".equals(props.getProperty("sonar.showSql", "false"));
  81. return showSql ? "DEBUG" : "WARN";
  82. }
  83. @VisibleForTesting
  84. protected static String getSqlResultsLevel(Properties props) {
  85. boolean showSql = "true".equals(props.getProperty("sonar.showSqlResults", "false"));
  86. return showSql ? "DEBUG" : "WARN";
  87. }
  88. }