3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.batch.issue.tracking;
22 import org.sonar.batch.cache.WSLoader.LoadStrategy;
24 import org.sonar.batch.cache.WSLoaderResult;
25 import org.sonar.batch.cache.WSLoader;
26 import org.apache.commons.lang.mutable.MutableBoolean;
28 import javax.annotation.Nullable;
30 import org.sonar.batch.util.BatchUtils;
31 import com.google.common.base.Splitter;
32 import com.google.common.collect.Iterators;
33 import org.sonar.api.utils.log.Loggers;
34 import org.sonar.api.utils.log.Profiler;
36 public class DefaultServerLineHashesLoader implements ServerLineHashesLoader {
38 private final WSLoader wsLoader;
40 public DefaultServerLineHashesLoader(WSLoader wsLoader) {
41 this.wsLoader = wsLoader;
45 public String[] getLineHashes(String fileKey, @Nullable MutableBoolean fromCache) {
46 String hashesFromWs = loadHashesFromWs(fileKey, fromCache);
47 return Iterators.toArray(Splitter.on('\n').split(hashesFromWs).iterator(), String.class);
50 private String loadHashesFromWs(String fileKey, @Nullable MutableBoolean fromCache) {
51 Profiler profiler = Profiler.createIfDebug(Loggers.get(getClass()))
52 .addContext("file", fileKey)
53 .startDebug("Load line hashes");
54 WSLoaderResult<String> result = wsLoader.loadString("/api/sources/hash?key=" + BatchUtils.encodeForUrl(fileKey), LoadStrategy.CACHE_FIRST);
56 if (fromCache != null) {
57 fromCache.setValue(result.isFromCache());
61 if (result.isFromCache()) {
62 profiler.stopDebug("Load line hashes (done from cache)");