Search in sources :

Example 6 with ProfileBuilder

use of org.apache.karaf.profile.ProfileBuilder in project karaf by apache.

the class Profiles method getEffective.

/**
 * <p>Gets an <em>effective</em> profile for given <code>profile</code>. Effective profile has all property
 * placeholders resolved. When <code>finalSubstitution</code> is <code>true</code>, placeholders that can't
 * be resolved are replaced with empty strings. When it's <code>false</code>, placeholders are left unchanged.</p>
 * @param profile
 * @param resolvers
 * @param finalSubstitution
 * @return
 */
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<>();
    final Map<String, TypedProperties> originals2 = 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);
                props = new TypedProperties(false);
                props.load(new ByteArrayInputStream(entry.getValue()));
                originals2.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 (String cfg : originals.keySet()) {
        TypedProperties original = originals.get(cfg);
        TypedProperties original2 = originals2.get(cfg);
        original2.putAll(original);
        builder.addFileConfiguration(cfg + Profile.PROPERTIES_SUFFIX, Utils.toBytes(original2));
    }
    // Compute the new profile
    return builder.getProfile();
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) TypedProperties(org.apache.felix.utils.properties.TypedProperties) HashMap(java.util.HashMap) Map(java.util.Map) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) PlaceholderResolver(org.apache.karaf.profile.PlaceholderResolver)

Aggregations

ProfileBuilder (org.apache.karaf.profile.ProfileBuilder)6 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Profile (org.apache.karaf.profile.Profile)3 Path (java.nio.file.Path)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 URI (java.net.URI)1 FileVisitResult (java.nio.file.FileVisitResult)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 ResolverImpl (org.apache.felix.resolver.ResolverImpl)1 Properties (org.apache.felix.utils.properties.Properties)1 TypedProperties (org.apache.felix.utils.properties.TypedProperties)1 Downloader (org.apache.karaf.features.internal.download.Downloader)1