aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/main
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-10-01 17:26:34 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-10-01 17:50:36 +0200
commit8cec954ad673e32bf46d2a3b9fa62bdb6d86b7c8 (patch)
tree0ef79668f8d4883dfa1e2df57dfbcb79ba2a2a18 /sonar-runner-api/src/main
parent71b0c573da6b4b3d89060f508d9091d3a230a217 (diff)
downloadsonar-scanner-cli-8cec954ad673e32bf46d2a3b9fa62bdb6d86b7c8.tar.gz
sonar-scanner-cli-8cec954ad673e32bf46d2a3b9fa62bdb6d86b7c8.zip
Change cache strategy
Diffstat (limited to 'sonar-runner-api/src/main')
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java10
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/cache/FileCacheBuilder.java2
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java4
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java2
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java67
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/impl/SimulatedLauncher.java2
6 files changed, 55 insertions, 32 deletions
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
index 021e129..bfffc51 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
@@ -183,9 +183,9 @@ public class EmbeddedRunner {
start(false);
}
- public void start(boolean forceSync) {
+ public void start(boolean preferCache) {
initGlobalDefaultValues();
- doStart(forceSync);
+ doStart(preferCache);
}
/**
@@ -244,10 +244,10 @@ public class EmbeddedRunner {
}
}
- protected void doStart(boolean forceSync) {
+ protected void doStart(boolean preferCache) {
checkLauncherDoesntExist();
ClassloadRules rules = new ClassloadRules(classloaderMask, classloaderUnmask);
- launcher = launcherFactory.createLauncher(globalProperties(), rules);
+ launcher = launcherFactory.createLauncher(globalProperties(), rules, preferCache);
if (VersionUtils.isAtLeast52(launcher.getVersion())) {
launcher.start(globalProperties(), new org.sonar.runner.batch.LogOutput() {
@@ -256,7 +256,7 @@ public class EmbeddedRunner {
logOutput.log(formattedMessage, LogOutput.Level.valueOf(level.name()));
}
- }, forceSync);
+ }, preferCache);
}
}
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/cache/FileCacheBuilder.java b/sonar-runner-api/src/main/java/org/sonar/runner/cache/FileCacheBuilder.java
index 9be8e11..0a668b8 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/cache/FileCacheBuilder.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/cache/FileCacheBuilder.java
@@ -49,7 +49,7 @@ public class FileCacheBuilder {
return FileCache.create(cacheDir, logger);
}
- private File findHome() {
+ private static File findHome() {
String path = System.getenv("SONAR_USER_HOME");
if (path == null) {
// Default
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java
index be92d02..0a1a0c7 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java
@@ -69,7 +69,7 @@ public class IsolatedLauncherFactory {
return classloader;
}
- public IsolatedLauncher createLauncher(Properties props, ClassloadRules rules) {
+ public IsolatedLauncher createLauncher(Properties props, ClassloadRules rules, boolean preferCache) {
if (props.containsKey(InternalProperties.RUNNER_DUMP_TO_FILE)) {
String version = props.getProperty(InternalProperties.RUNNER_VERSION_SIMULATION);
if (version == null) {
@@ -77,7 +77,7 @@ public class IsolatedLauncherFactory {
}
return new SimulatedLauncher(version, logger);
}
- ServerConnection serverConnection = ServerConnection.create(props, getCache(props), logger);
+ ServerConnection serverConnection = ServerConnection.create(props, getCache(props), logger, preferCache);
JarDownloader jarDownloader = new JarDownloader(serverConnection, logger, props);
return createLauncher(jarDownloader, rules);
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java
index 972675b..d957129 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java
@@ -76,7 +76,7 @@ class Jars {
try {
List<File> files = new ArrayList<File>();
logger.debug("Get bootstrap index...");
- String libs = connection.downloadStringCache(BOOTSTRAP_INDEX_PATH);
+ String libs = connection.loadString(BOOTSTRAP_INDEX_PATH);
logger.debug("Get bootstrap completed");
String[] lines = libs.split("[\r\n]+");
BatchFileDownloader batchFileDownloader = new BatchFileDownloader(connection);
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java
index 30c3cf1..8a40f41 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java
@@ -47,28 +47,30 @@ class ServerConnection {
private final String userAgent;
private final PersistentCache wsCache;
- private final boolean isCacheEnable;
+ private final boolean preferCache;
private final Logger logger;
+ private final boolean isCacheEnabled;
- private ServerConnection(String serverUrl, String app, String appVersion, boolean isCacheEnable, PersistentCache cache, Logger logger) {
+ private ServerConnection(String serverUrl, String app, String appVersion, boolean preferCache, boolean cacheEnabled, PersistentCache cache, Logger logger) {
+ this.isCacheEnabled = cacheEnabled;
this.logger = logger;
this.serverUrl = removeEndSlash(serverUrl);
this.userAgent = app + "/" + appVersion;
this.wsCache = cache;
- this.isCacheEnable = isCacheEnable;
+ this.preferCache = preferCache;
}
private static String removeEndSlash(String url) {
return url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
}
- static ServerConnection create(Properties properties, PersistentCache cache, Logger logger) {
+ static ServerConnection create(Properties properties, PersistentCache cache, Logger logger, boolean preferCache) {
String serverUrl = properties.getProperty("sonar.host.url");
String app = properties.getProperty(InternalProperties.RUNNER_APP);
String appVersion = properties.getProperty(InternalProperties.RUNNER_APP_VERSION);
boolean enableCache = isCacheEnabled(properties);
- return new ServerConnection(serverUrl, app, appVersion, enableCache, cache, logger);
+ return new ServerConnection(serverUrl, app, appVersion, preferCache, enableCache, cache, logger);
}
private static boolean isCacheEnabled(Properties properties) {
@@ -120,7 +122,7 @@ class ServerConnection {
httpRequest.receive(toFile);
} catch (Exception e) {
- if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
+ if (isCausedByConnection(e)) {
logger.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
}
FileUtils.deleteQuietly(toFile);
@@ -129,16 +131,45 @@ class ServerConnection {
}
/**
- * Tries to fetch from server and falls back to cache. If both attempts fail, it throws the exception
- * linked to the server connection failure.
+ * Tries to fetch from cache and server. If both attempts fail, it throws the exception linked to the server connection failure.
*/
- String downloadStringCache(String path) throws IOException {
+ String loadString(String path) throws IOException {
String fullUrl = serverUrl + path;
+
+ if (isCacheEnabled && preferCache) {
+ return tryCacheFirst(fullUrl);
+ } else {
+ return tryServerFirst(fullUrl, isCacheEnabled);
+ }
+
+ }
+
+ private String tryCacheFirst(String fullUrl) throws IOException {
+ String cached = getFromCache(fullUrl);
+ if (cached != null) {
+ return cached;
+ }
+
try {
- return downloadString(fullUrl, isCacheEnable);
+ return downloadString(fullUrl, preferCache);
+ } catch (Exception e) {
+ logger.error(MessageFormat.format("Data is not cached and " + SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
+ throw e;
+ }
+ }
+
+ private String tryServerFirst(String fullUrl, boolean cacheEnabled) throws IOException {
+ try {
+ return downloadString(fullUrl, cacheEnabled);
} catch (HttpRequest.HttpRequestException e) {
- if (isCausedByConnection(e) && isCacheEnable) {
- return fallbackToCache(fullUrl, e);
+ if (cacheEnabled && isCausedByConnection(e)) {
+ logger.info(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + ", trying cache", serverUrl));
+ String cached = getFromCache(fullUrl);
+ if (cached != null) {
+ return cached;
+ }
+ logger.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + " and data is not cached", serverUrl));
+ throw e;
}
logger.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
@@ -151,20 +182,12 @@ class ServerConnection {
e.getCause() instanceof java.net.SocketTimeoutException;
}
- private String fallbackToCache(String fullUrl, HttpRequest.HttpRequestException originalException) {
- logger.info(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + ", trying cache", serverUrl));
-
+ private String getFromCache(String fullUrl) {
try {
- String cached = wsCache.getString(fullUrl);
- if (cached != null) {
- return cached;
- }
- logger.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + " and data is not cached", serverUrl));
- throw originalException;
+ return wsCache.getString(fullUrl);
} catch (IOException e) {
throw new IllegalStateException("Failed to access cache", e);
}
-
}
private HttpRequest newHttpRequest(URL url) {
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/SimulatedLauncher.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/SimulatedLauncher.java
index aac6b25..8a83a3e 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/SimulatedLauncher.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/SimulatedLauncher.java
@@ -43,7 +43,7 @@ public class SimulatedLauncher implements IsolatedLauncher {
}
@Override
- public void start(Properties properties, LogOutput logOutput, boolean forceSync) {
+ public void start(Properties properties, LogOutput logOutput, boolean preferCache) {
globalProperties = properties;
}