@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.
AbstractLifeCycle.AbstractLifeCycleListener
Container.InheritedListener, Container.Listener
LifeCycle.Listener
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,
org.eclipse.jetty.util.component.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)
Dump this object to an Appendable with no indent.
|
void |
dump(java.lang.Appendable out,
java.lang.String indent)
Dump this object (and children) into an Appendable using the provided indent after any new lines.
|
static void |
dump(java.lang.Appendable out,
java.lang.String indent,
java.util.Collection<?>... collections)
Deprecated.
|
static java.lang.String |
dump(Dumpable dumpable)
Deprecated.
use
Dumpable.dump(Dumpable) instead |
protected void |
dumpBeans(java.lang.Appendable out,
java.lang.String indent,
java.util.Collection<?>... items)
Deprecated.
|
static void |
dumpObject(java.lang.Appendable out,
java.lang.Object obj)
Deprecated.
use
Dumpable.dumpObject(Appendable, Object) instead |
protected void |
dumpObjects(java.lang.Appendable out,
java.lang.String indent,
java.lang.Object... items)
Dump this object, it's contained beans and additional items to an Appendable
|
void |
dumpStdErr()
Dumps to
System.err . |
protected void |
dumpThis(java.lang.Appendable out)
Deprecated.
|
<T> T |
getBean(java.lang.Class<T> clazz) |
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 |
isAuto(java.lang.Object bean) |
boolean |
isManaged(java.lang.Object bean)
Test if this container manages a bean
|
boolean |
isUnmanaged(java.lang.Object 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.
|
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.
|
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, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
dumpObjects, dumpSelf
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 isAuto(java.lang.Object bean)
bean
- the bean to testpublic boolean isUnmanaged(java.lang.Object bean)
bean
- the bean to testpublic 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, org.eclipse.jetty.util.component.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)
public void unmanage(java.lang.Object 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()
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 removepublic 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()
@Deprecated public static java.lang.String dump(Dumpable dumpable)
Dumpable.dump(Dumpable)
insteadDumpable
Dumpable.dump()
by calling Dumpable.dump(Appendable, String)
public void dump(java.lang.Appendable out, java.lang.String indent) throws java.io.IOException
Dumpable
public void dump(java.lang.Appendable out) throws java.io.IOException
out
- The appendable to dump to.java.io.IOException
- May be thrown by the Appendable@Deprecated protected void dumpThis(java.lang.Appendable out) throws java.io.IOException
dump(Appendable, String)
out
- The appendable to dump tojava.io.IOException
- May be thrown by the Appendable@Deprecated public static void dumpObject(java.lang.Appendable out, java.lang.Object obj) throws java.io.IOException
Dumpable.dumpObject(Appendable, Object)
insteadDumpable
dumpObject
in interface Dumpable
out
- The Appendable to dump toobj
- The object to dumpjava.io.IOException
- May be thrown by the Appendableprotected void dumpObjects(java.lang.Appendable out, java.lang.String indent, java.lang.Object... items) throws java.io.IOException
out
- The appendable to dump toindent
- The indent to apply after any new linesitems
- Additional items to be dumped as contained.java.io.IOException
- May be thrown by the Appendable@Deprecated protected void dumpBeans(java.lang.Appendable out, java.lang.String indent, java.util.Collection<?>... items) throws java.io.IOException
dumpObjects(Appendable, String, Object...)
out
- The appendable to dump toindent
- The indent to apply after any new linesitems
- Additional collections to be dumpedjava.io.IOException
- May be thrown by the Appendable@Deprecated 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 hierarchyCopyright © 1995–2023 Webtide. All rights reserved.