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 2023/05/05 08:31:38 UTC

[camel] branch main updated (024e14284b1 -> 5b836fe80ea)

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 024e14284b1 Merge branch 'release/4.0.0-M3'
     new 2b8ba9da93f CAMEL-19320: camel-jbang - Reload on demand
     new 5b836fe80ea Fix CS

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:
 .../camel/impl/engine/DefaultRoutesLoader.java     |   6 +-
 .../camel/impl/console/ContextDevConsole.java      |  25 ++---
 .../apache/camel/main/MainPropertiesReload.java    |   5 +-
 .../support/DefaultContextReloadStrategy.java      |   5 +
 .../support/FileWatcherResourceReloadStrategy.java |  25 +++--
 .../camel/support/RouteOnDemandReloadStrategy.java | 112 +++++++++++++++++++++
 .../camel/support/RouteWatcherReloadStrategy.java  |  54 +++++++---
 .../camel/cli/connector/LocalCliConnector.java     |  26 ++++-
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  55 +++++-----
 9 files changed, 246 insertions(+), 67 deletions(-)
 create mode 100644 core/camel-support/src/main/java/org/apache/camel/support/RouteOnDemandReloadStrategy.java


[camel] 01/02: CAMEL-19320: camel-jbang - Reload on demand

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 2b8ba9da93f8571f66f9bcc22db019e7cf6c3b58
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 5 10:25:17 2023 +0200

    CAMEL-19320: camel-jbang - Reload on demand
---
 .../camel/impl/engine/DefaultRoutesLoader.java     |   6 +-
 .../camel/impl/console/ContextDevConsole.java      |  25 ++---
 .../apache/camel/main/MainPropertiesReload.java    |   5 +-
 .../support/DefaultContextReloadStrategy.java      |   5 +
 .../support/FileWatcherResourceReloadStrategy.java |  25 +++--
 .../camel/support/RouteOnDemandReloadStrategy.java | 112 +++++++++++++++++++++
 .../camel/support/RouteWatcherReloadStrategy.java  |  54 +++++++---
 .../camel/cli/connector/LocalCliConnector.java     |  26 ++++-
 .../apache/camel/dsl/jbang/core/commands/Run.java  |   1 +
 9 files changed, 217 insertions(+), 42 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
