Package org.pipservices3.container
Class Container
java.lang.Object
org.pipservices3.container.Container
- All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable
,org.pipservices3.commons.refer.IReferenceable
,org.pipservices3.commons.refer.IUnreferenceable
,org.pipservices3.commons.run.IClosable
,org.pipservices3.commons.run.IOpenable
- Direct Known Subclasses:
ProcessContainer
public class Container
extends Object
implements org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.refer.IReferenceable, org.pipservices3.commons.refer.IUnreferenceable, org.pipservices3.commons.run.IOpenable
Inversion of control (IoC) container that creates components and manages their lifecycle.
The container is driven by configuration, that usually stored in JSON or YAML file. The configuration contains a list of components identified by type or locator, followed by component configuration.
On container start it performs the following actions:
- Creates components using their types or calls registered factories to create components using their locators
- Configures components that implement IConfigurable interface and passes them their configuration parameters
- Sets references to components that implement IReferenceable interface and passes them references of all components in the container
- Opens components that implement IOpenable interface
On container stop actions are performed in reversed order:
- Closes components that implement IClosable interface
- Unsets references in components that implement IUnreferenceable interface
- Destroys components in the container.
The component configuration can be parameterized by dynamic values. That allows specialized containers to inject parameters from command line or from environment variables.
The container automatically creates a ContextInfo component that carries detail information about the container and makes it available for other components.
### Configuration parameters ###
- name: the context (container or process) name
- description: human-readable description of the context
- properties: entire section of additional descriptive properties
- ...
### Example ###
======= config.yml ========
- descriptor: mygroup:mycomponent1:default:default:1.0
param1: 123
param2: ABC
- type: mycomponent2,mypackage
param1: 321
param2: XYZ
============================
Container container = new Container();
container.addFactory(new MyComponentFactory());
ConfigParams parameters = ConfigParams.fromValue(process.env);
container.readConfigFromFile("123", "./config/config.yml", parameters);
container.open("123");
System.out.println("Container is opened");
...
container.close("123");
System.out.println("Container is closed");
- See Also:
-
IConfigurable
IReferenceable
IOpenable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ContainerConfig
protected DefaultContainerFactory
protected org.pipservices3.components.info.ContextInfo
protected org.pipservices3.components.log.ILogger
protected ContainerReferences
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of the container.Container
(ContainerConfig config) Creates a new instance of the container. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addFactory
(org.pipservices3.components.build.IFactory factory) Adds a factory to the container.void
Closes component and frees used resources.void
configure
(org.pipservices3.commons.config.ConfigParams config) Configures component by passing configuration parameters.org.pipservices3.commons.refer.IReferences
boolean
isOpen()
Checks if the component is opened.void
Opens the component.void
readConfigFromFile
(String correlationId, String path, org.pipservices3.commons.config.ConfigParams parameters) Reads container configuration from JSON or YAML file and parameterizes it with given values.void
setConfig
(ContainerConfig value) void
setReferences
(org.pipservices3.commons.refer.IReferences references) Sets references to dependent components.void
Unsets (clears) previously set references to dependent components.
-
Field Details
-
_logger
protected org.pipservices3.components.log.ILogger _logger -
_factories
-
_info
protected org.pipservices3.components.info.ContextInfo _info -
_config
-
_references
-
-
Constructor Details
-
Container
Creates a new instance of the container.- Parameters:
name
- (optional) a container name (accessible via ContextInfo)description
- (optional) a container description (accessible via ContextInfo)
-
Container
Creates a new instance of the container.- Parameters:
config
- container configuration
-
-
Method Details
-
getConfig
-
setConfig
-
getReferences
public org.pipservices3.commons.refer.IReferences getReferences() -
configure
public void configure(org.pipservices3.commons.config.ConfigParams config) throws org.pipservices3.commons.errors.ConfigException Configures component by passing configuration parameters.- Specified by:
configure
in interfaceorg.pipservices3.commons.config.IConfigurable
- Parameters:
config
- configuration parameters to be set.- Throws:
org.pipservices3.commons.errors.ConfigException
- when configuration is wrong.
-
readConfigFromFile
public void readConfigFromFile(String correlationId, String path, org.pipservices3.commons.config.ConfigParams parameters) throws org.pipservices3.commons.errors.ApplicationException Reads container configuration from JSON or YAML file and parameterizes it with given values.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.path
- a path to configuration fileparameters
- values to parameters the configuration or null to skip parameterization.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
setReferences
public void setReferences(org.pipservices3.commons.refer.IReferences references) throws org.pipservices3.commons.refer.ReferenceException, org.pipservices3.commons.errors.ConfigException Sets references to dependent components.- Specified by:
setReferences
in interfaceorg.pipservices3.commons.refer.IReferenceable
- Parameters:
references
- references to locate the component dependencies.- Throws:
org.pipservices3.commons.refer.ReferenceException
- when no found references.org.pipservices3.commons.errors.ConfigException
- when configuration is wrong
-
unsetReferences
public void unsetReferences()Unsets (clears) previously set references to dependent components.- Specified by:
unsetReferences
in interfaceorg.pipservices3.commons.refer.IUnreferenceable
-
addFactory
public void addFactory(org.pipservices3.components.build.IFactory factory) Adds a factory to the container. The factory is used to create components added to the container by their locators (descriptors).- Parameters:
factory
- a component factory to be added.
-
isOpen
public boolean isOpen()Checks if the component is opened.- Specified by:
isOpen
in interfaceorg.pipservices3.commons.run.IOpenable
- Returns:
- true if the component has been opened and false otherwise.
-
open
Opens the component.- Specified by:
open
in interfaceorg.pipservices3.commons.run.IOpenable
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
close
Closes component and frees used resources.- Specified by:
close
in interfaceorg.pipservices3.commons.run.IClosable
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-