]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5837 Log offending component when there is an error at startup
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 5 Nov 2014 13:31:59 +0000 (14:31 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 5 Nov 2014 13:31:59 +0000 (14:31 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java
sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java

index ac26bc0052122c06832104d0b24ea0fe4bf900bd..daf6a1e12d7556c373ac1a2b5798dcffc7e82c42 100644 (file)
@@ -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() : "");
   }
index d98040ecf6000667029a4d5eb69056dd2211a896..1eb849b35e73fa0dd8dda89a1e4c3625f3a96cc9 100644 (file)
@@ -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();