Class DependencyResolver

java.lang.Object
org.pipservices3.commons.refer.DependencyResolver
All Implemented Interfaces:
IConfigurable, IReconfigurable, IReferenceable

public class DependencyResolver extends Object implements IReferenceable, IReconfigurable
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 Details

    • DependencyResolver

      public DependencyResolver()
      Creates a new instance of the dependency resolver.
    • DependencyResolver

      public DependencyResolver(ConfigParams config)
      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

      public void configure(ConfigParams config) throws ConfigException
      Configures the component with specified parameters.
      Specified by:
      configure in interface IConfigurable
      Parameters:
      config - configuration parameters to set.
      Throws:
      ConfigException - when configuration is wrong.
      See Also:
    • setReferences

      public void setReferences(IReferences references) throws ReferenceException
      Sets the component references
      Specified by:
      setReferences in interface IReferenceable
      Parameters:
      references - references to set.
      Throws:
      ReferenceException - when no references found.
      See Also:
    • put

      public void put(String name, Object locator)
      Adds a new dependency into this resolver.
      Parameters:
      name - the dependency's name.
      locator - the locator to find the dependency by.
    • getOptional

      public List<Object> getOptional(String name)
      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

      public <T> List<T> getOptional(Class<T> type, String name)
      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

      public List<Object> getRequired(String name) throws ReferenceException
      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

      public <T> List<T> getRequired(Class<T> type, String name) throws ReferenceException
      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

      public Object getOneOptional(String name)
      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

      public <T> T getOneOptional(Class<T> type, String name)
      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

      public Object getOneRequired(String name) throws ReferenceException
      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

      public <T> T getOneRequired(Class<T> type, String name) throws ReferenceException
      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

      public List<Object> find(String name, boolean required) throws ReferenceException
      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

      public <T> List<T> find(Class<T> type, String name, boolean required) throws ReferenceException
      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

      public 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).
      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

      @SafeVarargs public 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).
      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.