From 4be016e61f4d1e4fed297dd7eb3b3ccb286f0f81 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 8 Sep 2015 15:24:50 +0200 Subject: SONAR-6817 Issues mode should support analysis of projects not associated --- .../main/java/org/sonar/home/cache/FileCache.java | 2 +- .../main/java/org/sonar/home/cache/FileHashes.java | 2 +- .../java/org/sonar/home/cache/PersistentCache.java | 67 +++++++++++++++++++++- 3 files changed, 68 insertions(+), 3 deletions(-) (limited to 'sonar-home/src') 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 valueLoader) throws IOException { ValueLoaderDecoder decoder = valueLoader != null ? new ValueLoaderDecoder(valueLoader) : null; - + byte[] cached = get(obj, decoder); if (cached == null) { @@ -87,6 +90,20 @@ public class PersistentCache { return new String(cached, ENCODING); } + @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 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; -- cgit v1.2.3