417225 added Container.addEventListener method
This commit is contained in:
parent
9113767907
commit
9fef8215ed
|
@ -22,6 +22,12 @@ import java.util.Collection;
|
||||||
|
|
||||||
public interface Container
|
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);
|
public boolean addBean(Object o);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,11 +51,26 @@ public interface Container
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the given bean.
|
* 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
|
* @return whether the bean was removed
|
||||||
* @see #removeBeans()
|
* @see #removeBeans()
|
||||||
*/
|
*/
|
||||||
public boolean removeBean(Object o);
|
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.
|
* A listener for Container events.
|
||||||
* If an added bean implements this interface it will receive the events
|
* If an added bean implements this interface it will receive the events
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.annotation.ManagedObject;
|
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||||
import org.eclipse.jetty.util.annotation.ManagedOperation;
|
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.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
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 the bean is a Listener
|
||||||
if (o instanceof Container.Listener)
|
if (o instanceof Container.Listener)
|
||||||
{
|
addEventListener((Container.Listener)o);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the bean
|
// Add the bean
|
||||||
_beans.add(new_bean);
|
_beans.add(new_bean);
|
||||||
|
@ -325,6 +308,31 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
* Manages a bean already contained by this aggregate, so that it is started/stopped/destroyed with this
|
||||||
* aggregate.
|
* aggregate.
|
||||||
|
@ -479,20 +487,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
|
||||||
l.beanRemoved(this,bean._bean);
|
l.beanRemoved(this,bean._bean);
|
||||||
|
|
||||||
if (bean._bean instanceof Container.Listener)
|
if (bean._bean instanceof Container.Listener)
|
||||||
{
|
removeEventListener((Container.Listener)bean._bean);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// stop managed beans
|
// stop managed beans
|
||||||
if (bean._managed==Managed.MANAGED && bean._bean instanceof LifeCycle)
|
if (bean._managed==Managed.MANAGED && bean._bean instanceof LifeCycle)
|
||||||
|
@ -515,7 +510,21 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
|
||||||
return false;
|
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
|
@Override
|
||||||
public void setStopTimeout(long stopTimeout)
|
public void setStopTimeout(long stopTimeout)
|
||||||
|
|
Loading…
Reference in New Issue