Search in sources :

Example 1 with ContextCustomizer

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

the class RuntimeSupportTest method testLoadCustomizerOrder.

@Test
public void testLoadCustomizerOrder() {
    DefaultCamelContext context = new DefaultCamelContext();
    context.setName("camel");
    context.getRegistry().bind("c1", new ContextCustomizer() {

        @Override
        public int getOrder() {
            return Ordered.LOWEST;
        }

        @Override
        public void apply(CamelContext camelContext) {
            camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContext.getName() + "-c1"));
        }
    });
    context.getRegistry().bind("c2", new ContextCustomizer() {

        @Override
        public int getOrder() {
            return Ordered.HIGHEST;
        }

        @Override
        public void apply(CamelContext camelContext) {
            camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContext.getName() + "-c2"));
        }
    });
    context.getRegistry().bind("c3", new ContextCustomizer() {

        @Override
        public void apply(CamelContext camelContext) {
            camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContext.getName() + "-c3"));
        }
    });
    Properties properties = new Properties();
    properties.setProperty("camel.k.customizer.c1.enabled", "true");
    properties.setProperty("camel.k.customizer.c2.enabled", "true");
    properties.setProperty("camel.k.customizer.c3.enabled", "true");
    context.getPropertiesComponent().setInitialProperties(properties);
    List<ContextCustomizer> customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(customizers).hasSize(3);
    assertThat(context.getName()).isEqualTo("camel-c2-c3-c1");
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ExplicitCamelContextNameStrategy(org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy) ContextCustomizer(org.apache.camel.k.ContextCustomizer) Properties(java.util.Properties) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.jupiter.api.Test)

Example 2 with ContextCustomizer

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

the class RuntimeSupport method lookupCustomizerByID.

public static ContextCustomizer lookupCustomizerByID(CamelContext context, String customizerId) {
    ContextCustomizer customizer = context.getRegistry().lookupByNameAndType(customizerId, ContextCustomizer.class);
    if (customizer == null) {
        customizer = context.adapt(ExtendedCamelContext.class).getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH).newInstance(customizerId, ContextCustomizer.class).orElseThrow(() -> new RuntimeException("Error creating instance for customizer: " + customizerId));
        LOGGER.debug("Found customizer {} with id {} from service definition", customizer, customizerId);
    } else {
        LOGGER.debug("Found customizer {} with id {} from the registry", customizer, customizerId);
    }
    return customizer;
}
Also used : ContextCustomizer(org.apache.camel.k.ContextCustomizer)

Example 3 with ContextCustomizer

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

the class RuntimeSupportTest method testLoadCustomizersFallback.

@Test
public void testLoadCustomizersFallback() {
    CamelContext context = new DefaultCamelContext();
    context.getRegistry().bind("converters", (ContextCustomizer) camelContext -> camelContext.setLoadTypeConverters(true));
    List<ContextCustomizer> customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isNotEqualTo("from-registry");
    assertThat(context.getName()).isNotEqualTo("default");
    assertThat(context.isLoadTypeConverters()).isFalse();
    assertThat(customizers).isEmpty();
    Properties properties = new Properties();
    properties.setProperty("customizer.name.enabled", "true");
    context.getPropertiesComponent().setInitialProperties(properties);
    customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isEqualTo("default");
    assertThat(customizers).hasSize(1);
    properties.setProperty("customizer.converters.enabled", "true");
    properties.setProperty("customizer.converters.enabled", "true");
    context.getPropertiesComponent().setInitialProperties(properties);
    customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isEqualTo("default");
    assertThat(context.isLoadTypeConverters()).isTrue();
    assertThat(customizers).hasSize(2);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Ordered(org.apache.camel.Ordered) Properties(java.util.Properties) ExplicitCamelContextNameStrategy(org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) File(java.io.File) Test(org.junit.jupiter.api.Test) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) ContextCustomizer(org.apache.camel.k.ContextCustomizer) Path(java.nio.file.Path) 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 4 with ContextCustomizer

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

the class RuntimeSupportTest method testLoadCustomizersWithList.

@Test
public void testLoadCustomizersWithList() {
    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(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "name");
    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 5 with ContextCustomizer

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

the class RuntimeSupportTest method testLoadCustomizers.

@Test
public void testLoadCustomizers() {
    CamelContext context = new DefaultCamelContext();
    context.getRegistry().bind("converters", (ContextCustomizer) camelContext -> camelContext.setLoadTypeConverters(true));
    List<ContextCustomizer> customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isNotEqualTo("from-registry");
    assertThat(context.getName()).isNotEqualTo("default");
    assertThat(context.isLoadTypeConverters()).isFalse();
    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("default");
    assertThat(customizers).hasSize(1);
    properties.setProperty("camel.k.customizer.converters.enabled", "true");
    context.getPropertiesComponent().setInitialProperties(properties);
    customizers = RuntimeSupport.configureContextCustomizers(context);
    assertThat(context.getName()).isEqualTo("default");
    assertThat(context.isLoadTypeConverters()).isTrue();
    assertThat(customizers).hasSize(2);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Ordered(org.apache.camel.Ordered) Properties(java.util.Properties) ExplicitCamelContextNameStrategy(org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) File(java.io.File) Test(org.junit.jupiter.api.Test) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) ContextCustomizer(org.apache.camel.k.ContextCustomizer) Path(java.nio.file.Path) 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)

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