This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.20.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.20.x by this push: new 44b87db848a CAMEL-19320: camel-jbang - Reload on demand 44b87db848a is described below commit 44b87db848a22492da8efb75b6dec826a58ffa35 Author: Claus Ibsen <claus.ib...@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 | 30 +++++++++++++++ 5 files changed, 130 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 a49fbc81bd6..eddcdcaab24 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 ecbe3f1269f..3a95fad2a7b 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 @@ -39,6 +39,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; @@ -110,6 +111,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; @@ -368,6 +370,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 90c773871d6..a9ba093012c 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 @@ -23,6 +23,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; @@ -32,10 +33,12 @@ import org.apache.camel.RuntimeCamelException; import org.apache.camel.ServiceStatus; import org.apache.camel.StartupSummaryLevel; import org.apache.camel.spi.PropertiesComponent; +import org.apache.camel.spi.PropertiesSource; import org.apache.camel.spi.Resource; import org.apache.camel.util.AntPathMatcher; import org.apache.camel.util.FileUtil; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -163,6 +166,20 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg PropertiesComponent pc = getCamelContext().getPropertiesComponent(); boolean reloaded = pc.reloadProperties(resource.getLocation()); + if (!reloaded) { + // 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; + } + } if (reloaded && reloadRoutes) { // trigger all routes to be reloaded onRouteReload(null, false); @@ -170,6 +187,19 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg 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<>();