index 84b59c47736..30755f3c931 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
@@ -194,8 +194,11 @@ public class DefaultRoutesLoader extends ServiceSupport implements RoutesLoader,
     @Override
     public Set<String> updateRoutes(Collection<Resource> resources) throws Exception {
         Set<String> answer = new LinkedHashSet<>();
-        Collection<RoutesBuilder> builders = findRoutesBuilders(resources);
+        if (resources == null || resources.isEmpty()) {
+            return answer;
+        }
 
+        Collection<RoutesBuilder> builders = findRoutesBuilders(resources);
         for (RoutesBuilder builder : builders) {
             // update any existing route configurations first
             if (builder instanceof RouteConfigurationsBuilder) {
@@ -203,7 +206,6 @@ public class DefaultRoutesLoader extends ServiceSupport implements RoutesLoader,
                 rcb.updateRouteConfigurationsToCamelContext(getCamelContext());
             }
         }
-
         for (RoutesBuilder builder : builders) {
             // update any existing routes
             Set<String> ids = builder.updateRoutesToCamelContext(getCamelContext());
diff --git a/core/camel-console/src/main/java/org/apache/camel/impl/console/ContextDevConsole.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/ContextDevConsole.java
index d232d0e964a..f8298454737 100644
--- a/core/camel-console/src/main/java/org/apache/camel/impl/console/ContextDevConsole.java
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/ContextDevConsole.java
@@ -19,6 +19,7 @@ package org.apache.camel.impl.console;
 import java.util.Date;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.camel.api.management.ManagedCamelContext;
 import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
@@ -52,13 +53,13 @@ public class ContextDevConsole extends AbstractDevConsole {
             ManagedCamelContextMBean mb = mcc.getManagedCamelContext();
             if (mb != null) {
                 int reloaded = 0;
-                ResourceReloadStrategy rrs = getCamelContext().hasService(ResourceReloadStrategy.class);
-                if (rrs != null) {
-                    reloaded += rrs.getReloadCounter();
+                Set<ResourceReloadStrategy> rrs = getCamelContext().hasServices(ResourceReloadStrategy.class);
+                for (ResourceReloadStrategy r : rrs) {
+                    reloaded += r.getReloadCounter();
                 }
-                ContextReloadStrategy crs = getCamelContext().hasService(ContextReloadStrategy.class);
-                if (crs != null) {
-                    reloaded += crs.getReloadCounter();
+                Set<ContextReloadStrategy> crs = getCamelContext().hasServices(ContextReloadStrategy.class);
+                for (ContextReloadStrategy r : crs) {
+                    reloaded += r.getReloadCounter();
                 }
                 String load1 = getLoad1(mb);
                 String load5 = getLoad5(mb);
@@ -122,13 +123,13 @@ public class ContextDevConsole extends AbstractDevConsole {
                 JsonObject stats = new JsonObject();
 
                 int reloaded = 0;
-                ResourceReloadStrategy rrs = getCamelContext().hasService(ResourceReloadStrategy.class);
-                if (rrs != null) {
-                    reloaded += rrs.getReloadCounter();
+                Set<ResourceReloadStrategy> rrs = getCamelContext().hasServices(ResourceReloadStrategy.class);
+                for (ResourceReloadStrategy r : rrs) {
+                    reloaded += r.getReloadCounter();
                 }
-                ContextReloadStrategy crs = getCamelContext().hasService(ContextReloadStrategy.class);
-                if (crs != null) {
-                    reloaded += crs.getReloadCounter();
+                Set<ContextReloadStrategy> crs = getCamelContext().hasServices(ContextReloadStrategy.class);
+                for (ContextReloadStrategy r : crs) {
+                    reloaded += r.getReloadCounter();
                 }
                 String load1 = getLoad1(mb);
                 String load5 = getLoad5(mb);
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainPropertiesReload.java b/core/camel-main/src/main/java/org/apache/camel/main/MainPropertiesReload.java
index 34ddef39139..ce2855d31a1 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainPropertiesReload.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainPropertiesReload.java
@@ -20,6 +20,8 @@ import java.util.Properties;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.NonManagedService;
+import org.apache.camel.StaticService;
 import org.apache.camel.spi.PropertiesReload;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.OrderedLocationProperties;
@@ -27,7 +29,8 @@ import org.apache.camel.util.OrderedLocationProperties;
 /**
  * Reloading of application.properties when using Camel Main in reload mode, such as when using camel-jbang.
  */
-public class MainPropertiesReload extends ServiceSupport implements PropertiesReload, CamelContextAware {
+public class MainPropertiesReload extends ServiceSupport
+        implements StaticService, NonManagedService, PropertiesReload, CamelContextAware {
 
     private final BaseMainSupport main;
     private CamelContext camelContext;
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultContextReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultContextReloadStrategy.java
index 4df77bc0ed2..83bf9147a35 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultContextReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultContextReloadStrategy.java
@@ -48,6 +48,11 @@ public class DefaultContextReloadStrategy extends ServiceSupport implements Cont
         this.camelContext = camelContext;
     }
 
+    @ManagedOperation(description = "Trigger on-demand reloading")
+    public void onReload() {
+        onReload("JMX Management");
+    }
+
     @Override
     public void onReload(Object source) {
         LOG.info("Reloading CamelContext ({}) triggered by: {}", camelContext.getName(), source);
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 e24606277a5..fcfd3718f1c 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
@@ -60,14 +60,15 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
 
     private static final Logger LOG = LoggerFactory.getLogger(FileWatcherResourceReloadStrategy.class);
 
-    private String folder;
-    private boolean isRecursive;
-    private WatchService watcher;
-    private ExecutorService executorService;
-    private WatchFileChangesTask task;
-    private Map<WatchKey, Path> folderKeys;
-    private long pollTimeout = 2000;
-    private FileFilter fileFilter;
+    WatchService watcher;
+    ExecutorService executorService;
+    WatchFileChangesTask task;
+    Map<WatchKey, Path> folderKeys;
+    FileFilter fileFilter;
+    String folder;
+    boolean isRecursive;
+    boolean scheduler = true;
+    long pollTimeout = 2000;
 
     public FileWatcherResourceReloadStrategy() {
         setRecursive(false);
@@ -95,6 +96,10 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
         this.isRecursive = isRecursive;
     }
 
+    public void setScheduler(boolean scheduler) {
+        this.scheduler = scheduler;
+    }
+
     /**
      * Sets the poll timeout in millis. The default value is 2000.
      */
@@ -136,6 +141,10 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
             // no folder configured
             return;
         }
+        if (!scheduler) {
+            // do not start scheduler so exit start phase
+            return;
+        }
 
         File dir = new File(folder);
         if (dir.exists() && dir.isDirectory()) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RouteOnDemandReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/RouteOnDemandReloadStrategy.java
new file mode 100644
index 00000000000..6012c9e564a
--- /dev/null
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RouteOnDemandReloadStrategy.java
@@ -0,0 +1,112 @@
+/*
+ * 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.support;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.api.management.ManagedOperation;
+import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.util.FileUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Strategy for triggering on-demand reloading of Camel routes in a running Camel application. The strategy is triggered
+ * on-demand and reload all files from a directory (and subdirectories).
+ */
+@ManagedResource(description = "Managed RouteOnDemandReloadStrategy")
+public class RouteOnDemandReloadStrategy extends RouteWatcherReloadStrategy {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RouteOnDemandReloadStrategy.class);
+
+    public RouteOnDemandReloadStrategy() {
+        setScheduler(false);
+    }
+
+    public RouteOnDemandReloadStrategy(String directory) {
+        super(directory);
+        setScheduler(false);
+    }
+
+    public RouteOnDemandReloadStrategy(String directory, boolean recursive) {
+        super(directory, recursive);
+        setScheduler(false);
+    }
+
+    /**
+     * Triggers on-demand reloading
+     */
+    @ManagedOperation(description = "Trigger on-demand reloading")
+    public void onReload() {
+        onReload("JMX Management");
+    }
+
+    /**
+     * Triggers on-demand reloading
+     */
+    public void onReload(Object source) {
+        try {
+            doOnReload(source);
+            incSucceededCounter();
+        } catch (Exception e) {
+            incFailedCounter();
+            LOG.warn("Error reloading routes due " + e.getMessage()
+                     + ". This exception is ignored.",
+                    e);
+        }
+    }
+
+    protected void doOnReload(Object source) throws Exception {
+        List<Resource> properties = new ArrayList<>();
+        List<Resource> routes = new ArrayList<>();
+
+        File dir = new File(getFolder());
+        for (Path path : ResourceHelper.findInFileSystem(dir.toPath(), getPattern())) {
+            Resource res = ResourceHelper.resolveResource(getCamelContext(), "file:" + path.toString());
+            String ext = FileUtil.onlyExt(path.getFileName().toString());
+            if ("properties".equals(ext)) {
+                properties.add(res);
+            } else {
+                routes.add(res);
+            }
+        }
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("On-demand reload scanned {} files (properties: {}, routes: {})",
+                    properties.size() + routes.size(), properties.size(), routes.size());
+        }
+
+        // reload properties first
+        boolean reloaded = false;
+        for (Resource res : properties) {
+            reloaded |= onPropertiesReload(res, false);
+        }
+        boolean removeEverything = routes.isEmpty();
+        if (reloaded || !routes.isEmpty()) {
+            // trigger routes to also reload if properties was reloaded
+            onRouteReload(routes, removeEverything);
+        } else {
+            // rare situation where all routes are deleted
+            onRouteReload(null, removeEverything);
+        }
+    }
+
+}
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 c4b78fa1290..00707899e7b 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
@@ -146,9 +146,9 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
             // attach listener that triggers the route update
             setResourceReload((name, resource) -> {
                 if (name.endsWith(".properties")) {
-                    onPropertiesReload(resource);
+                    onPropertiesReload(resource, true);
                 } else {
-                    onRouteReload(resource);
+                    onRouteReload(List.of(resource), true);
                 }
             });
         }
@@ -161,7 +161,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
         return "Live route reloading enabled (directory: " + dir + ")";
     }
 
-    protected void onPropertiesReload(Resource resource) throws Exception {
+    protected boolean onPropertiesReload(Resource resource, boolean reloadRoutes) throws Exception {
         LOG.info("Reloading properties: {}. (Only Camel routes and components can be updated with changes)",
                 resource.getLocation());
 
@@ -182,19 +182,21 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
             pc.keepOnlyChangeProperties(changed);
         }
 
-        if (changed == null || !changed.isEmpty()) {
-            boolean reloaded = pc.reloadProperties(resource.getLocation());
+        boolean reloaded = false;
+        if (changed != null && !changed.isEmpty()) {
+            reloaded = pc.reloadProperties(resource.getLocation());
             if (reloaded) {
-                if (pr != null) {
-                    pr.onReload(resource.getLocation(), changed);
-                }
+                pr.onReload(resource.getLocation(), changed);
                 // trigger all routes to be reloaded
-                onRouteReload(null);
+                if (reloadRoutes) {
+                    onRouteReload(null, true);
+                }
             }
         }
+        return reloaded;
     }
 
-    protected void onRouteReload(Resource resource) {
+    protected void onRouteReload(Collection<Resource> resources, boolean removeEverything) {
         // remember all existing resources
         List<Resource> sources = new ArrayList<>();
 
@@ -203,7 +205,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
             // to the last working set
             previousSources.forEach(rs -> {
                 // remember all the sources of the current routes (except the updated)
-                if (rs != null && !equalResourceLocation(resource, rs)) {
+                if (rs != null && !equalResourceLocation(resources, rs)) {
                     sources.add(rs);
                 }
             });
@@ -215,7 +217,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                 // remember all the sources of the current routes (except the updated)
                 getCamelContext().getRoutes().forEach(r -> {
                     Resource rs = r.getSourceResource();
-                    if (rs != null && !equalResourceLocation(resource, rs)) {
+                    if (rs != null && !equalResourceLocation(resources, rs)) {
                         sources.add(rs);
                     }
                 });
@@ -226,8 +228,12 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                 getCamelContext().getEndpointRegistry().clear();
             }
 
-            if (resource != null && Files.exists(Paths.get(resource.getURI()))) {
-                sources.add(resource);
+            if (resources != null) {
+                for (Resource resource : resources) {
+                    if (Files.exists(Paths.get(resource.getURI()))) {
+                        sources.add(resource);
+                    }
+                }
             }
 
             Collection<Resource> extras
@@ -245,6 +251,11 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
             previousSources.clear();
             previousSources.addAll(sources);
 
+            // special situation where we remove all routes
+            if (removeEverything) {
+                sources.clear();
+            }
+
             // reload those other routes that was stopped and removed as we want to keep running those
             Set<String> ids
                     = PluginHelper.getRoutesLoader(getCamelContext()).updateRoutes(sources);
@@ -311,6 +322,21 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
         }
     }
 
+    /**
+     * Whether the target is loading any of the given sources
+     */
+    private static boolean equalResourceLocation(Collection<Resource> sources, Resource target) {
+        if (sources == null || target == null || sources.isEmpty()) {
+            return false;
+        }
+        for (Resource source : sources) {
+            if (equalResourceLocation(source, target)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * Whether the two resources are loading the same resource
      */
diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index 6dd479e5bd0..fc0fa543adb 100644
--- a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -53,11 +53,14 @@ import org.apache.camel.console.DevConsoleRegistry;
 import org.apache.camel.spi.CliConnector;
 import org.apache.camel.spi.CliConnectorFactory;
 import org.apache.camel.spi.ContextReloadStrategy;
+import org.apache.camel.spi.ResourceReloadStrategy;
 import org.apache.camel.support.DefaultContextReloadStrategy;
 import org.apache.camel.support.EndpointHelper;
 import org.apache.camel.support.MessageHelper;
 import org.apache.camel.support.PatternHelper;
 import org.apache.camel.support.PluginHelper;
+import org.apache.camel.support.RouteOnDemandReloadStrategy;
+import org.apache.camel.support.RouteWatcherReloadStrategy;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.FileUtil;
@@ -264,12 +267,25 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
             } else if ("gc".equals(action)) {
                 System.gc();
             } else if ("reload".equals(action)) {
-                ContextReloadStrategy reloader = camelContext.hasService(ContextReloadStrategy.class);
-                if (reloader == null) {
-                    reloader = new DefaultContextReloadStrategy();
-                    camelContext.addService(reloader);
+                Optional<String> dir = camelContext.getPropertiesComponent().resolveProperty("camel.jbang.sourceDir");
+                if (dir.isPresent()) {
+                    // if using source-dir then reload this specific directory
+                    RouteOnDemandReloadStrategy reloader = camelContext.hasService(RouteOnDemandReloadStrategy.class);
+                    if (reloader == null) {
+                        reloader = new RouteOnDemandReloadStrategy(dir.get(), true);
+                        reloader.setPattern("*");
+                        camelContext.addService(reloader);
+                    }
+                    reloader.onReload("Camel CLI");
+                } else {
+                    // general camel reloading
+                    ContextReloadStrategy reloader = camelContext.hasService(ContextReloadStrategy.class);
+                    if (reloader == null) {
+                        reloader = new DefaultContextReloadStrategy();
+                        camelContext.addService(reloader);
+                    }
+                    reloader.onReload("Camel CLI");
                 }
-                reloader.onReload("Camel CLI");
             } else if ("reset-stats".equals(action)) {
                 ManagedCamelContext mcc = camelContext.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class);
                 if (mcc != null) {
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 55f8836eadb..5385fca1ec2 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
@@ -540,6 +540,7 @@ public class Run extends CamelCommand {
         if (dev && (sourceDir != null || sjReload.length() > 0)) {
             main.addInitialProperty("camel.main.routesReloadEnabled", "true");
             if (sourceDir != null) {
+                main.addInitialProperty("camel.jbang.sourceDir", sourceDir);
                 main.addInitialProperty("camel.main.routesReloadDirectory", sourceDir);
                 main.addInitialProperty("camel.main.routesReloadPattern", "*");
                 main.addInitialProperty("camel.main.routesReloadDirectoryRecursive", "true");


[camel] 02/02: Fix CS

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 5b836fe80eab73ccad4f23877ab52e2161e7576e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 5 10:31:23 2023 +0200

    Fix CS
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 56 ++++++++++++----------
 1 file changed, 30 insertions(+), 26 deletions(-)

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 5385fca1ec2..f7a42879d57 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
@@ -537,32 +537,7 @@ public class Run extends CamelCommand {
         }
 
         // we can only reload if file based
-        if (dev && (sourceDir != null || sjReload.length() > 0)) {
-            main.addInitialProperty("camel.main.routesReloadEnabled", "true");
-            if (sourceDir != null) {
-                main.addInitialProperty("camel.jbang.sourceDir", sourceDir);
-                main.addInitialProperty("camel.main.routesReloadDirectory", sourceDir);
-                main.addInitialProperty("camel.main.routesReloadPattern", "*");
-                main.addInitialProperty("camel.main.routesReloadDirectoryRecursive", "true");
-            } else {
-                String pattern = sjReload.toString();
-                String reloadDir = ".";
-                // use current dir, however if we run a file that are in another folder, then we should track that folder instead
-                for (String r : sjReload.toString().split(",")) {
-                    String path = FileUtil.onlyPath(r);
-                    if (path != null) {
-                        reloadDir = path;
-                        break;
-                    }
-                }
-                main.addInitialProperty("camel.main.routesReloadDirectory", reloadDir);
-                main.addInitialProperty("camel.main.routesReloadPattern", pattern);
-                main.addInitialProperty("camel.main.routesReloadDirectoryRecursive",
-                        isReloadRecursive(pattern) ? "true" : "false");
-            }
-            // do not shutdown the JVM but stop routes when max duration is triggered
-            main.addInitialProperty("camel.main.durationMaxAction", "stop");
-        }
+        setupReload(main, sjReload);
 
         if (propertiesFiles != null) {
             String[] filesLocation = propertiesFiles.split(",");
@@ -611,6 +586,35 @@ public class Run extends CamelCommand {
         }
     }
 
+    private void setupReload(KameletMain main, StringJoiner sjReload) {
+        if (dev && (sourceDir != null || sjReload.length() > 0)) {
+            main.addInitialProperty("camel.main.routesReloadEnabled", "true");
+            if (sourceDir != null) {
+                main.addInitialProperty("camel.jbang.sourceDir", sourceDir);
+                main.addInitialProperty("camel.main.routesReloadDirectory", sourceDir);
+                main.addInitialProperty("camel.main.routesReloadPattern", "*");
+                main.addInitialProperty("camel.main.routesReloadDirectoryRecursive", "true");
+            } else {
+                String pattern = sjReload.toString();
+                String reloadDir = ".";
+                // use current dir, however if we run a file that are in another folder, then we should track that folder instead
+                for (String r : sjReload.toString().split(",")) {
+                    String path = FileUtil.onlyPath(r);
+                    if (path != null) {
+                        reloadDir = path;
+                        break;
+                    }
+                }
+                main.addInitialProperty("camel.main.routesReloadDirectory", reloadDir);
+                main.addInitialProperty("camel.main.routesReloadPattern", pattern);
+                main.addInitialProperty("camel.main.routesReloadDirectoryRecursive",
+                        isReloadRecursive(pattern) ? "true" : "false");
+            }
+            // do not shutdown the JVM but stop routes when max duration is triggered
+            main.addInitialProperty("camel.main.durationMaxAction", "stop");
+        }
+    }
+
     private Properties loadProfileProperties() throws Exception {
         Properties answer = null;