You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/12/01 19:22:29 UTC

[camel] branch main updated (216e074 -> 13428f7)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from 216e074  CAMEL-17256: camel-jbang - Allow to run multiple files using wildcards. Detect properties files from the list of run files.
     new 9b6cf15  CAMEL-17257: camel-core - RouteBuilder should store source location of the file from the DSL loader
     new 13428f7  CAMEL-17260: camel-core - Reload routes - Should handle non changed routes

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/main/java/org/apache/camel/Route.java      |   8 ++
 .../java/org/apache/camel/spi/RouteFactory.java    |   4 +-
 .../java/org/apache/camel/spi/RoutesLoader.java    |  14 ++
 .../camel/impl/engine/AbstractCamelContext.java    |  16 ++-
 .../org/apache/camel/impl/engine/DefaultRoute.java |  12 +-
 .../camel/impl/engine/DefaultRouteFactory.java     |   6 +-
 .../org/apache/camel/builder/RouteBuilder.java     |  18 +++
 .../org/apache/camel/model/RouteDefinition.java    |  11 ++
 .../org/apache/camel/model/RoutesDefinition.java   |  14 ++
 .../org/apache/camel/reifier/RouteReifier.java     |   2 +-
 .../DefaultExceptionPolicyStrategyTest.java        |   2 +-
 .../apache/camel/reifier/ProcessorReifierTest.java |   2 +-
 .../builder/endpoint/EndpointRouteBuilder.java     |   1 +
 .../api/management/mbean/ManagedRouteMBean.java    |   3 +
 .../camel/management/mbean/ManagedRoute.java       |   7 +
 .../support/FileWatcherResourceReloadStrategy.java |   4 +-
 .../camel/support/RouteWatcherReloadStrategy.java  | 144 +++++++++++++--------
 .../dsl/support/RouteBuilderLoaderSupport.java     |   1 +
 .../apache/camel/dsl/jbang/core/commands/Run.java  |   2 +-
 .../org/apache/camel/main/KameletMainTest.java     |   2 +
 20 files changed, 202 insertions(+), 71 deletions(-)

[camel] 01/02: CAMEL-17257: camel-core - RouteBuilder should store source location of the file from the DSL loader

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9b6cf155990e52e9c605980d3ffb1a6a612abfef
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 1 19:35:14 2021 +0100

    CAMEL-17257: camel-core - RouteBuilder should store source location of the file from the DSL loader
---
 .../src/main/java/org/apache/camel/Route.java      |   8 ++
 .../java/org/apache/camel/spi/RouteFactory.java    |   4 +-
 .../camel/impl/engine/AbstractCamelContext.java    |  16 ++-
 .../org/apache/camel/impl/engine/DefaultRoute.java |  12 +-
 .../camel/impl/engine/DefaultRouteFactory.java     |   6 +-
 .../org/apache/camel/builder/RouteBuilder.java     |  18 +++
 .../org/apache/camel/model/RouteDefinition.java    |  11 ++
 .../org/apache/camel/model/RoutesDefinition.java   |  14 +++
 .../org/apache/camel/reifier/RouteReifier.java     |   2 +-
 .../DefaultExceptionPolicyStrategyTest.java        |   2 +-
 .../apache/camel/reifier/ProcessorReifierTest.java |   2 +-
 .../builder/endpoint/EndpointRouteBuilder.java     |   1 +
 .../api/management/mbean/ManagedRouteMBean.java    |   3 +
 .../camel/management/mbean/ManagedRoute.java       |   7 ++
 .../camel/support/RouteWatcherReloadStrategy.java  | 129 ++++++++++++---------
 .../dsl/support/RouteBuilderLoaderSupport.java     |   1 +
 .../org/apache/camel/main/KameletMainTest.java     |   2 +
 17 files changed, 169 insertions(+), 69 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/Route.java b/core/camel-api/src/main/java/org/apache/camel/Route.java
index 7f325ad..0d4d3ff 100644
--- a/core/camel-api/src/main/java/org/apache/camel/Route.java
+++ b/core/camel-api/src/main/java/org/apache/camel/Route.java
@@ -23,6 +23,7 @@ import java.util.Set;
 
 import org.apache.camel.spi.InterceptStrategy;
 import org.apache.camel.spi.ManagementInterceptStrategy;
+import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.RouteController;
 import org.apache.camel.spi.RouteError;
 import org.apache.camel.spi.RoutePolicy;
