*/
package org.sonar.batch;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import com.google.common.collect.Lists;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.List;
-
public class BatchPluginRepository extends AbstractPluginRepository {
private static final Logger LOG = LoggerFactory.getLogger(BatchPluginRepository.class);
invokeExtensionProviders(pico);
}
- private String getOldCoveragePluginKey(String pluginKey) {
- if (StringUtils.equals("sonar-jacoco-plugin", pluginKey)) {
- return "jacoco";
- }
- if (StringUtils.equals("sonar-emma-plugin", pluginKey)) {
- return "emma";
- }
- return null;
- }
-
@Override
protected boolean shouldRegisterExtension(PicoContainer container, String pluginKey, Object extension) {
boolean ok = isType(extension, BatchExtension.class);
if (ArrayUtils.isEmpty(selectedPluginKeys)) {
selectedPluginKeys = new String[] { AbstractCoverageExtension.DEFAULT_PLUGIN };
}
- String oldCoveragePluginKey = getOldCoveragePluginKey(pluginKey);
- ok = ArrayUtils.contains(selectedPluginKeys, pluginKey) || ArrayUtils.contains(selectedPluginKeys, oldCoveragePluginKey);
+ ok = ArrayUtils.contains(selectedPluginKeys, pluginKey);
}
return ok;
}
*/
package org.sonar.batch;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.IocContainer;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
-
public class BatchPluginRepositoryTest {
@Test
BatchPluginRepository repository = new BatchPluginRepository();
PropertiesConfiguration conf = new PropertiesConfiguration();
assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(), conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(),conf), is(false));
+ assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(), conf), is(false));
}
@Test
Configuration conf = new PropertiesConfiguration();
conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "clover,phpunit");
BatchPluginRepository repository = new BatchPluginRepository();
- assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(),conf), is(false));
+ assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(), conf), is(false));
assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(), conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("phpunit", newJavaProject(),conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("other", newJavaProject(),conf), is(false));
- }
-
- @Test
- public void shouldActivateOldVersionOfEmma() {
- Configuration conf = new PropertiesConfiguration();
- conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "emma");
- BatchPluginRepository repository = new BatchPluginRepository();
-
- assertThat(repository.shouldRegisterCoverageExtension("sonar-emma-plugin", newJavaProject(),conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("emma", newJavaProject(),conf), is(true));
-
- assertThat(repository.shouldRegisterCoverageExtension("sonar-jacoco-plugin", newJavaProject(),conf), is(false));
- assertThat(repository.shouldRegisterCoverageExtension("jacoco", newJavaProject(),conf), is(false));
- assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(),conf), is(false));
- assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(),conf), is(false));
- }
-
- @Test
- public void shouldActivateOldVersionOfJacoco() {
- Configuration conf = new PropertiesConfiguration();
- conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "cobertura,jacoco");
- BatchPluginRepository repository = new BatchPluginRepository();
-
- assertThat(repository.shouldRegisterCoverageExtension("sonar-jacoco-plugin", newJavaProject(),conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("jacoco", newJavaProject(),conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("emma", newJavaProject(),conf), is(false));
+ assertThat(repository.shouldRegisterCoverageExtension("phpunit", newJavaProject(), conf), is(true));
+ assertThat(repository.shouldRegisterCoverageExtension("other", newJavaProject(), conf), is(false));
}
@Test
conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "cobertura");
BatchPluginRepository repository = new BatchPluginRepository();
- assertThat(repository.shouldRegisterCoverageExtension("groovy", newGroovyProject(),conf), is(true));
- assertThat(repository.shouldRegisterCoverageExtension("groovy", newJavaProject(),conf), is(false));
+ assertThat(repository.shouldRegisterCoverageExtension("groovy", newGroovyProject(), conf), is(true));
+ assertThat(repository.shouldRegisterCoverageExtension("groovy", newJavaProject(), conf), is(false));
}
private static Project newJavaProject() {
*/
package org.sonar.server.plugins;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.sonar.core.plugin.JpaPluginDao;
import org.sonar.server.platform.DefaultServerFileSystem;
import org.sonar.server.platform.ServerStartException;
+import org.sonar.updatecenter.common.PluginKeyUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
public final class PluginDeployer implements ServerComponent {
private static final Logger LOG = LoggerFactory.getLogger(PluginDeployer.class);
}
private File moveDownloadedFile(File jar) {
+ File destDir = fileSystem.getUserPluginsDir();
+ File destFile = new File(destDir, jar.getName());
+ if (destFile.exists()) {
+ // plugin with same filename already installed
+ FileUtils.deleteQuietly(jar);
+ return destFile;
+ }
try {
- FileUtils.moveFileToDirectory(jar, fileSystem.getUserPluginsDir(), true);
- return new File(fileSystem.getUserPluginsDir(), jar.getName());
+ FileUtils.moveFileToDirectory(jar, destDir, true);
+ return new File(destDir, jar.getName());
} catch (IOException e) {
LOG.error("Fail to move the downloaded file: " + jar.getAbsolutePath(), e);
map.remove(pluginKey);
Logs.INFO.info("Old plugin " + existing.getFilename() + " replaced by new " + metadata.getFilename());
} else {
- throw new ServerStartException("Found two plugins with the same key '" + pluginKey + "': "
- + metadata.getFilename() + " and "
+ throw new ServerStartException("Found two plugins with the same key '" + pluginKey + "': " + metadata.getFilename() + " and "
+ existing.getFilename());
}
}
try {
URLClassLoader pluginClassLoader = URLClassLoader.newInstance(new URL[] { tempFile.toURI().toURL() }, getClass().getClassLoader());
Plugin pluginInstance = (Plugin) pluginClassLoader.loadClass(mainClass).newInstance();
- plugin.setKey(pluginInstance.getKey());
+ plugin.setKey(PluginKeyUtils.getPluginKey(pluginInstance.getKey()));
plugin.setDescription(pluginInstance.getDescription());
plugin.setName(pluginInstance.getName());
*/
package org.sonar.updatecenter.mavenplugin;
-import org.apache.commons.lang.StringUtils;
+import java.io.File;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
-
-import java.io.File;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import org.sonar.updatecenter.common.PluginKeyUtils;
/**
* Base class for Sonar-plugin-packaging related tasks.
* @author Evgeny Mandrikov
*/
public abstract class AbstractSonarPluginMojo extends AbstractMojo {
+
public static final String SONAR_GROUPID = "org.codehaus.sonar";
public static final String SONAR_PLUGIN_API_ARTIFACTID = "sonar-plugin-api";
public static final String SONAR_PLUGIN_API_TYPE = "jar";
}
public String getPluginKey() {
- if (StringUtils.startsWith(pluginKey, "sonar-") && StringUtils.endsWith(pluginKey, "-plugin")) {
- return StringUtils.removeEnd(StringUtils.removeStart(pluginKey, "sonar-"), "-plugin");
- }
- if (StringUtils.endsWith(pluginKey, "-sonar-plugin")) {
- return StringUtils.removeEnd(pluginKey, "-sonar-plugin");
- }
- return StringUtils.remove(pluginKey, "-");
+ return PluginKeyUtils.getPluginKey(pluginKey);
}
protected final String getPluginClass() {
return skipDependenciesPackaging;
}
- @SuppressWarnings( { "unchecked" })
+ @SuppressWarnings({ "unchecked" })
protected Set<Artifact> getDependencyArtifacts() {
return getProject().getDependencyArtifacts();
}
return result;
}
- @SuppressWarnings( { "unchecked" })
+ @SuppressWarnings({ "unchecked" })
protected Set<Artifact> getIncludedArtifacts() {
Set<Artifact> result = new HashSet<Artifact>();
Set<Artifact> artifacts = getProject().getArtifacts();
Set<Artifact> dependencies = getDependencyArtifacts();
if (dependencies != null) {
for (Artifact dep : dependencies) {
- if (SONAR_GROUPID.equals(dep.getGroupId())
- && SONAR_PLUGIN_API_ARTIFACTID.equals(dep.getArtifactId())
+ if (SONAR_GROUPID.equals(dep.getGroupId()) && SONAR_PLUGIN_API_ARTIFACTID.equals(dep.getArtifactId())
&& SONAR_PLUGIN_API_TYPE.equals(dep.getType())) {
return dep;
}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * 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.updatecenter.mavenplugin;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.junit.Test;
-
-public class AbstractSonarPluginMojoTest {
-
- @Test
- public void shouldExtractPluginKeyFromArtifactId() {
- AbstractSonarPluginMojo mojo = new AbstractSonarPluginMojo() {
- public void execute() throws MojoExecutionException, MojoFailureException {
- }
- };
- mojo.pluginKey = "sonar-test-plugin";
- assertThat(mojo.getPluginKey(), is("test"));
- mojo.pluginKey = "test-sonar-plugin";
- assertThat(mojo.getPluginKey(), is("test"));
- mojo.pluginKey = "test";
- assertThat(mojo.getPluginKey(), is("test"));
- mojo.pluginKey = "test-foo";
- assertThat(mojo.getPluginKey(), is("testfoo"));
-
- }
-
-}
--- /dev/null
+package org.sonar.updatecenter.common;
+
+import org.apache.commons.lang.StringUtils;
+
+public final class PluginKeyUtils {
+
+ public static String getPluginKey(String pluginKey) {
+ String key = pluginKey;
+ if (StringUtils.startsWith(pluginKey, "sonar-") && StringUtils.endsWith(pluginKey, "-plugin")) {
+ key = StringUtils.removeEnd(StringUtils.removeStart(pluginKey, "sonar-"), "-plugin");
+ } else if (StringUtils.endsWith(pluginKey, "-sonar-plugin")) {
+ key = StringUtils.removeEnd(pluginKey, "-sonar-plugin");
+ }
+ return StringUtils.remove(key, "-");
+ }
+
+ private PluginKeyUtils() {
+ }
+
+}
*/
package org.sonar.updatecenter.common;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.builder.ReflectionToStringBuilder;
+import static org.sonar.updatecenter.common.FormatUtils.toDate;
import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
-import static org.sonar.updatecenter.common.FormatUtils.toDate;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
/**
* This class loads Sonar plugin metadata from JAR manifest.
}
/**
- * @param manifest, can not be null
+ * @param manifest
+ * , can not be null
*/
public PluginManifest(Manifest manifest) {
loadManifest(manifest);
private void loadManifest(Manifest manifest) {
Attributes attributes = manifest.getMainAttributes();
- this.key = attributes.getValue(KEY);
+ this.key = PluginKeyUtils.getPluginKey(attributes.getValue(KEY));
this.mainClass = attributes.getValue(MAIN_CLASS);
this.name = attributes.getValue(NAME);
this.description = attributes.getValue(DESCRIPTION);
--- /dev/null
+package org.sonar.updatecenter.common;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import org.junit.Test;
+
+public class PluginKeyUtilsTest {
+
+ @Test
+ public void shouldExtractCorrectPluginKey() {
+ assertThat(PluginKeyUtils.getPluginKey("sonar-test-plugin"), is("test"));
+ assertThat(PluginKeyUtils.getPluginKey("test-sonar-plugin"), is("test"));
+ assertThat(PluginKeyUtils.getPluginKey("test"), is("test"));
+
+ assertThat(PluginKeyUtils.getPluginKey("sonar-test-foo-plugin"), is("testfoo"));
+ assertThat(PluginKeyUtils.getPluginKey("test-foo-sonar-plugin"), is("testfoo"));
+ assertThat(PluginKeyUtils.getPluginKey("test-foo"), is("testfoo"));
+ }
+}