Search in sources :

Example 6 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties 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();
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) TypedProperties(org.apache.felix.utils.properties.TypedProperties) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map) PlaceholderResolver(org.apache.karaf.profile.PlaceholderResolver)

Example 7 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class Utils method toProperties.

public static TypedProperties toProperties(Map<String, Object> source) {
    if (source instanceof TypedProperties) {
        return (TypedProperties) source;
    }
    TypedProperties rc = new TypedProperties(false);
    rc.putAll(source);
    return rc;
}
Also used : TypedProperties(org.apache.felix.utils.properties.TypedProperties)

Example 8 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class FileLockUtils method execute.

public static void execute(File file, Runnable<? super TypedProperties> callback, boolean writeToFile) throws IOException {
    execute(file, raf -> {
        TypedProperties props = load(raf);
        callback.run(props);
        if (writeToFile) {
            save(props, raf);
        }
    });
}
Also used : TypedProperties(org.apache.felix.utils.properties.TypedProperties)

Example 9 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class FileLockUtils method execute.

public static <T> T execute(File file, Callable<? super TypedProperties, T> callback, boolean writeToFile) throws IOException {
    return execute(file, raf -> {
        TypedProperties props = load(raf);
        T result = callback.call(props);
        if (writeToFile) {
            save(props, raf);
        }
        return result;
    });
}
Also used : TypedProperties(org.apache.felix.utils.properties.TypedProperties)

Aggregations

TypedProperties (org.apache.felix.utils.properties.TypedProperties)9 IOException (java.io.IOException)4 File (java.io.File)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 Configuration (org.osgi.service.cm.Configuration)2 InterruptedIOException (java.io.InterruptedIOException)1 RandomAccessFile (java.io.RandomAccessFile)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 FileLock (java.nio.channels.FileLock)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConfigFileInfo (org.apache.karaf.features.ConfigFileInfo)1 ConfigInfo (org.apache.karaf.features.ConfigInfo)1 PlaceholderResolver (org.apache.karaf.profile.PlaceholderResolver)1 ProfileBuilder (org.apache.karaf.profile.ProfileBuilder)1