From 568969fea6803e47f28a85459c2053ca377f0b87 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 16 Mar 2014 11:16:18 +0100 Subject: [PATCH] Remove org.sonar.api.platform.ServerPluginRepository --- .../api/platform/ServerPluginRepository.java | 43 -------- .../DefaultServerPluginRepository.java | 37 ++----- .../server/plugins/PluginDownloader.java | 3 +- .../plugins/ServerExtensionInstaller.java | 3 + .../plugins/UpdateCenterMatrixFactory.java | 17 ++- .../server/startup/GeneratePluginIndex.java | 12 +- .../DefaultServerPluginRepositoryTest.java | 103 +----------------- .../startup/GeneratePluginIndexTest.java | 19 +--- 8 files changed, 36 insertions(+), 201 deletions(-) delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/platform/ServerPluginRepository.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ServerPluginRepository.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ServerPluginRepository.java deleted file mode 100644 index d27e0b2a2bd..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ServerPluginRepository.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 02110-1301, USA. - */ -package org.sonar.api.platform; - -import org.sonar.api.ServerComponent; - -/** - * @since 2.11 - */ -public interface ServerPluginRepository extends PluginRepository, ServerComponent { - - /** - * Disabled plugins are not loaded by batch, but they are still installed : - * - */ - void disable(String pluginKey); - - /** - * @param pluginKey can not be null - */ - boolean isDisabled(String pluginKey); - -} diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/DefaultServerPluginRepository.java b/sonar-server/src/main/java/org/sonar/server/plugins/DefaultServerPluginRepository.java index 48d05d94262..b2cefb80450 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/DefaultServerPluginRepository.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/DefaultServerPluginRepository.java @@ -19,57 +19,42 @@ */ package org.sonar.server.plugins; -import com.google.common.collect.Sets; +import org.picocontainer.Startable; import org.slf4j.LoggerFactory; import org.sonar.api.Plugin; import org.sonar.api.platform.PluginMetadata; -import org.sonar.api.platform.ServerPluginRepository; +import org.sonar.api.platform.PluginRepository; import org.sonar.core.plugins.PluginClassloaders; import java.util.Collection; import java.util.Map; -import java.util.Set; /** - * @since 2.2 + * This class loads JAR files and initializes classloaders of plugins */ -public class DefaultServerPluginRepository implements ServerPluginRepository { +public class DefaultServerPluginRepository implements PluginRepository, Startable { - private PluginClassloaders classloaders; - private PluginDeployer deployer; + private final PluginDeployer deployer; + private final PluginClassloaders classloaders; private Map pluginsByKey; - private Set disabledPlugins = Sets.newHashSet(); public DefaultServerPluginRepository(PluginDeployer deployer) { this.classloaders = new PluginClassloaders(getClass().getClassLoader()); this.deployer = deployer; } + @Override public void start() { Collection metadata = deployer.getMetadata(); pluginsByKey = classloaders.init(metadata); } + @Override public void stop() { - if (classloaders != null) { - classloaders.clean(); - classloaders = null; - } - } - - public void disable(String pluginKey) { - disabledPlugins.add(pluginKey); - for (PluginMetadata metadata : getMetadata()) { - if (pluginKey.equals(metadata.getBasePlugin())) { - disable(metadata.getKey()); - } - } - } - - public boolean isDisabled(String pluginKey) { - return disabledPlugins.contains(pluginKey); + classloaders.clean(); } + @Override public Plugin getPlugin(String key) { return pluginsByKey.get(key); } @@ -92,10 +77,12 @@ public class DefaultServerPluginRepository implements ServerPluginRepository { return clazz; } + @Override public Collection getMetadata() { return deployer.getMetadata(); } + @Override public PluginMetadata getMetadata(String pluginKey) { return deployer.getMetadata(pluginKey); } diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java index 184b08bbb4c..d96c32e04ac 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java @@ -49,7 +49,8 @@ public class PluginDownloader implements ServerComponent, Startable { private final HttpDownloader downloader; private final File downloadDir; - public PluginDownloader(UpdateCenterMatrixFactory updateCenterMatrixFactory, HttpDownloader downloader, DefaultServerFileSystem fileSystem) { + public PluginDownloader(UpdateCenterMatrixFactory updateCenterMatrixFactory, HttpDownloader downloader, + DefaultServerFileSystem fileSystem) { this.updateCenterMatrixFactory = updateCenterMatrixFactory; this.downloader = downloader; this.downloadDir = fileSystem.getDownloadedPluginsDir(); diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java b/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java index 4031830850c..1432be8d124 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java @@ -31,6 +31,9 @@ import org.sonar.api.platform.PluginRepository; import java.util.Map; +/** + * This class adds to picocontainer the server extensions provided by plugins + */ public class ServerExtensionInstaller { private PluginRepository pluginRepository; diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java index 7caf18433e6..31a4c1b35d9 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java @@ -29,11 +29,12 @@ import org.sonar.updatecenter.common.Version; */ public class UpdateCenterMatrixFactory implements ServerComponent { - private UpdateCenterClient centerClient; - private Version sonarVersion; - private InstalledPluginReferentialFactory installedPluginReferentialFactory; + private final UpdateCenterClient centerClient; + private final Version sonarVersion; + private final InstalledPluginReferentialFactory installedPluginReferentialFactory; - public UpdateCenterMatrixFactory(UpdateCenterClient centerClient, InstalledPluginReferentialFactory installedPluginReferentialFactory, Server server) { + public UpdateCenterMatrixFactory(UpdateCenterClient centerClient, Server server, + InstalledPluginReferentialFactory installedPluginReferentialFactory) { this.centerClient = centerClient; this.installedPluginReferentialFactory = installedPluginReferentialFactory; this.sonarVersion = Version.create(server.getVersion()); @@ -43,12 +44,10 @@ public class UpdateCenterMatrixFactory implements ServerComponent { UpdateCenter updatePluginCenter = centerClient.getUpdateCenter(refreshUpdateCenter); if (updatePluginCenter != null) { return updatePluginCenter.setInstalledSonarVersion(sonarVersion).registerInstalledPlugins( - installedPluginReferentialFactory.getInstalledPluginReferential()) - .setDate(centerClient.getLastRefreshDate()); - } else { - return null; + installedPluginReferentialFactory.getInstalledPluginReferential()) + .setDate(centerClient.getLastRefreshDate()); } + return null; } - } diff --git a/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java b/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java index 8b6bbf1e6fd..bb9e08077df 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java @@ -23,10 +23,10 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.CharUtils; import org.sonar.api.platform.PluginMetadata; +import org.sonar.api.platform.PluginRepository; import org.sonar.core.plugins.DefaultPluginMetadata; import org.sonar.core.plugins.RemotePlugin; import org.sonar.server.platform.DefaultServerFileSystem; -import org.sonar.server.plugins.DefaultServerPluginRepository; import java.io.File; import java.io.FileWriter; @@ -38,9 +38,9 @@ import java.io.IOException; public final class GeneratePluginIndex { private DefaultServerFileSystem fileSystem; - private DefaultServerPluginRepository repository; + private PluginRepository repository; - public GeneratePluginIndex(DefaultServerFileSystem fileSystem, DefaultServerPluginRepository repository) { + public GeneratePluginIndex(DefaultServerFileSystem fileSystem, PluginRepository repository) { this.fileSystem = fileSystem; this.repository = repository; } @@ -54,10 +54,8 @@ public final class GeneratePluginIndex { FileWriter writer = new FileWriter(indexFile, false); try { for (PluginMetadata metadata : repository.getMetadata()) { - if (!repository.isDisabled(metadata.getKey())) { - writer.append(RemotePlugin.create((DefaultPluginMetadata) metadata).marshal()); - writer.append(CharUtils.LF); - } + writer.append(RemotePlugin.create((DefaultPluginMetadata) metadata).marshal()); + writer.append(CharUtils.LF); } writer.flush(); diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/DefaultServerPluginRepositoryTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/DefaultServerPluginRepositoryTest.java index f441e5987fd..6a6f943405f 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/DefaultServerPluginRepositoryTest.java +++ b/sonar-server/src/test/java/org/sonar/server/plugins/DefaultServerPluginRepositoryTest.java @@ -19,32 +19,24 @@ */ package org.sonar.server.plugins; -import org.hamcrest.core.Is; import org.junit.After; import org.junit.Test; -import org.sonar.api.BatchExtension; -import org.sonar.api.ExtensionProvider; -import org.sonar.api.ServerExtension; -import org.sonar.api.SonarPlugin; import org.sonar.api.platform.PluginMetadata; import org.sonar.core.plugins.DefaultPluginMetadata; import org.sonar.test.TestUtils; import java.io.File; import java.util.Arrays; -import java.util.List; -import static junit.framework.Assert.assertFalse; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class DefaultServerPluginRepositoryTest { - private DefaultServerPluginRepository repository; + DefaultServerPluginRepository repository; @After public void stop() { @@ -58,9 +50,9 @@ public class DefaultServerPluginRepositoryTest { PluginDeployer deployer = mock(PluginDeployer.class); File pluginFile = TestUtils.getResource("/org/sonar/server/plugins/DefaultServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar"); PluginMetadata plugin = DefaultPluginMetadata.create(pluginFile) - .setKey("artifactsize") - .setMainClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin") - .addDeployedFile(pluginFile); + .setKey("artifactsize") + .setMainClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin") + .addDeployedFile(pluginFile); when(deployer.getMetadata()).thenReturn(Arrays.asList(plugin)); repository = new DefaultServerPluginRepository(deployer); @@ -72,91 +64,4 @@ public class DefaultServerPluginRepositoryTest { assertThat(repository.getClass("artifactsize", "org.Unknown"), nullValue()); assertThat(repository.getClass("other", "org.sonar.plugins.artifactsize.ArtifactSizeMetrics"), nullValue()); } - - @Test - public void shouldDisablePlugin() { - DefaultServerPluginRepository repository = new DefaultServerPluginRepository(mock(PluginDeployer.class)); - repository.disable("checkstyle"); - - assertTrue(repository.isDisabled("checkstyle")); - assertFalse(repository.isDisabled("sqale")); - } - - @Test - public void shouldDisableDependentPlugins() { - PluginDeployer deployer = mock(PluginDeployer.class); - List metadata = Arrays.asList( - newMetadata("checkstyle", null), - newMetadata("checkstyle-extensions", "checkstyle"), - newMetadata("sqale", null) - ); - when(deployer.getMetadata()).thenReturn(metadata); - DefaultServerPluginRepository repository = new DefaultServerPluginRepository(deployer); - - repository.disable("checkstyle"); - - assertTrue(repository.isDisabled("checkstyle")); - assertTrue(repository.isDisabled("checkstyle-extensions")); - assertFalse(repository.isDisabled("sqale")); - } - - @Test - public void shouldNotDisableBasePlugin() { - PluginDeployer deployer = mock(PluginDeployer.class); - List metadata = Arrays.asList( - newMetadata("checkstyle", null), - newMetadata("checkstyle-extensions", "checkstyle"), - newMetadata("sqale", null) - ); - when(deployer.getMetadata()).thenReturn(metadata); - DefaultServerPluginRepository repository = new DefaultServerPluginRepository(deployer); - - repository.disable("checkstyle-extensions"); - - assertFalse(repository.isDisabled("checkstyle")); - assertTrue(repository.isDisabled("checkstyle-extensions")); - } - - private PluginMetadata newMetadata(String pluginKey, String basePluginKey) { - PluginMetadata plugin = mock(PluginMetadata.class); - when(plugin.getKey()).thenReturn(pluginKey); - when(plugin.getBasePlugin()).thenReturn(basePluginKey); - return plugin; - } - - public static class FakePlugin extends SonarPlugin { - private final List extensions; - - public FakePlugin(List extensions) { - this.extensions = extensions; - } - - public List getExtensions() { - return extensions; - } - } - - public static class FakeBatchExtension implements BatchExtension { - - } - - public static class FakeServerExtension implements ServerExtension { - - } - - public static class FakeExtensionProvider extends ExtensionProvider implements ServerExtension { - - @Override - public Object provide() { - return Arrays. asList(FakeBatchExtension.class, FakeServerExtension.class); - } - } - - public static class SuperExtensionProvider extends ExtensionProvider implements ServerExtension { - - @Override - public Object provide() { - return FakeExtensionProvider.class; - } - } } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java b/sonar-server/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java index c4e308fc52e..0fbabca058e 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java @@ -24,9 +24,9 @@ import org.hamcrest.core.Is; import org.junit.Before; import org.junit.Test; import org.sonar.api.platform.PluginMetadata; +import org.sonar.api.platform.PluginRepository; import org.sonar.core.plugins.DefaultPluginMetadata; import org.sonar.server.platform.DefaultServerFileSystem; -import org.sonar.server.plugins.DefaultServerPluginRepository; import java.io.File; import java.io.IOException; @@ -52,7 +52,7 @@ public class GeneratePluginIndexTest { @Test public void shouldWriteIndex() throws IOException { - DefaultServerPluginRepository repository = mock(DefaultServerPluginRepository.class); + PluginRepository repository = mock(PluginRepository.class); PluginMetadata sqale = newMetadata("sqale"); PluginMetadata checkstyle = newMetadata("checkstyle"); when(repository.getMetadata()).thenReturn(Arrays.asList(sqale, checkstyle)); @@ -65,21 +65,6 @@ public class GeneratePluginIndexTest { assertThat(lines.get(1), containsString("checkstyle")); } - @Test - public void shouldSkipDisabledPlugin() throws IOException { - DefaultServerPluginRepository repository = mock(DefaultServerPluginRepository.class); - PluginMetadata sqale = newMetadata("sqale"); - PluginMetadata checkstyle = newMetadata("checkstyle"); - when(repository.getMetadata()).thenReturn(Arrays.asList(sqale, checkstyle)); - when(repository.isDisabled("checkstyle")).thenReturn(true); - - new GeneratePluginIndex(fileSystem, repository).start(); - - List lines = FileUtils.readLines(index); - assertThat(lines.size(), Is.is(1)); - assertThat(lines.get(0), containsString("sqale")); - } - private PluginMetadata newMetadata(String pluginKey) { PluginMetadata plugin = mock(DefaultPluginMetadata.class); when(plugin.getKey()).thenReturn(pluginKey); -- 2.39.5