davsclaus commented on a change in pull request #4998:
URL: https://github.com/apache/camel/pull/4998#discussion_r569241689



##########
File path: core/camel-api/src/main/java/org/apache/camel/spi/Resource.java
##########
@@ -34,4 +34,34 @@
      * Each invocation must return a new {@link InputStream} instance.
      */
     InputStream getInputStream() throws IOException;
+
+    /**
+     * Finds a resource with a given name.
+     *
+     * @see Class#getResourceAsStream(String)
+     *

Review comment:
       Remove empty javadoc line (nitpicking)

##########
File path: core/camel-api/src/main/java/org/apache/camel/CamelContextAware.java
##########
@@ -36,14 +40,27 @@
     CamelContext getCamelContext();
 
     /**
-     * Set the {@link CamelContext} context if the object is an instance of 
{@link CamelContextAware}.
+     * Injects the {@link CamelContext}
+     *
+     * @param camelContext the Camel context
      */
-    static <T> T trySetCamelContext(T object, CamelContext camelContext) {
-        if (object instanceof CamelContextAware) {
-            ((CamelContextAware) object).setCamelContext(camelContext);
+    void setCamelContext(CamelContext camelContext);
+
+    /**
+     * Get the {@link CamelContext} adapted to the specialized type.
+     * <p/>
+     *
+     * @param  type the type to adapt to
+     * @return      this {@link CamelContext} adapted to the given type
+     *
+     * @see         CamelContext#adapt(Class)
+     */
+    default <T extends CamelContext> T getCamelContext(Class<T> type) {

Review comment:
       Do we really need this, there is already adapt on camel context. Its 
just that when we keep adding methods here and there, then we loose oversight 
what is there and what to use.

##########
File path: core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.Arrays;
+import java.util.Collection;
+
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RoutesBuilder;
+
+/**
+ * SPI for loading {@link RoutesBuilder} from a list of {@link Resource}.
+ */
+public interface RoutesLoader extends CamelContextAware {
+    /**
+     * Service factory key.
+     */
+    String FACTORY = "routes-loader";
+
+    /**
+     * Loads {@link RoutesBuilder} from the give list of {@link Resource} into 
the current
+     * {@link org.apache.camel.CamelContext}.
+     *
+     * @param resources the resources to be loaded.
+     */
+    default void loadRoutes(Collection<Resource> resources) throws Exception {
+        for (RoutesBuilder builder : findRoutesBuilders(resources)) {
+            getCamelContext().addRoutes(builder);
+        }
+    }
+
+    /**
+     * Loads {@link RoutesBuilder} from the give list of {@link Resource} into 
the current
+     * {@link org.apache.camel.CamelContext}.
+     *
+     * @param resources the resources to be loaded.
+     */
+    default void loadRoutes(Resource... resources) throws Exception {
+        for (RoutesBuilder builder : findRoutesBuilders(resources)) {
+            getCamelContext().addRoutes(builder);
+        }
+    }
+
+    /**
+     * Find {@link RoutesBuilder} from the give list of {@link Resource}.
+     *
+     * @param  resources the resource to be loaded.
+     * @return           a {@link RoutesBuilder}

Review comment:
       returns a collection of

##########
File path: 
core/camel-api/src/main/java/org/apache/camel/spi/RoutesBuilderLoader.java
##########
@@ -0,0 +1,47 @@
+/*
+ * 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 org.apache.camel.RoutesBuilder;
+
+/**
+ * SPI for loading {@link RoutesBuilder} from a {@link Resource}.
+ */
+public interface RoutesBuilderLoader {
+    /**
+     * Service factory base path for language specific loaders.
+     */
+    String FACTORY_PATH = "META-INF/services/org/apache/camel/routes-loader/";
+
+    /**
+     * Service factory group.
+     */
+    String FACTORY_GROUP = "routes-loader";
+
+    /**
+     * The supported file extension.

Review comment:
       We are limit to one file extension (which is good). We may want to 
mention this in the javadoc

##########
File path: 
core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
##########
@@ -883,66 +881,96 @@ public void setJavaRoutesExcludePattern(String 
javaRoutesExcludePattern) {
         this.javaRoutesExcludePattern = javaRoutesExcludePattern;
     }
 
-    public String getXmlRoutes() {
-        return xmlRoutes;
-    }
-
-    /**
-     * Directory to scan for adding additional XML routes. You can turn this 
off by setting the value to false.
-     *
-     * Files can be loaded from either classpath or file by prefixing with 
classpath: or file: Wildcards is supported
-     * using a ANT pattern style paths, such as 
classpath:&#42;&#42;/&#42;camel&#42;.xml
-     *
-     * Notice when using wildcards, then there is additional overhead as the 
classpath is scanned, where as if you
-     * specific the exact name for each XML file is faster as no classpath 
scanning is needed.
-     *
-     * Multiple directories can be specified and separated by comma, such as:
-     * file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml
-     */
-    public void setXmlRoutes(String xmlRoutes) {
-        this.xmlRoutes = xmlRoutes;
-    }
-
-    public String getXmlRouteTemplates() {
-        return xmlRouteTemplates;
-    }
-
-    /**
-     * Directory to scan for adding additional XML route templates. You can 
turn this off by setting the value to false.
-     *
-     * Files can be loaded from either classpath or file by prefixing with 
classpath: or file: Wildcards is supported
-     * using a ANT pattern style paths, such as 
classpath:&#42;&#42;/&#42;template-&#42;.xml
-     *
-     * Notice when using wildcards, then there is additional overhead as the 
classpath is scanned, where as if you
-     * specific the exact name for each XML file is faster as no classpath 
scanning is needed.
-     *
-     * Multiple directories can be specified and separated by comma, such as:
-     * file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml
-     */
-    public void setXmlRouteTemplates(String xmlRouteTemplates) {
-        this.xmlRouteTemplates = xmlRouteTemplates;
-    }
-
-    public String getXmlRests() {
-        return xmlRests;
-    }
-
-    /**
-     * Directory to scan for adding additional XML rests. You can turn this 
off by setting the value to false.
-     *
-     * Files can be loaded from either classpath or file by prefixing with 
classpath: or file: Wildcards is supported
-     * using a ANT pattern style paths, such as 
classpath:&#42;&#42;/&#42;camel&#42;.xml
-     *
-     * Notice when using wildcards, then there is additional overhead as the 
classpath is scanned, where as if you
-     * specific the exact name for each XML file is faster as no classpath 
scanning is needed.
-     *
-     * Multiple directories can be specified and separated by comma, such as:
-     * file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml
-     */
-    public void setXmlRests(String xmlRests) {
-        this.xmlRests = xmlRests;
-    }
-
+    public String getRoutesIncludePattern() {
+        return routesIncludePattern;
+    }
+
+    /**
+     * Used for inclusive filtering of routes from directories. This is used 
to retrieve the initial list of resource
+     * which can be filtered using the exclusive filter. The pattern is using 
Ant-path style pattern.
+     *
+     * Multiple patterns can be specified separated by comma, as example, to 
include all the routes from a directory
+     * whose name contains foo use: &#42;&#42;/*foo*.
+     */
+    public void setRoutesIncludePattern(String routesIncludePattern) {
+        this.routesIncludePattern = routesIncludePattern;
+    }
+
+    public String getRoutesExcludePattern() {
+        return routesExcludePattern;
+    }
+
+    /**
+     * Used for exclusive filtering of routes from directories. The exclusive 
filtering takes precedence over inclusive
+     * filtering. The pattern is using Ant-path style pattern.
+     *
+     * Multiple patterns can be specified separated by comma, as example, to 
exclude all the routes from a directory
+     * whose name contains foo use: &#42;&#42;/*foo*.
+     */
+    public void setRoutesExcludePattern(String routesExcludePattern) {
+        this.routesExcludePattern = routesExcludePattern;
+    }
+
+    //    public String getXmlRoutes() {

Review comment:
       Remove all this commented out code

##########
File path: 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.impl.engine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.spi.RoutesLoader;
+import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.ObjectHelper;
+
+@JdkService(RoutesLoader.FACTORY)
+public class DefaultRoutesLoader implements RoutesLoader {
+    private CamelContext camelContext;
+
+    public DefaultRoutesLoader() {
+    }
+
+    public DefaultRoutesLoader(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public Collection<RoutesBuilder> findRoutesBuilders(Collection<Resource> 
resources) throws Exception {
+        List<RoutesBuilder> answer = new ArrayList<>(resources.size());
+
+        for (Resource resource : resources) {
+            // language is derived from the file extension
+            final String extension = FileUtil.onlyExt(resource.getLocation(), 
true);
+
+            if (ObjectHelper.isEmpty(extension)) {
+                throw new IllegalArgumentException("Unable to determine 
extension for resource: " + resource.getLocation());
+            }
+
+            answer.add(getLoader(extension).loadRoutesBuilder(resource));
+        }
+
+        return answer;
+    }
+
+    /**
+     *
+     * @param  extension
+     * @return
+     */
+    private RoutesBuilderLoader getLoader(String extension) {
+        RoutesBuilderLoader answer = 
getCamelContext().getRegistry().lookupByNameAndType(extension, 
RoutesBuilderLoader.class);
+
+        if (answer == null) {
+            final ExtendedCamelContext ecc = 
getCamelContext(ExtendedCamelContext.class);
+            final FactoryFinder finder = 
ecc.getFactoryFinder(RoutesBuilderLoader.FACTORY_PATH);

Review comment:
       getBoostrapFactoryFinder

##########
File path: 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
##########
@@ -4547,10 +4550,28 @@ public XMLRoutesDefinitionLoader 
getXMLRoutesDefinitionLoader() {
         return xmlRoutesDefinitionLoader;
     }
 
+    @Override
     public void setXMLRoutesDefinitionLoader(XMLRoutesDefinitionLoader 
xmlRoutesDefinitionLoader) {
         this.xmlRoutesDefinitionLoader = 
doAddService(xmlRoutesDefinitionLoader);
     }
 
+    @Override
+    public RoutesLoader getRoutesLoader() {

Review comment:
       We should warm up the routes loader in doStartEagerServices like we do 
for the other services we really want to start early

##########
File path: 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.impl.engine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.spi.RoutesLoader;
+import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.ObjectHelper;
+
+@JdkService(RoutesLoader.FACTORY)
+public class DefaultRoutesLoader implements RoutesLoader {
+    private CamelContext camelContext;
+
+    public DefaultRoutesLoader() {
+    }
+
+    public DefaultRoutesLoader(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public Collection<RoutesBuilder> findRoutesBuilders(Collection<Resource> 
resources) throws Exception {
+        List<RoutesBuilder> answer = new ArrayList<>(resources.size());
+
+        for (Resource resource : resources) {
+            // language is derived from the file extension
+            final String extension = FileUtil.onlyExt(resource.getLocation(), 
true);
+
+            if (ObjectHelper.isEmpty(extension)) {
+                throw new IllegalArgumentException("Unable to determine 
extension for resource: " + resource.getLocation());
+            }
+
+            answer.add(getLoader(extension).loadRoutesBuilder(resource));
+        }
+
+        return answer;
+    }
+
+    /**
+     *
+     * @param  extension
+     * @return
+     */
+    private RoutesBuilderLoader getLoader(String extension) {
+        RoutesBuilderLoader answer = 
getCamelContext().getRegistry().lookupByNameAndType(extension, 
RoutesBuilderLoader.class);
+
+        if (answer == null) {
+            final ExtendedCamelContext ecc = 
getCamelContext(ExtendedCamelContext.class);
+            final FactoryFinder finder = 
ecc.getFactoryFinder(RoutesBuilderLoader.FACTORY_PATH);
+
+            final BaseServiceResolver<RoutesBuilderLoader> resolver
+                    = new BaseServiceResolver<>(extension, 
RoutesBuilderLoader.class, finder);
+            final Optional<RoutesBuilderLoader> loader
+                    = resolver.resolve(ecc);
+
+            if (loader.isPresent()) {
+                return CamelContextAware.trySetCamelContext(loader.get(), ecc);
+            }
+        }
+
+        if (answer == null) {
+            throw new IllegalArgumentException("Unable to fina a 
RoutesBuilderLoader for extension " + extension);

Review comment:
       Typo

##########
File path: 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.impl.engine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.spi.RoutesLoader;
+import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.ObjectHelper;
+
+@JdkService(RoutesLoader.FACTORY)
+public class DefaultRoutesLoader implements RoutesLoader {
+    private CamelContext camelContext;
+
+    public DefaultRoutesLoader() {
+    }
+
+    public DefaultRoutesLoader(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public Collection<RoutesBuilder> findRoutesBuilders(Collection<Resource> 
resources) throws Exception {
+        List<RoutesBuilder> answer = new ArrayList<>(resources.size());
+
+        for (Resource resource : resources) {
+            // language is derived from the file extension
+            final String extension = FileUtil.onlyExt(resource.getLocation(), 
true);
+
+            if (ObjectHelper.isEmpty(extension)) {
+                throw new IllegalArgumentException("Unable to determine 
extension for resource: " + resource.getLocation());
+            }
+
+            answer.add(getLoader(extension).loadRoutesBuilder(resource));
+        }
+
+        return answer;
+    }
+
+    /**
+     *
+     * @param  extension
+     * @return
+     */
+    private RoutesBuilderLoader getLoader(String extension) {
+        RoutesBuilderLoader answer = 
getCamelContext().getRegistry().lookupByNameAndType(extension, 
RoutesBuilderLoader.class);
+
+        if (answer == null) {
+            final ExtendedCamelContext ecc = 
getCamelContext(ExtendedCamelContext.class);
+            final FactoryFinder finder = 
ecc.getFactoryFinder(RoutesBuilderLoader.FACTORY_PATH);
+
+            final BaseServiceResolver<RoutesBuilderLoader> resolver
+                    = new BaseServiceResolver<>(extension, 
RoutesBuilderLoader.class, finder);
+            final Optional<RoutesBuilderLoader> loader
+                    = resolver.resolve(ecc);
+
+            if (loader.isPresent()) {
+                return CamelContextAware.trySetCamelContext(loader.get(), ecc);
+            }
+        }
+
+        if (answer == null) {
+            throw new IllegalArgumentException("Unable to fina a 
RoutesBuilderLoader for extension " + extension);

Review comment:
       Also extension may be ambigious, is it quarkus extension or what? Maybe 
say file extension

##########
File path: core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.Arrays;
+import java.util.Collection;
+
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RoutesBuilder;
+
+/**
+ * SPI for loading {@link RoutesBuilder} from a list of {@link Resource}.
+ */
+public interface RoutesLoader extends CamelContextAware {
+    /**
+     * Service factory key.
+     */
+    String FACTORY = "routes-loader";
+
+    /**
+     * Loads {@link RoutesBuilder} from the give list of {@link Resource} into 
the current
+     * {@link org.apache.camel.CamelContext}.
+     *
+     * @param resources the resources to be loaded.
+     */
+    default void loadRoutes(Collection<Resource> resources) throws Exception {
+        for (RoutesBuilder builder : findRoutesBuilders(resources)) {
+            getCamelContext().addRoutes(builder);
+        }
+    }
+
+    /**
+     * Loads {@link RoutesBuilder} from the give list of {@link Resource} into 
the current
+     * {@link org.apache.camel.CamelContext}.
+     *
+     * @param resources the resources to be loaded.
+     */
+    default void loadRoutes(Resource... resources) throws Exception {
+        for (RoutesBuilder builder : findRoutesBuilders(resources)) {
+            getCamelContext().addRoutes(builder);
+        }
+    }
+
+    /**
+     * Find {@link RoutesBuilder} from the give list of {@link Resource}.
+     *
+     * @param  resources the resource to be loaded.
+     * @return           a {@link RoutesBuilder}
+     */
+    default Collection<RoutesBuilder> findRoutesBuilders(Resource... 
resources) throws Exception {
+        return findRoutesBuilders(Arrays.asList(resources));
+    }
+
+    /**
+     * Find {@link RoutesBuilder} from the give list of {@link Resource}.
+     *
+     * @param  resources the resource to be loaded.
+     * @return           a {@link RoutesBuilder}
+     */
+    Collection<RoutesBuilder> findRoutesBuilders(Collection<Resource> 
resources) throws Exception;

Review comment:
       returns a collection of

##########
File path: 
core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java
##########
@@ -76,58 +53,78 @@ public void setAddRestsToRoutes(boolean addRestsToRoutes) {
      * @param camelContext the Camel context
      * @param config       the configuration
      */
-    public void configureRoutes(CamelContext camelContext, 
DefaultConfigurationProperties config) {
-        if (config.isRoutesCollectorEnabled()) {
-            try {
-                LOG.debug("RoutesCollectorEnabled: {}", routesCollector);
-                final List<RoutesBuilder> routes = 
routesCollector.collectRoutesFromRegistry(camelContext,
-                        config.getJavaRoutesExcludePattern(),
-                        config.getJavaRoutesIncludePattern());
-
-                // add newly discovered routes
-                routesBuilders.addAll(routes);
-                // sort routes according to ordered
-                routesBuilders.sort(OrderedComparator.get());
-                // then add the routes
-                for (RoutesBuilder builder : routesBuilders) {
-                    LOG.debug("Adding routes into CamelContext from 
RoutesBuilder: {}", builder);
-                    camelContext.addRoutes(builder);
-                }
+    public void configureRoutes(CamelContext camelContext, 
MainConfigurationProperties config) throws Exception {
+        final List<RoutesBuilder> routes = new 
ArrayList<>(config.getRoutesBuilders());
 
-                boolean scan = !config.getXmlRoutes().equals("false");
-                if (scan) {
-                    List<RoutesDefinition> defs
-                            = 
routesCollector.collectXmlRoutesFromDirectory(camelContext, 
config.getXmlRoutes());
-                    for (RoutesDefinition def : defs) {
-                        LOG.debug("Adding routes into CamelContext from XML 
files: {}", config.getXmlRoutes());
-                        
camelContext.getExtension(Model.class).addRouteDefinitions(def.getRoutes());
-                    }
+        if (config.getRoutesBuilderClasses() != null) {
+            String[] routeClasses = 
config.getRoutesBuilderClasses().split(",");
+            for (String routeClass : routeClasses) {
+                Class<RoutesBuilder> routeClazz = 
camelContext.getClassResolver().resolveClass(routeClass, RoutesBuilder.class);
+                if (routeClazz == null) {
+                    LOG.warn("Unable to resolve class: {}", routeClass);
+                    continue;
                 }
 
-                boolean scanTemplates = 
!config.getXmlRouteTemplates().equals("false");
-                if (scanTemplates) {
-                    List<RouteTemplatesDefinition> defs = 
routesCollector.collectXmlRouteTemplatesFromDirectory(camelContext,
-                            config.getXmlRouteTemplates());
-                    for (RouteTemplatesDefinition def : defs) {
-                        LOG.debug("Adding route templates into CamelContext 
from XML files: {}", config.getXmlRouteTemplates());
-                        
camelContext.getExtension(Model.class).addRouteTemplateDefinitions(def.getRouteTemplates());
-                    }
-                }
+                // lets use Camel's injector so the class has some support for 
dependency injection
+                RoutesBuilder builder = 
camelContext.getInjector().newInstance(routeClazz);
 
-                boolean scanRests = !config.getXmlRests().equals("false");
-                if (scanRests) {
-                    List<RestsDefinition> defs
-                            = 
routesCollector.collectXmlRestsFromDirectory(camelContext, 
config.getXmlRests());
-                    for (RestsDefinition def : defs) {
-                        LOG.debug("Adding rests into CamelContext from XML 
files: {}", config.getXmlRests());
-                        
camelContext.getExtension(Model.class).addRestDefinitions(def.getRests(), 
addRestsToRoutes);
-                    }
+                routes.add(builder);
+            }
+        }
+
+        if (config.getPackageScanRouteBuilders() != null) {
+            String[] pkgs = config.getPackageScanRouteBuilders().split(",");
+            Set<Class<?>> set = camelContext.adapt(ExtendedCamelContext.class)
+                    .getPackageScanClassResolver()
+                    .findImplementations(RoutesBuilder.class, pkgs);
+
+            for (Class<?> routeClazz : set) {
+                Object builder = 
camelContext.getInjector().newInstance(routeClazz);
+                if (builder instanceof RoutesBuilder) {
+                    routes.add((RoutesBuilder) builder);
+                } else {
+                    LOG.warn("Class {} is not a RouteBuilder class", 
routeClazz);
                 }
+            }
+        }
+
+        if (config.isRoutesCollectorEnabled()) {
+            try {
+                LOG.debug("RoutesCollectorEnabled: {}", routesCollector);
+
+                // add discovered routes from registry
+                routes.addAll(routesCollector.collectRoutesFromRegistry(
+                        camelContext,
+                        config.getJavaRoutesExcludePattern(),
+                        config.getJavaRoutesIncludePattern()));
+                // add discovered routes from directories
+                routes.addAll(routesCollector.collectRoutesFromDirectory(
+                        camelContext,
+                        config.getRoutesExcludePattern(),
+                        config.getRoutesIncludePattern()));
+
             } catch (Exception e) {
                 throw RuntimeCamelException.wrapRuntimeException(e);
             }
         }
 
+        // lets use Camel's bean post processor on any existing route builder 
classes
+        // so the instance has some support for dependency injection
+        CamelBeanPostProcessor postProcessor = 
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor();

Review comment:
       Ah yeah this is a great idea

##########
File path: 
core/camel-api/src/main/java/org/apache/camel/spi/XMLRoutesDefinitionLoader.java
##########
@@ -22,7 +22,10 @@
 
 /**
  * SPI for loading routes/rests from XML input streams and parsing this to 
model definition classes.
+ *
+ * @deprecated sue {@link RoutesLoader}

Review comment:
       use




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to