417225 added Container.addEventListener method

This commit is contained in:
Greg Wilkins 2013-09-16 10:48:51 +10:00
parent 9113767907
commit 9fef8215ed
2 changed files with 63 additions and 33 deletions

View File

@ -22,6 +22,12 @@ import java.util.Collection;
public interface Container
{
/* ------------------------------------------------------------ */
/**
* Add a bean. If the bean is-a {@link Listener}, then also do an implicit {@link #addEventListener(Listener)}.
* @param o the bean object to add
* @return true if the bean was added, false if it was already present
*/
public boolean addBean(Object o);
/**
@ -45,11 +51,26 @@ public interface Container
/**
* Removes the given bean.
* If the bean is-a {@link Listener}, then also do an implicit {@link #removeEventListener(Listener)}.
* @return whether the bean was removed
* @see #removeBeans()
*/
public boolean removeBean(Object o);
/**
* Add an event listener.
* @see Container#addBean(Object), which also adds listeners if the bean is-a Listener
* @param listener
*/
public void addEventListener(Listener listener);
/**
* Remove an event listener.
* @see Container#removeBean(Object), which also adds listeners if the bean is-a Listener
* @param listener
*/
public void removeEventListener(Listener listener);
/**
* A listener for Container events.
* If an added bean implements this interface it will receive the events

View File

@ -27,6 +27,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.ManagedOperation;
import org.eclipse.jetty.util.component.Container.Listener;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -239,25 +240,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
// if the bean is a Listener
if (o instanceof Container.Listener)
{
Container.Listener listener = (Container.Listener)o;
_listeners.add(listener);
// tell it about existing beans
for (Bean b:_beans)
{
listener.beanAdded(this,b._bean);
// handle inheritance
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
{
if (b._bean instanceof ContainerLifeCycle)
((ContainerLifeCycle)b._bean).addBean(listener, false);
else
((Container)b._bean).addBean(listener);
}
}
}
addEventListener((Container.Listener)o);
// Add the bean
_beans.add(new_bean);
@ -324,6 +307,31 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
return true;
}
@Override
public void addEventListener(Container.Listener listener)
{
if (_listeners.contains(listener))
return;
_listeners.add(listener);
// tell it about existing beans
for (Bean b:_beans)
{
listener.beanAdded(this,b._bean);
// handle inheritance
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
{
if (b._bean instanceof ContainerLifeCycle)
((ContainerLifeCycle)b._bean).addBean(listener, false);
else
((Container)b._bean).addBean(listener);
}
}
}
/**
* Manages a bean already contained by this aggregate, so that it is started/stopped/destroyed with this
@ -479,20 +487,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
l.beanRemoved(this,bean._bean);
if (bean._bean instanceof Container.Listener)
{
Container.Listener listener = (Container.Listener)bean._bean;
if (_listeners.remove(listener))
{
// remove existing beans
for (Bean b:_beans)
{
listener.beanRemoved(this,b._bean);
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
((Container)b._bean).removeBean(listener);
}
}
}
removeEventListener((Container.Listener)bean._bean);
// stop managed beans
if (bean._managed==Managed.MANAGED && bean._bean instanceof LifeCycle)
@ -515,7 +510,21 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
return false;
}
@Override
public void removeEventListener(Container.Listener listener)
{
if (_listeners.remove(listener))
{
// remove existing beans
for (Bean b:_beans)
{
listener.beanRemoved(this,b._bean);
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
((Container)b._bean).removeBean(listener);
}
}
}
@Override
public void setStopTimeout(long stopTimeout)