Fixes #854 - If container.destroy() is called, calling container.start() again should throw an IllegalStateException.

This commit is contained in:
Simone Bordet 2016-08-18 15:46:20 +02:00
parent 7a7d5e2f1e
commit 10f994a0a2
2 changed files with 17 additions and 0 deletions

View File

@ -78,6 +78,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
private final List<Bean> _beans = new CopyOnWriteArrayList<>();
private final List<Container.Listener> _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<Bean> reverse = new ArrayList<>(_beans);
Collections.reverse(reverse);
for (Bean b : reverse)

View File

@ -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
{