@ManagedObject(value="Implementation of Container and LifeCycle") public class ContainerLifeCycle extends AbstractLifeCycle implements Container, Destroyable, Dumpable
LifeCycle
implementation for a collection of contained beans.
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 dump()
, but its
lifecycle must be managed externally.
When a LifeCycle
bean is added without a managed state being specified the state is
determined heuristically:
When stopping the container, a contained bean will be stopped by this aggregate only if it is started by this aggregate.
The methods addBean(Object, boolean)
, manage(Object)
and unmanage(Object)
can be used to
explicitly control the life cycle relationship.
If adding a bean that is shared between multiple 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. In the dump, the following symbols are used to indicate the type of contained object:
SomeContainerLifeCycleInstance +- contained POJO instance += contained MANAGED object, started and stopped with this instance +~ referenced UNMANAGED object, with separate lifecycle +? referenced AUTO object that could become MANAGED or UNMANAGED.
Modifier and Type | Class and Description |
---|---|
private static class |
ContainerLifeCycle.Bean |
(package private) static class |
ContainerLifeCycle.Managed |
AbstractLifeCycle.AbstractLifeCycleListener
Container.InheritedListener, Container.Listener
LifeCycle.Listener
Modifier and Type | Field and Description |
---|---|
private java.util.List<ContainerLifeCycle.Bean> |
_beans |
private boolean |
_destroyed |
private boolean |
_doStarted |
private java.util.List<Container.Listener> |
_listeners |
private static Logger |
LOG |
FAILED, RUNNING, STARTED, STARTING, STOP_ON_FAILURE, STOPPED, STOPPING
Constructor and Description |
---|
ContainerLifeCycle() |
Modifier and Type | Method and Description |
---|---|
boolean |
addBean(java.lang.Object o)
Adds the given bean, detecting whether to manage it or not.
|
boolean |
addBean(java.lang.Object o,
boolean managed)
Adds the given bean, explicitly managing it or not.
|
boolean |
addBean(java.lang.Object o,
ContainerLifeCycle.Managed managed) |
void |
addEventListener(Container.Listener listener)
Add an event listener.
|
void |
addManaged(LifeCycle lifecycle)
Adds a managed lifecycle.
|
boolean |
contains(java.lang.Object bean) |
void |
destroy()
Destroys the managed Destroyable beans in the reverse order they were added.
|
protected void |
doStart()
Starts the managed lifecycle beans in the order they were added.
|
protected void |
doStop()
Stops the managed lifecycle beans in the reverse order they were added.
|
java.lang.String |
dump() |
void |
dump(java.lang.Appendable out) |
void |
dump(java.lang.Appendable out,
java.lang.String indent) |
static void |
dump(java.lang.Appendable out,
java.lang.String indent,
java.util.Collection<?>... collections) |
static java.lang.String |
dump(Dumpable dumpable) |
protected void |
dumpBeans(java.lang.Appendable out,
java.lang.String indent,
java.util.Collection<?>... collections) |
static void |
dumpObject(java.lang.Appendable out,
java.lang.Object o) |
void |
dumpStdErr()
Dumps to
System.err . |
protected void |
dumpThis(java.lang.Appendable out) |
<T> T |
getBean(java.lang.Class<T> clazz) |
private ContainerLifeCycle.Bean |
getBean(java.lang.Object o) |
java.util.Collection<java.lang.Object> |
getBeans() |
<T> java.util.Collection<T> |
getBeans(java.lang.Class<T> clazz) |
<T> java.util.Collection<T> |
getContainedBeans(java.lang.Class<T> clazz) |
protected <T> void |
getContainedBeans(java.lang.Class<T> clazz,
java.util.Collection<T> beans) |
boolean |
isManaged(java.lang.Object bean)
Test if this container manages a bean
|
private void |
manage(ContainerLifeCycle.Bean bean) |
void |
manage(java.lang.Object bean)
Manages a bean already contained by this aggregate, so that it is started/stopped/destroyed with this
aggregate.
|
private boolean |
remove(ContainerLifeCycle.Bean bean) |
boolean |
removeBean(java.lang.Object o)
Removes the given bean.
|
void |
removeBeans()
Removes all bean
|
void |
removeEventListener(Container.Listener listener)
Remove an event listener.
|
void |
setBeans(java.util.Collection<java.lang.Object> beans) |
void |
setStopTimeout(long stopTimeout) |
protected void |
start(LifeCycle l)
Starts the given lifecycle.
|
protected void |
stop(LifeCycle l)
Stops the given lifecycle.
|
private void |
unmanage(ContainerLifeCycle.Bean bean) |
void |
unmanage(java.lang.Object bean)
Unmanages a bean already contained by this aggregate, so that it is not started/stopped/destroyed with this
aggregate.
|
void |
updateBean(java.lang.Object oldBean,
java.lang.Object newBean) |
void |
updateBean(java.lang.Object oldBean,
java.lang.Object newBean,
boolean managed) |
void |
updateBeans(java.lang.Object[] oldBeans,
java.lang.Object[] newBeans) |
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
private static final Logger LOG
private final java.util.List<ContainerLifeCycle.Bean> _beans
private final java.util.List<Container.Listener> _listeners
private boolean _doStarted
private boolean _destroyed
protected void doStart() throws java.lang.Exception
doStart
in class AbstractLifeCycle
java.lang.Exception
protected void start(LifeCycle l) throws java.lang.Exception
l
- the lifecycle to startjava.lang.Exception
- if unable to start lifecycleprotected void stop(LifeCycle l) throws java.lang.Exception
l
- the lifecycle to stopjava.lang.Exception
- if unable to stop the lifecycleprotected void doStop() throws java.lang.Exception
doStop
in class AbstractLifeCycle
java.lang.Exception
public void destroy()
destroy
in interface Destroyable
public boolean contains(java.lang.Object bean)
bean
- the bean to testpublic boolean isManaged(java.lang.Object bean)
Container
public boolean addBean(java.lang.Object o)
LifeCycle
, then it will be managed if it is not
already started and not managed if it is already started.
The addBean(Object, boolean)
method should be used if this is not correct, or the manage(Object)
and unmanage(Object)
methods may be used after an add to change the status.public boolean addBean(java.lang.Object o, boolean managed)
public boolean addBean(java.lang.Object o, ContainerLifeCycle.Managed managed)
public void addManaged(LifeCycle 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 wrapped as RuntimeExceptions
lifecycle
- the managed lifecycle to addpublic void addEventListener(Container.Listener listener)
Container
addEventListener
in interface Container
listener
- the listener to addContainer.addBean(Object)
public void manage(java.lang.Object bean)
private void manage(ContainerLifeCycle.Bean bean)
public void unmanage(java.lang.Object bean)
private void unmanage(ContainerLifeCycle.Bean bean)
public java.util.Collection<java.lang.Object> getBeans()
getBeans
in interface Container
Container.getBean(Class)
public void setBeans(java.util.Collection<java.lang.Object> beans)
public <T> java.util.Collection<T> getBeans(java.lang.Class<T> clazz)
getBeans
in interface Container
T
- the Bean typeclazz
- the class of the beansContainer.getBeans()
,
Container.getContainedBeans(Class)
public <T> T getBean(java.lang.Class<T> clazz)
public void removeBeans()
private ContainerLifeCycle.Bean getBean(java.lang.Object o)
public boolean removeBean(java.lang.Object o)
Container
Container.Listener
, then also do an implicit Container.removeEventListener(Listener)
.removeBean
in interface Container
o
- the bean to removeprivate boolean remove(ContainerLifeCycle.Bean bean)
public void removeEventListener(Container.Listener listener)
Container
removeEventListener
in interface Container
listener
- the listener to removeContainer.removeBean(Object)
public void setStopTimeout(long stopTimeout)
setStopTimeout
in class AbstractLifeCycle
@ManagedOperation(value="Dump the object to stderr") public void dumpStdErr()
System.err
.dump()
@ManagedOperation(value="Dump the object to a string") public java.lang.String dump()
public static java.lang.String dump(Dumpable dumpable)
public void dump(java.lang.Appendable out) throws java.io.IOException
java.io.IOException
protected void dumpThis(java.lang.Appendable out) throws java.io.IOException
java.io.IOException
public static void dumpObject(java.lang.Appendable out, java.lang.Object o) throws java.io.IOException
java.io.IOException
public void dump(java.lang.Appendable out, java.lang.String indent) throws java.io.IOException
protected void dumpBeans(java.lang.Appendable out, java.lang.String indent, java.util.Collection<?>... collections) throws java.io.IOException
java.io.IOException
public static void dump(java.lang.Appendable out, java.lang.String indent, java.util.Collection<?>... collections) throws java.io.IOException
java.io.IOException
public void updateBean(java.lang.Object oldBean, java.lang.Object newBean)
public void updateBean(java.lang.Object oldBean, java.lang.Object newBean, boolean managed)
public void updateBeans(java.lang.Object[] oldBeans, java.lang.Object[] newBeans)
public <T> java.util.Collection<T> getContainedBeans(java.lang.Class<T> clazz)
getContainedBeans
in interface Container
T
- the Bean typeclazz
- the class of the beansprotected <T> void getContainedBeans(java.lang.Class<T> clazz, java.util.Collection<T> beans)
T
- the Bean typeclazz
- the class of the beansbeans
- the collection to add beans of the given class from the entire managed hierarchy