Code cleanup.

This commit is contained in:
Simone Bordet 2016-08-18 15:23:53 +02:00
parent bef4b56fc0
commit 7a7d5e2f1e
2 changed files with 25 additions and 51 deletions

View File

@ -31,23 +31,26 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* An ContainerLifeCycle is an {@link LifeCycle} implementation for a collection of contained beans.
* A ContainerLifeCycle is an {@link LifeCycle} implementation for a collection of contained beans.
* <p>
* Beans can be added the ContainerLifeCycle either as managed beans or as unmanaged beans. A managed bean is started, stopped and destroyed with the aggregate.
* An unmanaged bean is associated with the aggregate for the purposes of {@link #dump()}, but it's lifecycle must be managed externally.
* Beans can be added to the ContainerLifeCycle either as managed beans or as unmanaged beans.
* A managed bean is started, stopped and destroyed with the aggregate.
* An unmanaged bean is associated with the aggregate for the purposes of {@link #dump()}, but its
* lifecycle must be managed externally.
* <p>
* When a {@link LifeCycle} bean is added without a managed state being specified the state is determined heuristically:
* When a {@link LifeCycle} bean is added without a managed state being specified the state is
* determined heuristically:
* <ul>
* <li>If the added bean is running, it will be added as an unmanaged bean.
* <li>If the added bean is !running and the container is !running, it will be added as an AUTO bean (see below).
* <li>If the added bean is !running and the container is starting, it will be added as an managed bean and will be started (this handles the frequent case of
* new beans added during calls to doStart).
* <li>If the added bean is !running and the container is started, it will be added as an unmanaged bean.
* <li>If the added bean is running, it will be added as an unmanaged bean.</li>
* <li>If the added bean is !running and the container is !running, it will be added as an AUTO bean (see below).</li>
* <li>If the added bean is !running and the container is starting, it will be added as a managed bean
* and will be started (this handles the frequent case of new beans added during calls to doStart).</li>
* <li>If the added bean is !running and the container is started, it will be added as an unmanaged bean.</li>
* </ul>
* When the container is started, then all contained managed beans will also be started. Any contained Auto beans
* will be check for their status and if already started will be switched unmanaged beans, else they will be
* started and switched to managed beans. Beans added after a container is started are not started and their state needs to
* be explicitly managed.
* When the container is started, then all contained managed beans will also be started.
* Any contained AUTO beans will be check for their status and if already started will be switched unmanaged beans,
* else they will be started and switched to managed beans.
* Beans added after a container is started are not started and their state needs to be explicitly managed.
* <p>
* When stopping the container, a contained bean will be stopped by this aggregate only if it
* is started by this aggregate.
@ -55,10 +58,11 @@ import org.eclipse.jetty.util.log.Logger;
* The methods {@link #addBean(Object, boolean)}, {@link #manage(Object)} and {@link #unmanage(Object)} can be used to
* explicitly control the life cycle relationship.
* <p>
* If adding a bean that is shared between multiple {@link ContainerLifeCycle} instances, then it should be started before being added, so it is unmanaged, or
* the API must be used to explicitly set it as unmanaged.
* If adding a bean that is shared between multiple {@link ContainerLifeCycle} instances, then it should be started
* before being added, so it is unmanaged, or the API must be used to explicitly set it as unmanaged.
* <p>
* This class also provides utility methods to dump deep structures of objects. It the dump, the following symbols are used to indicate the type of contained object:
* This class also provides utility methods to dump deep structures of objects.
* In the dump, the following symbols are used to indicate the type of contained object:
* <pre>
* SomeContainerLifeCycleInstance
* +- contained POJO instance
@ -67,22 +71,13 @@ import org.eclipse.jetty.util.log.Logger;
* +? referenced AUTO object that could become MANAGED or UNMANAGED.
* </pre>
*/
/* ------------------------------------------------------------ */
/**
*/
@ManagedObject("Implementation of Container and LifeCycle")
public class ContainerLifeCycle extends AbstractLifeCycle implements Container, Destroyable, Dumpable
{
private static final Logger LOG = Log.getLogger(ContainerLifeCycle.class);
private final List<Bean> _beans = new CopyOnWriteArrayList<>();
private final List<Container.Listener> _listeners = new CopyOnWriteArrayList<>();
private boolean _doStarted = false;
public ContainerLifeCycle()
{
}
private boolean _doStarted;
/**
* Starts the managed lifecycle beans in the order they were added.
@ -182,7 +177,6 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
_beans.clear();
}
/**
* @param bean the bean to test
* @return whether this aggregate contains the bean
@ -325,9 +319,8 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
return true;
}
/* ------------------------------------------------------------ */
/** Add a managed lifecycle.
/**
* Adds a managed lifecycle.
* <p>This is a convenience method that uses addBean(lifecycle,true)
* and then ensures that the added bean is started iff this container
* is running. Exception from nested calls to start are caught and
@ -741,8 +734,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
}
}
enum Managed { POJO, MANAGED, UNMANAGED, AUTO };
enum Managed { POJO, MANAGED, UNMANAGED, AUTO }
private static class Bean
{

View File

@ -29,11 +29,8 @@ import org.eclipse.jetty.util.TypeUtil;
import org.junit.Assert;
import org.junit.Test;
public class ContainerLifeCycleTest
{
@Test
public void testStartStopDestroy() throws Exception
{
@ -65,10 +62,8 @@ public class ContainerLifeCycleTest
destroyed.incrementAndGet();
super.destroy();
}
};
a0.addBean(a1);
a0.start();
@ -142,7 +137,6 @@ public class ContainerLifeCycleTest
Assert.assertEquals(3,started.get());
Assert.assertEquals(3,stopped.get());
Assert.assertEquals(2,destroyed.get());
}
@Test
@ -176,7 +170,6 @@ public class ContainerLifeCycleTest
destroyed.incrementAndGet();
super.destroy();
}
};
// Start the a1 bean before adding, makes it auto disjoint
@ -219,7 +212,6 @@ public class ContainerLifeCycleTest
Assert.assertEquals(1,stopped.get());
Assert.assertEquals(0,destroyed.get());
a0.start();
Assert.assertEquals(2,started.get());
Assert.assertEquals(1,stopped.get());
@ -230,7 +222,6 @@ public class ContainerLifeCycleTest
Assert.assertEquals(2,stopped.get());
Assert.assertEquals(0,destroyed.get());
a0.unmanage(a1);
Assert.assertFalse(a0.isManaged(a1));
@ -243,7 +234,6 @@ public class ContainerLifeCycleTest
Assert.assertEquals(2,started.get());
Assert.assertEquals(2,stopped.get());
Assert.assertEquals(1,destroyed.get());
}
@Test
@ -317,7 +307,6 @@ public class ContainerLifeCycleTest
final ContainerLifeCycle a3 = new ContainerLifeCycle();
final ContainerLifeCycle a4 = new ContainerLifeCycle();
ContainerLifeCycle aa = new ContainerLifeCycle()
{
@Override
@ -381,7 +370,6 @@ public class ContainerLifeCycleTest
dump=check(dump," | += org.eclipse.jetty.util.component.Container");
dump=check(dump," +~ org.eclipse.jetty.util.component.ContainerLife");
dump=check(dump,"");
}
@Test
@ -414,8 +402,7 @@ public class ContainerLifeCycleTest
public @Override String toString() {return "listener";}
};
ContainerLifeCycle c0 = new ContainerLifeCycle() { public @Override String toString() {return "c0";}};
ContainerLifeCycle c00 = new ContainerLifeCycle() { public @Override String toString() {return "c00";}};
c0.addBean(c00);
@ -434,7 +421,6 @@ public class ContainerLifeCycleTest
Assert.assertEquals(c0,parent.poll());
Assert.assertEquals(listener,child.poll());
Container.InheritedListener inherited= new Container.InheritedListener()
{
@Override
@ -513,7 +499,6 @@ public class ContainerLifeCycleTest
Assert.assertEquals("removed",operation.poll());
Assert.assertEquals(c0,parent.poll());
Assert.assertEquals(c00,child.poll());
}
private final class InheritedListenerLifeCycle extends AbstractLifeCycle implements Container.InheritedListener
@ -575,7 +560,4 @@ public class ContainerLifeCycleTest
return r;
}
}