Skip to content

Commit

Permalink
Refactor customizer activation #46
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed May 21, 2019
1 parent 6de97a3 commit d2833a8
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class Constants {
public static final String ROUTES_LOADER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/loader/";
public static final String CONTEXT_CUSTOMIZER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/customizer/";
public static final String PROPERTY_CAMEL_K_CUSTOMIZER = "camel.k.customizer";
public static final String ENABLE_CUSTOMIZER_PATTERN = "customizer.([\\w][\\w-]*).enabled";

private Constants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@
package org.apache.camel.k;

import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;

@FunctionalInterface
public interface ContextCustomizer {
public interface ContextCustomizer extends Ordered, Comparable<ContextCustomizer>{
/**
* Perform CamelContext customization.
*
* @param camelContext the camel context to customize.
* @param registry the runtime registry.
*/
void apply(CamelContext camelContext, Runtime.Registry registry);

@Override
default int getOrder() {
return 0;
}

@Override
default int compareTo(ContextCustomizer o) {
return Integer.compare(getOrder(), o.getOrder());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.camel.CamelContext;
import org.apache.camel.NoFactoryAvailableException;
Expand All @@ -46,7 +49,6 @@
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -60,44 +62,92 @@ private RuntimeSupport() {

public static List<ContextCustomizer> configureContext(CamelContext context, Runtime.Registry registry) {
List<ContextCustomizer> appliedCustomizers = new ArrayList<>();
Set<String> customizers = lookupCustomizerIDs(context);
Map<String, ContextCustomizer> customizers = lookupCustomizers(context);

// this is to initialize all customizers that might be already present in
// the context injected by other means.
for (Map.Entry<String, ContextCustomizer> entry: context.getRegistry().findByTypeWithName(ContextCustomizer.class).entrySet()) {
if (!customizers.remove(entry.getKey())) {
continue;
}
customizers.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEach(e -> {
LOGGER.info("Apply ContextCustomizer with id={} and type={}", e.getKey(), e.getValue().getClass().getName());

bindProperties(context, e.getValue(), "customizer." + e.getKey() + ".");
e.getValue().apply(context, registry);

appliedCustomizers.add(e.getValue());
});

applyCustomizer(context, entry.getKey(), entry.getValue(), registry);
return appliedCustomizers;
}

appliedCustomizers.add(entry.getValue());
public static void configureRest(CamelContext context) {
RestConfiguration configuration = new RestConfiguration();

if (RuntimeSupport.bindProperties(context, configuration, "camel.rest.") > 0) {
//
// Set the rest configuration if only if at least one
// rest parameter has been set.
//
context.setRestConfiguration(configuration);
}
}

try {
FactoryFinder finder = context.getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH);
// *********************************
//
// Helpers - Customizers
//
// *********************************

for (String customizerId : customizers) {
ContextCustomizer customizer = (ContextCustomizer) finder.newInstance(customizerId);
applyCustomizer(context, customizerId, customizer, registry);
public static Map<String, ContextCustomizer> lookupCustomizers(CamelContext context) {
Map<String, ContextCustomizer> customizers = new ConcurrentHashMap<>();

appliedCustomizers.add(customizer);
PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
Properties properties = component.getInitialProperties();

if (properties != null) {
//
// Lookup customizers listed in Constants.ENV_CAMEL_K_CUSTOMIZERS or Constants.PROPERTY_CAMEL_K_CUSTOMIZER
// for backward compatibility
//
for (String customizerId: lookupCustomizerIDs(context)) {
customizers.computeIfAbsent(customizerId, id -> lookupCustomizerByID(context, id));
}
} catch (NoFactoryAvailableException e) {
throw new RuntimeException(e);

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() && matcher.groupCount() == 1) {
if (Boolean.valueOf(String.valueOf(val))) {
//
// Do not override customizers eventually found
// in the registry
//
customizers.computeIfAbsent(matcher.group(1), id -> lookupCustomizerByID(context, id));
}
}
}
);
}

return appliedCustomizers;
return customizers;
}

public static void applyCustomizer(CamelContext context, String customizerId, ContextCustomizer customizer, Runtime.Registry registry) {
ObjectHelper.notNull(customizer, "customizer");
StringHelper.notEmpty(customizerId, "customizerId");

LOGGER.info("Apply ContextCustomizer with id={} and type={}", customizerId, customizer.getClass().getName());
public static ContextCustomizer lookupCustomizerByID(CamelContext context, String customizerId) {
ContextCustomizer customizer = context.getRegistry().lookupByNameAndType(customizerId, ContextCustomizer.class);
if (customizer == null) {
try {
customizer = (ContextCustomizer) context.getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH).newInstance(customizerId);
} catch (NoFactoryAvailableException e) {
throw new RuntimeException(e);
}
}

bindProperties(context, customizer, "customizer." + customizerId + ".");
customizer.apply(context, registry);
return customizer;
}

public static Set<String> lookupCustomizerIDs(CamelContext context) {
Expand All @@ -122,17 +172,11 @@ public static Set<String> lookupCustomizerIDs(CamelContext context) {
return customizers;
}

public static void configureRest(CamelContext context) {
RestConfiguration configuration = new RestConfiguration();

if (RuntimeSupport.bindProperties(context, configuration, "camel.rest.") > 0) {
//
// Set the rest configuration if only if at least one
// rest parameter has been set.
//
context.setRestConfiguration(configuration);
}
}
// *********************************
//
// Helpers - Properties
//
// *********************************

public static int bindProperties(CamelContext context, Object target, String prefix) {
final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
Expand Down Expand Up @@ -169,27 +213,6 @@ public static int bindProperties(Properties properties, Object target, String pr
return count.get();
}

public static RoutesLoader loaderFor(CamelContext context, Source source) {
return context.getRegistry().findByType(RoutesLoader.class).stream()
.filter(rl -> rl.getSupportedLanguages().contains(source.getLanguage()))
.findFirst()
.orElseGet(() -> lookupLoaderFromResource(context, source));
}

public static RoutesLoader lookupLoaderFromResource(CamelContext context, Source source) {
final FactoryFinder finder;
final RoutesLoader loader;

try {
finder = context.getFactoryFinder(Constants.ROUTES_LOADER_RESOURCE_PATH);
loader = (RoutesLoader)finder.newInstance(source.getLanguage());
} catch (NoFactoryAvailableException e) {
throw new IllegalArgumentException("Unable to find loader for: " + source, e);
}

return loader;
}

public static Properties loadProperties() {
final String conf = System.getenv(Constants.ENV_CAMEL_K_CONF);
final String confd = System.getenv(Constants.ENV_CAMEL_K_CONF_D);
Expand Down Expand Up @@ -250,4 +273,31 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO

return properties;
}

// *********************************
//
// Helpers - Loaders
//
// *********************************

public static RoutesLoader loaderFor(CamelContext context, Source source) {
return context.getRegistry().findByType(RoutesLoader.class).stream()
.filter(rl -> rl.getSupportedLanguages().contains(source.getLanguage()))
.findFirst()
.orElseGet(() -> lookupLoaderFromResource(context, source));
}

public static RoutesLoader lookupLoaderFromResource(CamelContext context, Source source) {
final FactoryFinder finder;
final RoutesLoader loader;

try {
finder = context.getFactoryFinder(Constants.ROUTES_LOADER_RESOURCE_PATH);
loader = (RoutesLoader)finder.newInstance(source.getLanguage());
} catch (NoFactoryAvailableException e) {
throw new IllegalArgumentException("Unable to find loader for: " + source, e);
}

return loader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.k.support;

import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;
import org.apache.camel.impl.ExplicitCamelContextNameStrategy;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.Runtime;
Expand All @@ -32,6 +33,11 @@ public NameCustomizer(String name) {
this.name = name;
}

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

@Override
public void apply(CamelContext camelContext, Runtime.Registry registry) {
camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(name));
Expand Down
Loading

0 comments on commit d2833a8

Please sign in to comment.