diff options
Diffstat (limited to 'sonar-runner-impl')
8 files changed, 134 insertions, 40 deletions
diff --git a/sonar-runner-impl/pom.xml b/sonar-runner-impl/pom.xml index a8f725f..6d48d0c 100644 --- a/sonar-runner-impl/pom.xml +++ b/sonar-runner-impl/pom.xml @@ -24,6 +24,10 @@ <artifactId>sonar-home</artifactId> </dependency> <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + <dependency> <groupId>${project.groupId}</groupId> <artifactId>sonar-runner-batch</artifactId> <version>${project.version}</version> diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java index f19e43f..eebb592 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java @@ -19,6 +19,10 @@ */ package org.sonar.runner.impl; +import org.sonar.home.cache.PersistentCacheBuilder; + +import org.sonar.home.cache.PersistentCache; + import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -44,11 +48,21 @@ public class BatchLauncher { } public void execute(Properties props, List<Object> extensions) { - ServerConnection serverConnection = ServerConnection.create(props); + ServerConnection serverConnection = ServerConnection.create(props, getCache(props)); JarDownloader jarDownloader = new JarDownloader(serverConnection); doExecute(jarDownloader, props, extensions); } + private static PersistentCache getCache(Properties props) { + PersistentCacheBuilder builder = new PersistentCacheBuilder(); + + if ("true".equals(props.getProperty("sonar.forceUpdate"))) { + builder.forceUpdate(true); + } + + return builder.build(); + } + private static String[][] getMaskRules(final Properties props) { String maskRulesProp = props.getProperty(InternalProperties.RUNNER_MASK_RULES, null); String[] maskRulesConcat = maskRulesProp != null ? maskRulesProp.split(",") : new String[0]; diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java index 3ec1c7b..ab70776 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java @@ -19,8 +19,6 @@ */ package org.sonar.runner.impl; -import org.apache.commons.io.IOUtils; - import java.io.FileInputStream; import java.io.IOException; import java.util.Collections; @@ -43,15 +41,12 @@ public class BatchLauncherMain { private Properties loadProperties(String arg) throws IOException { Properties props = new Properties(); - FileInputStream input = new FileInputStream(arg); - try { + try (FileInputStream input = new FileInputStream(arg)) { props.load(input); // just to be clean, do not forward properties that do not make sense in fork mode props.remove(InternalProperties.RUNNER_MASK_RULES); - - } finally { - IOUtils.closeQuietly(input); } + return props; } diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Jars.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Jars.java index 3593e94..5e4342e 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Jars.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Jars.java @@ -19,13 +19,14 @@ */ package org.sonar.runner.impl; +import org.sonar.home.cache.FileCache; +import org.sonar.home.cache.FileCacheBuilder; +import org.sonar.home.log.StandardLog; + import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.sonar.home.cache.FileCache; -import org.sonar.home.cache.FileCacheBuilder; -import org.sonar.home.log.StandardLog; class Jars { private static final String BOOTSTRAP_INDEX_PATH = "/batch_bootstrap/index"; @@ -62,7 +63,7 @@ class Jars { try { List<File> files = new ArrayList<File>(); Logs.debug("Get bootstrap index..."); - String libs = connection.downloadString(BOOTSTRAP_INDEX_PATH); + String libs = connection.downloadStringCache(BOOTSTRAP_INDEX_PATH); Logs.debug("Get bootstrap completed"); String[] lines = libs.split("[\r\n]+"); BatchFileDownloader batchFileDownloader = new BatchFileDownloader(connection); diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java index b68e480..c5b67e3 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java @@ -19,6 +19,7 @@ */ package org.sonar.runner.impl; +import org.sonar.home.cache.PersistentCache; import com.github.kevinsawicki.http.HttpRequest; import org.apache.commons.io.FileUtils; @@ -29,6 +30,7 @@ import java.net.URL; import java.net.UnknownHostException; import java.text.MessageFormat; import java.util.Properties; +import java.util.concurrent.Callable; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -43,9 +45,14 @@ class ServerConnection { private final String serverUrl; private final String userAgent; - private ServerConnection(String serverUrl, String app, String appVersion) { + private final PersistentCache wsCache; + private final boolean isModePreview; + + private ServerConnection(String serverUrl, String app, String appVersion, boolean preview, PersistentCache cache) { this.serverUrl = removeEndSlash(serverUrl); this.userAgent = app + "/" + appVersion; + this.wsCache = cache; + this.isModePreview = preview; } private String removeEndSlash(String url) { @@ -55,11 +62,42 @@ class ServerConnection { return url.endsWith("/") ? url.substring(0, url.length() - 1) : url; } - static ServerConnection create(Properties properties) { + static ServerConnection create(Properties properties, PersistentCache cache) { String serverUrl = properties.getProperty("sonar.host.url"); String app = properties.getProperty(InternalProperties.RUNNER_APP); String appVersion = properties.getProperty(InternalProperties.RUNNER_APP_VERSION); - return new ServerConnection(serverUrl, app, appVersion); + String analysisMode = properties.getProperty("sonar.analysis.mode"); + boolean preview = "preview".equalsIgnoreCase(analysisMode); + + return new ServerConnection(serverUrl, app, appVersion, preview, cache); + } + + private class StringDownloader implements Callable<String> { + private String url; + + StringDownloader(String url) { + this.url = url; + } + + @Override + public String call() throws Exception { + HttpRequest httpRequest = null; + try { + httpRequest = newHttpRequest(new URL(url)); + String charset = getCharsetFromContentType(httpRequest.contentType()); + if (charset == null || "".equals(charset)) { + charset = "UTF-8"; + } + if (!httpRequest.ok()) { + throw new IOException(MessageFormat.format(STATUS_RETURNED_BY_URL_IS_INVALID, url, httpRequest.code())); + } + return httpRequest.body(charset); + } finally { + if (httpRequest != null) { + httpRequest.disconnect(); + } + } + } } void download(String path, File toFile) { @@ -78,31 +116,22 @@ class ServerConnection { } FileUtils.deleteQuietly(toFile); throw new IllegalStateException("Fail to download: " + fullUrl, e); - } } - String downloadString(String path) throws IOException { + String downloadStringCache(String path) throws Exception { String fullUrl = serverUrl + path; - HttpRequest httpRequest = newHttpRequest(new URL(fullUrl)); try { - String charset = getCharsetFromContentType(httpRequest.contentType()); - if (charset == null || "".equals(charset)) { - charset = "UTF-8"; + if (isModePreview) { + return wsCache.getString(serverUrl, new StringDownloader(fullUrl)); + } else { + return new StringDownloader(fullUrl).call(); } - if (!httpRequest.ok()) { - throw new IOException(MessageFormat.format(STATUS_RETURNED_BY_URL_IS_INVALID, fullUrl, httpRequest.code())); - } - return httpRequest.body(charset); - } catch (HttpRequest.HttpRequestException e) { if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) { Logs.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl)); } throw e; - - } finally { - httpRequest.disconnect(); } } diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java index 487c579..aff8805 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java @@ -38,7 +38,7 @@ public class JarDownloaderTest { @Test public void should_download_jar_files() { - doReturn(new ArrayList()).when(downloader).download(); + doReturn(new ArrayList<File>()).when(downloader).download(); List<File> jarFiles = downloader.download(); assertThat(jarFiles).isNotNull(); } diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarsTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarsTest.java index cf685f8..b6fead1 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarsTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/JarsTest.java @@ -51,7 +51,7 @@ public class JarsTest { File batchJar = temp.newFile("sonar-runner-batch.jar"); when(jarExtractor.extractToTemp("sonar-runner-batch")).thenReturn(batchJar); // index of the files to download - when(connection.downloadString("/batch_bootstrap/index")).thenReturn( + when(connection.downloadStringCache("/batch_bootstrap/index")).thenReturn( "cpd.jar|CA124VADFSDS\n" + "squid.jar|34535FSFSDF\n" ); @@ -60,7 +60,7 @@ public class JarsTest { List<File> files = jars35.download(); assertThat(files).isNotNull(); - verify(connection, times(1)).downloadString("/batch_bootstrap/index"); + verify(connection, times(1)).downloadStringCache("/batch_bootstrap/index"); verifyNoMoreInteractions(connection); verify(fileCache, times(1)).get(eq("cpd.jar"), eq("CA124VADFSDS"), any(FileCache.Downloader.class)); verify(fileCache, times(1)).get(eq("squid.jar"), eq("34535FSFSDF"), any(FileCache.Downloader.class)); @@ -72,7 +72,7 @@ public class JarsTest { File batchJar = temp.newFile("sonar-runner-batch.jar"); when(jarExtractor.extractToTemp("sonar-runner-batch")).thenReturn(batchJar); // index of the files to download - when(connection.downloadString("/batch_bootstrap/index")).thenThrow(new IllegalStateException()); + when(connection.downloadStringCache("/batch_bootstrap/index")).thenThrow(new IllegalStateException()); Jars jars35 = new Jars(fileCache, connection, jarExtractor); try { diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java index bb671bf..c8bf525 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java @@ -19,12 +19,17 @@ */ package org.sonar.runner.impl; +import org.junit.Before; +import org.sonar.home.cache.PersistentCacheBuilder; +import org.sonar.home.cache.PersistentCache; import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; import java.util.Properties; import static org.fest.assertions.Assertions.assertThat; @@ -38,14 +43,21 @@ public class ServerConnectionTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + private PersistentCache cache = null; + + @Before + public void setUp() { + cache = new PersistentCacheBuilder().setSonarHome(temp.getRoot().toPath()).build(); + } + @Test public void should_download_to_string() throws Exception { httpServer.setMockResponseData("abcde"); Properties props = new Properties(); props.setProperty("sonar.host.url", httpServer.url()); - ServerConnection connection = ServerConnection.create(props); - String response = connection.downloadString("/batch/index.txt"); + ServerConnection connection = ServerConnection.create(props, cache); + String response = connection.downloadStringCache("/batch/index.txt"); assertThat(response).isEqualTo("abcde"); } @@ -56,12 +68,51 @@ public class ServerConnectionTest { Properties props = new Properties(); props.setProperty("sonar.host.url", httpServer.url()); - ServerConnection connection = ServerConnection.create(props); + ServerConnection connection = ServerConnection.create(props, cache); File toFile = temp.newFile(); connection.download("/batch/index.txt", toFile); assertThat(FileUtils.readFileToString(toFile)).isEqualTo("abcde"); } + + @Test + public void should_cache_jar_list() throws Exception { + File cacheDir = new File(temp.getRoot(), "ws_cache"); + httpServer.setMockResponseData("abcde"); + Properties props = new Properties(); + props.setProperty("sonar.host.url", httpServer.url() + "/"); + props.setProperty("sonar.analysis.mode", "preview"); + + assertThat(cacheDir.list().length).isEqualTo(0); + ServerConnection connection = ServerConnection.create(props, cache); + String str = connection.downloadStringCache("/batch/index.txt"); + + assertThat(str).isEqualTo("abcde"); + assertThat(cacheDir.list().length).isEqualTo(2); + + httpServer.setMockResponseData("never requested"); + str = connection.downloadStringCache("/batch/index.txt"); + assertThat(str).isEqualTo("abcde"); + } + + @Test + public void should_not_cache_not_preview() throws Exception { + File cacheDir = new File(temp.getRoot(), "ws_cache"); + httpServer.setMockResponseData("abcde"); + Properties props = new Properties(); + props.setProperty("sonar.host.url", httpServer.url() + "/"); + + assertThat(cacheDir.list().length).isEqualTo(0); + ServerConnection connection = ServerConnection.create(props, cache); + String str = connection.downloadStringCache("/batch/index.txt"); + + assertThat(str).isEqualTo("abcde"); + assertThat(cacheDir.list().length).isEqualTo(0); + + httpServer.setMockResponseData("request2"); + str = connection.downloadStringCache("/batch/index.txt"); + assertThat(str).isEqualTo("request2"); + } // SONARPLUGINS-3061 @Test @@ -70,7 +121,7 @@ public class ServerConnectionTest { Properties props = new Properties(); props.setProperty("sonar.host.url", httpServer.url() + "/"); - ServerConnection connection = ServerConnection.create(props); + ServerConnection connection = ServerConnection.create(props, cache); File toFile = temp.newFile(); connection.download("/batch/index.txt", toFile); @@ -82,7 +133,7 @@ public class ServerConnectionTest { Properties props = new Properties(); props.setProperty("sonar.host.url", "http://localhost:" + NetworkUtil.getNextAvailablePort()); - ServerConnection connection = ServerConnection.create(props); + ServerConnection connection = ServerConnection.create(props, cache); File toFile = temp.newFile(); try { connection.download("/batch/index.txt", toFile); @@ -97,9 +148,9 @@ public class ServerConnectionTest { Properties props = new Properties(); props.setProperty("sonar.host.url", "http://localhost:" + NetworkUtil.getNextAvailablePort()); - ServerConnection connection = ServerConnection.create(props); + ServerConnection connection = ServerConnection.create(props, cache); try { - connection.downloadString("/batch/index.txt"); + connection.downloadStringCache("/batch/index.txt"); fail(); } catch (Exception e) { // success |