@@ -134,6 +135,13 @@ public interface Route extends RuntimeConfiguration {
     String getConfigurationId();
 
     /**
+     * Gets the source resource that this route is located from
+     *
+     * @return the source, or null if this route is not loaded from a resource
+     */
+    Resource getSourceResource();
+
+    /**
      * Gets the camel context
      *
      * @return the camel context
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/RouteFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/RouteFactory.java
index cadab64..b419f8b 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/RouteFactory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/RouteFactory.java
@@ -34,10 +34,12 @@ public interface RouteFactory {
      * @param  routeId          the route id
      * @param  routeDescription the route description
      * @param  endpoint         the input endpoint (consumer)
+     * @param  resource         the source resource (if loaded via a DSL routes loader)
      * @return                  the created route
      */
     Route createRoute(
             CamelContext camelContext, NamedNode routeDefinition,
-            String routeId, String routeDescription, Endpoint endpoint);
+            String routeId, String routeDescription, Endpoint endpoint,
+            Resource resource);
 
 }
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 0c3b44b..3ebc21f 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -3068,8 +3068,13 @@ public abstract class AbstractCamelContext extends BaseService
                 // use basic endpoint uri to not log verbose details or potential sensitive data
                 String uri = order.getRoute().getEndpoint().getEndpointBaseUri();
                 uri = URISupport.sanitizeUri(uri);
