From 7a7d5e2f1e8e54042cff914d77a8a6c4cce8ab97 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 18 Aug 2016 15:23:53 +0200 Subject: [PATCH] Code cleanup. --- .../util/component/ContainerLifeCycle.java | 56 ++++++++----------- .../component/ContainerLifeCycleTest.java | 20 +------ 2 files changed, 25 insertions(+), 51 deletions(-) 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 f24d22a36b3..bb511b204f5 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 @@ -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. *

- * 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. *

- * 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: *

- * 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. *

* 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. *

- * 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. *

- * 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: *

  * SomeContainerLifeCycleInstance
  *   +- contained POJO instance
@@ -67,22 +71,13 @@ import org.eclipse.jetty.util.log.Logger;
  *   +? referenced AUTO object that could become MANAGED or UNMANAGED.
  * 
*/ - -/* ------------------------------------------------------------ */ -/** - */ @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 _beans = new CopyOnWriteArrayList<>(); private final List _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. *

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 { 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 6d2ff1a0a87..f1010ab6e64 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 @@ -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; } - - - }