public final class Container
extends java.lang.Object
Container
is a container in the sense of Inversion of Control (IoC).
Its primary responsibility is to govern the life-cycle of a modular application. It can be started and it can be stopped.
The order in which Component
s are started and stopped, is the responsibility of the ComponentLoader
s that loads them. Stopping will be done in the reverse order of the order they were loaded.
The responsibility of configuring Component
s are handled by one or more ComponentConfigurator
s. One such configuration could be Dependency Injection (DI), which is not the responsibility of any given
Component
.
Another useful feature of a Container
is filtering. To filter out which Component
s are eligible to start, use one or more ComponentFilter
s.
To get started using this API, consider the following example:
Container container = new Container();
container.addComponentConfigurator(new MyComponentConfigurator());
container.addComponentFilter(new MyComponentFilter());
container.addComponentLoader(new MyComponentLoader());
container.addContainerObserver(new MyContainerObserver());
container.start();
This class is not thread-safe.
Constructor and Description |
---|
Container()
Constructs a new empty
Container instance. |
Modifier and Type | Method and Description |
---|---|
boolean |
addComponentConfigurator(ComponentConfigurator componentConfigurator)
Adds
componentConfigurator , if not present. |
boolean |
addComponentFilter(ComponentFilter componentFilter)
Adds
componentFilter , if not present. |
boolean |
addComponentLoader(ComponentLoader componentLoader)
Adds
componentLoader , if not present. |
boolean |
addContainerObserver(ContainerObserver containerObserver)
Adds
containerObserver , if not present. |
boolean |
isRunning()
Returns
true if, and only if, this Container instance is currently running, false otherwise. |
boolean |
removeComponentConfigurator(ComponentConfigurator componentConfigurator)
Removes
componentConfigurator , if present. |
boolean |
removeComponentFilter(ComponentFilter componentFilter)
Removes
componentFilter , if present. |
boolean |
removeComponentLoader(ComponentLoader componentLoader)
Removes
componentLoader , if present. |
boolean |
removeContainerObserver(ContainerObserver containerObserver)
Removes
containerObserver , if present. |
boolean |
start()
Starts this
Container instance, if not already running. |
boolean |
stop()
Stops this
Container instance, if it's currently running. |
public Container()
Container
instance.
This new Container
instance will not be started automatically.
public boolean addComponentConfigurator(ComponentConfigurator componentConfigurator)
componentConfigurator
, if not present.
Returns true
if, and only if, componentConfigurator
was successfully added, false
otherwise.
If componentConfigurator
is null
, a NullPointerException
will be thrown.
componentConfigurator
- the ComponentConfigurator
to addtrue
if, and only if, componentConfigurator
was successfully added, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, componentConfigurator
is null
public boolean addComponentFilter(ComponentFilter componentFilter)
componentFilter
, if not present.
Returns true
if, and only if, componentFilter
was successfully added, false
otherwise.
If componentFilter
is null
, a NullPointerException
will be thrown.
componentFilter
- the ComponentFilter
to addtrue
if, and only if, componentFilter
was successfully added, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, componentFilter
is null
public boolean addComponentLoader(ComponentLoader componentLoader)
componentLoader
, if not present.
Returns true
if, and only if, componentLoader
was successfully added, false
otherwise.
If componentLoader
is null
, a NullPointerException
will be thrown.
componentLoader
- the ComponentLoader
to addtrue
if, and only if, componentLoader
was successfully added, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, componentLoader
is null
public boolean addContainerObserver(ContainerObserver containerObserver)
containerObserver
, if not present.
Returns true
if, and only if, containerObserver
was successfully added, false
otherwise.
If containerObserver
is null
, a NullPointerException
will be thrown.
containerObserver
- the ContainerObserver
to addtrue
if, and only if, containerObserver
was successfully added, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, containerObserver
is null
public boolean isRunning()
true
if, and only if, this Container
instance is currently running, false
otherwise.true
if, and only if, this Container
instance is currently running, false
otherwisepublic boolean removeComponentConfigurator(ComponentConfigurator componentConfigurator)
componentConfigurator
, if present.
Returns true
if, and only if, componentConfigurator
was successfully removed, false
otherwise.
If componentConfigurator
is null
, a NullPointerException
will be thrown.
componentConfigurator
- the ComponentConfigurator
to removetrue
if, and only if, componentConfigurator
was successfully removed, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, componentConfigurator
is null
public boolean removeComponentFilter(ComponentFilter componentFilter)
componentFilter
, if present.
Returns true
if, and only if, componentFilter
was successfully removed, false
otherwise.
If componentFilter
is null
, a NullPointerException
will be thrown.
componentFilter
- the ComponentFilter
to removetrue
if, and only if, componentFilter
was successfully removed, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, componentFilter
is null
public boolean removeComponentLoader(ComponentLoader componentLoader)
componentLoader
, if present.
Returns true
if, and only if, componentLoader
was successfully removed, false
otherwise.
If componentLoader
is null
, a NullPointerException
will be thrown.
componentLoader
- the ComponentLoader
to removetrue
if, and only if, componentLoader
was successfully removed, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, componentLoader
is null
public boolean removeContainerObserver(ContainerObserver containerObserver)
containerObserver
, if present.
Returns true
if, and only if, containerObserver
was successfully removed, false
otherwise.
If containerObserver
is null
, a NullPointerException
will be thrown.
containerObserver
- the ContainerObserver
to removetrue
if, and only if, containerObserver
was successfully removed, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, containerObserver
is null
public boolean start()
Container
instance, if not already running.
If this Container
instance is successfully started, all currently added ComponentLoader
s will be informed to load their Component
s.
When a Component
has been loaded, passed filtering and, optionally, been configured, it will be started.
The order in which a Component
is started, is first and foremost dependent on the order it was loaded. But also the filtering and configuration operations that are performed in sequence play a crucial role.
All currently added ContainerObserver
s will be notified of all Component
s that are successfully started.
If an Exception
is caught while loading, filtering, configuring or starting Component
s, all currently added ContainerObserver
s will be notified of this.
Returns true
if, and only if, this Container
instance was successfully started, false
otherwise.
true
if, and only if, this Container
instance was successfully started, false
otherwisepublic boolean stop()
Container
instance, if it's currently running.
If this Container
instance is successfully stopped, all currently loaded Component
s will be stopped in the reverse order of the order they were loaded.
If an Exception
is caught while stopping a Component
, all currently added ContainerObserver
s will be notified of this.
Returns true
if, and only if, this Container
instance was successfully stopped, false
otherwise.
true
if, and only if, this Container
instance was successfully stopped, false
otherwise