diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-28 14:56:39 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-28 14:59:48 +0100 |
commit | ee10ce224ea3f6eaf389ad0c4572270de80b1cd9 (patch) | |
tree | 67e6152d54e107bc6f13a1799f1a93656f653e09 /sonar-core | |
parent | 1e73831ed5264c2add9dfc1a4a48f6ed251056cc (diff) | |
download | sonarqube-ee10ce224ea3f6eaf389ad0c4572270de80b1cd9.tar.gz sonarqube-ee10ce224ea3f6eaf389ad0c4572270de80b1cd9.zip |
SONAR-2291 Implement caching of downloaded Sonar plugins.
* By default cache location is ~/.sonar/.cache
* Cache location can be changed by property sonar.cacheLocation
* To know if a plugin file have to be downloaded there is a checksum (MD5) comparison
Diffstat (limited to 'sonar-core')
3 files changed, 83 insertions, 25 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/RemotePlugin.java b/sonar-core/src/main/java/org/sonar/core/plugins/RemotePlugin.java index b00c3230a05..7ae665f1c8c 100644 --- a/sonar-core/src/main/java/org/sonar/core/plugins/RemotePlugin.java +++ b/sonar-core/src/main/java/org/sonar/core/plugins/RemotePlugin.java @@ -20,14 +20,16 @@ package org.sonar.core.plugins; import com.google.common.collect.Lists; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; import java.io.File; +import java.io.FileInputStream; import java.util.List; public class RemotePlugin { private String pluginKey; - private List<String> filenames = Lists.newArrayList(); + private List<RemotePluginFile> files = Lists.newArrayList(); private boolean core; public RemotePlugin(String pluginKey, boolean core) { @@ -37,9 +39,9 @@ public class RemotePlugin { public static RemotePlugin create(DefaultPluginMetadata metadata) { RemotePlugin result = new RemotePlugin(metadata.getKey(), metadata.isCore()); - result.addFilename(metadata.getFile().getName()); + result.addFile(metadata.getFile()); for (File file : metadata.getDeprecatedExtensions()) { - result.addFilename(file.getName()); + result.addFile(file); } return result; } @@ -49,7 +51,8 @@ public class RemotePlugin { RemotePlugin result = new RemotePlugin(fields[0], Boolean.parseBoolean(fields[1])); if (fields.length > 2) { for (int index = 2; index < fields.length; index++) { - result.addFilename(fields[index]); + String[] nameAndMd5 = StringUtils.split(fields[index], "|"); + result.addFile(nameAndMd5[0], nameAndMd5.length > 1 ? nameAndMd5[1] : null); } } return result; @@ -59,8 +62,11 @@ public class RemotePlugin { StringBuilder sb = new StringBuilder(); sb.append(pluginKey).append(","); sb.append(String.valueOf(core)); - for (String filename : filenames) { - sb.append(",").append(filename); + for (RemotePluginFile file : files) { + sb.append(",").append(file.getFilename()); + if (StringUtils.isNotBlank(file.getMd5())) { + sb.append("|").append(file.getMd5()); + } } return sb.toString(); } @@ -69,22 +75,32 @@ public class RemotePlugin { return pluginKey; } - public boolean isCore() { return core; } - public RemotePlugin addFilename(String s) { - filenames.add(s); + public RemotePlugin addFile(String filename, String md5) { + files.add(new RemotePluginFile(filename, md5)); return this; } - public List<String> getFilenames() { - return filenames; + public RemotePlugin addFile(File f) { + String md5; + try { + FileInputStream fis = new FileInputStream(f); + md5 = DigestUtils.md5Hex(fis); + } catch (Exception e) { + md5 = null; + } + return this.addFile(f.getName(), md5); + } + + public List<RemotePluginFile> getFiles() { + return files; } public String getPluginFilename() { - return (!filenames.isEmpty() ? filenames.get(0) : null); + return (!files.isEmpty() ? files.get(0).getFilename() : null); } @Override diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/RemotePluginFile.java b/sonar-core/src/main/java/org/sonar/core/plugins/RemotePluginFile.java new file mode 100644 index 00000000000..88d85c3485b --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/plugins/RemotePluginFile.java @@ -0,0 +1,40 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.plugins; + + +public class RemotePluginFile { + + private String filename; + private String md5; + + public RemotePluginFile(String filename, String md5) { + this.filename = filename; + this.md5 = md5; + } + + public String getFilename() { + return filename; + } + + public String getMd5() { + return md5; + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/plugins/RemotePluginTest.java b/sonar-core/src/test/java/org/sonar/core/plugins/RemotePluginTest.java index f882da5683d..b62dc1d3ad4 100644 --- a/sonar-core/src/test/java/org/sonar/core/plugins/RemotePluginTest.java +++ b/sonar-core/src/test/java/org/sonar/core/plugins/RemotePluginTest.java @@ -23,7 +23,6 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; -import static org.junit.matchers.JUnitMatchers.hasItems; public class RemotePluginTest { @Test @@ -38,29 +37,30 @@ public class RemotePluginTest { @Test public void shouldMarshal() { - RemotePlugin clirr = new RemotePlugin("clirr", false).addFilename("clirr-1.1.jar"); + RemotePlugin clirr = new RemotePlugin("clirr", false).addFile("clirr-1.1.jar", "fakemd5"); String text = clirr.marshal(); - assertThat(text, is("clirr,false,clirr-1.1.jar")); + assertThat(text, is("clirr,false,clirr-1.1.jar|fakemd5")); } @Test public void shouldMarshalDeprecatedExtensions() { - RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); - checkstyle.addFilename("checkstyle-2.8.jar"); - checkstyle.addFilename("ext.xml"); - checkstyle.addFilename("ext.jar"); + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true) + .addFile("checkstyle-2.8.jar", "fakemd51") + .addFile("ext.xml", "fakemd52") + .addFile("ext.jar", "fakemd53"); String text = checkstyle.marshal(); - assertThat(text, is("checkstyle,true,checkstyle-2.8.jar,ext.xml,ext.jar")); + assertThat(text, is("checkstyle,true,checkstyle-2.8.jar|fakemd51,ext.xml|fakemd52,ext.jar|fakemd53")); } @Test public void shouldUnmarshal() { - RemotePlugin clirr = RemotePlugin.unmarshal("clirr,false,clirr-1.1.jar"); + RemotePlugin clirr = RemotePlugin.unmarshal("clirr,false,clirr-1.1.jar|fakemd5"); assertThat(clirr.getKey(), is("clirr")); assertThat(clirr.isCore(), is(false)); - assertThat(clirr.getFilenames().size(), is(1)); - assertThat(clirr.getFilenames().get(0), is("clirr-1.1.jar")); + assertThat(clirr.getFiles().size(), is(1)); + assertThat(clirr.getFiles().get(0).getFilename(), is("clirr-1.1.jar")); + assertThat(clirr.getFiles().get(0).getMd5(), is("fakemd5")); } @@ -69,7 +69,9 @@ public class RemotePluginTest { RemotePlugin checkstyle = RemotePlugin.unmarshal("checkstyle,true,checkstyle-2.8.jar,ext.xml,ext.jar"); assertThat(checkstyle.getKey(), is("checkstyle")); assertThat(checkstyle.isCore(), is(true)); - assertThat(checkstyle.getFilenames().size(), is(3)); - assertThat(checkstyle.getFilenames(), hasItems("checkstyle-2.8.jar", "ext.xml", "ext.jar")); + assertThat(checkstyle.getFiles().size(), is(3)); + assertThat(checkstyle.getFiles().get(0).getFilename(), is("checkstyle-2.8.jar")); + assertThat(checkstyle.getFiles().get(1).getFilename(), is("ext.xml")); + assertThat(checkstyle.getFiles().get(2).getFilename(), is("ext.jar")); } } |