Extension Point: Setting Value Retrieval
Basics
Parameter | Value |
---|---|
Extension Point Key | SETTING_VALUE_RETRIEVAL |
Extension Point Name | Retrieve setting values |
Extension Interface | SettingValueRetrievalStrategy |
Supports Multiple Extensions? | Yes |
Selector Type | None |
Available Since | 1.0.0 |
Use Cases
Extensions implementing this Extension Point can retrieve setting values from a source outside the Elastic Path database. For example, sensitive values could be retrieved from a secrets management service, or environment-specific values could be retrieved from a configuration vault.
Methods
getSettingValue
The getSettingValue
method is invoked whenever Elastic Path Commerce code requests a setting value through SettingValueProvider
. Each SettingValueRetrievalStrategy
is invoked in priority order. The first strategy method that returns a present XPFSettingValue
"wins" and the value is used. If the strategy method returns a non-present result, then the next SettingValueRetrievalStrategy
is executed. If no SettingValueRetrievalStrategy
extensions return a result, then an exception is thrown.
There is an embedded extension named DBSettingValueRetrievalStrategy
with priority 50 that will attempt to read the setting value from the database using the SettingsService
.
note
Settings retrieved through SettingsReader
or SettingsService
read directly from the database and do not invoke this Extension Point.
Extension Sample
@Extension
@XPFAssignment(extensionPoint = XPFExtensionPointEnum.SETTING_VALUE_RETRIEVAL, priority = 100)
public class PropertyFileSettingValueRetrievalStrategy extends XPFExtensionPointImpl implements SettingValueRetrievalStrategy {
private static final String COMMA_DELIMITER = ",";
private static final String PROPERTY_FILE_SETTING_NAME = "PROPERTIES_FILE_LOCATION";
private static final String DEFAULT_PROPERTIES_FILE = "/ep/settings.properties";
private static final int ARRAY_INDEX_VALUE = 0;
private static final int ARRAY_INDEX_DATA_TYPE = 1;
private String propertiesFilePath;
@Override
public void initialize(final XPFExtensionInitializationContext xpfExtensionInitializationContext) {
Optional<XPFPluginSetting> settingOptional =
Optional.ofNullable(xpfExtensionInitializationContext.getSettings().get(PROPERTY_FILE_SETTING_NAME));
propertiesFilePath = settingOptional
.map(XPFPluginSetting::getSettingValues)
.map(xpfPluginSettingValues -> xpfPluginSettingValues.get(0))
.map(XPFPluginSettingValue::getShortTextValue)
.orElse(DEFAULT_PROPERTIES_FILE);
}
@Override
public Optional<XPFSettingValue> getSettingValue(final XPFSettingValueRetrievalContext context) {
Properties settings = new Properties();
try (FileInputStream fis = new FileInputStream(System.getProperty("user.home") + propertiesFilePath)) {
settings.load(fis);
} catch (IOException e) {
throw new XPFPluginRuntimeException("Could not load setting properties file ", e);
}
String settingValueTuple = settings.getProperty(context.getSettingPath());
if (settingValueTuple == null) {
return Optional.notPresent();
}
String[] propertyValues = StringUtils.split(settingValueTuple, COMMA_DELIMITER);
return Optional.of(new XPFSettingValue(propertyValues[ARRAY_INDEX_VALUE],
XPFSettingValueTypeEnum.valueForTypeKey(propertyValues[ARRAY_INDEX_DATA_TYPE])));
}
}