Search in sources :

Example 6 with ContextCustomizer

use of org.apache.camel.k.ContextCustomizer in project camel-k-runtime by apache.

the class RuntimeSupportTest method testLoadCustomizersWithPropertiesFlags.

@Test
public void testLoadCustomizersWithPropertiesFlags() {
    CamelContext context = new DefaultCamelContext();
    NameCustomizer customizer = new NameCustomizer("from-registry");
    context.getRegistry().bind("name", customizer);
    List<ContextCustomizer> customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isNotEqualTo("from-registry");
    assertThat(context.getName()).isNotEqualTo("default");
    assertThat(customizers).isEmpty();
    Properties properties = new Properties();
    properties.setProperty("camel.k.customizer.name.enabled", "true");
    context.getPropertiesComponent().setInitialProperties(properties);
    customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isEqualTo("from-registry");
    assertThat(customizers).hasSize(1);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ContextCustomizer(org.apache.camel.k.ContextCustomizer) Properties(java.util.Properties) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.jupiter.api.Test)

Example 7 with ContextCustomizer

use of org.apache.camel.k.ContextCustomizer in project camel-k-runtime by apache.

the class RuntimeSupport method lookupCustomizers.

public static Map<String, ContextCustomizer> lookupCustomizers(CamelContext context) {
    Map<String, ContextCustomizer> customizers = new ConcurrentHashMap<>();
    Properties properties = context.getPropertiesComponent().loadProperties(n -> n.startsWith(Constants.CUSTOMIZER_PREFIX) || n.startsWith(Constants.CUSTOMIZER_PREFIX_FALLBACK));
    if (properties != null) {
        // 
        for (String customizerId : lookupCustomizerIDs(context)) {
            customizers.computeIfAbsent(customizerId, id -> lookupCustomizerByID(context, id));
        }
        Pattern pattern = Pattern.compile(Constants.ENABLE_CUSTOMIZER_PATTERN);
        properties.entrySet().stream().filter(entry -> entry.getKey() instanceof String).filter(entry -> entry.getValue() != null).forEach(entry -> {
            final String key = (String) entry.getKey();
            final Object val = entry.getValue();
            final Matcher matcher = pattern.matcher(key);
            if (matcher.matches()) {
                String customizerId = null;
                if (matcher.groupCount() == 1) {
                    customizerId = matcher.group(1);
                } else if (matcher.groupCount() == 2) {
                    customizerId = matcher.group(2);
                }
                if (customizerId != null && Boolean.parseBoolean(String.valueOf(val))) {
                    // 
                    // Do not override customizers eventually found
                    // in the registry
                    // 
                    customizers.computeIfAbsent(customizerId, id -> lookupCustomizerByID(context, id));
                }
            }
        });
    }
    return customizers;
}
Also used : MalformedInputException(java.nio.charset.MalformedInputException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) Map(java.util.Map) ContextCustomizer(org.apache.camel.k.ContextCustomizer) RouteBuilderLifecycleStrategy(org.apache.camel.builder.RouteBuilderLifecycleStrategy) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) CamelContext(org.apache.camel.CamelContext) Properties(java.util.Properties) Logger(org.slf4j.Logger) FileVisitor(java.nio.file.FileVisitor) Files(java.nio.file.Files) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) Reader(java.io.Reader) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Paths(java.nio.file.Paths) Source(org.apache.camel.k.Source) HasCamelContext(org.apache.camel.spi.HasCamelContext) IOHelper(org.apache.camel.util.IOHelper) ObjectHelper(org.apache.camel.util.ObjectHelper) Pattern(java.util.regex.Pattern) InputStream(java.io.InputStream) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ContextCustomizer(org.apache.camel.k.ContextCustomizer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Properties(java.util.Properties)

Aggregations

ContextCustomizer (org.apache.camel.k.ContextCustomizer)7 Properties (java.util.Properties)6 CamelContext (org.apache.camel.CamelContext)6 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)5 Test (org.junit.jupiter.api.Test)5 Path (java.nio.file.Path)3 Paths (java.nio.file.Paths)3 List (java.util.List)3 Map (java.util.Map)3 ExplicitCamelContextNameStrategy (org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy)3 File (java.io.File)2 Ordered (org.apache.camel.Ordered)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 MalformedInputException (java.nio.charset.MalformedInputException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 FileVisitResult (java.nio.file.FileVisitResult)1 FileVisitor (java.nio.file.FileVisitor)1