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.

SimulatedLauncher.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.com
  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.impl;
  21. import org.sonar.runner.batch.IsolatedLauncher;
  22. import org.sonar.runner.batch.IssueListener;
  23. import org.sonar.runner.batch.LogOutput;
  24. import org.sonar.runner.cache.Logger;
  25. import javax.annotation.Nullable;
  26. import java.io.File;
  27. import java.io.FileOutputStream;
  28. import java.io.OutputStream;
  29. import java.util.List;
  30. import java.util.Properties;
  31. public class SimulatedLauncher implements IsolatedLauncher {
  32. private final String version;
  33. private final Logger logger;
  34. private Properties globalProperties;
  35. SimulatedLauncher(String version, Logger logger) {
  36. this.version = version;
  37. this.logger = logger;
  38. }
  39. @Override
  40. public void start(Properties properties, LogOutput logOutput, boolean preferCache) {
  41. globalProperties = properties;
  42. }
  43. @Override
  44. public void stop() {
  45. globalProperties = null;
  46. }
  47. @Override
  48. public void execute(Properties properties) {
  49. dumpProperties(globalProperties, properties);
  50. }
  51. @Override
  52. public void execute(Properties properties, IssueListener listener) {
  53. dumpProperties(globalProperties, properties);
  54. }
  55. private void dumpProperties(@Nullable Properties global, Properties analysis) {
  56. // for old versions, analysis will have global properties merged in it
  57. String filePath;
  58. String filePathGlobal = null;
  59. if (global != null) {
  60. filePath = global.getProperty(InternalProperties.RUNNER_DUMP_TO_FILE);
  61. filePathGlobal = filePath + ".global";
  62. } else {
  63. filePath = analysis.getProperty(InternalProperties.RUNNER_DUMP_TO_FILE);
  64. }
  65. if (filePath == null) {
  66. throw new IllegalStateException("No file to dump properties");
  67. }
  68. if (global != null) {
  69. File dumpFileGlobal = new File(filePathGlobal);
  70. writeProperties(dumpFileGlobal, global, "global properties");
  71. }
  72. File dumpFile = new File(filePath);
  73. writeProperties(dumpFile, analysis, "analysis properties");
  74. logger.info("Simulation mode. Configuration written to " + dumpFile.getAbsolutePath());
  75. }
  76. private static void writeProperties(File outputFile, Properties p, String comment) {
  77. try (OutputStream output = new FileOutputStream(outputFile)) {
  78. p.store(output, "Generated by sonar-runner - " + comment);
  79. } catch (Exception e) {
  80. throw new IllegalStateException("Fail to export sonar-runner properties", e);
  81. }
  82. }
  83. @Override
  84. public void syncProject(String projectKey) {
  85. // no op
  86. }
  87. @Override
  88. public void executeOldVersion(Properties properties, List<Object> extensions) {
  89. dumpProperties(null, properties);
  90. }
  91. @Override
  92. public String getVersion() {
  93. return version;
  94. }
  95. }