From: SimonBrandhof Date: Mon, 8 Apr 2013 08:46:01 +0000 (+0200) Subject: Delete the generated sonar-runner-batch.jar in temp dir X-Git-Tag: 2.5-rc1~159 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3df1496b29c383b09ff4a2882620f6b922a7196c;p=sonar-scanner-cli.git Delete the generated sonar-runner-batch.jar in temp dir --- diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java index b1ab426..db479ca 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java @@ -20,6 +20,7 @@ package org.sonar.runner.api; import org.apache.commons.io.FileUtils; +import org.sonar.runner.impl.Logs; import java.io.File; @@ -55,6 +56,7 @@ class Dirs { } FileUtils.deleteQuietly(workDir); runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath()); + Logs.info("Work directory: " + workDir.getAbsolutePath()); } /** diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java index 7792d53..d43ab29 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java @@ -40,6 +40,7 @@ public class OsTest { @Test public void testUsedJavaExe() throws Exception { + System.out.println(System.getProperty("java.io.tmpdir")); File javaExe = new Os().thisJavaExe(); assertThat(javaExe).isNotNull().isFile().exists(); assertThat(javaExe.getName()).contains("java"); diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java index a0f557b..227e577 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java @@ -46,7 +46,7 @@ import java.util.Properties; * * @since 1.5 */ -public final class ProjectReactorBuilder { +class ProjectReactorBuilder { private static final Logger LOG = LoggerFactory.getLogger(ProjectReactorBuilder.class); @@ -113,11 +113,11 @@ public final class ProjectReactorBuilder { private Properties properties; private File rootProjectWorkDir; - public ProjectReactorBuilder(Properties properties) { + ProjectReactorBuilder(Properties properties) { this.properties = properties; } - public ProjectReactor build() { + ProjectReactor build() { ProjectDefinition rootProject = defineProject(properties, null); rootProjectWorkDir = rootProject.getWorkDir(); defineChildren(rootProject); diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java index 99d8f37..8bf39f9 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java @@ -38,7 +38,7 @@ class Utils { * This works even if they are separated by whitespace characters (space char, EOL, ...) * */ - public static String[] getListFromProperty(Properties properties, String key) { + static String[] getListFromProperty(Properties properties, String key) { return StringUtils.stripAll(StringUtils.split(properties.getProperty(key, ""), ',')); } diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java index 57a1623..d8fb4a5 100644 --- a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java @@ -68,7 +68,6 @@ public class Main { Logs.info("Error stacktraces are turned on."); } runnerFactory.create(conf.properties()).execute(); - // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath()); } catch (Exception e) { displayExecutionResult(stats, "FAILURE"); 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 6a7a207..51b917a 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 @@ -29,16 +29,18 @@ import java.util.Properties; public class BatchLauncher { final String isolatedLauncherClass; + private final TempCleaning tempCleaning; /** * For unit tests */ - BatchLauncher(String isolatedLauncherClass) { + BatchLauncher(String isolatedLauncherClass, TempCleaning tempCleaning) { this.isolatedLauncherClass = isolatedLauncherClass; + this.tempCleaning = tempCleaning; } public BatchLauncher() { - this.isolatedLauncherClass = "org.sonar.runner.batch.IsolatedLauncher"; + this("org.sonar.runner.batch.IsolatedLauncher", new TempCleaning()); } public void execute(Properties props, List extensions) { @@ -49,7 +51,7 @@ public class BatchLauncher { } /** - * @return the {@link IsolatedLauncher} instance for unit tests + * @return the {@link org.sonar.runner.batch.IsolatedLauncher} instance for unit tests */ Object doExecute(final JarDownloader jarDownloader, final Properties props, final List extensions) { Object launcher = AccessController.doPrivileged(new PrivilegedAction() { @@ -58,7 +60,9 @@ public class BatchLauncher { String unmaskedPackages = props.getProperty(InternalProperties.RUNNER_UNMASKED_PACKAGES, ""); IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), unmaskedPackages.split(":")); classloader.addFiles(jarFiles); - return delegateExecution(classloader, props, extensions); + Object launcher = delegateExecution(classloader, props, extensions); + tempCleaning.clean(); + return launcher; } private Object delegateExecution(IsolatedClassloader classloader, Properties properties, List extensions) { 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 new file mode 100644 index 0000000..813f4bb --- /dev/null +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/TempCleaning.java @@ -0,0 +1,61 @@ +/* + * Sonar 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.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} + */ +class TempCleaning { + static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000; + + final File tempDir; + + TempCleaning() { + this(new File(System.getProperty("java.io.tmpdir"))); + } + + /** + * For unit tests + */ + TempCleaning(File tempDir) { + this.tempDir = tempDir; + } + + void clean() { + long cutoff = System.currentTimeMillis() - ONE_DAY_IN_MILLISECONDS; + Collection files = FileUtils.listFiles(tempDir, new AndFileFilter( + new PrefixFileFilter("sonar-runner-batch"), + new AgeFileFilter(cutoff) + ), null); + + for (File file : files) { + FileUtils.deleteQuietly(file); + } + } +} 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 2d8f2e7..b914a39 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 @@ -42,7 +42,8 @@ public class BatchLauncherTest { @Test public void should_download_jars_and_execute_batch() { - BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName()); + TempCleaning tempCleaning = mock(TempCleaning.class); + BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName(), tempCleaning); Properties props = new Properties(); props.put("foo", "bar"); @@ -54,11 +55,12 @@ public class BatchLauncherTest { assertThat(isolatedLauncher.props.get("foo")).isEqualTo("bar"); assertThat(isolatedLauncher.extensions).isSameAs(extensions); verify(jarDownloader).download(); + verify(tempCleaning).clean(); } @Test public void should_use_isolated_classloader() { - BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName()); + BatchLauncher launcher = new BatchLauncher(FakeIsolatedLauncher.class.getName(), mock(TempCleaning.class)); Properties props = new Properties(); // The current classloader in not available -> fail to load FakeIsolatedLauncher diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/TempCleaningTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/TempCleaningTest.java new file mode 100644 index 0000000..25d5b82 --- /dev/null +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/TempCleaningTest.java @@ -0,0 +1,64 @@ +/* + * Sonar 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.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; + +import static org.fest.assertions.Assertions.assertThat; + +public class TempCleaningTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void should_clean_jvm_tmp_dir( ){ + TempCleaning cleaning = new TempCleaning(); + assertThat(cleaning.tempDir).isDirectory().exists(); + } + + @Test + public void should_clean() throws Exception { + File dir = temp.newFolder(); + File oldBatch = new File(dir, "sonar-runner-batch656.jar"); + FileUtils.write(oldBatch, "foo"); + oldBatch.setLastModified(System.currentTimeMillis() - 3 * TempCleaning.ONE_DAY_IN_MILLISECONDS); + + File youngBatch = new File(dir, "sonar-runner-batch123.jar"); + FileUtils.write(youngBatch, "foo"); + + File doNotDelete = new File(dir, "jacoco.txt"); + FileUtils.write(doNotDelete, "foo"); + + assertThat(oldBatch).exists(); + assertThat(youngBatch).exists(); + assertThat(doNotDelete).exists(); + new TempCleaning(dir).clean(); + + assertThat(oldBatch).doesNotExist(); + assertThat(youngBatch).exists(); + assertThat(doNotDelete).exists(); + } +}