Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Represents an XML <routeContextRef/> element
*
* @version
* @version
*/
@XmlRootElement(name = "routeContextRef")
@XmlAccessorType(XmlAccessType.FIELD)
Expand All @@ -51,9 +51,15 @@ public void setRef(String ref) {
this.ref = ref;
}

@SuppressWarnings({"unchecked", "rawtypes"})
public List<RouteDefinition> lookupRoutes(CamelContext camelContext) {
return RouteContextRefDefinitionHelper.lookupRoutes(camelContext, ref);
}

public List<RouteBuilderDefinition> lookupRouteBuilders(CamelContext camelContext) {
return RouteContextRefDefinitionHelper.lookupRouteBuilders(camelContext, ref);
}

public List<RouteContextRefDefinition> lookupRouteContextRefs(CamelContext camelContext) {
return RouteContextRefDefinitionHelper.lookupRouteContextRefs(camelContext, ref);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,22 @@ public static synchronized List<RouteDefinition> lookupRoutes(CamelContext camel
ObjectHelper.notNull(camelContext, "camelContext");
ObjectHelper.notNull(ref, "ref");

List<RouteDefinition> answer = CamelContextHelper.lookup(camelContext, ref, List.class);
CamelRouteContext answer = CamelContextHelper.lookup(camelContext, ref, CamelRouteContext.class);
if (answer == null) {
throw new IllegalArgumentException("Cannot find RouteContext with id " + ref);
}

if ( answer.getRoutes() == null ) {
return Collections.emptyList();
}

// must clone the route definitions as they can be reused with multiple CamelContexts
// and they would need their own instances of the definitions to not have side effects among
// the CamelContext - for example property placeholder resolutions etc.
List<RouteDefinition> clones = new ArrayList<RouteDefinition>(answer.size());
List<RouteDefinition> clones = new ArrayList<RouteDefinition>(answer.getRoutes().size());
try {
JAXBContext jaxb = getOrCreateJAXBContext();
for (RouteDefinition def : answer) {
for (RouteDefinition def : answer.getRoutes()) {
RouteDefinition clone = cloneRouteDefinition(jaxb, def);
if (clone != null) {
clones.add(clone);
Expand All @@ -85,6 +89,30 @@ public static synchronized List<RouteDefinition> lookupRoutes(CamelContext camel
return clones;
}

public static synchronized List<RouteBuilderDefinition> lookupRouteBuilders(CamelContext camelContext, String ref) {
ObjectHelper.notNull(camelContext, "camelContext");
ObjectHelper.notNull(ref, "ref");

CamelRouteContext answer = CamelContextHelper.lookup(camelContext, ref, CamelRouteContext.class);
if (answer == null) {
throw new IllegalArgumentException("Cannot find RouteContext with id " + ref);
}

return answer.getBuilderRefs();
}

public static synchronized List<RouteContextRefDefinition> lookupRouteContextRefs(CamelContext camelContext, String ref) {
ObjectHelper.notNull(camelContext, "camelContext");
ObjectHelper.notNull(ref, "ref");

CamelRouteContext answer = CamelContextHelper.lookup(camelContext, ref, CamelRouteContext.class);
if (answer == null) {
throw new IllegalArgumentException("Cannot find RouteContext with id " + ref);
}

return answer.getRouteRefs();
}

private static synchronized JAXBContext getOrCreateJAXBContext() throws JAXBException {
if (jaxbContext == null) {
// must use classloader from CamelContext to have JAXB working
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.spi;

import java.util.List;

import org.apache.camel.model.RouteBuilderDefinition;
import org.apache.camel.model.RouteContextRefDefinition;
import org.apache.camel.model.RouteDefinition;

public interface CamelRouteContext {

public List<RouteDefinition> getRoutes();

public List<RouteBuilderDefinition> getBuilderRefs();

public List<RouteContextRefDefinition> getRouteRefs();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.core.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -98,16 +99,16 @@
* or found by searching the classpath for Java classes which extend
* {@link org.apache.camel.builder.RouteBuilder}.
*
* @version
* @version
*/
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContext> extends IdentifiedType implements RouteContainer {

/**
* JVM system property to control lazy loading of type converters.
*/
public static final String LAZY_LOAD_TYPE_CONVERTERS = "CamelLazyLoadTypeConverters";

private static final Logger LOG = LoggerFactory.getLogger(AbstractCamelContextFactoryBean.class);

@XmlTransient
Expand Down Expand Up @@ -326,7 +327,7 @@ private void prepareRoutes() {
}

protected abstract void initCustomRegistry(T context);

@SuppressWarnings("deprecation")
protected void initLazyLoadTypeConverters() {
if (getLazyLoadTypeConverters() != null) {
Expand Down Expand Up @@ -481,14 +482,14 @@ protected void initPropertyPlaceholder() throws Exception {
PropertiesParser.class);
pc.setPropertiesParser(parser);
}

pc.setPropertyPrefix(def.getPropertyPrefix());
pc.setPropertySuffix(def.getPropertySuffix());

if (def.isFallbackToUnaugmentedProperty() != null) {
pc.setFallbackToUnaugmentedProperty(def.isFallbackToUnaugmentedProperty());
}

pc.setPrefixToken(def.getPrefixToken());
pc.setSuffixToken(def.getSuffixToken());

Expand All @@ -498,18 +499,43 @@ protected void initPropertyPlaceholder() throws Exception {
}

protected void initRouteRefs() throws Exception {
// add route refs to existing routes
if (getRouteRefs() != null) {
for (RouteContextRefDefinition ref : getRouteRefs()) {
List<RouteDefinition> defs = ref.lookupRoutes(getContext());
for (RouteDefinition def : defs) {
LOG.debug("Adding route from {} -> {}", ref, def);
// add in top as they are most likely to be common/shared
// which you may want to start first
getRoutes().add(0, def);
}
}
}
// add route refs to existing routes
if (getRouteRefs() != null) {
Map<String, RouteContextRefDefinition> flattenedRouteRefs = new HashMap<String, RouteContextRefDefinition>();
for (RouteContextRefDefinition ref : getRouteRefs()) {
flattenedRouteRefs.putAll(flattenRouteRefs(ref, flattenedRouteRefs));
}
for (RouteContextRefDefinition ref : flattenedRouteRefs.values() ) {
LOG.debug("Adding RouteContextRef {} items", ref);
addingRouteRefItems(ref);
}
}
}

protected Map<String, RouteContextRefDefinition> flattenRouteRefs(RouteContextRefDefinition ref, final Map<String, RouteContextRefDefinition> routeRefMap) throws Exception {
Map<String, RouteContextRefDefinition> currentRouteRefsMap = new HashMap<String, RouteContextRefDefinition>(routeRefMap);
if (!currentRouteRefsMap.containsKey(ref.getRef())) {
currentRouteRefsMap.put(ref.getRef(), ref);
for (RouteContextRefDefinition it : ref.lookupRouteContextRefs(getContext())) {
currentRouteRefsMap.putAll(flattenRouteRefs(it, currentRouteRefsMap));
}
} else {
LOG.warn("Ignoring circular reference ", ref);
}
return currentRouteRefsMap;
}

protected void addingRouteRefItems(RouteContextRefDefinition ref) throws Exception {
for (final RouteBuilderDefinition builderRef : ref.lookupRouteBuilders(getContext())) {
LOG.debug("Adding BuilderRef {} ", builderRef);
getBuilderRefs().add(builderRef);
}
for (RouteDefinition def : ref.lookupRoutes(getContext())) {
LOG.debug("Adding route from {} -> {}", ref, def);
// add in top as they are most likely to be common/shared
// which you may want to start first
getRoutes().add(0, def);
}
}

protected abstract <S> S getBeanForType(Class<S> clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.camel.model.IdentifiedType;
import org.apache.camel.model.RouteBuilderDefinition;
import org.apache.camel.model.RouteContextRefDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.spi.CamelRouteContext;
import org.springframework.beans.factory.FactoryBean;

/**
* @version
* @version
*/
@XmlRootElement(name = "routeContext")
@XmlAccessorType(XmlAccessType.FIELD)
public class CamelRouteContextFactoryBean extends IdentifiedType implements FactoryBean<List<RouteDefinition>> {
public class CamelRouteContextFactoryBean extends IdentifiedType implements CamelRouteContext, FactoryBean<CamelRouteContext> {

@XmlElement(name = "route", required = true)
@XmlElement(name = "route", required = false)
private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
@XmlElement(name = "routeBuilder", required = false)
private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
@XmlElement(name = "routeContextRef", required = false)
private List<RouteContextRefDefinition> routeRefs = new ArrayList<RouteContextRefDefinition>();

public List<RouteDefinition> getObject() throws Exception {
return routes;
public CamelRouteContext getObject() throws Exception {
return this;
}

public Class<?> getObjectType() {
Expand All @@ -56,5 +63,21 @@ public List<RouteDefinition> getRoutes() {
public void setRoutes(List<RouteDefinition> routes) {
this.routes = routes;
}


public List<RouteBuilderDefinition> getBuilderRefs() {
return builderRefs;
}

public void setBuilderRefs(List<RouteBuilderDefinition> builderRefs) {
this.builderRefs = builderRefs;
}

public List<RouteContextRefDefinition> getRouteRefs() {
return routeRefs;
}

public void setRouteRefs(List<RouteContextRefDefinition> routeRefs) {
this.routeRefs = routeRefs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
protected BeanDefinitionParser beanPostProcessorParser = new BeanDefinitionParser(CamelBeanPostProcessor.class, false);
protected Set<String> parserElementNames = new HashSet<String>();
protected Map<String, BeanDefinitionParser> parserMap = new HashMap<String, BeanDefinitionParser>();

private JAXBContext jaxbContext;
private Map<String, BeanDefinition> autoRegisterMap = new HashMap<String, BeanDefinition>();

Expand All @@ -100,7 +100,7 @@ public ModelFileGenerator createModelFileGenerator() throws JAXBException {
public void init() {
// register routeContext parser
registerParser("routeContext", new RouteContextDefinitionParser());

addBeanDefinitionParser("keyStoreParameters", KeyStoreParametersFactoryBean.class, true, true);
addBeanDefinitionParser("secureRandomParameters", SecureRandomParametersFactoryBean.class, true, true);
registerBeanDefinitionParser("sslContextParameters", new SSLContextParametersFactoryBeanBeanDefinitionParser());
Expand Down Expand Up @@ -144,7 +144,7 @@ public void init() {
}
if (osgi) {
LOG.info("OSGi environment detected.");
}
}
LOG.debug("Using {} as CamelContextBeanDefinitionParser", cl.getCanonicalName());
registerParser("camelContext", new CamelContextBeanDefinitionParser(cl));
}
Expand Down Expand Up @@ -201,7 +201,7 @@ protected Set<Class<?>> getJaxbPackages() {
classes.add(org.apache.camel.util.spring.SSLContextParametersFactoryBean.class);
return classes;
}

protected class SSLContextParametersFactoryBeanBeanDefinitionParser extends BeanDefinitionParser {

public SSLContextParametersFactoryBeanBeanDefinitionParser() {
Expand All @@ -211,29 +211,29 @@ public SSLContextParametersFactoryBeanBeanDefinitionParser() {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
super.doParse(element, builder);
// Note: prefer to use doParse from parent and postProcess; however, parseUsingJaxb requires

// Note: prefer to use doParse from parent and postProcess; however, parseUsingJaxb requires
// parserContext for no apparent reason.
Binder<Node> binder;
try {
binder = getJaxbContext().createBinder();
} catch (JAXBException e) {
throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e);
}

Object value = parseUsingJaxb(element, parserContext, binder);

if (value instanceof SSLContextParametersFactoryBean) {
SSLContextParametersFactoryBean bean = (SSLContextParametersFactoryBean)value;

builder.addPropertyValue("cipherSuites", bean.getCipherSuites());
builder.addPropertyValue("cipherSuitesFilter", bean.getCipherSuitesFilter());
builder.addPropertyValue("secureSocketProtocols", bean.getSecureSocketProtocols());
builder.addPropertyValue("secureSocketProtocolsFilter", bean.getSecureSocketProtocolsFilter());
builder.addPropertyValue("keyManagers", bean.getKeyManagers());
builder.addPropertyValue("trustManagers", bean.getTrustManagers());
builder.addPropertyValue("secureRandom", bean.getSecureRandom());

builder.addPropertyValue("clientParameters", bean.getClientParameters());
builder.addPropertyValue("serverParameters", bean.getServerParameters());
} else {
Expand Down Expand Up @@ -267,6 +267,8 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
if (value instanceof CamelRouteContextFactoryBean) {
CamelRouteContextFactoryBean factoryBean = (CamelRouteContextFactoryBean) value;
builder.addPropertyValue("routes", factoryBean.getRoutes());
builder.addPropertyValue("builderRefs", factoryBean.getBuilderRefs());
builder.addPropertyValue("routeRefs", factoryBean.getRouteRefs());
}

// lets inject the namespaces into any namespace aware POJOs
Expand Down