]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9734 fix file leaks
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Aug 2017 20:08:36 +0000 (22:08 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 21 Aug 2017 16:09:34 +0000 (18:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/TrackerRawInputFactory.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java

index be172abfac0ad342315409a7ce52121c8ab8b96a..9de67f6b6100779434e63780fcc57a59ef900342 100644 (file)
@@ -77,7 +77,9 @@ public class TrackerRawInputFactory {
     protected LineHashSequence loadLineHashSequence() {
       List<String> lines;
       if (component.getType() == Component.Type.FILE) {
-        lines = newArrayList(sourceLinesRepository.readLines(component));
+        try (CloseableIterator<String> linesIt = sourceLinesRepository.readLines(component)) {
+          lines = newArrayList(linesIt);
+        }
       } else {
         lines = Collections.emptyList();
       }
index e34bdc8b387bd2a406aa9ae24691ecbffca0339c..3994aba0a028e6d962349ad7426e2abcd8d326ac 100644 (file)
@@ -25,6 +25,7 @@ import java.util.List;
 import javax.annotation.Nonnull;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.CloseableIterator;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.duplication.DuplicationUnitDto;
@@ -96,10 +97,13 @@ public class LoadCrossProjectDuplicationsRepositoryStep implements ComputationSt
 
     @Override
     public void visitFile(Component file) {
-      List<CpdTextBlock> cpdTextBlocks = newArrayList(reportReader.readCpdTextBlocks(file.getReportAttributes().getRef()));
-      LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey());
-      if (cpdTextBlocks.isEmpty()) {
-        return;
+      List<CpdTextBlock> cpdTextBlocks;
+      try (CloseableIterator<CpdTextBlock> blocksIt = reportReader.readCpdTextBlocks(file.getReportAttributes().getRef())) {
+        cpdTextBlocks = newArrayList(blocksIt);
+        LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey());
+        if (cpdTextBlocks.isEmpty()) {
+          return;
+        }
       }
 
       Collection<String> hashes = from(cpdTextBlocks).transform(CpdTextBlockToHash.INSTANCE).toList();
index e0d324a89c5508059c8baf380fdc253cbbc77fe8..79a8aeec7261e9bbb982b12deb664a8cfebddffa 100644 (file)
@@ -122,17 +122,13 @@ public class PersistFileSourcesStep implements ComputationStep {
         return;
       }
 
-      CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
-      LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file);
-      try {
+      try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
+           LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file)) {
         ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), file.getFileAttributes().getLines());
         ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute();
         persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange());
       } catch (Exception e) {
         throw new IllegalStateException(String.format("Cannot persist sources of %s", file.getKey()), e);
-      } finally {
-        linesIterator.close();
-        lineReaders.close();
       }
     }
 
@@ -188,7 +184,7 @@ public class PersistFileSourcesStep implements ComputationStep {
     }
   }
 
-  private static class LineReaders {
+  private static class LineReaders implements AutoCloseable {
     private final List<LineReader> readers = new ArrayList<>();
     private final List<CloseableIterator<?>> closeables = new ArrayList<>();
     @CheckForNull
@@ -223,7 +219,8 @@ public class PersistFileSourcesStep implements ComputationStep {
       return readers;
     }
 
-    void close() {
+    @Override
+    public void close() {
       for (CloseableIterator<?> reportIterator : closeables) {
         reportIterator.close();
       }