Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Launcher.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Sonar Runner - Implementation
  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 com.google.common.collect.Maps;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.slf4j.LoggerFactory;
  29. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  30. import org.sonar.api.batch.bootstrap.ProjectReactor;
  31. import org.sonar.api.utils.SonarException;
  32. import org.sonar.batch.bootstrapper.Batch;
  33. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  34. import org.sonar.runner.Runner;
  35. import java.io.InputStream;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Properties;
  39. /**
  40. * Contrary to {@link org.sonar.runner.Runner}, this class is executed within the classloader
  41. * provided by the server. It contains the installed plugins and the same version of sonar-batch as the server.
  42. */
  43. public class Launcher {
  44. private String command;
  45. private Properties globalProperties;
  46. private Properties projectProperties;
  47. private List<Object> containerExtensions;
  48. /**
  49. * @deprecated Use {@link Launcher#Launcher(String, Properties, Properties, List)} instead
  50. */
  51. @Deprecated
  52. public Launcher(Properties properties, List<Object> containerExtensions) {
  53. this("project-analysis", new Properties(), properties, containerExtensions);
  54. }
  55. public Launcher(String command, Properties globalProperties, Properties projectProperties, List<Object> containerExtensions) {
  56. this.command = command;
  57. this.globalProperties = globalProperties;
  58. this.projectProperties = projectProperties;
  59. this.containerExtensions = containerExtensions;
  60. }
  61. /**
  62. * Main entry point.
  63. */
  64. public void execute() {
  65. Properties globalConfiguration = getInitialConfiguration();
  66. globalConfiguration.putAll(globalProperties);
  67. initLogging(globalConfiguration);
  68. Properties projectConfiguration = new Properties();
  69. projectConfiguration.putAll(globalConfiguration);
  70. projectConfiguration.putAll(projectProperties);
  71. ProjectDefinition project = SonarProjectBuilder.create(command, projectConfiguration).generateProjectDefinition();
  72. executeBatch(globalConfiguration, project);
  73. }
  74. private void executeBatch(Properties globalConfiguration, ProjectDefinition project) {
  75. String envKey = globalProperties.getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_KEY);
  76. String envVersion = globalProperties.getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_VERSION);
  77. Batch.Builder builder = Batch.builder()
  78. .setEnvironment(new EnvironmentInformation(envKey, envVersion));
  79. for (Object extension : containerExtensions) {
  80. builder.addComponent(extension);
  81. }
  82. if (project != null) {
  83. builder.setProjectReactor(new ProjectReactor(project));
  84. }
  85. if (StringUtils.isNotBlank(command)) {
  86. // This code can only works on Sonar 3.5+
  87. builder
  88. .setGlobalProperties(toMap(globalConfiguration))
  89. .setTaskCommand(command);
  90. }
  91. Batch batch = builder.build();
  92. batch.execute();
  93. }
  94. private Map<String, String> toMap(Properties props) {
  95. Map<String, String> result = Maps.newHashMap();
  96. for (Map.Entry<Object, Object> entry : props.entrySet()) {
  97. result.put(entry.getKey().toString(), entry.getValue().toString());
  98. }
  99. return result;
  100. }
  101. private void initLogging(Properties props) {
  102. LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  103. JoranConfigurator jc = new JoranConfigurator();
  104. jc.setContext(context);
  105. context.reset();
  106. InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
  107. System.setProperty("ROOT_LOGGER_LEVEL", isDebug() ? "DEBUG" : "INFO");
  108. context.putProperty("SQL_LOGGER_LEVEL", getSqlLevel(props));
  109. context.putProperty("SQL_RESULTS_LOGGER_LEVEL", getSqlResultsLevel(props));
  110. try {
  111. jc.doConfigure(input);
  112. } catch (JoranException e) {
  113. throw new SonarException("can not initialize logging", e);
  114. } finally {
  115. IOUtils.closeQuietly(input);
  116. }
  117. }
  118. @VisibleForTesting
  119. protected boolean isDebug() {
  120. return Boolean.parseBoolean(projectProperties.getProperty(Runner.PROPERTY_VERBOSE,
  121. projectProperties.getProperty(Runner.PROPERTY_OLD_DEBUG_MODE, "false")));
  122. }
  123. @VisibleForTesting
  124. protected static String getSqlLevel(Properties props) {
  125. boolean showSql = "true".equals(props.get("sonar.showSql"));
  126. return showSql ? "DEBUG" : "WARN";
  127. }
  128. @VisibleForTesting
  129. protected static String getSqlResultsLevel(Properties props) {
  130. boolean showSql = "true".equals(props.get("sonar.showSqlResults"));
  131. return showSql ? "DEBUG" : "WARN";
  132. }
  133. private Properties getInitialConfiguration() {
  134. Properties props = new Properties();
  135. props.putAll(System.getProperties());
  136. props.putAll(System.getenv());
  137. return props;
  138. }
  139. }