From 36d75006568fa302a0262907a193bd3f3b19e8f0 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 28 May 2015 09:57:07 +0200 Subject: [PATCH] SONARUNNER-133 Remove version check and add some profiling logs in debug mode --- .../sonar/runner/batch/IsolatedLauncher.java | 15 ++- .../runner/batch/IsolatedLauncherTest.java | 7 +- sonar-runner-dist/pom.xml | 4 +- .../org/sonar/runner/impl/BatchLauncher.java | 21 ++-- .../org/sonar/runner/impl/JarDownloader.java | 15 +-- .../main/java/org/sonar/runner/impl/Jars.java | 10 +- .../org/sonar/runner/impl/ServerVersion.java | 73 ----------- .../org/sonar/runner/impl/TempCleaning.java | 13 +- .../sonar/runner/impl/BatchLauncherTest.java | 17 ++- .../sonar/runner/impl/JarDownloaderTest.java | 25 +--- .../sonar/runner/impl/ServerVersionTest.java | 115 ------------------ 11 files changed, 48 insertions(+), 267 deletions(-) delete mode 100644 sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java delete mode 100644 sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerVersionTest.java diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java index a451dbd..b845654 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java @@ -23,17 +23,16 @@ import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; import com.google.common.annotations.VisibleForTesting; +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.Properties; import org.apache.commons.io.IOUtils; import org.slf4j.LoggerFactory; import org.sonar.api.utils.SonarException; import org.sonar.batch.bootstrapper.Batch; import org.sonar.batch.bootstrapper.EnvironmentInformation; -import java.io.InputStream; -import java.util.List; -import java.util.Map; -import java.util.Properties; - /** * This class is executed within the classloader provided by the server. It contains the installed plugins and * the same version of sonar-batch as the server. @@ -44,11 +43,11 @@ public class IsolatedLauncher { private static final String DEBUG = "DEBUG"; private static final String FALSE = "false"; - public void execute(String sonarVersion, Properties properties, List extensions) { - createBatch(sonarVersion, properties, extensions).execute(); + public void execute(Properties properties, List extensions) { + createBatch(properties, extensions).execute(); } - Batch createBatch(String sonarVersion, Properties properties, List extensions) { + Batch createBatch(Properties properties, List extensions) { initLogging(properties); EnvironmentInformation env = new EnvironmentInformation(properties.getProperty("sonarRunner.app"), properties.getProperty("sonarRunner.appVersion")); return Batch.builder() diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java index fff4b14..cc43305 100644 --- a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java @@ -19,11 +19,10 @@ */ package org.sonar.runner.batch; -import org.junit.Test; -import org.sonar.batch.bootstrapper.Batch; - import java.util.Collections; import java.util.Properties; +import org.junit.Test; +import org.sonar.batch.bootstrapper.Batch; import static org.fest.assertions.Assertions.assertThat; @@ -39,7 +38,7 @@ public class IsolatedLauncherTest { props.setProperty("sonar.projectName", "Sample"); props.setProperty("sonar.projectVersion", "1.0"); props.setProperty("sonar.sources", "src"); - Batch batch = launcher.createBatch("3.5", props, Collections.emptyList()); + Batch batch = launcher.createBatch(props, Collections.emptyList()); assertThat(batch).isNotNull(); } diff --git a/sonar-runner-dist/pom.xml b/sonar-runner-dist/pom.xml index 927f863..0066ac9 100644 --- a/sonar-runner-dist/pom.xml +++ b/sonar-runner-dist/pom.xml @@ -112,8 +112,8 @@ - 280000 - 310000 + 279000 + 300000 ${project.build.directory}/sonar-runner-${project.version}.zip 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 49383ce..f19e43f 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 @@ -45,9 +45,8 @@ public class BatchLauncher { public void execute(Properties props, List extensions) { ServerConnection serverConnection = ServerConnection.create(props); - ServerVersion serverVersion = new ServerVersion(serverConnection); - JarDownloader jarDownloader = new JarDownloader(serverConnection, serverVersion); - doExecute(jarDownloader, serverVersion, props, extensions); + JarDownloader jarDownloader = new JarDownloader(serverConnection); + doExecute(jarDownloader, props, extensions); } private static String[][] getMaskRules(final Properties props) { @@ -65,33 +64,35 @@ public class BatchLauncher { /** * @return the {@link org.sonar.runner.batch.IsolatedLauncher} instance for unit tests */ - Object doExecute(final JarDownloader jarDownloader, final ServerVersion serverVersion, final Properties props, final List extensions) { + Object doExecute(final JarDownloader jarDownloader, final Properties props, final List extensions) { return AccessController.doPrivileged(new PrivilegedAction() { public Object run() { - List jarFiles = jarDownloader.checkVersionAndDownload(); + List jarFiles = jarDownloader.download(); + Logs.debug("Create isolated classloader..."); String[][] maskRules = getMaskRules(props); IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), maskRules); classloader.addFiles(jarFiles); - Object launcher = delegateExecution(classloader, serverVersion.version(), props, extensions); + Object launcher = delegateExecution(classloader, props, extensions); tempCleaning.clean(); return launcher; } - private Object delegateExecution(IsolatedClassloader classloader, String sonarVersion, Properties properties, List extensions) { + private Object delegateExecution(IsolatedClassloader classloader, Properties properties, List extensions) { ClassLoader initialContextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(classloader); Class launcherClass = classloader.loadClass(isolatedLauncherClass); - Method executeMethod = launcherClass.getMethod("execute", String.class, Properties.class, List.class); + Method executeMethod = launcherClass.getMethod("execute", Properties.class, List.class); Object launcher = launcherClass.newInstance(); - executeMethod.invoke(launcher, sonarVersion, properties, extensions); + Logs.debug("Start IsolatedLauncher"); + executeMethod.invoke(launcher, properties, extensions); return launcher; } catch (InvocationTargetException e) { // Unwrap original exception throw new RunnerException("Unable to execute Sonar", e.getTargetException()); } catch (Exception e) { // Catch all other exceptions, which relates to reflection - throw new RunnerException("Unable to execute Sonar", e); + throw new RunnerException("Unable to execute SonarQube", e); } finally { Thread.currentThread().setContextClassLoader(initialContextClassLoader); } diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/JarDownloader.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/JarDownloader.java index ddd87ae..b64aa39 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/JarDownloader.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/JarDownloader.java @@ -24,22 +24,9 @@ import java.util.List; class JarDownloader { private final ServerConnection serverConnection; - private final ServerVersion serverVersion; - JarDownloader(ServerConnection conn, ServerVersion version) { + JarDownloader(ServerConnection conn) { this.serverConnection = conn; - this.serverVersion = version; - } - - List checkVersionAndDownload() { - List jarFiles; - if (serverVersion.is37Compatible()) { - jarFiles = download(); - } else { - throw new IllegalStateException("SonarQube " + serverVersion.version() - + " is not supported. Please upgrade SonarQube to version 3.7 or more."); - } - return jarFiles; } List download() { 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 7fb8c84..3593e94 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,14 +19,13 @@ */ 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"; @@ -53,6 +52,7 @@ class Jars { List download() { List files = new ArrayList(); + Logs.debug("Extract sonar-runner-batch in temp..."); files.add(jarExtractor.extractToTemp("sonar-runner-batch")); files.addAll(dowloadFiles()); return files; @@ -61,7 +61,9 @@ class Jars { private List dowloadFiles() { try { List files = new ArrayList(); + Logs.debug("Get bootstrap index..."); String libs = connection.downloadString(BOOTSTRAP_INDEX_PATH); + Logs.debug("Get bootstrap completed"); String[] lines = libs.split("[\r\n]+"); BatchFileDownloader batchFileDownloader = new BatchFileDownloader(connection); for (String line : lines) { diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java deleted file mode 100644 index 6b599f0..0000000 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * SonarQube Runner - Implementation - * Copyright (C) 2011 SonarSource - * dev@sonar.codehaus.org - * - * This program 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. - * - * This program 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 02 - */ -package org.sonar.runner.impl; - -import java.io.IOException; - -class ServerVersion { - - private static final String[] LESS_THAN_3_7 = {"0", "1", "2", "3.0", "3.1", "3.2", "3.3", "3.4", "3.5", "3.6"}; - - private final ServerConnection serverConnection; - private String version; - - ServerVersion(ServerConnection serverConnection) { - this.serverConnection = serverConnection; - } - - String version() { - // Guava Suppliers#memoize() would be great here :D - if (version == null) { - version = downloadVersion(); - Logs.info("SonarQube Server " + version); - } - return version; - } - - private String downloadVersion() { - String result; - try { - result = serverConnection.downloadString("/api/server/version"); - } catch (IOException e) { - throw new IllegalStateException("Fail to request server version", e); - } - if (result == null || "".equals(result.trim())) { - throw new IllegalStateException("Server version is not set"); - } - return result; - } - - boolean is37Compatible() { - return !inVersions(version(), LESS_THAN_3_7); - } - - private static boolean inVersions(String version, String[] versions) { - for (String s : versions) { - if (isVersion(version, s)) { - return true; - } - } - return false; - } - - private static boolean isVersion(String version, String prefix) { - return version.startsWith(prefix + ".") || version.equals(prefix); - } -} diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java index 4f3002e..698a174 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java @@ -19,14 +19,13 @@ */ package org.sonar.runner.impl; +import java.io.File; +import java.util.Collection; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.AgeFileFilter; import org.apache.commons.io.filefilter.AndFileFilter; import org.apache.commons.io.filefilter.PrefixFileFilter; -import java.io.File; -import java.util.Collection; - /** * The file sonar-runner-batch.jar is locked by the classloader on Windows and can't be dropped at the end of the execution. * See {@link BatchLauncher} @@ -48,14 +47,16 @@ class TempCleaning { } void clean() { + Logs.debug("Start temp cleaning..."); long cutoff = System.currentTimeMillis() - ONE_DAY_IN_MILLISECONDS; Collection files = FileUtils.listFiles(tempDir, new AndFileFilter( - new PrefixFileFilter("sonar-runner-batch"), - new AgeFileFilter(cutoff) - ), null); + new PrefixFileFilter("sonar-runner-batch"), + new AgeFileFilter(cutoff) + ), null); for (File file : files) { FileUtils.deleteQuietly(file); } + Logs.debug("Temp cleaning done"); } } diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java index 0924e26..763d7c5 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java @@ -19,13 +19,12 @@ */ package org.sonar.runner.impl; -import org.junit.Test; -import org.sonar.runner.batch.IsolatedLauncher; - import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Properties; +import org.junit.Test; +import org.sonar.runner.batch.IsolatedLauncher; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -51,10 +50,10 @@ public class BatchLauncherTest { props.put(InternalProperties.RUNNER_MASK_RULES, "UNMASK|org.sonar.runner.impl."); List extensions = new ArrayList(); - FakeIsolatedLauncher isolatedLauncher = (FakeIsolatedLauncher) launcher.doExecute(jarDownloader, mock(ServerVersion.class), props, extensions); + FakeIsolatedLauncher isolatedLauncher = (FakeIsolatedLauncher) launcher.doExecute(jarDownloader, props, extensions); assertThat(isolatedLauncher.props.get("foo")).isEqualTo("bar"); assertThat(isolatedLauncher.extensions).isSameAs(extensions); - verify(jarDownloader).checkVersionAndDownload(); + verify(jarDownloader).download(); verify(tempCleaning).clean(); } @@ -66,7 +65,7 @@ public class BatchLauncherTest { // The current classloader in not available -> fail to load FakeIsolatedLauncher props.put(InternalProperties.RUNNER_MASK_RULES, ""); try { - launcher.doExecute(jarDownloader, mock(ServerVersion.class), props, Collections.emptyList()); + launcher.doExecute(jarDownloader, props, Collections.emptyList()); fail(); } catch (RunnerException e) { // success @@ -86,7 +85,7 @@ public class BatchLauncherTest { Properties props = new Properties(); List extensions = Collections.emptyList(); BatchLauncher launcher = spy(new BatchLauncher()); - doReturn(new Object()).when(launcher).doExecute(any(JarDownloader.class), any(ServerVersion.class), eq(props), eq(extensions)); + doReturn(new Object()).when(launcher).doExecute(any(JarDownloader.class), eq(props), eq(extensions)); launcher.execute(props, extensions); @@ -96,10 +95,8 @@ public class BatchLauncherTest { public static class FakeIsolatedLauncher { public Properties props = null; public List extensions = null; - private String sonarVersion; - public void execute(String sonarVersion, Properties props, List extensions) { - this.sonarVersion = sonarVersion; + public void execute(Properties props, List extensions) { this.props = props; this.extensions = extensions; } 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 a3e84c6..487c579 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 @@ -19,44 +19,27 @@ */ package org.sonar.runner.impl; -import org.junit.Test; - import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; +import org.junit.Test; import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; public class JarDownloaderTest { ServerConnection serverConnection = mock(ServerConnection.class); - ServerVersion serverVersion = mock(ServerVersion.class); Properties props = new Properties(); - JarDownloader downloader = spy(new JarDownloader(serverConnection, serverVersion)); + JarDownloader downloader = spy(new JarDownloader(serverConnection)); @Test - public void should_download_3_7_jar_files() { - when(serverVersion.is37Compatible()).thenReturn(true); + public void should_download_jar_files() { doReturn(new ArrayList()).when(downloader).download(); - List jarFiles = downloader.checkVersionAndDownload(); + List jarFiles = downloader.download(); assertThat(jarFiles).isNotNull(); } - - @Test - public void should_fail_if_2_x() { - when(serverVersion.version()).thenReturn("2.10"); - when(serverVersion.is37Compatible()).thenReturn(false); - try { - downloader.checkVersionAndDownload(); - fail(); - } catch (IllegalStateException e) { - assertThat(e).hasMessage("SonarQube 2.10 is not supported. Please upgrade SonarQube to version 3.7 or more."); - } - } } diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerVersionTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerVersionTest.java deleted file mode 100644 index 567a25d..0000000 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerVersionTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * SonarQube Runner - Implementation - * Copyright (C) 2011 SonarSource - * dev@sonar.codehaus.org - * - * This program 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. - * - * This program 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 02 - */ -package org.sonar.runner.impl; - -import org.junit.Test; - -import java.net.ConnectException; - -import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class ServerVersionTest { - - ServerConnection connection = mock(ServerConnection.class); - ServerVersion version = new ServerVersion(connection); - - @Test - public void should_fail_on_connection_error() throws Exception { - when(connection.downloadString("/api/server/version")).thenThrow(new ConnectException()); - try { - version.version(); - fail(); - } catch (IllegalStateException e) { - // success - } - } - - @Test - public void should_fail_if_bad_version() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn(""); - try { - version.version(); - fail(); - } catch (IllegalStateException e) { - assertThat(e).hasMessage("Server version is not set"); - } - } - - @Test - public void test_2_x() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("2.10"); - assertThat(version.version()).isEqualTo("2.10"); - assertThat(version.is37Compatible()).isFalse(); - verify(connection, times(1)).downloadString("/api/server/version"); - } - - @Test - public void test_3_0() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("3.0"); - assertThat(version.version()).isEqualTo("3.0"); - assertThat(version.is37Compatible()).isFalse(); - verify(connection, times(1)).downloadString("/api/server/version"); - } - - @Test - public void test_3_1() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("3.1"); - assertThat(version.version()).isEqualTo("3.1"); - assertThat(version.is37Compatible()).isFalse(); - verify(connection, times(1)).downloadString("/api/server/version"); - } - - @Test - public void test_3_6() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("3.6"); - assertThat(version.version()).isEqualTo("3.6"); - assertThat(version.is37Compatible()).isFalse(); - verify(connection, times(1)).downloadString("/api/server/version"); - } - - @Test - public void test_3_7() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("3.7"); - assertThat(version.version()).isEqualTo("3.7"); - assertThat(version.is37Compatible()).isTrue(); - verify(connection, times(1)).downloadString("/api/server/version"); - } - - @Test - public void test_3_7_1() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("3.7.1"); - assertThat(version.version()).isEqualTo("3.7.1"); - assertThat(version.is37Compatible()).isTrue(); - verify(connection, times(1)).downloadString("/api/server/version"); - } - - @Test - public void test_4_0() throws Exception { - when(connection.downloadString("/api/server/version")).thenReturn("4.0"); - assertThat(version.version()).isEqualTo("4.0"); - assertThat(version.is37Compatible()).isTrue(); - verify(connection, times(1)).downloadString("/api/server/version"); - } -} -- 2.39.5