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.

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