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/07 11:55:20 UTC

[camel] branch camel-3.x updated: CAMEL-19320: camel-jbang - Reload on demand

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

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


The following commit(s) were added to refs/heads/camel-3.x by this push:
     new 73a8e1e423f CAMEL-19320: camel-jbang - Reload on demand
73a8e1e423f is described below

commit 73a8e1e423f8f0e51e7d635d214bf1d2ca14b41c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun May 7 13:54:37 2023 +0200

    CAMEL-19320: camel-jbang - Reload on demand
---
 .../org/apache/camel/spi/PropertiesComponent.java  |  5 +++
 .../apache/camel/spi/PropertiesSourceFactory.java  | 44 ++++++++++++++++++++++
 .../properties/DefaultPropertiesSourceFactory.java | 44 ++++++++++++++++++++++
 .../component/properties/PropertiesComponent.java  |  7 ++++
 .../camel/support/RouteWatcherReloadStrategy.java  | 29 ++++++++++++++
 5 files changed, 129 insertions(+)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
index 2f3d3030400..0a201077fd1 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
@@ -163,6 +163,11 @@ public interface PropertiesComponent extends StaticService {
      */
     void addLocation(String location);
 
+    /**
+     * Gets the {@link PropertiesSourceFactory}.
+     */
+    PropertiesSourceFactory getPropertiesSourceFactory();
+
     /**
      * Adds a custom {@link PropertiesSource} to use as source for loading and/or looking up property values.
      */
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesSourceFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesSourceFactory.java
new file mode 100644
index 00000000000..bde47dd204b
--- /dev/null
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesSourceFactory.java
@@ -0,0 +1,44 @@
+/*
+ * 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;
+
+/**
+ * Factory for creating out of the box {@link PropertiesSource}.
+ */
+public interface PropertiesSourceFactory {
+
+    /**
+     * New file based {@link PropertiesSource}
+     *
+     * @param location location of the file
+     */
+    PropertiesSource newFilePropertiesSource(String location);
+
+    /**
+     * New classpath based {@link PropertiesSource}
+     *
+     * @param location location of the file in the classpath
+     */
+    PropertiesSource newClasspathPropertiesSource(String location);
+
+    /**
+     * New ref based {@link PropertiesSource}
+     *
+     * @param ref id for the {@link java.util.Properties} bean.
+     */
+    PropertiesSource newRefPropertiesSource(String ref);
+}
diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesSourceFactory.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesSourceFactory.java
new file mode 100644
index 00000000000..a1d43dcebd4
--- /dev/null
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesSourceFactory.java
@@ -0,0 +1,44 @@
+/*
+ * 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.component.properties;
+
+import org.apache.camel.spi.PropertiesSource;
+import org.apache.camel.spi.PropertiesSourceFactory;
+
+public class DefaultPropertiesSourceFactory implements PropertiesSourceFactory {
+
+    private final PropertiesComponent pc;
+
+    public DefaultPropertiesSourceFactory(PropertiesComponent pc) {
+        this.pc = pc;
+    }
+
+    @Override
+    public PropertiesSource newFilePropertiesSource(String location) {
+        return new FilePropertiesSource(pc, new PropertiesLocation("file", location));
+    }
+
+    @Override
+    public PropertiesSource newClasspathPropertiesSource(String location) {
+        return new ClasspathPropertiesSource(pc, new PropertiesLocation("classpath", location));
+    }
+
+    @Override
+    public PropertiesSource newRefPropertiesSource(String ref) {
+        return new RefPropertiesSource(pc, new PropertiesLocation("ref", ref));
+    }
+}
diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index dee58f6cccf..a67bc638cfb 100644
--- a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -40,6 +40,7 @@ import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.spi.PropertiesFunction;
 import org.apache.camel.spi.PropertiesSource;
+import org.apache.camel.spi.PropertiesSourceFactory;
 import org.apache.camel.spi.annotations.JdkService;
 import org.apache.camel.support.OrderedComparator;
 import org.apache.camel.support.PatternHelper;
@@ -111,6 +112,7 @@ public class PropertiesComponent extends ServiceSupport
     private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
     private final PropertiesLookup propertiesLookup = new DefaultPropertiesLookup(this);
     private final List<PropertiesLookupListener> propertiesLookupListeners = new ArrayList<>();
+    private final PropertiesSourceFactory propertiesSourceFactory = new DefaultPropertiesSourceFactory(this);
     private final List<PropertiesSource> sources = new ArrayList<>();
     private List<PropertiesLocation> locations = new ArrayList<>();
     private String location;
@@ -369,6 +371,11 @@ public class PropertiesComponent extends ServiceSupport
         setLocations(propertiesLocations);
     }
 
+    @Override
+    public PropertiesSourceFactory getPropertiesSourceFactory() {
+        return propertiesSourceFactory;
+    }
+
     public void addLocation(PropertiesLocation location) {
         this.locations.add(location);
     }
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 590061ca839..0dbc3467de3 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
@@ -24,6 +24,7 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.StringJoiner;
 
@@ -34,6 +35,7 @@ import org.apache.camel.ServiceStatus;
 import org.apache.camel.StartupSummaryLevel;
 import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.PropertiesReload;
+import org.apache.camel.spi.PropertiesSource;
 import org.apache.camel.spi.Resource;
 import org.apache.camel.util.AntPathMatcher;
 import org.apache.camel.util.FileUtil;
@@ -41,6 +43,7 @@ import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.OrderedLocationProperties;
 import org.apache.camel.util.OrderedProperties;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -192,11 +195,37 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                 if (reloadRoutes) {
                     onRouteReload(null, false);
                 }
+            } else {
+                // this may be a new properties file, so we need to add as new known location
+                String existing = getPropertiesByLocation(resource.getLocation());
+                if (existing == null) {
+                    // remove scheme
+                    String loc = resource.getLocation();
+                    if (loc.contains(":")) {
+                        loc = StringHelper.after(loc, ":");
+                    }
+                    PropertiesSource ps = pc.getPropertiesSourceFactory().newFilePropertiesSource(loc);
+                    pc.addPropertiesSource(ps);
+                    reloaded = true;
+                }
             }
         }
         return reloaded;
     }
 
+    private String getPropertiesByLocation(String loc) {
+        PropertiesComponent pc = getCamelContext().getPropertiesComponent();
+        for (String s : pc.getLocations()) {
+            if (s.endsWith(";optional=true")) {
+                s = s.substring(0, s.length() - 14);
+            }
+            if (Objects.equals(s, loc)) {
+                return loc;
+            }
+        }
+        return null;
+    }
+
     protected void onRouteReload(Collection<Resource> resources, boolean removeEverything) {
         // remember all existing resources
         List<Resource> sources = new ArrayList<>();