From b8473122de07c37f87643bf0347d5dde07da3569 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 5 Mar 2013 08:08:52 +1100 Subject: [PATCH] 399967 call Destroyable.destroy() when a managed been is removed from ContainerLifeCycle --- .../org/eclipse/jetty/jndi/DataSourceCloser.java | 1 + .../jetty/util/component/ContainerLifeCycle.java | 16 +++++++++++++++- .../jetty/util/thread/ShutdownThread.java | 10 +++++++++- .../util/component/ContainerLifeCycleTest.java | 6 +++--- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java index dc39e9cc075..61dae6f3cd3 100644 --- a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java +++ b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java @@ -58,6 +58,7 @@ public class DataSourceCloser implements Destroyable _shutdown=shutdownSQL; } + @Override public void destroy() { try 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 f7e46939d8c..70b1b38a60f 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 @@ -139,7 +139,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, Collections.reverse(reverse); for (Bean b : reverse) { - if (b._bean instanceof Destroyable && b._managed==Managed.MANAGED) + if (b._bean instanceof Destroyable && (b._managed==Managed.MANAGED || b._managed==Managed.POJO)) { Destroyable d = (Destroyable)b._bean; d.destroy(); @@ -447,6 +447,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, { if (_beans.remove(bean)) { + boolean managed=bean.isManaged(); unmanage(bean); for (Container.Listener l:_listeners) @@ -467,6 +468,19 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, } } } + + if (managed && bean._bean instanceof Destroyable) + { + try + { + ((Destroyable)bean._bean).destroy(); + } + catch(Exception e) + { + LOG.warn(e); + } + } + return true; } return false; diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ShutdownThread.java b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ShutdownThread.java index 427f9514752..b5e46810c98 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ShutdownThread.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ShutdownThread.java @@ -18,10 +18,12 @@ package org.eclipse.jetty.util.thread; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import org.eclipse.jetty.util.component.Destroyable; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -79,7 +81,7 @@ public class ShutdownThread extends Thread catch(Exception e) { LOG.ignore(e); - LOG.info("shutdown already commenced"); + LOG.debug("shutdown already commenced"); } } @@ -131,6 +133,12 @@ public class ShutdownThread extends Thread lifeCycle.stop(); LOG.debug("Stopped {}",lifeCycle); } + + if (lifeCycle instanceof Destroyable) + { + ((Destroyable)lifeCycle).destroy(); + LOG.debug("Destroyed {}",lifeCycle); + } } catch (Exception ex) { 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 6ce4a260065..b527a5f1d14 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 @@ -117,17 +117,17 @@ public class ContainerLifeCycleTest a0.destroy(); Assert.assertEquals(3,started.get()); Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(1,destroyed.get()); + Assert.assertEquals(2,destroyed.get()); a1.stop(); Assert.assertEquals(3,started.get()); Assert.assertEquals(3,stopped.get()); - Assert.assertEquals(1,destroyed.get()); + Assert.assertEquals(2,destroyed.get()); a1.destroy(); Assert.assertEquals(3,started.get()); Assert.assertEquals(3,stopped.get()); - Assert.assertEquals(2,destroyed.get()); + Assert.assertEquals(3,destroyed.get()); }