aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-01-15 11:10:10 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-01-20 13:27:34 +0100
commit275dafcd9f4911c3d71d3dc7b86d68e62f06f86a (patch)
tree6058df1e78cdeea8a86ba408d845f98d57ed9427 /sonar-plugin-api/src
parent9030b566aa37558726e14138c292326f93699f8f (diff)
downloadsonarqube-275dafcd9f4911c3d71d3dc7b86d68e62f06f86a.tar.gz
sonarqube-275dafcd9f4911c3d71d3dc7b86d68e62f06f86a.zip
SONAR-6056 API: closeable components must be closed when stopping picocontainer
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java4
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java54
2 files changed, 56 insertions, 2 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 daf6a1e12d7..d92592678eb 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,7 +32,6 @@ import org.sonar.api.ServerComponent;
import org.sonar.api.config.PropertyDefinitions;
import javax.annotation.Nullable;
-
import java.util.Collection;
import java.util.List;
@@ -122,6 +121,7 @@ public class ComponentContainer implements BatchComponent, ServerComponent {
public ComponentContainer stopComponents(boolean swallowException) {
try {
pico.stop();
+ pico.dispose();
} catch (RuntimeException e) {
if (!swallowException) {
@@ -230,7 +230,7 @@ public class ComponentContainer implements BatchComponent, ServerComponent {
}
static MutablePicoContainer createPicoContainer() {
- ReflectionLifecycleStrategy lifecycleStrategy = new ReflectionLifecycleStrategy(new NullComponentMonitor(), "start", "stop", "dispose");
+ ReflectionLifecycleStrategy lifecycleStrategy = new ReflectionLifecycleStrategy(new NullComponentMonitor(), "start", "stop", "close");
return new DefaultPicoContainer(new OptInCaching(), lifecycleStrategy, null);
}
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 4135639a877..44660bf61c7 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
@@ -288,6 +288,37 @@ public class ComponentContainerTest {
assertThat(component.stopped).isTrue();
}
+ /**
+ * Method close() must be called even if the methods start() or stop()
+ * are not defined.
+ */
+ @Test
+ public void should_close_components_without_lifecycle() throws Exception {
+ ComponentContainer container = new ComponentContainer();
+ CloseableComponent component = new CloseableComponent();
+ container.add(component);
+
+ container.execute();
+
+ assertThat(component.isClosed).isTrue();
+ }
+
+ /**
+ * Method close() must be executed after stop()
+ */
+ @Test
+ public void should_close_components_with_lifecycle() throws Exception {
+ ComponentContainer container = new ComponentContainer();
+ StartableCloseableComponent component = new StartableCloseableComponent();
+ container.add(component);
+
+ container.execute();
+
+ assertThat(component.isStopped).isTrue();
+ assertThat(component.isClosed).isTrue();
+ assertThat(component.isClosedAfterStop).isTrue();
+ }
+
public static class StartableComponent {
public boolean started = false, stopped = false;
@@ -333,4 +364,27 @@ public class ComponentContainerTest {
return new SimpleComponent();
}
}
+
+ public static class CloseableComponent implements AutoCloseable {
+ public boolean isClosed = false;
+
+ @Override
+ public void close() throws Exception {
+ isClosed = true;
+ }
+ }
+
+ public static class StartableCloseableComponent implements AutoCloseable {
+ public boolean isClosed = false, isStopped = false, isClosedAfterStop = false;
+
+ public void stop() {
+ isStopped = true;
+ }
+
+ @Override
+ public void close() throws Exception {
+ isClosed = true;
+ isClosedAfterStop = isStopped;
+ }
+ }
}