use of org.apache.karaf.profile.PlaceholderResolver in project karaf by apache.
the class Profiles method getEffective.
public static Profile getEffective(final Profile profile, final Collection<PlaceholderResolver> resolvers, boolean finalSubstitution) {
assertNotNull(profile, "profile is null");
assertNotNull(profile, "resolvers is null");
final Map<String, TypedProperties> originals = new HashMap<>();
for (Map.Entry<String, byte[]> entry : profile.getFileConfigurations().entrySet()) {
if (entry.getKey().endsWith(Profile.PROPERTIES_SUFFIX)) {
try {
String key = entry.getKey().substring(0, entry.getKey().length() - Profile.PROPERTIES_SUFFIX.length());
TypedProperties props = new TypedProperties(false);
props.load(new ByteArrayInputStream(entry.getValue()));
originals.put(key, props);
} catch (IOException e) {
throw new IllegalArgumentException("Can not load properties for " + entry.getKey());
}
}
}
final Map<String, Map<String, String>> dynamic = TypedProperties.prepare(originals);
TypedProperties.substitute(originals, dynamic, (pid, key, value) -> {
if (value != null) {
for (PlaceholderResolver resolver : resolvers) {
if (resolver.getScheme() == null) {
String val = resolver.resolve(dynamic, pid, key, value);
if (val != null) {
return val;
}
}
}
if (value.contains(":")) {
String scheme = value.substring(0, value.indexOf(":"));
String toSubst = value.substring(scheme.length() + 1);
for (PlaceholderResolver resolver : resolvers) {
if (scheme.equals(resolver.getScheme())) {
String val = resolver.resolve(dynamic, pid, key, toSubst);
if (val != null) {
return val;
}
}
}
}
}
return null;
}, finalSubstitution);
// Force computation while preserving layout
ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
for (Map.Entry<String, TypedProperties> cfg : originals.entrySet()) {
TypedProperties original = cfg.getValue();
builder.addFileConfiguration(cfg.getKey() + Profile.PROPERTIES_SUFFIX, Utils.toBytes(original));
}
// Compute the new profile
return builder.getProfile();
}
Aggregations