Search in sources :

Example 6 with PlaceholderResolver

use of io.fabric8.karaf.core.properties.PlaceholderResolver in project fabric8 by jboss-fuse.

the class FabricServiceImpl method substituteConfigurations.

/**
 * Performs substitution to configuration based on the registered {@link PlaceholderResolver} instances.
 */
public Map<String, Map<String, String>> substituteConfigurations(final Map<String, Map<String, String>> configurations) {
    final Map<String, PlaceholderResolver> resolversSnapshot = new HashMap<String, PlaceholderResolver>(placeholderResolvers);
    // Check that all resolvers are available
    Set<String> requiredSchemes = getSchemesForProfileConfigurations(configurations);
    Set<String> availableSchemes = resolversSnapshot.keySet();
    if (!availableSchemes.containsAll(requiredSchemes)) {
        StringBuilder sb = new StringBuilder();
        sb.append("Missing Placeholder Resolvers:");
        for (String scheme : requiredSchemes) {
            if (!availableSchemes.contains(scheme)) {
                sb.append(" ").append(scheme);
            }
        }
        throw new FabricException(sb.toString());
    }
    final Map<String, Map<String, String>> mutableConfigurations = new HashMap<>();
    for (Entry<String, Map<String, String>> entry : configurations.entrySet()) {
        String key = entry.getKey();
        Map<String, String> value = new HashMap<>(entry.getValue());
        mutableConfigurations.put(key, value);
    }
    final FabricService fabricService = this;
    for (Map.Entry<String, Map<String, String>> entry : mutableConfigurations.entrySet()) {
        final String pid = entry.getKey();
        Map<String, String> props = entry.getValue();
        Map<String, String> original = new HashMap<>(props);
        for (Map.Entry<String, String> e : original.entrySet()) {
            final String key = e.getKey();
            final String value = e.getValue();
            try {
                props.put(key, InterpolationHelper.substVars(value, key, null, props, new InterpolationHelper.SubstitutionCallback() {

                    public String getValue(String toSubstitute) {
                        if (toSubstitute != null && toSubstitute.contains(":")) {
                            String scheme = toSubstitute.substring(0, toSubstitute.indexOf(":"));
                            return resolversSnapshot.get(scheme).resolve(fabricService, mutableConfigurations, pid, key, toSubstitute);
                        }
                        return substituteBundleProperty(toSubstitute, bundleContext);
                    }
                }));
            } catch (EncryptionOperationNotPossibleException exception) {
                LOGGER.warn("Error resolving " + key, exception);
            }
        }
    }
    return mutableConfigurations;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FabricException(io.fabric8.api.FabricException) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException) FabricService(io.fabric8.api.FabricService) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) PlaceholderResolver(io.fabric8.api.PlaceholderResolver)

Example 7 with PlaceholderResolver

use of io.fabric8.karaf.core.properties.PlaceholderResolver in project fabric8 by jboss-fuse.

the class ContainerPlaceholderResolverTest method testResolveNameContainerAttributes.

@Test
public void testResolveNameContainerAttributes() throws Exception {
    PlaceholderResolver resolver = getContainerPlaceholderResolver();
    assertEquals(ip, resolver.resolve(fabricService, null, null, null, "container:root/ip"));
    assertEquals(localhostname, resolver.resolve(fabricService, null, null, null, "container:root/localhostname"));
    assertEquals(bindaddress, resolver.resolve(fabricService, null, null, null, "container:root/bindaddress"));
    assertEquals(containerResolver, resolver.resolve(fabricService, null, null, null, "container:root/resolver"));
    verify(fabricService);
    verify(dataStore);
}
Also used : PlaceholderResolver(io.fabric8.api.PlaceholderResolver) Test(org.junit.Test)

Example 8 with PlaceholderResolver

use of io.fabric8.karaf.core.properties.PlaceholderResolver in project fabric8 by jboss-fuse.

the class ContainerPlaceholderResolverTest method testResolveCurrentAttributes.

@Test
public void testResolveCurrentAttributes() throws Exception {
    PlaceholderResolver resolver = getContainerPlaceholderResolver();
    assertEquals(ip, resolver.resolve(fabricService, null, null, null, "container:ip"));
    assertEquals(localhostname, resolver.resolve(fabricService, null, null, null, "container:localhostname"));
    assertEquals(bindaddress, resolver.resolve(fabricService, null, null, null, "container:bindaddress"));
    assertEquals(containerResolver, resolver.resolve(fabricService, null, null, null, "container:resolver"));
    verify(fabricService);
    verify(dataStore);
}
Also used : PlaceholderResolver(io.fabric8.api.PlaceholderResolver) Test(org.junit.Test)

Example 9 with PlaceholderResolver

use of io.fabric8.karaf.core.properties.PlaceholderResolver in project fabric8 by fabric8io.

the class Fabric8PropertyEvaluator method evaluate.

@Override
public String evaluate(String key, Dictionary<String, String> dictionary) {
    PlaceholderResolver res = resolver.get();
    String value = null;
    if (res != null) {
        value = res.resolve(key);
    }
    return value != null ? value : dictionary.get(key);
}
Also used : PlaceholderResolver(io.fabric8.karaf.core.properties.PlaceholderResolver)

Aggregations

PlaceholderResolver (io.fabric8.api.PlaceholderResolver)7 Test (org.junit.Test)6 FabricService (io.fabric8.api.FabricService)4 FabricException (io.fabric8.api.FabricException)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 GetDataBuilder (org.apache.curator.framework.api.GetDataBuilder)2 EncryptionOperationNotPossibleException (org.jasypt.exceptions.EncryptionOperationNotPossibleException)2 DataStore (io.fabric8.api.DataStore)1 ContainerImpl (io.fabric8.internal.ContainerImpl)1 PlaceholderResolver (io.fabric8.karaf.core.properties.PlaceholderResolver)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NodeCacheExtended (org.apache.curator.framework.recipes.cache.NodeCacheExtended)1 PersistenceManager (org.apache.felix.cm.PersistenceManager)1 EncryptingPersistenceManager (org.apache.felix.cm.file.EncryptingPersistenceManager)1 PBEStringEncryptor (org.jasypt.encryption.pbe.PBEStringEncryptor)1