Package org.pipservices3.commons.refer
Class DependencyResolver
java.lang.Object
org.pipservices3.commons.refer.DependencyResolver
- All Implemented Interfaces:
IConfigurable
,IReconfigurable
,IReferenceable
Helper class for resolving component dependencies.
The resolver is configured to resolve named dependencies by specific locator. During deployment the dependency locator can be changed.
This mechanism can be used to clarify specific dependency among several alternatives. Typically components are configured to retrieve the first dependency that matches logical group, type and version. But if container contains more than one instance and resolution has to be specific about those instances, they can be given a unique name and dependency resolvers can be reconfigured to retrieve dependencies by their name.
Configuration parameters:
- dependencies:
- [dependency name 1]: [dependency 1 locator (descriptor)]
- ...
- [dependency name N]: [dependency N locator (descriptor)]
- References:
- [references that match configured dependencies]
### Example ###
class MyComponent implements IConfigurable, IReferenceable {
private DependencyResolver _dependencyResolver = new DependencyResolver();
private IMyPersistence _persistence;
...
public MyComponent() {
this._dependencyResolver.put("persistence", new Descriptor("mygroup", "persistence", "*", "*", "1.0"));
}
public void configure(ConfigParams config) {
this._dependencyResolver.configure(config);
}
public void setReferences(IReferences references) {
this._dependencyResolver.setReferences(references);
this._persistence = (IMyPersistence)this._dependencyResolver.getOneRequired("persistence");
}
}
// Create mycomponent and set specific dependency out of many
MyComponent component = new MyComponent();
component.configure(ConfigParams.fromTuples(
"dependencies.persistence", "mygroup:persistence:*:persistence2:1.0" // Override default persistence dependency
));
component.setReferences(References.fromTuples(
new Descriptor("mygroup","persistence","*","persistence1","1.0"), new MyPersistence(),
new Descriptor("mygroup","persistence","*","persistence2","1.0"), new MyPersistence() // This dependency shall be set
));
- See Also:
-
Constructor Summary
ConstructorDescriptionCreates a new instance of the dependency resolver.DependencyResolver
(ConfigParams config) Creates a new instance of the dependency resolver. -
Method Summary
Modifier and TypeMethodDescriptionvoid
configure
(ConfigParams config) Configures the component with specified parameters.<T> List<T>
Finds all matching dependencies by their name and matching to the specified type.Finds all matching dependencies by their name.static DependencyResolver
fromTuples
(Object... tuples) Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator (descriptor).static DependencyResolver
fromTuples
(List<Object>... tuples) Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator (descriptor).<T> T
getOneOptional
(Class<T> type, String name) Gets one optional dependency by its name and matching to the specified type.getOneOptional
(String name) Gets one optional dependency by its name.<T> T
getOneRequired
(Class<T> type, String name) Gets one required dependency by its name and matching to the specified type.getOneRequired
(String name) Gets one required dependency by its name.<T> List<T>
getOptional
(Class<T> type, String name) Gets all optional dependencies by their name.getOptional
(String name) Gets all optional dependencies by their name.<T> List<T>
getRequired
(Class<T> type, String name) Gets all required dependencies by their name.getRequired
(String name) Gets all required dependencies by their name.void
Adds a new dependency into this resolver.void
setReferences
(IReferences references) Sets the component references
-
Constructor Details
-
DependencyResolver
public DependencyResolver()Creates a new instance of the dependency resolver. -
DependencyResolver
Creates a new instance of the dependency resolver.- Parameters:
config
- (optional) default configuration where key is dependency name and value is locator (descriptor)- See Also:
-
-
Method Details
-
configure
Configures the component with specified parameters.- Specified by:
configure
in interfaceIConfigurable
- Parameters:
config
- configuration parameters to set.- Throws:
ConfigException
- when configuration is wrong.- See Also:
-
setReferences
Sets the component references- Specified by:
setReferences
in interfaceIReferenceable
- Parameters:
references
- references to set.- Throws:
ReferenceException
- when no references found.- See Also:
-
put
Adds a new dependency into this resolver.- Parameters:
name
- the dependency's name.locator
- the locator to find the dependency by.
-
getOptional
Gets all optional dependencies by their name.- Parameters:
name
- the dependency name to locate.- Returns:
- a list with found dependencies or empty list of no dependencies was found.
-
getOptional
Gets all optional dependencies by their name.- Parameters:
type
- the Class type that defined the type of the result.name
- the dependency name to locate.- Returns:
- a list with found dependencies or empty list of no dependencies was found.
-
getRequired
Gets all required dependencies by their name. At least one dependency must present. If no dependencies was found it throws a ReferenceException- Parameters:
name
- the dependency name to locate.- Returns:
- a list with found dependencies.
- Throws:
ReferenceException
- when no single component reference is found
-
getRequired
Gets all required dependencies by their name. At least one dependency must present. If no dependencies was found it throws a ReferenceException- Parameters:
type
- the Class type that defined the type of the result.name
- the dependency name to locate.- Returns:
- a list with found dependencies.
- Throws:
ReferenceException
- when no single component reference is found
-
getOneOptional
Gets one optional dependency by its name.- Parameters:
name
- the dependency name to locate.- Returns:
- a dependency reference or null of the dependency was not found
-
getOneOptional
Gets one optional dependency by its name and matching to the specified type.- Parameters:
type
- the Class type that defined the type of the result.name
- the dependency name to locate.- Returns:
- a dependency reference or null of the dependency was not found
-
getOneRequired
Gets one required dependency by its name. At least one dependency must present. If the dependency was found it throws a ReferenceException- Parameters:
name
- the dependency name to locate.- Returns:
- a dependency reference
- Throws:
ReferenceException
- if dependency was not found.
-
getOneRequired
Gets one required dependency by its name and matching to the specified type. At least one dependency must present. If the dependency was found it throws a ReferenceException- Parameters:
type
- the Class type that defined the type of the result.name
- the dependency name to locate.- Returns:
- a dependency reference
- Throws:
ReferenceException
- if dependency was not found.
-
find
Finds all matching dependencies by their name.- Parameters:
name
- the dependency name to locate.required
- true to raise an exception when no dependencies are found.- Returns:
- a list of found dependencies
- Throws:
ReferenceException
- of required is true and no dependencies found.
-
find
Finds all matching dependencies by their name and matching to the specified type.- Parameters:
type
- the Class type that defined the type of the result.name
- the dependency name to locate.required
- true to raise an exception when no dependencies are found.- Returns:
- a list of found dependencies
- Throws:
ReferenceException
- of required is true and no dependencies found.
-
fromTuples
Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator (descriptor).- Parameters:
tuples
- a list of values where odd elements are dependency name and the following even elements are dependency locator (descriptor)- Returns:
- a newly created DependencyResolver.
-
fromTuples
Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator (descriptor).- Parameters:
tuples
- a list of values where odd elements are dependency name and the following even elements are dependency locator (descriptor)- Returns:
- a newly created DependencyResolver.
-