diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java index bb511b204f5..d7e7f16f872 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java @@ -78,6 +78,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, private final List _beans = new CopyOnWriteArrayList<>(); private final List _listeners = new CopyOnWriteArrayList<>(); private boolean _doStarted; + private boolean _destroyed; /** * Starts the managed lifecycle beans in the order they were added. @@ -85,6 +86,9 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, @Override protected void doStart() throws Exception { + if (_destroyed) + throw new IllegalStateException("Destroyed container cannot be restarted"); + // indicate that we are started, so that addBean will start other beans added. _doStarted = true; @@ -164,6 +168,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, @Override public void destroy() { + _destroyed = true; List reverse = new ArrayList<>(_beans); Collections.reverse(reverse); for (Bean b : reverse) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java index f1010ab6e64..f470b7a7b2e 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java @@ -139,6 +139,18 @@ public class ContainerLifeCycleTest Assert.assertEquals(2,destroyed.get()); } + @Test(expected = IllegalStateException.class) + public void testIllegalToStartAfterDestroy() throws Exception + { + ContainerLifeCycle container = new ContainerLifeCycle(); + container.start(); + container.stop(); + container.destroy(); + + // Should throw IllegalStateException. + container.start(); + } + @Test public void testDisJoint() throws Exception {