aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-08 15:24:50 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-09 11:57:38 +0200
commit4be016e61f4d1e4fed297dd7eb3b3ccb286f0f81 (patch)
tree7ce08a4beef4bf70b6521bdd04e27cc9e2907e92 /sonar-home
parent84c39f50d53f7f4a76fcda6c58b6120875501e40 (diff)
downloadsonarqube-4be016e61f4d1e4fed297dd7eb3b3ccb286f0f81.tar.gz
sonarqube-4be016e61f4d1e4fed297dd7eb3b3ccb286f0f81.zip
SONAR-6817 Issues mode should support analysis of projects not associated
Diffstat (limited to 'sonar-home')
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/FileCache.java2
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java2
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java67
3 files changed, 68 insertions, 3 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
index 922a59e5e8d..6d926a918e6 100644
--- a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
+++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
@@ -117,7 +117,7 @@ public class FileCache {
return new File(dir, hash);
}
- private void mkdirQuietly(File hashDir) {
+ private static void mkdirQuietly(File hashDir) {
try {
Files.createDirectories(hashDir.toPath());
} catch (IOException e) {
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java b/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java
index 549f8d10f63..e79a9b4fd74 100644
--- a/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java
+++ b/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java
@@ -56,7 +56,7 @@ public class FileHashes {
}
}
- private byte[] digest(InputStream input, MessageDigest digest) throws IOException {
+ private static byte[] digest(InputStream input, MessageDigest digest) throws IOException {
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = input.read(buffer, 0, STREAM_BUFFER_LENGTH);
while (read > -1) {
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
index c5c45090ed4..2b32c65b7f9 100644
--- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
+++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
@@ -19,7 +19,9 @@
*/
package org.sonar.home.cache;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
@@ -28,6 +30,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -77,7 +80,7 @@ public class PersistentCache {
@CheckForNull
public synchronized String getString(@Nonnull String obj, @Nullable final PersistentCacheLoader<String> valueLoader) throws IOException {
ValueLoaderDecoder decoder = valueLoader != null ? new ValueLoaderDecoder(valueLoader) : null;
-
+
byte[] cached = get(obj, decoder);
if (cached == null) {
@@ -88,6 +91,20 @@ public class PersistentCache {
}
@CheckForNull
+ public synchronized InputStream getStream(@Nonnull String obj) throws IOException {
+ String key = getKey(obj);
+
+ try {
+ lock();
+ Path path = getCacheCopy(key);
+ return new DeleteOnCloseInputStream(new FileInputStream(path.toFile()), path);
+
+ } finally {
+ unlock();
+ }
+ }
+
+ @CheckForNull
public synchronized byte[] get(@Nonnull String obj, @Nullable PersistentCacheLoader<byte[]> valueLoader) throws IOException {
String key = getKey(obj);
@@ -116,6 +133,16 @@ public class PersistentCache {
return null;
}
+
+ public synchronized void put(@Nonnull String obj, @Nonnull InputStream stream) throws IOException {
+ String key = getKey(obj);
+ try {
+ lock();
+ putCache(key, stream);
+ } finally {
+ unlock();
+ }
+ }
public synchronized void put(@Nonnull String obj, @Nonnull byte[] value) throws IOException {
String key = getKey(obj);
@@ -266,6 +293,11 @@ public class PersistentCache {
Path cachePath = getCacheEntryPath(key);
Files.write(cachePath, value, CREATE, WRITE, TRUNCATE_EXISTING);
}
+
+ private void putCache(String key, InputStream stream) throws IOException {
+ Path cachePath = getCacheEntryPath(key);
+ Files.copy(stream, cachePath, StandardCopyOption.REPLACE_EXISTING);
+ }
private byte[] getCache(String key) throws IOException {
Path cachePath = getCacheEntryPath(key);
@@ -277,6 +309,39 @@ public class PersistentCache {
return Files.readAllBytes(cachePath);
}
+ private Path getCacheCopy(String key) throws IOException {
+ Path cachePath = getCacheEntryPath(key);
+
+ if (!validateCacheEntry(cachePath, this.defaultDurationToExpireMs)) {
+ return null;
+ }
+
+ Path temp = Files.createTempFile("sonar_cache", null);
+ Files.copy(cachePath, temp, StandardCopyOption.REPLACE_EXISTING);
+ return temp;
+ }
+
+ private static class DeleteOnCloseInputStream extends InputStream {
+ private final InputStream stream;
+ private final Path p;
+
+ private DeleteOnCloseInputStream(InputStream stream, Path p) {
+ this.stream = stream;
+ this.p = p;
+ }
+
+ @Override
+ public int read() throws IOException {
+ return stream.read();
+ }
+
+ @Override
+ public void close() throws IOException {
+ stream.close();
+ Files.delete(p);
+ }
+ }
+
private boolean validateCacheEntry(Path cacheEntryPath, long durationToExpireMs) throws IOException {
if (!Files.exists(cacheEntryPath)) {
return false;