Skip to content

Commit

Permalink
Merge pull request #49 from lburgazzoli/customizer-cleanup
Browse files Browse the repository at this point in the history
customizer: honour cutomizer list property/env
  • Loading branch information
nicolaferraro authored Apr 19, 2019
2 parents 373cda1 + e07ef27 commit da362ed
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.camel.CamelContext;
Expand All @@ -41,48 +46,82 @@
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;


public final class RuntimeSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeSupport.class);

private RuntimeSupport() {
}

public static void configureContext(CamelContext context, Runtime.Registry registry) {
public static List<ContextCustomizer> configureContext(CamelContext context, Runtime.Registry registry) {
List<ContextCustomizer> appliedCustomizers = new ArrayList<>();
Set<String> customizers = lookupCustomizerIDs(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;
}

applyCustomizer(context, entry.getKey(), entry.getValue(), registry);

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

try {
FactoryFinder finder = context.getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH);
String customizerIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_CUSTOMIZERS, "");

if (ObjectHelper.isEmpty(customizerIDs)) {
PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
Properties properties = component.getInitialProperties();
for (String customizerId : customizers) {
ContextCustomizer customizer = (ContextCustomizer) finder.newInstance(customizerId);
applyCustomizer(context, customizerId, customizer, registry);

if (properties != null) {
customizerIDs = properties.getProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "");
}
}

if (ObjectHelper.isNotEmpty(customizerIDs)) {
for (String customizerId : customizerIDs.split(",", -1)) {
configureContext(context, customizerId, (ContextCustomizer) finder.newInstance(customizerId), registry);
}
appliedCustomizers.add(customizer);
}
} catch (NoFactoryAvailableException e) {
// ignored
throw new RuntimeException(e);
}

//this is to initialize all customizers that might be already present in the context injected by other means.
context.getRegistry().findByTypeWithName(ContextCustomizer.class).forEach(
(customizerId, customizer) -> configureContext(context, customizerId, customizer, registry)
);
return appliedCustomizers;
}

public static void configureContext(CamelContext context, String customizerId, ContextCustomizer customizer, Runtime.Registry registry) {
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());

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

public static Set<String> lookupCustomizerIDs(CamelContext context) {
Set<String> customizers = new TreeSet<>();

String customizerIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_CUSTOMIZERS, "");
if (ObjectHelper.isEmpty(customizerIDs)) {
PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
Properties properties = component.getInitialProperties();

if (properties != null) {
customizerIDs = properties.getProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "");
}
}

if (ObjectHelper.isNotEmpty(customizerIDs)) {
for (String customizerId : customizerIDs.split(",", -1)) {
customizers.add(customizerId);
}
}

return customizers;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.k.support;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.ExplicitCamelContextNameStrategy;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.Runtime;

public final class NameCustomizer implements ContextCustomizer {
private String name;

public NameCustomizer() {
this("default");
}

public NameCustomizer(String name) {
this.name = name;
}

@Override
public void apply(CamelContext camelContext, Runtime.Registry registry) {
camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.k.support;

import java.util.List;
import java.util.Properties;

import org.apache.camel.CamelContext;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.Constants;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.InMemoryRegistry;
import org.apache.camel.k.Runtime;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class RuntimeSupportTest {

@Test
public void testLoadCustomizers() {
PropertiesComponent pc = new PropertiesComponent();
Runtime.Registry registry = new InMemoryRegistry();
CamelContext context = new DefaultCamelContext(registry);
context.addComponent("properties", pc);

NameCustomizer customizer = new NameCustomizer("from-registry");
registry.bind("name", customizer);

List<ContextCustomizer> customizers = RuntimeSupport.configureContext(context, registry);
assertThat(context.getName()).isNotEqualTo("from-registry");
assertThat(context.getName()).isNotEqualTo("default");
assertThat(customizers).hasSize(0);

Properties properties = new Properties();
properties.setProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "name");
pc.setInitialProperties(properties);

customizers = RuntimeSupport.configureContext(context, registry);
assertThat(context.getName()).isEqualTo("from-registry");
assertThat(customizers).hasSize(1);
}

@Test
public void testLoadCustomizersOrdering() {
PropertiesComponent pc = new PropertiesComponent();
Runtime.Registry registry = new InMemoryRegistry();
CamelContext context = new DefaultCamelContext(registry);
context.addComponent("properties", pc);

Properties properties = new Properties();
properties.setProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "name");
pc.setInitialProperties(properties);

List<ContextCustomizer> customizers = RuntimeSupport.configureContext(context, registry);
assertThat(context.getName()).isEqualTo("default");
assertThat(customizers).hasSize(1);

NameCustomizer customizer = new NameCustomizer("from-registry");
registry.bind("name", customizer);

customizers = RuntimeSupport.configureContext(context, registry);
assertThat(context.getName()).isEqualTo("from-registry");
assertThat(customizers).hasSize(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class=org.apache.camel.k.support.NameCustomizer

0 comments on commit da362ed

Please sign in to comment.