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.

Launcher.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Sonar Standalone Runner
  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.model;
  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.configuration.CompositeConfiguration;
  26. import org.apache.commons.configuration.Configuration;
  27. import org.apache.commons.configuration.EnvironmentConfiguration;
  28. import org.apache.commons.configuration.MapConfiguration;
  29. import org.apache.commons.configuration.SystemConfiguration;
  30. import org.apache.commons.io.IOUtils;
  31. import org.slf4j.LoggerFactory;
  32. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  33. import org.sonar.api.batch.bootstrap.ProjectReactor;
  34. import org.sonar.api.utils.SonarException;
  35. import org.sonar.batch.Batch;
  36. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  37. import org.sonar.runner.Runner;
  38. import java.io.InputStream;
  39. import java.util.Properties;
  40. /**
  41. * Contrary to {@link org.sonar.runner.Runner}, this class is executed within the classloader
  42. * provided by the server. It contains the installed plugins and the same version of sonar-batch as the server.
  43. */
  44. public class Launcher {
  45. private Properties propertiesFromRunner;
  46. public Launcher(Properties properties) {
  47. this.propertiesFromRunner = properties;
  48. }
  49. /**
  50. * Main entry point.
  51. */
  52. public void execute() {
  53. ProjectDefinition project = SonarProjectBuilder.create(propertiesFromRunner).generateProjectDefinition();
  54. Configuration initialConfiguration = getInitialConfiguration(project);
  55. initLogging(initialConfiguration);
  56. executeBatch(project, initialConfiguration);
  57. }
  58. private void executeBatch(ProjectDefinition project, Configuration initialConfiguration) {
  59. ProjectReactor reactor = new ProjectReactor(project);
  60. String envKey = propertiesFromRunner.getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_KEY);
  61. String envVersion = propertiesFromRunner.getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_VERSION);
  62. Batch batch = Batch.create(reactor, initialConfiguration, new EnvironmentInformation(envKey, envVersion));
  63. batch.execute();
  64. }
  65. private void initLogging(Configuration initialConfiguration) {
  66. LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  67. JoranConfigurator jc = new JoranConfigurator();
  68. jc.setContext(context);
  69. context.reset();
  70. InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
  71. System.setProperty("ROOT_LOGGER_LEVEL", isDebug() ? "DEBUG" : "INFO");
  72. context.putProperty("SQL_LOGGER_LEVEL", getSqlLevel(initialConfiguration));// since 2.14. Ignored on previous versions.
  73. context.putProperty("SQL_RESULTS_LOGGER_LEVEL", getSqlResultsLevel(initialConfiguration));// since 2.14. Ignored on previous versions.
  74. try {
  75. jc.doConfigure(input);
  76. } catch (JoranException e) {
  77. throw new SonarException("can not initialize logging", e);
  78. } finally {
  79. IOUtils.closeQuietly(input);
  80. }
  81. }
  82. @VisibleForTesting
  83. protected boolean isDebug() {
  84. return Boolean.parseBoolean(propertiesFromRunner.getProperty(Runner.PROPERTY_VERBOSE, propertiesFromRunner.getProperty(Runner.PROPERTY_OLD_DEBUG_MODE, "false")));
  85. }
  86. @VisibleForTesting
  87. protected static String getSqlLevel(Configuration config) {
  88. boolean showSql = config.getBoolean("sonar.showSql", false);
  89. return showSql ? "DEBUG" : "WARN";
  90. }
  91. @VisibleForTesting
  92. protected static String getSqlResultsLevel(Configuration config) {
  93. boolean showSql = config.getBoolean("sonar.showSqlResults", false);
  94. return showSql ? "DEBUG" : "WARN";
  95. }
  96. private Configuration getInitialConfiguration(ProjectDefinition project) {
  97. CompositeConfiguration configuration = new CompositeConfiguration();
  98. configuration.addConfiguration(new SystemConfiguration());
  99. configuration.addConfiguration(new EnvironmentConfiguration());
  100. configuration.addConfiguration(new MapConfiguration(project.getProperties()));
  101. return configuration;
  102. }
  103. }