3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.source;
22 import java.util.HashMap;
24 import javax.annotation.Nullable;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.core.hash.SourceHashComputer;
27 import org.sonar.core.util.CloseableIterator;
29 import static com.google.common.base.Preconditions.checkArgument;
30 import static com.google.common.base.Preconditions.checkState;
31 import static java.util.Objects.requireNonNull;
33 public class SourceHashRepositoryImpl implements SourceHashRepository {
34 private static final String SOURCE_OR_HASH_FAILURE_ERROR_MSG = "Failed to read source and compute hashes for component %s";
36 private final SourceLinesRepository sourceLinesRepository;
37 private final Map<String, String> rawSourceHashesByKey = new HashMap<>();
39 public SourceHashRepositoryImpl(SourceLinesRepository sourceLinesRepository) {
40 this.sourceLinesRepository = sourceLinesRepository;
44 public String getRawSourceHash(Component file) {
45 checkComponentArgument(file);
46 if (rawSourceHashesByKey.containsKey(file.getDbKey())) {
47 return checkSourceHash(file.getDbKey(), rawSourceHashesByKey.get(file.getDbKey()));
49 String newSourceHash = computeRawSourceHash(file);
50 rawSourceHashesByKey.put(file.getDbKey(), newSourceHash);
51 return checkSourceHash(file.getDbKey(), newSourceHash);
55 private static void checkComponentArgument(Component file) {
56 requireNonNull(file, "Specified component can not be null");
57 checkArgument(file.getType() == Component.Type.FILE, "File source information can only be retrieved from FILE components (got %s)", file.getType());
60 private String computeRawSourceHash(Component file) {
61 SourceHashComputer sourceHashComputer = new SourceHashComputer();
62 try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file)) {
63 while (linesIterator.hasNext()) {
64 sourceHashComputer.addLine(linesIterator.next(), linesIterator.hasNext());
66 return sourceHashComputer.getHash();
70 private static String checkSourceHash(String fileKey, @Nullable String newSourceHash) {
71 checkState(newSourceHash != null, SOURCE_OR_HASH_FAILURE_ERROR_MSG, fileKey);