diff options
9 files changed, 70 insertions, 32 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PersistentCacheProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PersistentCacheProvider.java index 323b5aa0a0e..ea9f3a14fc4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PersistentCacheProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PersistentCacheProvider.java @@ -19,7 +19,13 @@ */ package org.sonar.batch.bootstrap; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.nio.file.Paths; + import org.picocontainer.injectors.ProviderAdapter; import org.sonar.home.cache.PersistentCache; import org.sonar.home.cache.PersistentCacheBuilder; @@ -36,9 +42,22 @@ public class PersistentCacheProvider extends ProviderAdapter { builder.setSonarHome(Paths.get(home)); } + builder.setVersion(getVersion()); cache = builder.build(); - } - + } + return cache; } + + private String getVersion() { + InputStream is = this.getClass().getClassLoader().getResourceAsStream("sq-version.txt"); + if (is == null) { + return null; + } + try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { + return br.readLine(); + } catch (IOException e) { + return null; + } + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java index 5cbcfe3671d..94065d8a2d3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java @@ -108,15 +108,6 @@ public final class Batch { /** * @since 5.2 */ - public Batch executeTask(Map<String, String> analysisProperties) { - checkStarted(); - bootstrapContainer.executeAnalysis(analysisProperties, components); - return this; - } - - /** - * @since 5.2 - */ public Batch executeTask(Map<String, String> analysisProperties, IssueListener issueListener) { checkStarted(); components.add(issueListener); diff --git a/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java b/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java index 4ec4c85cb93..0102366217d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java @@ -19,12 +19,14 @@ */ package org.sonar.batch.platform; +import org.apache.commons.lang.StringUtils; + +import org.sonar.batch.bootstrap.BootstrapProperties; import org.slf4j.LoggerFactory; import org.sonar.api.batch.BatchSide; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; -import org.sonar.batch.bootstrap.ServerClient; import javax.annotation.CheckForNull; @@ -37,11 +39,11 @@ import java.util.Date; public class DefaultServer extends Server { private Settings settings; - private ServerClient client; + private BootstrapProperties props; - public DefaultServer(Settings settings, ServerClient client) { + public DefaultServer(Settings settings, BootstrapProperties props) { this.settings = settings; - this.client = client; + this.props = props; } @Override @@ -86,7 +88,7 @@ public class DefaultServer extends Server { @Override public String getURL() { - return client.getURL(); + return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/"); } @Override diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java index 4857621a910..4f975659a89 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java @@ -20,14 +20,12 @@ package org.sonar.batch.bootstrap; import java.util.Collections; - import org.junit.Before; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class PersistentCacheProviderTest { private PersistentCacheProvider provider = null; - private BootstrapProperties props = null; @Before diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTestWithServer.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTestWithServer.java index 5b5f2c7efee..b99d851d321 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTestWithServer.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTestWithServer.java @@ -51,7 +51,7 @@ public class WSLoaderTestWithServer { when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort()); client = new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4")); - cache = new PersistentCache(temp.getRoot().toPath(), 1000 * 60, new Slf4jLogger()); + cache = new PersistentCache(temp.getRoot().toPath(), 1000 * 60, new Slf4jLogger(), null); loader = new WSLoader(cache, client); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/platform/DefaultServerTest.java b/sonar-batch/src/test/java/org/sonar/batch/platform/DefaultServerTest.java index adb2b134b1f..47bc2f9c012 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/platform/DefaultServerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/platform/DefaultServerTest.java @@ -19,11 +19,11 @@ */ package org.sonar.batch.platform; +import org.sonar.batch.bootstrap.BootstrapProperties; + import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; -import org.sonar.batch.bootstrap.ServerClient; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -37,10 +37,10 @@ public class DefaultServerTest { settings.setProperty(CoreProperties.SERVER_VERSION, "2.2"); settings.setProperty(CoreProperties.SERVER_STARTTIME, "2010-05-18T17:59:00+0000"); settings.setProperty(CoreProperties.PERMANENT_SERVER_ID, "abcde"); - ServerClient client = mock(ServerClient.class); - when(client.getURL()).thenReturn("http://foo.com"); + BootstrapProperties props = mock(BootstrapProperties.class); + when(props.property("sonar.host.url")).thenReturn("http://foo.com"); - DefaultServer metadata = new DefaultServer(settings, client); + DefaultServer metadata = new DefaultServer(settings, props); assertThat(metadata.getId()).isEqualTo("123"); assertThat(metadata.getVersion()).isEqualTo("2.2"); 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 058075f83ed..640d5c29401 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 @@ -31,6 +31,7 @@ import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; + import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -50,11 +51,13 @@ public class PersistentCache { // eviction strategy is to expire entries after modification once a time duration has elapsed private final long defaultDurationToExpireMs; private final Logger logger; + private final String version; - public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger) { + public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger, String version) { this.baseDir = baseDir; this.defaultDurationToExpireMs = defaultDurationToExpireMs; this.logger = logger; + this.version = version; reconfigure(); logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs); @@ -200,10 +203,14 @@ public class PersistentCache { lockChannel = null; } - private static String getKey(String uri) { + private String getKey(String uri) { try { + String key = uri; + if (version != null) { + key += version; + } MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO); - digest.update(uri.getBytes(StandardCharsets.UTF_8)); + digest.update(key.getBytes(StandardCharsets.UTF_8)); return byteArrayToHex(digest.digest()); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Couldn't create hash", e); 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 f0fa1596917..055d2615ed6 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 @@ -30,6 +30,7 @@ public class PersistentCacheBuilder { private Path cachePath; private final Logger logger; + private String version; public PersistentCacheBuilder(Logger logger) { this.logger = logger; @@ -40,7 +41,12 @@ public class PersistentCacheBuilder { setSonarHome(findHome()); } - return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger); + return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger, version); + } + + public PersistentCacheBuilder setVersion(String version) { + this.version = version; + return this; } public PersistentCacheBuilder setSonarHome(@Nullable Path p) { 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 62e105bf861..c327ff7c5eb 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 @@ -41,7 +41,7 @@ public class PersistentCacheTest { @Before public void setUp() { - cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class)); + cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null); } @Test @@ -79,7 +79,7 @@ public class PersistentCacheTest { @Test public void testReconfigure() throws Exception { - cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class)); + cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null); assertCacheHit(false); assertCacheHit(true); @@ -97,14 +97,29 @@ 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, mock(Logger.class)); + cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null); assertCacheHit(false); assertCacheHit(false); } + + @Test + public void testDifferentServerVersions() throws Exception { + assertCacheHit(false); + assertCacheHit(true); + + PersistentCache cache2 = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), "5.2"); + assertCacheHit(cache2, false); + assertCacheHit(cache2, true); + + } private void assertCacheHit(boolean hit) throws Exception { + assertCacheHit(cache, hit); + } + + private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception { CacheFillerString c = new CacheFillerString(); - assertThat(cache.getString(URI, c)).isEqualTo(VALUE); + assertThat(pCache.getString(URI, c)).isEqualTo(VALUE); assertThat(c.wasCalled).isEqualTo(!hit); } |