Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Camel 5717 #91

Open
wants to merge 1 commit into from

2 participants

@SvenNold

https://issues.apache.org/jira/browse/CAMEL-5717

Supporting:

  • empty routeContext
  • routeBuilderRef
    • routeContextRef
@davsclaus

Please do only change relevant code that is part of the needed changes.

The other changes makes noise, such as adding final / changing indent / etc.

@SvenNold

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2014
  1. @SvenNold

    Satisfying Diff

    SvenNold authored
This page is out of date. Refresh to see the latest.
View
10 camel-core/src/main/java/org/apache/camel/model/RouteContextRefDefinition.java
@@ -27,7 +27,7 @@
/**
* Represents an XML <routeContextRef/> element
*
- * @version
+ * @version
*/
@XmlRootElement(name = "routeContextRef")
@XmlAccessorType(XmlAccessType.FIELD)
@@ -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);
+ }
}
View
34 camel-core/src/main/java/org/apache/camel/model/RouteContextRefDefinitionHelper.java
@@ -61,18 +61,22 @@ private RouteContextRefDefinitionHelper() {
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);
@@ -85,6 +89,30 @@ private RouteContextRefDefinitionHelper() {
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
View
33 camel-core/src/main/java/org/apache/camel/spi/CamelRouteContext.java
@@ -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();
+
+}
View
64 components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -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;
@@ -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
@@ -326,7 +327,7 @@ private void prepareRoutes() {
}
protected abstract void initCustomRegistry(T context);
-
+
@SuppressWarnings("deprecation")
protected void initLazyLoadTypeConverters() {
if (getLazyLoadTypeConverters() != null) {
@@ -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());
@@ -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);
View
35 components/camel-spring/src/main/java/org/apache/camel/spring/CamelRouteContextFactoryBean.java
@@ -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() {
@@ -56,5 +63,21 @@ public boolean isSingleton() {
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;
+ }
+
}
View
22 components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
@@ -76,7 +76,7 @@
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>();
@@ -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());
@@ -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));
}
@@ -201,7 +201,7 @@ protected JAXBContext createJaxbContext() throws JAXBException {
classes.add(org.apache.camel.util.spring.SSLContextParametersFactoryBean.class);
return classes;
}
-
+
protected class SSLContextParametersFactoryBeanBeanDefinitionParser extends BeanDefinitionParser {
public SSLContextParametersFactoryBeanBeanDefinitionParser() {
@@ -211,8 +211,8 @@ 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 {
@@ -220,12 +220,12 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
} 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());
@@ -233,7 +233,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
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 {
@@ -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
Something went wrong with that request. Please try again.