*/
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;
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;
+ }
+ }
}
return this;
}
- /**
- * @since 5.2
- */
- public Batch executeTask(Map<String, String> analysisProperties) {
- checkStarted();
- bootstrapContainer.executeAnalysis(analysisProperties, components);
- return this;
- }
-
/**
* @since 5.2
*/
*/
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;
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
@Override
public String getURL() {
- return client.getURL();
+ return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
}
@Override
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
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);
}
*/
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;
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");
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;
// 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);
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);
private Path cachePath;
private final Logger logger;
+ private String version;
public PersistentCacheBuilder(Logger logger) {
this.logger = logger;
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) {
@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
@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);
@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);
}