Skip to content

Commit

Permalink
chore: Bind Camel components to Citrus reference resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Mar 11, 2022
1 parent 3476f70 commit 920e8e0
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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.citrusframework.yaks.camel;

import java.util.Map;
import java.util.Set;

import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.spi.ReferenceResolver;
import com.consol.citrus.spi.SimpleReferenceResolver;
import org.apache.camel.CamelContext;

/**
* @author Christoph Deppisch
*/
public class CamelReferenceResolver implements ReferenceResolver {

private CamelContext camelContext;

private ReferenceResolver fallback = new SimpleReferenceResolver();

public CamelReferenceResolver() {
super();
}

/**
* Constructor initializes with given Camel context.
* @param camelContext
*/
public CamelReferenceResolver(CamelContext camelContext) {
this.camelContext = camelContext;
}

@Override
public <T> T resolve(Class<T> type) {
Set<T> components = camelContext.getRegistry().findByType(type);

if (components.isEmpty()) {
throw new CitrusRuntimeException(String.format("Unable to find bean reference for type '%s'", type));
}

return components.iterator().next();
}

@Override
public <T> T resolve(String name, Class<T> type) {
T component = camelContext.getRegistry().lookupByNameAndType(name, type);

if (component != null) {
return component;
}

if (fallback.isResolvable(name)) {
return fallback.resolve(name, type);
}
throw new CitrusRuntimeException(String.format("Unable to find bean reference for name '%s'", name));
}

@Override
public Object resolve(String name) {
Object component = camelContext.getRegistry().lookupByName(name);

if (component != null) {
return component;
}

if (fallback.isResolvable(name)) {
return fallback.resolve(name);
}

throw new CitrusRuntimeException(String.format("Unable to find bean reference for name '%s'", name));
}

@Override
public <T> Map<String, T> resolveAll(Class<T> requiredType) {
return camelContext.getRegistry().findByTypeWithName(requiredType);
}

@Override
public boolean isResolvable(String name) {
return camelContext.getRegistry().lookupByName(name) != null || fallback.isResolvable(name);
}

@Override
public boolean isResolvable(Class<?> type) {
return !camelContext.getRegistry().findByType(type).isEmpty() || fallback.isResolvable(type);
}

@Override
public boolean isResolvable(String name, Class<?> type) {
return camelContext.getRegistry().lookupByNameAndType(name, type) != null || fallback.isResolvable(name, type);
}

/**
* Specifies the fallback.
* @param fallback
*/
public CamelReferenceResolver withFallback(ReferenceResolver fallback) {
this.fallback = fallback;
return this;
}

@Override
public void bind(String name, Object value) {
camelContext.getRegistry().bind(name, value);
}

public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}

/**
* Specifies the fallback.
* @param fallback
*/
public void setFallback(ReferenceResolver fallback) {
this.fallback = fallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public void before(Scenario scenario) {
}
}

if (camelContext != null && !(context.getReferenceResolver() instanceof CamelReferenceResolver)) {
context.setReferenceResolver(new CamelReferenceResolver(camelContext)
.withFallback(citrus.getCitrusContext().getReferenceResolver()));
}

headers = new HashMap<>();
body = null;
}
Expand Down Expand Up @@ -177,6 +182,7 @@ public void bindComponent(String name, String configurationScript) {
}

camelContext().getRegistry().bind(name, component);
citrus.getCitrusContext().bind(name, component);
}

@Given("^load to Camel registry ([^\"\\s]+)\\.groovy$")
Expand Down Expand Up @@ -407,6 +413,8 @@ private CamelContext camelContext() {
if (camelContext == null) {
try {
camelContext = new DefaultCamelContext();
context.setReferenceResolver(new CamelReferenceResolver(camelContext)
.withFallback(citrus.getCitrusContext().getReferenceResolver()));
camelContext.start();
} catch (Exception e) {
throw new IllegalStateException("Failed to start default Camel context", e);
Expand All @@ -422,6 +430,8 @@ private CamelContext springCamelContext(String beans) {
ApplicationContext ctx = new GenericXmlApplicationContext(
new ByteArrayResource(context.replaceDynamicContentInString(beans).getBytes(StandardCharsets.UTF_8)));
camelContext = ctx.getBean(SpringCamelContext.class);
context.setReferenceResolver(new CamelReferenceResolver(camelContext)
.withFallback(citrus.getCitrusContext().getReferenceResolver()));
camelContext.start();
} catch (Exception e) {
throw new IllegalStateException("Failed to start Spring Camel context", e);
Expand Down

0 comments on commit 920e8e0

Please sign in to comment.