+++ /dev/null
-/*
- * 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 :
- * <ul>
- * <li>Plugin properties are available in General Settings</li>
- * <li>Plugin is marked as installed in Update Center</li>
- * </ul>
- */
- void disable(String pluginKey);
-
- /**
- * @param pluginKey can not be null
- */
- boolean isDisabled(String pluginKey);
-
-}
*/
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<String, Plugin> pluginsByKey;
- private Set<String> disabledPlugins = Sets.newHashSet();
public DefaultServerPluginRepository(PluginDeployer deployer) {
this.classloaders = new PluginClassloaders(getClass().getClassLoader());
this.deployer = deployer;
}
+ @Override
public void start() {
Collection<PluginMetadata> 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);
}
return clazz;
}
+ @Override
public Collection<PluginMetadata> getMetadata() {
return deployer.getMetadata();
}
+ @Override
public PluginMetadata getMetadata(String pluginKey) {
return deployer.getMetadata(pluginKey);
}
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();
import java.util.Map;
+/**
+ * This class adds to picocontainer the server extensions provided by plugins
+ */
public class ServerExtensionInstaller {
private PluginRepository pluginRepository;
*/
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());
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;
}
-
}
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;
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;
}
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();
*/
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() {
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);
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<PluginMetadata> 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<PluginMetadata> 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<Class> extensions;
-
- public FakePlugin(List<Class> extensions) {
- this.extensions = extensions;
- }
-
- public List<Class> 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.<Object> asList(FakeBatchExtension.class, FakeServerExtension.class);
- }
- }
-
- public static class SuperExtensionProvider extends ExtensionProvider implements ServerExtension {
-
- @Override
- public Object provide() {
- return FakeExtensionProvider.class;
- }
- }
}
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;
@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));
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<String> 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);