Class SettingsPlaceholderService

java.lang.Object
net.solarnetwork.node.service.support.SettingsPlaceholderService
All Implemented Interfaces:
PlaceholderService

public class SettingsPlaceholderService extends Object implements PlaceholderService
Implementation of PlaceholderService that manages parameter values via a SettingDao.

The StringUtils.expandTemplateString(String, Map) method is used for parameter resolution, so parameters take the form {name:default} where :default is optional.

This service can also load "static" parameter values from a directory of property files or a single property file. Note that these properties are loaded lazily, the first time resolvePlaceholders(String, Map) is called, and then cached for the life of the instance.

Version:
2.4
Author:
matt
  • Field Details

    • SETTING_KEY

      public static final String SETTING_KEY
      The setting key to use for placeholder parameter settings.
      See Also:
    • DEFAULT_CACHE_SECONDS

      public static final int DEFAULT_CACHE_SECONDS
      The default cacheSeconds property value.
      Since:
      1.1
      See Also:
    • DEFAULT_DAO_RETRY_COUNT

      public static final int DEFAULT_DAO_RETRY_COUNT
      The default daoRetryCount property value.
      Since:
      2.2
      See Also:
  • Constructor Details

    • SettingsPlaceholderService

      public SettingsPlaceholderService(net.solarnetwork.service.OptionalService<SettingDao> settingDao)
      Constructor.
      Parameters:
      settingDao - the DAO to persist placeholders with
  • Method Details

    • resolvePlaceholders

      public String resolvePlaceholders(String s, Map<String,?> parameters)
      Description copied from interface: PlaceholderService
      Resolve all placeholders.
      Specified by:
      resolvePlaceholders in interface PlaceholderService
      Parameters:
      s - the string to resolve placeholders in
      parameters - parameters to use while resolving placeholders, or null
      Returns:
      the resolved string, or null if s is null
    • mapPlaceholders

      public <T> void mapPlaceholders(Map<String,T> destination, Function<Stream<Map.Entry<String,?>>,Stream<Map.Entry<String,T>>> filter)
      Description copied from interface: PlaceholderService
      Copy placeholders to a map using a stream filter.

      This method allows you the most flexibility in copying placeholders to a map. The filter is a function that is passed a stream of placeholder entries and can then filter and/or map that stream to a new stream, whose output entries will be copied to destination. For example, you could copy specific placeholders converted to integer values:

       
       mapPlaceholders(params, s -> {
           return s.filter(e -> {
               return e.getKey().startsWith("max");
           }).map(e -> {
               return new SimpleEntry<>(e.getKey(), Integer.valueOf(e.getValue().toString()));
           });
       });
       
       
      Specified by:
      mapPlaceholders in interface PlaceholderService
      Type Parameters:
      T - the parameters value type; typically String or Object
      Parameters:
      destination - the map to copy all placeholders to
      filter - an optional filter to restrict copying matching placeholders into destination; the function takes a stream of placeholder entries as input and returns a stream of desired output entries to add to destination
    • copyPlaceholders

      public <T> void copyPlaceholders(Map<String,T> destination, Predicate<Map.Entry<String,T>> filter)
      Description copied from interface: PlaceholderService
      Copy placeholders to a map.
      Specified by:
      copyPlaceholders in interface PlaceholderService
      Type Parameters:
      T - the parameters value type; typically String or Object
      Parameters:
      destination - the map to copy all placeholders to
      filter - an optional filter to restrict copying matching placeholders into destination
    • registerParameters

      public void registerParameters(Map<String,?> parameters)
      Description copied from interface: PlaceholderService
      Register a set of parameters for future use.
      Specified by:
      registerParameters in interface PlaceholderService
      Parameters:
      parameters - the parameters to register
    • getStaticPropertiesPath

      public Path getStaticPropertiesPath()
      Get the path to the static properties directory.
      Returns:
      the properties directory
    • setStaticPropertiesPath

      public void setStaticPropertiesPath(Path staticPropertiesPath)
      Set a path to a directory of static properties files to load as placeholders.

      If this path represents a properties file, that file will be loaded as placeholder parameter values. If this path represents a directory, the directory will be searched for property files. All files will be loaded and their values made available as placeholder parameters. A property file must have a .properties file name suffix.

      Parameters:
      staticPropertiesPath - the directory path to set
    • getCacheSeconds

      public int getCacheSeconds()
      Get the cache seconds value.
      Returns:
      the cache seconds; defaults to DEFAULT_CACHE_SECONDS
      Since:
      1.1
    • setCacheSeconds

      public void setCacheSeconds(int cacheSeconds)
      Set the cache seconds value.

      If set to a value greater than 0 then placeholder values read from SettingDao will be cached for a minimum of the given number of seconds before being refreshed. This can be very helpful when placeholders are evaluated frequently. Additionally, once cached if refreshing from the DAO fails for any reason, the previously cached values will be used.

      Parameters:
      cacheSeconds - the cache seconds to set, or 0 to disable
      Since:
      1.1
    • getTaskExecutor

      public org.springframework.core.task.AsyncTaskExecutor getTaskExecutor()
      Get the task executor.
      Returns:
      the task executor
      Since:
      1.1
    • setTaskExecutor

      public void setTaskExecutor(org.springframework.core.task.AsyncTaskExecutor taskExecutor)
      An executor to handle cache refreshing with.

      If configured then cache refresh operations will occur asynchronously after expiring.

      Parameters:
      taskExecutor - a task executor
      Since:
      1.1
    • getDaoRetryCount

      public int getDaoRetryCount()
      Get the maximum number of time to retry transient DAO exceptions when loading placeholder values from SettingDao.
      Returns:
      the maximum number of retry times
      Since:
      2.2
    • setDaoRetryCount

      public void setDaoRetryCount(int daoRetryCount)
      Set the number of time to retry transient DAO exceptions when loading placeholder values from SettingDao.
      Parameters:
      daoRetryCount - the maximum number of retry times, or 0 to disable
      Since:
      2.2