aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-07-17 10:05:49 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-07-24 13:14:57 +0200
commit8566436b507addd3b891cbe84a04e3e0225358ba (patch)
tree3d820452d1ba481673c6aac8830495135c1e8965 /sonar-home
parent26bd5e1700d2d0a89706c61c7404ffb4f80fec3e (diff)
downloadsonarqube-8566436b507addd3b891cbe84a04e3e0225358ba.tar.gz
sonarqube-8566436b507addd3b891cbe84a04e3e0225358ba.zip
SONAR-6577 Offline mode in preview mode
Diffstat (limited to 'sonar-home')
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java55
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java8
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheLoader.java26
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java36
4 files changed, 63 insertions, 62 deletions
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 95b93e6ed27..e4c8bd02912 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
@@ -32,7 +32,6 @@ import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
-import java.util.concurrent.Callable;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -51,25 +50,18 @@ public class PersistentCache {
// eviction strategy is to expire entries after modification once a time duration has elapsed
private final long defaultDurationToExpireMs;
- private boolean forceUpdate;
private final Logger logger;
- public PersistentCache(Path baseDir, long defaultDurationToExpireMs, boolean forceUpdate, Logger logger) {
+ public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger) {
this.baseDir = baseDir;
this.defaultDurationToExpireMs = defaultDurationToExpireMs;
this.logger = logger;
- reconfigure(forceUpdate);
+ reconfigure();
logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs);
}
- public void reconfigure(boolean forceUpdate) {
- this.forceUpdate = forceUpdate;
-
- if (forceUpdate) {
- logger.debug("cache: forcing update");
- }
-
+ public void reconfigure() {
try {
Files.createDirectories(baseDir);
} catch (IOException e) {
@@ -81,16 +73,12 @@ public class PersistentCache {
return baseDir;
}
- public boolean isForceUpdate() {
- return forceUpdate;
- }
-
@CheckForNull
- public synchronized String getString(@Nonnull String obj, @Nullable final Callable<String> valueLoader) throws Exception {
- byte[] cached = get(obj, new Callable<byte[]>() {
+ public synchronized String getString(@Nonnull String obj, @Nullable final PersistentCacheLoader<String> valueLoader) throws IOException {
+ byte[] cached = get(obj, new PersistentCacheLoader<byte[]>() {
@Override
- public byte[] call() throws Exception {
- String s = valueLoader.call();
+ public byte[] get() throws IOException {
+ String s = valueLoader.get();
if (s != null) {
return s.getBytes(ENCODING);
}
@@ -106,26 +94,23 @@ public class PersistentCache {
}
@CheckForNull
- public synchronized byte[] get(@Nonnull String obj, @Nullable Callable<byte[]> valueLoader) throws Exception {
+ public synchronized byte[] get(@Nonnull String obj, @Nullable PersistentCacheLoader<byte[]> valueLoader) throws IOException {
String key = getKey(obj);
try {
lock();
- if (!forceUpdate) {
- byte[] cached = getCache(key);
- if (cached != null) {
- logger.debug("cache hit for " + obj + " -> " + key);
- return cached;
- }
+ byte[] cached = getCache(key);
- logger.debug("cache miss for " + obj + " -> " + key);
- } else {
- logger.debug("cache force update for " + obj + " -> " + key);
+ if (cached != null) {
+ logger.debug("cache hit for " + obj + " -> " + key);
+ return cached;
}
+ logger.debug("cache miss for " + obj + " -> " + key);
+
if (valueLoader != null) {
- byte[] value = valueLoader.call();
+ byte[] value = valueLoader.get();
if (value != null) {
putCache(key, value);
}
@@ -138,6 +123,16 @@ public class PersistentCache {
return null;
}
+ public synchronized void put(@Nonnull String obj, @Nonnull byte[] value) throws IOException {
+ String key = getKey(obj);
+ try {
+ lock();
+ putCache(key, value);
+ } finally {
+ unlock();
+ }
+ }
+
/**
* Deletes all cache entries
*/
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java
index e115f1cab64..f0fa1596917 100644
--- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java
+++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java
@@ -28,7 +28,6 @@ public class PersistentCacheBuilder {
private static final long DEFAULT_EXPIRE_DURATION = TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS);
private static final String DIR_NAME = "ws_cache";
- private boolean forceUpdate = false;
private Path cachePath;
private final Logger logger;
@@ -41,7 +40,7 @@ public class PersistentCacheBuilder {
setSonarHome(findHome());
}
- return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, forceUpdate, logger);
+ return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger);
}
public PersistentCacheBuilder setSonarHome(@Nullable Path p) {
@@ -51,11 +50,6 @@ public class PersistentCacheBuilder {
return this;
}
- public PersistentCacheBuilder forceUpdate(boolean update) {
- this.forceUpdate = update;
- return this;
- }
-
private static Path findHome() {
String home = System.getenv("SONAR_USER_HOME");
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheLoader.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheLoader.java
new file mode 100644
index 00000000000..ee906b4b67d
--- /dev/null
+++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheLoader.java
@@ -0,0 +1,26 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.home.cache;
+
+import java.io.IOException;
+
+public interface PersistentCacheLoader<T> {
+ T get() throws IOException;
+}
diff --git a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
index 09335dffc3b..62e105bf861 100644
--- a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
+++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
@@ -20,7 +20,6 @@
package org.sonar.home.cache;
import java.io.File;
-import java.util.concurrent.Callable;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
@@ -42,7 +41,7 @@ public class PersistentCacheTest {
@Before
public void setUp() {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, false, mock(Logger.class));
+ cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class));
}
@Test
@@ -59,9 +58,9 @@ public class PersistentCacheTest {
@Test
public void testNullValue() throws Exception {
// mocks have their methods returning null by default
- Callable<byte[]> c = mock(Callable.class);
+ PersistentCacheLoader<byte[]> c = mock(PersistentCacheLoader.class);
assertThat(cache.get(URI, c)).isNull();
- verify(c).call();
+ verify(c).get();
assertCacheHit(false);
}
@@ -79,29 +78,16 @@ public class PersistentCacheTest {
}
@Test
- public void testForceUpdate() throws Exception {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, true, mock(Logger.class));
-
- assertCacheHit(false);
- assertCacheHit(false);
- assertCacheHit(false);
-
- // with forceUpdate, it should still have cached the last call
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, false, mock(Logger.class));
- assertCacheHit(true);
- }
-
- @Test
public void testReconfigure() throws Exception {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, true, mock(Logger.class));
- assertCacheHit(false);
+ cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class));
assertCacheHit(false);
+ assertCacheHit(true);
File root = tmp.getRoot();
FileUtils.deleteQuietly(root);
// should re-create cache directory and start using the cache
- cache.reconfigure(false);
+ cache.reconfigure();
assertThat(root).exists();
assertCacheHit(false);
@@ -111,7 +97,7 @@ public class PersistentCacheTest {
@Test
public void testExpiration() throws Exception {
// negative time to make sure it is expired on the second call
- cache = new PersistentCache(tmp.getRoot().toPath(), -100, false, mock(Logger.class));
+ cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class));
assertCacheHit(false);
assertCacheHit(false);
}
@@ -122,11 +108,11 @@ public class PersistentCacheTest {
assertThat(c.wasCalled).isEqualTo(!hit);
}
- private class CacheFillerString implements Callable<String> {
+ private class CacheFillerString implements PersistentCacheLoader<String> {
public boolean wasCalled = false;
@Override
- public String call() throws Exception {
+ public String get() {
wasCalled = true;
return VALUE;
}
@@ -139,8 +125,8 @@ public class PersistentCacheTest {
*/
@Test(expected = ArithmeticException.class)
public void testExceptions() throws Exception {
- Callable<byte[]> c = mock(Callable.class);
- when(c.call()).thenThrow(ArithmeticException.class);
+ PersistentCacheLoader<byte[]> c = mock(PersistentCacheLoader.class);
+ when(c.get()).thenThrow(ArithmeticException.class);
cache.get(URI, c);
}