-                lines.add(String.format("    %s %s (%s)", status, id, uri));
-
+                String loc = order.getRoute().getSourceResource() != null
+                        ? order.getRoute().getSourceResource().getLocation() : null;
+                if (startupSummaryLevel == StartupSummaryLevel.Verbose && loc != null) {
+                    lines.add(String.format("    %s %s (%s) (source: %s)", status, id, uri, loc));
+                } else {
+                    lines.add(String.format("    %s %s (%s)", status, id, uri));
+                }
                 String cid = order.getRoute().getConfigurationId();
                 if (cid != null) {
                     configs.add(String.format("    %s (%s)", id, cid));
@@ -3087,7 +3092,12 @@ public abstract class AbstractCamelContext extends BaseService
                     // use basic endpoint uri to not log verbose details or potential sensitive data
                     String uri = route.getEndpoint().getEndpointBaseUri();
                     uri = URISupport.sanitizeUri(uri);
-                    lines.add(String.format("    %s %s (%s)", status, id, uri));
+                    String loc = route.getSourceResource() != null ? route.getSourceResource().getLocation() : null;
+                    if (startupSummaryLevel == StartupSummaryLevel.Verbose && loc != null) {
+                        lines.add(String.format("    %s %s (%s) (source: %s)", status, id, uri, loc));
+                    } else {
+                        lines.add(String.format("    %s %s (%s)", status, id, uri));
+                    }
 
                     String cid = route.getConfigurationId();
                     if (cid != null) {
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java
index 716ad28..377c1a5 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java
@@ -43,6 +43,7 @@ import org.apache.camel.SuspendableService;
 import org.apache.camel.spi.IdAware;
 import org.apache.camel.spi.InterceptStrategy;
 import org.apache.camel.spi.ManagementInterceptStrategy;
+import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.RouteController;
 import org.apache.camel.spi.RouteError;
 import org.apache.camel.spi.RouteIdAware;
@@ -66,6 +67,7 @@ public class DefaultRoute extends ServiceSupport implements Route {
     private NamedNode route;
     private final String routeId;
     private final String routeDescription;
+    private final Resource sourceResource;
     private final List<Processor> eventDrivenProcessors = new ArrayList<>();
     private final List<InterceptStrategy> interceptStrategies = new ArrayList<>(0);
     private ManagementInterceptStrategy managementInterceptStrategy;
@@ -100,12 +102,13 @@ public class DefaultRoute extends ServiceSupport implements Route {
     private Consumer consumer;
 
     public DefaultRoute(CamelContext camelContext, NamedNode route, String routeId,
-                        String routeDescription, Endpoint endpoint) {
+                        String routeDescription, Endpoint endpoint, Resource resource) {
         this.camelContext = camelContext;
         this.route = route;
         this.routeId = routeId;
         this.routeDescription = routeDescription;
         this.endpoint = endpoint;
+        this.sourceResource = resource;
     }
 
     @Override
@@ -168,6 +171,11 @@ public class DefaultRoute extends ServiceSupport implements Route {
     }
 
     @Override
+    public Resource getSourceResource() {
+        return sourceResource;
+    }
+
+    @Override
     public void initializeServices() throws Exception {
         // gather all the services for this route
         gatherServices(services);
@@ -631,7 +639,7 @@ public class DefaultRoute extends ServiceSupport implements Route {
     public Navigate<Processor> navigate() {
         Processor answer = getProcessor();
 
-        // we want navigating routes to be easy, so skip the initial channel
+        // we want to navigate routes to be easy, so skip the initial channel
         // and navigate to its output where it all starts from end user point of view
         if (answer instanceof Navigate) {
             Navigate<Processor> nav = (Navigate<Processor>) answer;
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRouteFactory.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRouteFactory.java
index 52a422c..c607558 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRouteFactory.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRouteFactory.java
@@ -20,6 +20,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.NamedNode;
 import org.apache.camel.Route;
+import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.RouteFactory;
 
 /**
@@ -29,7 +30,8 @@ public class DefaultRouteFactory implements RouteFactory {
 
     @Override
     public Route createRoute(
-            CamelContext camelContext, NamedNode routeDefinition, String routeId, String routeDescription, Endpoint endpoint) {
-        return new DefaultRoute(camelContext, routeDefinition, routeId, routeDescription, endpoint);
+            CamelContext camelContext, NamedNode routeDefinition, String routeId, String routeDescription,
+            Endpoint endpoint, Resource resource) {
+        return new DefaultRoute(camelContext, routeDefinition, routeId, routeDescription, endpoint, resource);
     }
 }
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java
index 045b003..f3ae520 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java
@@ -65,6 +65,7 @@ import org.slf4j.LoggerFactory;
 public abstract class RouteBuilder extends BuilderSupport implements RoutesBuilder, Ordered {
     protected Logger log = LoggerFactory.getLogger(getClass());
 
+    private Resource resource;
     private final AtomicBoolean initialized = new AtomicBoolean();
     private final List<RouteBuilderLifecycleStrategy> lifecycleInterceptors = new ArrayList<>();
     private final List<TransformerBuilder> transformerBuilders = new ArrayList<>();
@@ -84,6 +85,20 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild
     }
 
     /**
+     * The {@link Resource} which is the source code for this route (such as XML, YAML, Groovy or Java source file)
+     */
+    public Resource getResource() {
+        return resource;
+    }
+
+    /**
+     * Sets the {@link Resource} which is the source code for this route (such as XML, YAML, Groovy or Java source file)
+     */
+    public void setResource(Resource resource) {
+        this.resource = resource;
+    }
+
+    /**
      * Add routes to a context using a lambda expression. It can be used as following:
      *
      * <pre>
@@ -591,6 +606,9 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild
 
             configure();
 
+            // remember the source resource
+            getRouteCollection().setResource(getResource());
+
             for (RouteDefinition route : getRouteCollection().getRoutes()) {
                 // ensure the route is prepared after configure method is complete
                 getRouteCollection().prepareRoute(route);
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java
index ea473a2..91c7b66 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java
@@ -46,6 +46,7 @@ import org.apache.camel.model.rest.RestBindingDefinition;
 import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.spi.AsEndpointUri;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.RoutePolicy;
 
 /**
@@ -88,6 +89,7 @@ public class RouteDefinition extends OutputDefinition<RouteDefinition> implement
     private List<PropertyDefinition> routeProperties;
     private Map<String, Object> templateParameters;
     private RouteTemplateContext routeTemplateContext;
+    private Resource resource;
 
     public RouteDefinition() {
     }
@@ -702,6 +704,15 @@ public class RouteDefinition extends OutputDefinition<RouteDefinition> implement
         this.routeTemplateContext = routeTemplateContext;
     }
 
+    public Resource getResource() {
+        return resource;
+    }
+
+    @XmlTransient
+    public void setResource(Resource resource) {
+        this.resource = resource;
+    }
+
     // Properties
     // -----------------------------------------------------------------------
 
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java
index 02bd6be..fce66a6 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java
@@ -32,6 +32,7 @@ import org.apache.camel.ErrorHandlerFactory;
 import org.apache.camel.builder.EndpointConsumerBuilder;
 import org.apache.camel.spi.AsEndpointUri;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.Resource;
 import org.apache.camel.support.OrderedComparator;
 import org.apache.camel.support.PatternHelper;
 import org.slf4j.Logger;
@@ -64,6 +65,8 @@ public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinit
     private CamelContext camelContext;
     @XmlTransient
     private ErrorHandlerFactory errorHandlerFactory;
+    @XmlTransient
+    private Resource resource;
 
     public RoutesDefinition() {
     }
@@ -151,6 +154,14 @@ public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinit
         this.errorHandlerFactory = errorHandlerFactory;
     }
 
+    public Resource getResource() {
+        return resource;
+    }
+
+    public void setResource(Resource resource) {
+        this.resource = resource;
+    }
+
     // Fluent API
     // -------------------------------------------------------------------------
 
@@ -222,6 +233,9 @@ public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinit
         // reset before preparing route
         route.resetPrepare();
 
+        // remember the source resource
+        route.setResource(resource);
+
         // merge global and route scoped together
         List<OnExceptionDefinition> oe = new ArrayList<>(onExceptions);
         List<InterceptDefinition> icp = new ArrayList<>(intercepts);
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java
index d55b699..b13b98b 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java
@@ -99,7 +99,7 @@ public class RouteReifier extends ProcessorReifier<RouteDefinition> {
         String id = definition.idOrCreate(camelContext.adapt(ExtendedCamelContext.class).getNodeIdFactory());
         String desc = definition.getDescriptionText();
         Route route = camelContext.adapt(ExtendedCamelContext.class).getRouteFactory().createRoute(camelContext, definition, id,
-                desc, endpoint);
+                desc, endpoint, definition.getResource());
 
         // configure error handler
         route.setErrorHandlerFactory(definition.getErrorHandlerFactory());
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java
index 1c795c5..7889cc3 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java
@@ -55,7 +55,7 @@ public class DefaultExceptionPolicyStrategyTest {
 
     private ExceptionPolicy exceptionPolicy(Class<? extends Throwable> exceptionClass) {
         CamelContext cc = new DefaultCamelContext();
-        Route context = new DefaultRoute(cc, null, null, null, null);
+        Route context = new DefaultRoute(cc, null, null, null, null, null);
         return new DefaultErrorHandlerReifier<>(context, null)
                 .createExceptionPolicy(new OnExceptionDefinition(exceptionClass));
     }
diff --git a/core/camel-core/src/test/java/org/apache/camel/reifier/ProcessorReifierTest.java b/core/camel-core/src/test/java/org/apache/camel/reifier/ProcessorReifierTest.java
index 22f8af0..89ce12b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/reifier/ProcessorReifierTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/reifier/ProcessorReifierTest.java
@@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test;
 public class ProcessorReifierTest {
     @Test
     public void testHandleCustomProcessorDefinition() {
-        Route ctx = new DefaultRoute(null, null, null, null, null);
+        Route ctx = new DefaultRoute(null, null, null, null, null, null);
         ProcessorReifier.registerReifier(MyProcessorDefinition.class, ProcessReifier::new);
         ProcessReifier ref = (ProcessReifier) ProcessorReifier.reifier(ctx, new MyProcessorDefinition());
         Assertions.assertTrue(ref.definition instanceof MyProcessorDefinition);
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java
index 77ad7fc..82dbc21 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java
@@ -73,6 +73,7 @@ public abstract class EndpointRouteBuilder extends RouteBuilder implements Endpo
             @Override
             public void configure() throws Exception {
                 CamelContextAware.trySetCamelContext(resource, getContext());
+                setResource(resource);
 
                 try (Reader reader = resource.getReader()) {
                     consumer.accept(reader, this);
diff --git a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
index e019e88..dc0ef4b 100644
--- a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
+++ b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
@@ -35,6 +35,9 @@ public interface ManagedRouteMBean extends ManagedPerformanceCounterMBean {
     @ManagedAttribute(description = "Route Description")
     String getDescription();
 
+    @ManagedAttribute(description = "Route Source Location")
+    String getSourceLocation();
+
     @ManagedAttribute(description = "Route Configuration ID")
     String getRouteConfigurationId();
 
diff --git a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
index 988dc20..c022566 100644
--- a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
+++ b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
@@ -72,6 +72,7 @@ public class ManagedRoute extends ManagedPerformanceCounter implements TimerList
     protected final Route route;
     protected final String description;
     protected final String configurationId;
+    protected final String sourceLocation;
     protected final CamelContext context;
     private final LoadTriplet load = new LoadTriplet();
     private final String jmxDomain;
@@ -81,6 +82,7 @@ public class ManagedRoute extends ManagedPerformanceCounter implements TimerList
         this.context = context;
         this.description = route.getDescription();
         this.configurationId = route.getConfigurationId();
+        this.sourceLocation = route.getSourceResource() != null ? route.getSourceResource().getLocation() : null;
         this.jmxDomain = context.getManagementStrategy().getManagementAgent().getMBeanObjectDomainName();
     }
 
@@ -145,6 +147,11 @@ public class ManagedRoute extends ManagedPerformanceCounter implements TimerList
     }
 
     @Override
+    public String getSourceLocation() {
+        return sourceLocation;
+    }
+
+    @Override
     public String getRouteConfigurationId() {
         return configurationId;
     }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
index b974eb7..60a1afc 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
@@ -27,6 +27,7 @@ import org.apache.camel.Route;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.StartupSummaryLevel;
+import org.apache.camel.spi.Resource;
 import org.apache.camel.util.AntPathMatcher;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
@@ -119,70 +120,82 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
         if (getResourceReload() == null) {
             // attach listener that triggers the route update
             setResourceReload((name, resource) -> {
-                try {
-                    // should all existing routes be stopped and removed first?
-                    if (removeAllRoutes) {
-                        // first stop and remove all routes
-                        getCamelContext().getRouteController().removeAllRoutes();
-                        // remove left-over route templates and endpoints, so we can start on a fresh
-                        getCamelContext().removeRouteTemplates("*");
-                        getCamelContext().getEndpointRegistry().clear();
-                    }
-                    Set<String> ids
-                            = getCamelContext().adapt(ExtendedCamelContext.class).getRoutesLoader().updateRoutes(resource);
-                    if (!ids.isEmpty()) {
-                        List<String> lines = new ArrayList<>();
-                        int total = 0;
-                        int started = 0;
-                        for (String id : ids) {
-                            total++;
-                            String status = getCamelContext().getRouteController().getRouteStatus(id).name();
-                            if (ServiceStatus.Started.name().equals(status)) {
-                                started++;
-                            }
-                            // use basic endpoint uri to not log verbose details or potential sensitive data
-                            String uri = getCamelContext().getRoute(id).getEndpoint().getEndpointBaseUri();
-                            uri = URISupport.sanitizeUri(uri);
-                            lines.add(String.format("    %s %s (%s)", status, id, uri));
-                        }
-                        LOG.info(String.format("Routes reloaded summary (total:%s started:%s)", total, started));
-                        // if we are default/verbose then log each route line
-                        if (getCamelContext().getStartupSummaryLevel() == StartupSummaryLevel.Default
-                                || getCamelContext().getStartupSummaryLevel() == StartupSummaryLevel.Verbose) {
-                            for (String line : lines) {
-                                LOG.info(line);
-                            }
-                        }
+                if (name.endsWith(".properties")) {
+                    onPropertiesReload(name, resource);
+                } else {
+                    onRouteReload(name, resource);
+                }
+            });
+        }
+
+        super.doStart();
+    }
+
+    protected void onRouteReload(String name, Resource resource) {
+        try {
+            // should all existing routes be stopped and removed first?
+            if (removeAllRoutes) {
+                // first stop and remove all routes
+                getCamelContext().getRouteController().removeAllRoutes();
+                // remove left-over route templates and endpoints, so we can start on a fresh
+                getCamelContext().removeRouteTemplates("*");
+                getCamelContext().getEndpointRegistry().clear();
+            }
+            Set<String> ids
+                    = getCamelContext().adapt(ExtendedCamelContext.class).getRoutesLoader().updateRoutes(resource);
+            if (!ids.isEmpty()) {
+                List<String> lines = new ArrayList<>();
+                int total = 0;
+                int started = 0;
+                for (String id : ids) {
+                    total++;
+                    String status = getCamelContext().getRouteController().getRouteStatus(id).name();
+                    if (ServiceStatus.Started.name().equals(status)) {
+                        started++;
                     }
-                    // fire events for routes reloaded
-                    for (String id : ids) {
-                        Route route = getCamelContext().getRoute(id);
-                        EventHelper.notifyRouteReloaded(getCamelContext(), route);
+                    // use basic endpoint uri to not log verbose details or potential sensitive data
+                    String uri = getCamelContext().getRoute(id).getEndpoint().getEndpointBaseUri();
+                    uri = URISupport.sanitizeUri(uri);
+                    lines.add(String.format("    %s %s (%s)", status, id, uri));
+                }
+                LOG.info(String.format("Routes reloaded summary (total:%s started:%s)", total, started));
+                // if we are default/verbose then log each route line
+                if (getCamelContext().getStartupSummaryLevel() == StartupSummaryLevel.Default
+                        || getCamelContext().getStartupSummaryLevel() == StartupSummaryLevel.Verbose) {
+                    for (String line : lines) {
+                        LOG.info(line);
                     }
-
-                    if (!removeAllRoutes) {
-                        // if not all previous routes are removed then to have safe route reloading
-                        // it is recommended to configure ids on the routes
-                        StringJoiner sj = new StringJoiner("\n    ");
-                        for (String id : ids) {
-                            Route route = getCamelContext().getRoute(id);
-                            if (route.isCustomId()) {
-                                sj.add(route.getEndpoint().getEndpointUri());
-                            }
-                        }
-                        if (sj.length() > 0) {
-                            LOG.warn(
-                                    "Routes with no id's detected. Its recommended to assign route id's to your routes so Camel can reload the routes correctly.\n    Unassigned routes:\n    {}",
-                                    sj);
-                        }
+                }
+            }
+            // fire events for routes reloaded
+            for (String id : ids) {
+                Route route = getCamelContext().getRoute(id);
+                EventHelper.notifyRouteReloaded(getCamelContext(), route);
+            }
+
+            if (!removeAllRoutes) {
+                // if not all previous routes are removed then to have safe route reloading
+                // it is recommended to configure ids on the routes
+                StringJoiner sj = new StringJoiner("\n    ");
+                for (String id : ids) {
+                    Route route = getCamelContext().getRoute(id);
+                    if (route.isCustomId()) {
+                        sj.add(route.getEndpoint().getEndpointUri());
                     }
-                } catch (Exception e) {
-                    throw RuntimeCamelException.wrapRuntimeException(e);
                 }
-            });
+                if (sj.length() > 0) {
+                    LOG.warn(
+                            "Routes with no id's detected. Its recommended to assign route id's to your routes so Camel can reload the routes correctly.\n    Unassigned routes:\n    {}",
+                            sj);
+                }
+            }
+        } catch (Exception e) {
+            throw RuntimeCamelException.wrapRuntimeException(e);
         }
+    }
 
-        super.doStart();
+    protected void onPropertiesReload(String name, Resource resource) {
+        // TODO: implement me
     }
 
 }
diff --git a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java
index b0b846e..4340378 100644
--- a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java
+++ b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java
@@ -59,6 +59,7 @@ public abstract class RouteBuilderLoaderSupport extends RoutesBuilderLoaderSuppo
     public RoutesBuilder loadRoutesBuilder(Resource resource) throws Exception {
         final RouteBuilder builder = doLoadRouteBuilder(resource);
         CamelContextAware.trySetCamelContext(builder, getCamelContext());
+        builder.setResource(resource);
 
         if (recorder != null) {
             StartupStep step = recorder.beginStep(
diff --git a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java
index 2592e70..7fbcf7c 100644
--- a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java
+++ b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.main;
 
+import org.apache.camel.StartupSummaryLevel;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
@@ -35,6 +36,7 @@ public class KameletMainTest {
     @Test
     public void testReload() throws Exception {
         KameletMain main = new KameletMain();
+        main.configure().setStartupSummaryLevel(StartupSummaryLevel.Verbose);
         main.setDownload(true);
         main.configure().withDurationMaxMessages(10);
         main.configure().withDurationMaxAction("stop");

[camel] 02/02: CAMEL-17260: camel-core - Reload routes - Should handle non changed routes

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 13428f71c968d1e0b3c76bd4cbca1457a6a9fde9
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 1 20:21:42 2021 +0100

    CAMEL-17260: camel-core - Reload routes - Should handle non changed routes
---
 .../java/org/apache/camel/spi/RoutesLoader.java    | 14 +++++++++++++
 .../support/FileWatcherResourceReloadStrategy.java |  4 ++--
 .../camel/support/RouteWatcherReloadStrategy.java  | 23 +++++++++++++++++++---
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  2 +-
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java b/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
index b161cf2..69d2cfe 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
@@ -84,6 +84,20 @@ public interface RoutesLoader extends CamelContextAware {
      * @return           route ids for the routes that was loaded or updated.
      */
     default Set<String> updateRoutes(Resource... resources) throws Exception {
+        return updateRoutes(Arrays.asList(resources));
+    }
+
+    /**
+     * Loads or updates existing {@link RoutesBuilder} from the give list of {@link Resource} into the current
+     * {@link org.apache.camel.CamelContext}.
+     *
+     * If a route is loaded with a route id for an existing route, then the existing route is stopped and remove, so it
+     * can be updated.
+     *
+     * @param  resources the resources to be loaded or updated.
+     * @return           route ids for the routes that was loaded or updated.
+     */
+    default Set<String> updateRoutes(Collection<Resource> resources) throws Exception {
         Set<String> answer = new LinkedHashSet<>();
         Collection<RoutesBuilder> builders = findRoutesBuilders(resources);
         for (RoutesBuilder builder : builders) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
index 2881d04..87506f3 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
@@ -265,8 +265,8 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
                     for (WatchEvent<?> event : key.pollEvents()) {
                         WatchEvent<Path> we = (WatchEvent<Path>) event;
                         Path path = we.context();
-                        File file = pathToReload.resolve(path).toAbsolutePath().toFile();
-                        String name = FileUtil.compactPath(file.getAbsolutePath());
+                        File file = pathToReload.resolve(path).toFile();
+                        String name = FileUtil.compactPath(file.getPath());
                         LOG.debug("Detected Modified/Created file: {}", name);
                         boolean accept = fileFilter == null || fileFilter.accept(file);
                         if (accept) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
index 60a1afc..bf0db4c 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
@@ -132,17 +132,31 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
     }
 
     protected void onRouteReload(String name, Resource resource) {
+        // remember all existing resources
+        List<Resource> sources = new ArrayList<>();
+
         try {
             // should all existing routes be stopped and removed first?
             if (removeAllRoutes) {
+                // remember all the sources of the current routes (except the updated)
+                getCamelContext().getRoutes().forEach(r -> {
+                    Resource rs = r.getSourceResource();
+                    if (rs != null && !rs.getLocation().equals(resource.getLocation())) {
+                        sources.add(rs);
+                    }
+                });
                 // first stop and remove all routes
                 getCamelContext().getRouteController().removeAllRoutes();
                 // remove left-over route templates and endpoints, so we can start on a fresh
                 getCamelContext().removeRouteTemplates("*");
                 getCamelContext().getEndpointRegistry().clear();
             }
+
+            sources.add(resource);
+
+            // reload those other routes that was stopped and removed as we want to keep running those
             Set<String> ids
-                    = getCamelContext().adapt(ExtendedCamelContext.class).getRoutesLoader().updateRoutes(resource);
+                    = getCamelContext().adapt(ExtendedCamelContext.class).getRoutesLoader().updateRoutes(sources);
             if (!ids.isEmpty()) {
                 List<String> lines = new ArrayList<>();
                 int total = 0;
@@ -153,10 +167,12 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                     if (ServiceStatus.Started.name().equals(status)) {
                         started++;
                     }
+                    Route route = getCamelContext().getRoute(id);
                     // use basic endpoint uri to not log verbose details or potential sensitive data
-                    String uri = getCamelContext().getRoute(id).getEndpoint().getEndpointBaseUri();
+                    String uri = route.getEndpoint().getEndpointBaseUri();
                     uri = URISupport.sanitizeUri(uri);
-                    lines.add(String.format("    %s %s (%s)", status, id, uri));
+                    String loc = route.getSourceResource() != null ? route.getSourceResource().getLocation() : "";
+                    lines.add(String.format("    %s %s (%s) (source: %s)", status, id, uri, loc));
                 }
                 LOG.info(String.format("Routes reloaded summary (total:%s started:%s)", total, started));
                 // if we are default/verbose then log each route line
@@ -167,6 +183,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                     }
                 }
             }
+
             // fire events for routes reloaded
             for (String id : ids) {
                 Route route = getCamelContext().getRoute(id);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 9c90b9f..c324f7d 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -174,7 +174,7 @@ class Run implements Callable<Integer> {
                 // check if file exist
                 File inputFile = new File(file.substring(5));
                 if (!inputFile.exists() && !inputFile.isFile()) {
-                    System.err.println("File does not exist: " + files);
+                    System.err.println("File does not exist: " + file);
                     return 1;
                 }
             }