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

View File

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