Class ReferenceResolver

java.lang.Object
org.openhab.core.automation.util.ReferenceResolver

public class ReferenceResolver extends Object
Resolves Module references. They can be
  • Module configuration property to Rule Configuration property
  • Module configuration property to Composite Module configuration property
  • Module inputs to Composite Module inputs
  • Module inputs to Composite Module Configuration
Module 'A' Configuration properties can have references to either CompositeModule Configuration properties or Rule Configuration properties depending where Module 'A' is placed.
Note. If Module 'A' is child of CompositeModule - it cannot have direct configuration references to the Rule that is holding the CompositeModule.
  • Single reference configuration value where whole configuration property value is replaced(if found) with the referenced value
    'configurationProperty': '{{singleReference}}'
  • Complex reference configuration value where only reference parts are replaced in the whole configuration property value.
    'configurationProperty': '{key1: {{complexReference1}}, key2: {{complexReference2}}}'
Given Module 'A' is child of CompositeModule then its inputs can have '${singleReferences}' to CompositeModule.
  • Single reference to CompositeModule inputs where whole input value is replaced with the referenced value
    'childInput' : '{{compositeModuleInput}}'
  • Single reference to CompositeModule configuration where whole input value is replaced with the referenced value
    'childInput' : '{{compositeModuleConfiguration}}'
Author:
Vasil Ilchev - Initial contribution, Ana Dimova - new reference syntax: list[index], map["key"], bean.field
  • Constructor Details

    • ReferenceResolver

      public ReferenceResolver()
  • Method Details

    • updateConfiguration

      public static void updateConfiguration(Configuration config, Map<String,?> context, org.slf4j.Logger logger)
      Updates (changes) configuration properties of module base on given context (it can be CompositeModule Configuration or Rule Configuration). For example: 1) If a module configuration property has a value '{{name}}' the method looks for such key in context and if found - replace the module's configuration value as it is. 2) If a module configuration property has complex value 'Hello {{firstName}} {{lastName}}' the method tries to parse it and replace (if values are found) referenced parts in module's configuration value. Will try to find values for {{firstName}} and {{lastName}} in the given context and replace them. References that are not found in the context - are not replaced.
      Parameters:
      context - containing Rule configuration or Composite configuration values.
    • getCompositeChildContext

      public static Map<String,Object> getCompositeChildContext(Module module, Map<String,?> compositeContext)
      Resolves Composite child module's references to CompositeModule context (inputs and configuration).
      Parameters:
      module - Composite Module's child module.
      compositeContext - Composite Module's context
      Returns:
      context for given module ready for execution.
    • resolveReference

      public static Object resolveReference(String reference, Map<String,?> context)
      Resolves single reference '{{singleReference}}' from given context.
      Parameters:
      reference - single reference expression for resolving
      context - contains the values that will be used for reference resolving
      Returns:
      resolved value.
    • getNextRefToken

      public static int getNextRefToken(String ref, int startIndex)
      Gets the end of current token of reference path.
      Parameters:
      ref - reference path used to access value in bean or map objects
      startIndex - starting point to check for next tokens
      Returns:
      end of current token.
    • splitReferenceToTokens

      public static String[] splitReferenceToTokens(String reference) throws IllegalArgumentException
      Splits a given reference to tokens.
      The reference must have the following syntax: list[index], map["key"], bean.field.
      It is possible to chain references in one bigger expression. For example: list[1].name.values.
      Parameters:
      reference - the reference that should be split
      Returns:
      array of the tokens in the reference
      Throws:
      IllegalArgumentException
    • resolveComplexDataReference

      public static Object resolveComplexDataReference(Object object, String... tokens) throws IllegalArgumentException, SecurityException
      Gets an object by given hierarchical path of tokens.
      Parameters:
      object - bean, map or list
      tokens - a sequence of field names, indexes, or keys that represent the hierarchical path to the required object
      Returns:
      the value of the object to witch the hierarchical path is pointing.
      Throws:
      IllegalArgumentException - if one of the tokens point to field that is not existing or the object is null
      SecurityException - If a security manager, s, is present and any of the following conditions is met:
      • the caller's class loader is not the same as the class loader of this class and invocation of s.checkPermission method with RuntimePermission("accessDeclaredMembers") denies access to the declared field
      • the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
      ArrayIndexOutOfBoundsException - if one of the tokens represent an invalid index in the list.
      NullPointerException - if the path references something in a non existing map entry.
      NumberFormatException - if one of the tokens is accessing a list and the token that represent the index can't be converted to integer.