From 5f44c1eacebe6d57d7bac2fbf80d69841698d7b0 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 5 Nov 2014 14:31:59 +0100 Subject: [PATCH] SONAR-5837 Log offending component when there is an error at startup --- .../sonar/api/platform/ComponentContainer.java | 15 +++++++++++++-- .../api/platform/ComponentContainerTest.java | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java index ac26bc00521..daf6a1e12d7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java @@ -32,6 +32,7 @@ import org.sonar.api.ServerComponent; import org.sonar.api.config.PropertyDefinitions; import javax.annotation.Nullable; + import java.util.Collection; import java.util.List; @@ -122,7 +123,6 @@ public class ComponentContainer implements BatchComponent, ServerComponent { try { pico.stop(); - } catch (RuntimeException e) { if (!swallowException) { throw PicoUtils.propagate(e); @@ -180,11 +180,22 @@ public class ComponentContainer implements BatchComponent, ServerComponent { public ComponentContainer addExtension(@Nullable PluginMetadata plugin, Object extension) { Object key = componentKeys.of(extension); - pico.as(Characteristics.CACHE).addComponent(key, extension); + try { + pico.as(Characteristics.CACHE).addComponent(key, extension); + } catch (Throwable t) { + throw new IllegalStateException("Unable to register extension " + getName(extension), t); + } declareExtension(plugin, extension); return this; } + private String getName(Object extension) { + if (extension instanceof Class) { + return ((Class) extension).getName(); + } + return getName(extension.getClass()); + } + public void declareExtension(@Nullable PluginMetadata plugin, Object extension) { propertyDefinitions.addComponent(extension, plugin != null ? plugin.getName() : ""); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java index d98040ecf60..1eb849b35e7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java @@ -31,7 +31,9 @@ import java.util.Arrays; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; public class ComponentContainerTest { @@ -218,6 +220,20 @@ public class ComponentContainerTest { container.startComponents(); } + @Test + public void display_plugin_name_when_failing_to_add_extension() { + ComponentContainer container = new ComponentContainer(); + PluginMetadata plugin = mock(PluginMetadata.class); + + container.startComponents(); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Unable to register extension org.sonar.api.platform.ComponentContainerTest$UnstartableComponent"); + + container.addExtension(plugin, UnstartableComponent.class); + + } + @Test public void test_start_failure() { ComponentContainer container = new ComponentContainer(); -- 2.39.5