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.

BatchReportReaderImpl.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT 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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.batch;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.util.NoSuchElementException;
  25. import java.util.Optional;
  26. import javax.annotation.CheckForNull;
  27. import org.apache.commons.io.FileUtils;
  28. import org.apache.commons.io.IOUtils;
  29. import org.apache.commons.io.LineIterator;
  30. import org.sonar.core.util.CloseableIterator;
  31. import org.sonar.core.util.LineReaderIterator;
  32. import org.sonar.scanner.protocol.output.ScannerReport;
  33. import org.sonar.scanner.protocol.output.ScannerReport.LineSgnificantCode;
  34. import static java.nio.charset.StandardCharsets.UTF_8;
  35. public class BatchReportReaderImpl implements BatchReportReader {
  36. private final BatchReportDirectoryHolder batchReportDirectoryHolder;
  37. private org.sonar.scanner.protocol.output.ScannerReportReader delegate;
  38. // caching of metadata which are read often
  39. private ScannerReport.Metadata metadata;
  40. public BatchReportReaderImpl(BatchReportDirectoryHolder batchReportDirectoryHolder) {
  41. this.batchReportDirectoryHolder = batchReportDirectoryHolder;
  42. }
  43. private void ensureInitialized() {
  44. if (this.delegate == null) {
  45. this.delegate = new org.sonar.scanner.protocol.output.ScannerReportReader(batchReportDirectoryHolder.getDirectory());
  46. }
  47. }
  48. @Override
  49. public ScannerReport.Metadata readMetadata() {
  50. ensureInitialized();
  51. if (this.metadata == null) {
  52. this.metadata = delegate.readMetadata();
  53. }
  54. return this.metadata;
  55. }
  56. @Override
  57. public CloseableIterator<String> readScannerLogs() {
  58. ensureInitialized();
  59. File file = delegate.getFileStructure().analysisLog();
  60. if (!file.exists()) {
  61. return CloseableIterator.emptyCloseableIterator();
  62. }
  63. try {
  64. InputStreamReader reader = new InputStreamReader(FileUtils.openInputStream(file), UTF_8);
  65. return new LineReaderIterator(reader);
  66. } catch (IOException e) {
  67. throw new IllegalStateException("Fail to open file " + file, e);
  68. }
  69. }
  70. @Override
  71. public CloseableIterator<ScannerReport.ActiveRule> readActiveRules() {
  72. ensureInitialized();
  73. return delegate.readActiveRules();
  74. }
  75. @Override
  76. public CloseableIterator<ScannerReport.AdHocRule> readAdHocRules() {
  77. ensureInitialized();
  78. return delegate.readAdHocRules();
  79. }
  80. @Override
  81. public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) {
  82. ensureInitialized();
  83. return delegate.readComponentMeasures(componentRef);
  84. }
  85. @Override
  86. @CheckForNull
  87. public ScannerReport.Changesets readChangesets(int componentRef) {
  88. ensureInitialized();
  89. return delegate.readChangesets(componentRef);
  90. }
  91. @Override
  92. public ScannerReport.Component readComponent(int componentRef) {
  93. ensureInitialized();
  94. return delegate.readComponent(componentRef);
  95. }
  96. @Override
  97. public CloseableIterator<ScannerReport.Issue> readComponentIssues(int componentRef) {
  98. ensureInitialized();
  99. return delegate.readComponentIssues(componentRef);
  100. }
  101. @Override
  102. public CloseableIterator<ScannerReport.ExternalIssue> readComponentExternalIssues(int componentRef) {
  103. ensureInitialized();
  104. return delegate.readComponentExternalIssues(componentRef);
  105. }
  106. @Override
  107. public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) {
  108. ensureInitialized();
  109. return delegate.readComponentDuplications(componentRef);
  110. }
  111. @Override
  112. public CloseableIterator<ScannerReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
  113. ensureInitialized();
  114. return delegate.readCpdTextBlocks(componentRef);
  115. }
  116. @Override
  117. public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) {
  118. ensureInitialized();
  119. return delegate.readComponentSymbols(componentRef);
  120. }
  121. @Override
  122. public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) {
  123. ensureInitialized();
  124. return delegate.readComponentSyntaxHighlighting(fileRef);
  125. }
  126. @Override
  127. public CloseableIterator<ScannerReport.LineCoverage> readComponentCoverage(int fileRef) {
  128. ensureInitialized();
  129. return delegate.readComponentCoverage(fileRef);
  130. }
  131. @Override
  132. public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
  133. ensureInitialized();
  134. File file = delegate.readFileSource(fileRef);
  135. if (file == null) {
  136. return Optional.empty();
  137. }
  138. try {
  139. return Optional.of(new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), UTF_8)));
  140. } catch (IOException e) {
  141. throw new IllegalStateException("Fail to traverse file: " + file, e);
  142. }
  143. }
  144. private static class CloseableLineIterator extends CloseableIterator<String> {
  145. private final LineIterator lineIterator;
  146. public CloseableLineIterator(LineIterator lineIterator) {
  147. this.lineIterator = lineIterator;
  148. }
  149. @Override
  150. public boolean hasNext() {
  151. return lineIterator.hasNext();
  152. }
  153. @Override
  154. public String next() {
  155. return lineIterator.next();
  156. }
  157. @Override
  158. protected String doNext() {
  159. // never called anyway
  160. throw new NoSuchElementException("Empty closeable Iterator has no element");
  161. }
  162. @Override
  163. protected void doClose() throws IOException {
  164. lineIterator.close();
  165. }
  166. }
  167. @Override
  168. public CloseableIterator<ScannerReport.ContextProperty> readContextProperties() {
  169. ensureInitialized();
  170. return delegate.readContextProperties();
  171. }
  172. @Override
  173. public Optional<CloseableIterator<LineSgnificantCode>> readComponentSignificantCode(int fileRef) {
  174. ensureInitialized();
  175. return Optional.ofNullable(delegate.readComponentSignificantCode(fileRef));
  176. }
  177. @Override
  178. public Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef) {
  179. ensureInitialized();
  180. return Optional.ofNullable(delegate.readComponentChangedLines(fileRef));
  181. }
  182. @Override
  183. public CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings() {
  184. ensureInitialized();
  185. return delegate.readAnalysisWarnings();
  186. }
  187. }