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
The following commit(s) were added to refs/heads/main by this push: new bd55bc90e96 CAMEL-19320: camel-jbang - Reload on demand bd55bc90e96 is described below commit bd55bc90e96a40af5183bd7b2af9a35eb6fafc54 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 | 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 8089126c123..3252d2fb516 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 @@ -38,6 +38,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; @@ -109,6 +110,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; @@ -367,6 +369,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 df932199758..0e1145a4e7c 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; @@ -33,6 +34,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; @@ -40,6 +42,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; @@ -191,11 +194,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<>();