diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-31 15:54:15 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-31 16:00:29 +0100 |
commit | b159d9680d1703e3785f4cf8dcf2d30ae52c9fb7 (patch) | |
tree | f975773f5d5a309804e9af4ebd46b4ee2add3e2f | |
parent | 5780fbec1b757a9afbaa3c8ba88e591d6b9534d6 (diff) | |
download | sonarqube-b159d9680d1703e3785f4cf8dcf2d30ae52c9fb7.tar.gz sonarqube-b159d9680d1703e3785f4cf8dcf2d30ae52c9fb7.zip |
SONAR-2291 Minor changes on cache
* default cache folder is ~/.sonar/cache
* fail execution when there is a checksum mismatch
* improve logging
7 files changed, 41 insertions, 20 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java index be4e3837230..1d9929cdc1f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java @@ -74,12 +74,14 @@ public class BatchPluginRepository implements PluginRepository { List<File> pluginFiles = pluginDownloader.downloadPlugin(remote); List<File> extensionFiles = pluginFiles.subList(1, pluginFiles.size()); File targetDir = workingDirectories.getDir("plugins/" + remote.getKey()); - LOG.debug("Installing plugin " + remote.getKey() + " into " + targetDir); + LOG.debug("Installing plugin {} into {}", remote.getKey(), targetDir.getAbsolutePath()); PluginMetadata metadata = extractor.install(pluginFiles.get(0), remote.isCore(), extensionFiles, targetDir); if (StringUtils.isBlank(metadata.getBasePlugin()) || filter.accepts(metadata.getBasePlugin())) { - LOG.debug("Excluded plugin: " + metadata.getKey()); metadataByKey.put(metadata.getKey(), metadata); } + else { + LOG.debug("Excluded plugin: " + metadata.getKey()); + } } } classLoaders = new PluginClassloaders(Thread.currentThread().getContextClassLoader()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java index d0852e487a2..dadabefd1d8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java @@ -53,13 +53,8 @@ public class PluginDownloader implements BatchComponent { try { List<File> files = Lists.newArrayList(); for (RemotePluginFile file : remote.getFiles()) { - LOG.debug("Looking if plugin file {} with md5 {} is already in cache", file.getFilename(), file.getMd5()); File fileInCache = getSonarCache().getFileFromCache(file.getFilename(), file.getMd5()); - if (fileInCache != null) { - LOG.debug("File is already cached at location {}", fileInCache.getAbsolutePath()); - } - else { - LOG.debug("File is not cached"); + if (fileInCache == null) { File tmpDownloadFile = getSonarCache().getTemporaryFile(); String url = "/deploy/plugins/" + remote.getKey() + "/" + file.getFilename(); if (LOG.isDebugEnabled()) { @@ -69,19 +64,19 @@ public class PluginDownloader implements BatchComponent { LOG.info("Downloading {}", file.getFilename()); } server.download(url, tmpDownloadFile); - LOG.debug("Trying to cache file"); String md5 = getSonarCache().cacheFile(tmpDownloadFile, file.getFilename()); fileInCache = getSonarCache().getFileFromCache(file.getFilename(), md5); if (!md5.equals(file.getMd5())) { - LOG.warn("INVALID CHECKSUM: File {} was expected to have checksum {} but was cached with checksum {}", - new String[] {fileInCache.getAbsolutePath(), file.getMd5(), md5}); + throw new SonarException("INVALID CHECKSUM: File " + fileInCache.getAbsolutePath() + " was expected to have checksum " + file.getMd5() + + " but was downloaded with checksum " + md5); } - LOG.debug("File cached at location {}", fileInCache.getAbsolutePath()); } files.add(fileInCache); } return files; + } catch (SonarException e) { + throw e; } catch (Exception e) { throw new SonarException("Fail to download plugin: " + remote.getKey(), e); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/SonarCache.java b/sonar-batch/src/main/java/org/sonar/batch/cache/SonarCache.java index da4caca7ee3..4ae8b950417 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/SonarCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/cache/SonarCache.java @@ -54,7 +54,7 @@ public class SonarCache { private SonarCache(File cacheLocation) { this.cacheLocation = cacheLocation; - tmpDir = new File(cacheLocation, ".tmp"); + tmpDir = new File(cacheLocation, "tmp"); if (!cacheLocation.exists()) { LOG.debug("Creating cache directory: {}", cacheLocation.getAbsolutePath()); try { @@ -77,7 +77,7 @@ public class SonarCache { public SonarCache build() { if (cacheLocation == null) { File sonarHome = new File(System.getProperty("user.home"), ".sonar"); - return new SonarCache(new File(sonarHome, ".cache")); + return new SonarCache(new File(sonarHome, "cache")); } else { return new SonarCache(cacheLocation); @@ -96,6 +96,7 @@ public class SonarCache { * @throws IOException */ public String cacheFile(File sourceFile, String filename) throws IOException { + LOG.debug("Trying to cache file {} with filename {}", sourceFile.getAbsolutePath(), filename); File tmpFileName = null; try { if (!sourceFile.getParentFile().equals(getTmpDir())) { @@ -129,6 +130,7 @@ public class SonarCache { FileUtils.moveFile(tmpFileName, finalFileName); } } + LOG.debug("File cached at {}", finalFileName.getAbsolutePath()); return md5; } finally { FileUtils.deleteQuietly(tmpFileName); @@ -142,6 +144,7 @@ public class SonarCache { */ public File getFileFromCache(String filename, String md5) { File location = new File(new File(cacheLocation, md5), filename); + LOG.debug("Looking for {}", location.getAbsolutePath()); if (location.exists()) { return location; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PluginDownloaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PluginDownloaderTest.java index 65c9fdbe149..86fca9177d4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PluginDownloaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PluginDownloaderTest.java @@ -119,4 +119,26 @@ public class PluginDownloaderTest { new PluginDownloader(new BatchSonarCache(new Settings()), server).downloadPluginIndex(); } + + @Test + public void should_fail_if_invalid_checksum() throws Exception { + thrown.expect(SonarException.class); + thrown.expectMessage("INVALID CHECKSUM"); + + SonarCache cache = mock(SonarCache.class); + BatchSonarCache batchCache = mock(BatchSonarCache.class); + when(batchCache.getCache()).thenReturn(cache); + + File fileInCache = temp.newFile(); + when(cache.cacheFile(Mockito.any(File.class), Mockito.anyString())).thenReturn("fakemd51diff"); + when(cache.getFileFromCache(Mockito.anyString(), Mockito.anyString())) + .thenReturn(null) + .thenReturn(fileInCache); + ServerClient server = mock(ServerClient.class); + PluginDownloader downloader = new PluginDownloader(batchCache, server); + + RemotePlugin plugin = new RemotePlugin("checkstyle", true) + .addFile("checkstyle-plugin.jar", "fakemd51"); + downloader.downloadPlugin(plugin); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/SonarCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/SonarCacheTest.java index 2566de8e6bf..a20cbb45636 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/SonarCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/SonarCacheTest.java @@ -50,7 +50,7 @@ public class SonarCacheTest { // Put it in the cache String md5 = cache.cacheFile(fileToCache, "foo.txt"); // Verify the temporary location was created to do the copy in the cache in 2 stages - File tmpCache = new File(cache.getCacheLocation(), ".tmp"); + File tmpCache = new File(cache.getCacheLocation(), "tmp"); assertThat(tmpCache).exists(); // The tmp location should be empty as the file was moved inside the cache assertThat(tmpCache.list()).isEmpty(); @@ -69,7 +69,7 @@ public class SonarCacheTest { // Create a file in the cache temp location File fileToCache = cache.getTemporaryFile(); // Verify the temporary location was created - File tmpCache = new File(cache.getCacheLocation(), ".tmp"); + File tmpCache = new File(cache.getCacheLocation(), "tmp"); assertThat(tmpCache).exists(); assertThat(tmpCache.list().length).isEqualTo(1); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index e4c1a85868c..458d186dfcd 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -379,7 +379,7 @@ public interface CoreProperties { /** * @since 3.5 */ - String CACHE_LOCATION = "sonar.cacheLocation"; + String CACHE_LOCATION = "sonar.cachePath"; /** * @deprecated replaced in v3.4 by properties specific to languages, for example sonar.java.coveragePlugin diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/PluginDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/PluginDeployerTest.java index f2ce1d5db7c..0078d749ffc 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/PluginDeployerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/plugins/PluginDeployerTest.java @@ -77,11 +77,10 @@ public class PluginDeployerTest { // check that the file is deployed File deployedJar = new File(deployDir, "plugins/foo/foo-plugin.jar"); - assertThat(deployedJar.exists()).isTrue(); - assertThat(deployedJar.isFile()).isTrue(); + assertThat(deployedJar).exists(); + assertThat(deployedJar).isFile(); } - @Test public void deployPluginExtensions() { deployer.start(); |