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.9KB

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