diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-06-10 14:06:37 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-06-10 14:06:37 +0200 |
commit | 23d30654116e9088b92cbbe8ed70fc71e5598854 (patch) | |
tree | 0cc65d0a54f11a98510b87ca57c3303b440f4f06 /sonar-core | |
parent | e9957c5d4049a2f8f39c83a6ff6a09e5fa333f7f (diff) | |
download | sonarqube-23d30654116e9088b92cbbe8ed70fc71e5598854.tar.gz sonarqube-23d30654116e9088b92cbbe8ed70fc71e5598854.zip |
SONAR-2507 support deprecated directory /extensions/rules/
Diffstat (limited to 'sonar-core')
5 files changed, 197 insertions, 11 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/DefaultPluginMetadata.java b/sonar-core/src/main/java/org/sonar/core/plugins/DefaultPluginMetadata.java index 798cd20bf2b..56cb45aa239 100644 --- a/sonar-core/src/main/java/org/sonar/core/plugins/DefaultPluginMetadata.java +++ b/sonar-core/src/main/java/org/sonar/core/plugins/DefaultPluginMetadata.java @@ -20,14 +20,12 @@ package org.sonar.core.plugins; import com.google.common.collect.Lists; -import org.apache.commons.collections.ComparatorUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.platform.PluginMetadata; import java.io.File; -import java.util.ArrayList; import java.util.List; public final class DefaultPluginMetadata implements PluginMetadata, Comparable<PluginMetadata> { @@ -82,6 +80,11 @@ public final class DefaultPluginMetadata implements PluginMetadata, Comparable<P return this; } + public DefaultPluginMetadata setDeprecatedExtensions(List<File> files) { + this.deprecatedExtensions = (files==null ? Lists.<File>newArrayList() : files); + return this; + } + public String[] getPathsToInternalDeps() { return pathsToInternalDeps; } diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/PluginFileExtractor.java b/sonar-core/src/main/java/org/sonar/core/plugins/PluginFileExtractor.java index e248ffd078e..da200c937be 100644 --- a/sonar-core/src/main/java/org/sonar/core/plugins/PluginFileExtractor.java +++ b/sonar-core/src/main/java/org/sonar/core/plugins/PluginFileExtractor.java @@ -20,28 +20,28 @@ package org.sonar.core.plugins; import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; import org.sonar.api.Plugin; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.ZipUtils; import org.sonar.updatecenter.common.PluginKeyUtils; import org.sonar.updatecenter.common.PluginManifest; -import javax.swing.plaf.metal.MetalTabbedPaneUI; import java.io.File; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; +import java.util.List; import java.util.zip.ZipEntry; public class PluginFileExtractor { - public DefaultPluginMetadata installInSameLocation(File pluginFile, boolean isCore) { - return install(pluginFile, isCore, null); + public DefaultPluginMetadata installInSameLocation(File pluginFile, boolean isCore, List<File> deprecatedExtensions) { + return install(pluginFile, isCore, deprecatedExtensions, null); } - public DefaultPluginMetadata install(File pluginFile, boolean isCore, File toDir) { + public DefaultPluginMetadata install(File pluginFile, boolean isCore, List<File> deprecatedExtensions, File toDir) { DefaultPluginMetadata metadata = extractMetadata(pluginFile, isCore); + metadata.setDeprecatedExtensions(deprecatedExtensions); return install(metadata, toDir); } @@ -78,7 +78,9 @@ public class PluginFileExtractor { for (File extension : metadata.getDeprecatedExtensions()) { File toFile = new File(pluginBasedir, extension.getName()); - FileUtils.copyFile(extension, toFile); + if (!toFile.equals(extension)) { + FileUtils.copyFile(extension, toFile); + } metadata.addDeployedFile(toFile); } @@ -123,7 +125,7 @@ public class PluginFileExtractor { // copy file in a temp directory because Windows+Oracle JVM Classloader lock the JAR file File tempFile = File.createTempFile(pluginFile.getName(), null); FileUtils.copyFile(pluginFile, tempFile); - + URLClassLoader pluginClassLoader = URLClassLoader.newInstance(new URL[]{tempFile.toURI().toURL()}, getClass().getClassLoader()); Plugin pluginInstance = (Plugin) pluginClassLoader.loadClass(mainClass).newInstance(); metadata.setKey(PluginKeyUtils.sanitize(pluginInstance.getKey())); 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 new file mode 100644 index 00000000000..bf3477f2663 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/plugins/RemotePlugin.java @@ -0,0 +1,106 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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; + +import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; + +import java.io.File; +import java.util.List; + +public class RemotePlugin { + private String pluginKey; + private List<String> filenames = Lists.newArrayList(); + private boolean core; + + public RemotePlugin(String pluginKey, boolean core) { + this.pluginKey = pluginKey; + this.core = core; + } + + public static RemotePlugin create(DefaultPluginMetadata metadata) { + RemotePlugin result = new RemotePlugin(metadata.getKey(), metadata.isCore()); + result.addFilename(metadata.getFile().getName()); + for (File file : metadata.getDeprecatedExtensions()) { + result.addFilename(file.getName()); + } + return result; + } + + public static RemotePlugin unmarshal(String row) { + String[] fields = StringUtils.split(row, ","); + 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]); + } + } + return result; + } + + public String marshal() { + StringBuilder sb = new StringBuilder(); + sb.append(pluginKey).append(","); + sb.append(String.valueOf(core)); + for (String filename : filenames) { + sb.append(",").append(filename); + } + return sb.toString(); + } + + public String getKey() { + return pluginKey; + } + + + public boolean isCore() { + return core; + } + + public RemotePlugin addFilename(String s) { + filenames.add(s); + return this; + } + + public List<String> getFilenames() { + return filenames; + } + + public String getPluginFilename() { + return (filenames.size()>0 ? filenames.get(0) : null); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + RemotePlugin that = (RemotePlugin) o; + return pluginKey.equals(that.pluginKey); + } + + @Override + public int hashCode() { + return pluginKey.hashCode(); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/plugins/PluginFileExtractorTest.java b/sonar-core/src/test/java/org/sonar/core/plugins/PluginFileExtractorTest.java index 4923a470ab9..1dff51b0119 100644 --- a/sonar-core/src/test/java/org/sonar/core/plugins/PluginFileExtractorTest.java +++ b/sonar-core/src/test/java/org/sonar/core/plugins/PluginFileExtractorTest.java @@ -64,7 +64,7 @@ public class PluginFileExtractorTest { FileUtils.forceMkdir(toDir); FileUtils.cleanDirectory(toDir); - DefaultPluginMetadata metadata = extractor.install(getFile("sonar-checkstyle-plugin-2.8.jar"), true, toDir); + DefaultPluginMetadata metadata = extractor.install(getFile("sonar-checkstyle-plugin-2.8.jar"), true, null, toDir); assertThat(metadata.getKey(), is("checkstyle")); assertThat(new File(toDir, "sonar-checkstyle-plugin-2.8.jar").exists(), is(true)); @@ -77,7 +77,7 @@ public class PluginFileExtractorTest { FileUtils.forceMkdir(toDir); FileUtils.cleanDirectory(toDir); - extractor.install(getFile("sonar-checkstyle-plugin-2.8.jar"), true, toDir); + extractor.install(getFile("sonar-checkstyle-plugin-2.8.jar"), true, null, toDir); assertThat(new File(toDir, "sonar-checkstyle-plugin-2.8.jar").exists(), is(true)); assertThat(new File(toDir, "META-INF/MANIFEST.MF").exists(), is(false)); 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 new file mode 100644 index 00000000000..941c9301791 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/plugins/RemotePluginTest.java @@ -0,0 +1,75 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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; + +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 + public void shouldEqual() { + RemotePlugin clirr1 = new RemotePlugin("clirr", false); + RemotePlugin clirr2 = new RemotePlugin("clirr", false); + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); + assertThat(clirr1.equals(clirr2), is(true)); + assertThat(clirr1.equals(clirr1), is(true)); + assertThat(clirr1.equals(checkstyle), is(false)); + } + + @Test + public void shouldMarshal() { + RemotePlugin clirr = new RemotePlugin("clirr", false).addFilename("clirr-1.1.jar"); + String text = clirr.marshal(); + assertThat(text, is("clirr,false,clirr-1.1.jar")); + } + + @Test + public void shouldMarshalDeprecatedExtensions() { + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); + checkstyle.addFilename("checkstyle-2.8.jar"); + checkstyle.addFilename("ext.xml"); + checkstyle.addFilename("ext.jar"); + + String text = checkstyle.marshal(); + assertThat(text, is("checkstyle,true,checkstyle-2.8.jar,ext.xml,ext.jar")); + } + + @Test + public void shouldUnmarshal() { + RemotePlugin clirr = RemotePlugin.unmarshal("clirr,false,clirr-1.1.jar"); + 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")); + + } + + @Test + public void shouldUnmarshalDeprecatedExtensions() { + 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")); + } +} |