aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2020-08-26 18:30:15 -0500
committersonartech <sonartech@sonarsource.com>2020-09-18 20:07:13 +0000
commitd189558e9c7b3994254eaa8d67dff0169c21d5dd (patch)
treedb9d6937b26ad9ce923c408a8d3ee94045203d82 /server/sonar-ce/src
parent13ef209f1b2cc5f82d0c3bde502ba02649b6c5e2 (diff)
downloadsonarqube-d189558e9c7b3994254eaa8d67dff0169c21d5dd.tar.gz
sonarqube-d189558e9c7b3994254eaa8d67dff0169c21d5dd.zip
SONAR-13643 Save plugins with type
Diffstat (limited to 'server/sonar-ce/src')
-rw-r--r--server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginJarExploder.java2
-rw-r--r--server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java24
-rw-r--r--server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java4
-rw-r--r--server/sonar-ce/src/test/java/org/sonar/ce/container/CePluginRepositoryTest.java69
4 files changed, 55 insertions, 44 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginJarExploder.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginJarExploder.java
index 18959e76b31..b5d95a6f04c 100644
--- a/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginJarExploder.java
+++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginJarExploder.java
@@ -51,7 +51,7 @@ public class CePluginJarExploder extends PluginJarExploder {
File jarTarget = new File(toDir, jarSource.getName());
FileUtils.copyFile(jarSource, jarTarget);
ZipUtils.unzip(jarSource, toDir, newLibFilter());
- return explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir);
+ return explodeFromUnzippedDir(pluginInfo, jarTarget, toDir);
} catch (Exception e) {
throw new IllegalStateException(String.format(
"Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e);
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java
index 4c9476647ce..cc85465e78b 100644
--- a/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java
+++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java
@@ -26,12 +26,14 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.picocontainer.Startable;
import org.sonar.api.Plugin;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.platform.ExplodedPlugin;
+import org.sonar.core.platform.PluginClassLoader;
import org.sonar.core.platform.PluginInfo;
-import org.sonar.core.platform.PluginLoader;
import org.sonar.core.platform.PluginRepository;
import org.sonar.server.platform.ServerFileSystem;
@@ -49,16 +51,18 @@ public class CePluginRepository implements PluginRepository, Startable {
private static final String NOT_STARTED_YET = "not started yet";
private final ServerFileSystem fs;
- private final PluginLoader loader;
+ private final PluginClassLoader loader;
+ private final CePluginJarExploder cePluginJarExploder;
private final AtomicBoolean started = new AtomicBoolean(false);
// following fields are available after startup
private final Map<String, PluginInfo> pluginInfosByKeys = new HashMap<>();
private final Map<String, Plugin> pluginInstancesByKeys = new HashMap<>();
- public CePluginRepository(ServerFileSystem fs, PluginLoader loader) {
+ public CePluginRepository(ServerFileSystem fs, PluginClassLoader loader, CePluginJarExploder cePluginJarExploder) {
this.fs = fs;
this.loader = loader;
+ this.cePluginJarExploder = cePluginJarExploder;
}
@Override
@@ -66,7 +70,8 @@ public class CePluginRepository implements PluginRepository, Startable {
Loggers.get(getClass()).info("Load plugins");
registerPluginsFromDir(fs.getInstalledBundledPluginsDir());
registerPluginsFromDir(fs.getInstalledExternalPluginsDir());
- pluginInstancesByKeys.putAll(loader.load(pluginInfosByKeys));
+ Map<String, ExplodedPlugin> explodedPluginsByKey = extractPlugins(pluginInfosByKeys);
+ pluginInstancesByKeys.putAll(loader.load(explodedPluginsByKey));
started.set(true);
}
@@ -77,6 +82,12 @@ public class CePluginRepository implements PluginRepository, Startable {
}
}
+ private Map<String, ExplodedPlugin> extractPlugins(Map<String, PluginInfo> pluginsByKey) {
+ return pluginsByKey.values().stream()
+ .map(cePluginJarExploder::explode)
+ .collect(Collectors.toMap(ExplodedPlugin::getKey, p -> p));
+ }
+
@Override
public void stop() {
// close classloaders
@@ -111,6 +122,11 @@ public class CePluginRepository implements PluginRepository, Startable {
}
@Override
+ public Collection<Plugin> getPluginInstances() {
+ return pluginInstancesByKeys.values();
+ }
+
+ @Override
public boolean hasPlugin(String key) {
checkState(started.get(), NOT_STARTED_YET);
return pluginInfosByKeys.containsKey(key);
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
index f7e3793a818..7d57bbfcb72 100644
--- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
+++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
@@ -77,7 +77,7 @@ import org.sonar.core.platform.EditionProvider;
import org.sonar.core.platform.Module;
import org.sonar.core.platform.PlatformEditionProvider;
import org.sonar.core.platform.PluginClassloaderFactory;
-import org.sonar.core.platform.PluginLoader;
+import org.sonar.core.platform.PluginClassLoader;
import org.sonar.core.util.UuidFactoryImpl;
import org.sonar.db.DBSessionsImpl;
import org.sonar.db.DaoModule;
@@ -335,7 +335,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
// plugins
PluginClassloaderFactory.class,
CePluginJarExploder.class,
- PluginLoader.class,
+ PluginClassLoader.class,
CePluginRepository.class,
InstalledPluginReferentialFactory.class,
ComputeEngineExtensionInstaller.class,
diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/CePluginRepositoryTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/CePluginRepositoryTest.java
index 047eebf6ead..4e3c93dd164 100644
--- a/server/sonar-ce/src/test/java/org/sonar/ce/container/CePluginRepositoryTest.java
+++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/CePluginRepositoryTest.java
@@ -20,21 +20,22 @@
package org.sonar.ce.container;
import java.io.File;
+import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.sonar.api.Plugin;
-import org.sonar.core.platform.PluginInfo;
-import org.sonar.core.platform.PluginLoader;
+import org.sonar.core.platform.ExplodedPlugin;
+import org.sonar.core.platform.PluginClassLoader;
import org.sonar.server.platform.ServerFileSystem;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -43,12 +44,10 @@ public class CePluginRepositoryTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private ServerFileSystem fs = mock(ServerFileSystem.class, Mockito.RETURNS_DEEP_STUBS);
- private PluginLoader pluginLoader = new DumbPluginLoader();
- private CePluginRepository underTest = new CePluginRepository(fs, pluginLoader);
+ private PluginClassLoader pluginClassLoader = new DumbPluginClassLoader();
+ private CePluginJarExploder cePluginJarExploder = new CePluginJarExploder(fs);
+ private CePluginRepository underTest = new CePluginRepository(fs, pluginClassLoader, cePluginJarExploder);
@After
public void tearDown() {
@@ -67,8 +66,9 @@ public class CePluginRepositoryTest {
}
@Test
- public void load_plugins() {
+ public void load_plugins() throws IOException {
String pluginKey = "test";
+ when(fs.getTempDir()).thenReturn(temp.newFolder());
when(fs.getInstalledExternalPluginsDir()).thenReturn(new File("src/test/plugins/sonar-test-plugin/target"));
underTest.start();
@@ -76,74 +76,69 @@ public class CePluginRepositoryTest {
assertThat(underTest.getPluginInfos()).extracting("key").containsOnly(pluginKey);
assertThat(underTest.getPluginInfo(pluginKey).getKey()).isEqualTo(pluginKey);
assertThat(underTest.getPluginInstance(pluginKey)).isNotNull();
+ assertThat(underTest.getPluginInstances()).isNotEmpty();
assertThat(underTest.hasPlugin(pluginKey)).isTrue();
}
@Test
public void getPluginInfo_fails_if_plugin_does_not_exist() throws Exception {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Plugin [foo] does not exist");
-
// empty folder
when(fs.getInstalledExternalPluginsDir()).thenReturn(temp.newFolder());
underTest.start();
- underTest.getPluginInfo("foo");
+ assertThatThrownBy(() -> underTest.getPluginInfo("foo"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Plugin [foo] does not exist");
}
@Test
public void getPluginInstance_fails_if_plugin_does_not_exist() throws Exception {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Plugin [foo] does not exist");
-
// empty folder
when(fs.getInstalledExternalPluginsDir()).thenReturn(temp.newFolder());
underTest.start();
- underTest.getPluginInstance("foo");
+ assertThatThrownBy(() -> underTest.getPluginInstance("foo"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Plugin [foo] does not exist");
}
@Test
public void getPluginInstance_throws_ISE_if_repo_is_not_started() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("not started yet");
-
- underTest.getPluginInstance("foo");
+ assertThatThrownBy(() -> underTest.getPluginInstance("foo"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("not started yet");
}
@Test
public void getPluginInfo_throws_ISE_if_repo_is_not_started() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("not started yet");
-
- underTest.getPluginInfo("foo");
+ assertThatThrownBy(() -> underTest.getPluginInfo("foo"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("not started yet");
}
@Test
public void hasPlugin_throws_ISE_if_repo_is_not_started() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("not started yet");
-
- underTest.hasPlugin("foo");
+ assertThatThrownBy(() -> underTest.hasPlugin("foo"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("not started yet");
}
@Test
public void getPluginInfos_throws_ISE_if_repo_is_not_started() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("not started yet");
-
- underTest.getPluginInfos();
+ assertThatThrownBy(() -> underTest.getPluginInfos())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("not started yet");
}
- private static class DumbPluginLoader extends PluginLoader {
+ private static class DumbPluginClassLoader extends PluginClassLoader {
- public DumbPluginLoader() {
- super(null, null);
+ public DumbPluginClassLoader() {
+ super(null);
}
/**
* Does nothing except returning the specified list of plugins
*/
@Override
- public Map<String, Plugin> load(Map<String, PluginInfo> infoByKeys) {
+ public Map<String, Plugin> load(Map<String, ExplodedPlugin> infoByKeys) {
Map<String, Plugin> result = new HashMap<>();
for (String pluginKey : infoByKeys.keySet()) {
result.put(pluginKey, mock(Plugin.class));