in src/main/java/org/apache/sling/provisioning/model/ModelUtility.java [271:311]
public static Model applyVariables(final Model model, final VariableResolver resolver) {
// define delegating resolver that collects all variable names and value per feature
final Map<String,Map<String,String>> collectedVars = new HashMap<>();
VariableResolver variableCollector = new VariableResolver() {
@Override
public String resolve(Feature feature, String name) {
String value = resolver.resolve(feature, name);
if (value != null) {
Map<String,String> featureVars = collectedVars.get(feature.getName());
if (featureVars == null) {
featureVars = new HashMap<>();
collectedVars.put(feature.getName(), featureVars);
}
featureVars.put(name, value);
}
return value;
}
};
// use effective model processor to collect variables, but drop the resulting model
new EffectiveModelProcessor(new ResolverOptions().variableResolver(variableCollector)).process(model);
// define a processor that updates the "variables" sections in the features
ModelProcessor variablesUpdater = new ModelProcessor() {
@Override
protected KeyValueMap<String> processVariables(KeyValueMap<String> variables, Feature newFeature) {
KeyValueMap<String> newVariables = new KeyValueMap<>();
Map<String,String> featureVars = collectedVars.get(newFeature.getName());
if (featureVars != null) {
for (Map.Entry<String, String> entry : featureVars.entrySet()) {
newVariables.put(entry.getKey(), entry.getValue());
}
}
return newVariables;
}
};
// return model with replaced "variables" sections
return variablesUpdater.process(model);
}