Class SettingsPlaceholderService
- All Implemented Interfaces:
PlaceholderService
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe defaultcacheSecondsproperty value.static final intThe defaultdaoRetryCountproperty value.static final StringThe setting key to use for placeholder parameter settings. -
Constructor Summary
ConstructorsConstructorDescriptionSettingsPlaceholderService(net.solarnetwork.service.OptionalService<SettingDao> settingDao) Constructor. -
Method Summary
Modifier and TypeMethodDescription<T> voidCopy placeholders to a map.intGet the cache seconds value.intGet the maximum number of time to retry transient DAO exceptions when loading placeholder values fromSettingDao.Get the path to the static properties directory.org.springframework.core.task.AsyncTaskExecutorGet the task executor.<T> voidmapPlaceholders(Map<String, T> destination, Function<Stream<Map.Entry<String, ?>>, Stream<Map.Entry<String, T>>> filter) Copy placeholders to a map using a stream filter.voidregisterParameters(Map<String, ?> parameters) Register a set of parameters for future use.resolvePlaceholders(String s, Map<String, ?> parameters) Resolve all placeholders.voidsetCacheSeconds(int cacheSeconds) Set the cache seconds value.voidsetDaoRetryCount(int daoRetryCount) Set the number of time to retry transient DAO exceptions when loading placeholder values fromSettingDao.voidsetStaticPropertiesPath(Path staticPropertiesPath) Set a path to a directory of static properties files to load as placeholders.voidsetTaskExecutor(org.springframework.core.task.AsyncTaskExecutor taskExecutor) An executor to handle cache refreshing with.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.solarnetwork.node.service.PlaceholderService
copyPlaceholders, copyPlaceholders, smartCopyPlaceholders, smartCopyPlaceholders, smartCopyPlaceholders
-
Field Details
-
SETTING_KEY
The setting key to use for placeholder parameter settings.- See Also:
-
DEFAULT_CACHE_SECONDS
public static final int DEFAULT_CACHE_SECONDSThe defaultcacheSecondsproperty value.- Since:
- 1.1
- See Also:
-
DEFAULT_DAO_RETRY_COUNT
public static final int DEFAULT_DAO_RETRY_COUNTThe defaultdaoRetryCountproperty value.- Since:
- 2.2
- See Also:
-
-
Constructor Details
-
SettingsPlaceholderService
Constructor.- Parameters:
settingDao- the DAO to persist placeholders with
-
-
Method Details
-
resolvePlaceholders
Description copied from interface:PlaceholderServiceResolve all placeholders.- Specified by:
resolvePlaceholdersin interfacePlaceholderService- Parameters:
s- the string to resolve placeholders inparameters- parameters to use while resolving placeholders, or null- Returns:
- the resolved string, or null if
sis 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:PlaceholderServiceCopy placeholders to a map using a stream filter.This method allows you the most flexibility in copying placeholders to a map. The
filteris 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 todestination. 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:
mapPlaceholdersin interfacePlaceholderService- Type Parameters:
T- the parameters value type; typicallyStringorObject- Parameters:
destination- the map to copy all placeholders tofilter- an optional filter to restrict copying matching placeholders intodestination; the function takes a stream of placeholder entries as input and returns a stream of desired output entries to add todestination
-
copyPlaceholders
Description copied from interface:PlaceholderServiceCopy placeholders to a map.- Specified by:
copyPlaceholdersin interfacePlaceholderService- Type Parameters:
T- the parameters value type; typicallyStringorObject- Parameters:
destination- the map to copy all placeholders tofilter- an optional filter to restrict copying matching placeholders intodestination
-
registerParameters
Description copied from interface:PlaceholderServiceRegister a set of parameters for future use.- Specified by:
registerParametersin interfacePlaceholderService- Parameters:
parameters- the parameters to register
-
getStaticPropertiesPath
Get the path to the static properties directory.- Returns:
- the properties directory
-
setStaticPropertiesPath
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
SettingDaowill 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, or0to 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 fromSettingDao.- 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 fromSettingDao.- Parameters:
daoRetryCount- the maximum number of retry times, or 0 to disable- Since:
- 2.2
-