This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
commit 42d86cc741df881e6cda77564643643f9f3e3209 Author: Pasquale Congiusti <pasquale.congiu...@gmail.com> AuthorDate: Tue Apr 13 14:27:03 2021 +0200 fix(core): NPE on error handler source sorting --- .../main/java/org/apache/camel/k/listener/SourcesConfigurer.java | 7 +++++-- .../src/main/java/org/apache/camel/k/support/SourcesSupport.java | 6 +++--- .../java/org/apache/camel/k/listener/SourceConfigurerTest.java | 6 ++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/listener/SourcesConfigurer.java b/camel-k-core/support/src/main/java/org/apache/camel/k/listener/SourcesConfigurer.java index 95ddd92..4360ed2 100644 --- a/camel-k-core/support/src/main/java/org/apache/camel/k/listener/SourcesConfigurer.java +++ b/camel-k-core/support/src/main/java/org/apache/camel/k/listener/SourcesConfigurer.java @@ -86,8 +86,8 @@ public class SourcesConfigurer extends AbstractPhaseListener { } static void checkUniqueErrorHandler(SourceDefinition[] sources) { - long errorHandlers = Arrays.stream(sources).filter(s -> s.getType() == SourceType.errorHandler).count(); - if ( errorHandlers > 1) { + long errorHandlers = sources == null ? 0 : Arrays.stream(sources).filter(s -> s.getType() == SourceType.errorHandler).count(); + if (errorHandlers > 1) { throw new IllegalArgumentException("Expected only one error handler source type, got " + errorHandlers); } } @@ -97,6 +97,9 @@ public class SourcesConfigurer extends AbstractPhaseListener { } static void sortSources(SourceDefinition[] sources) { + if (sources == null) { + return; + } // We must ensure the following source type order: errorHandler, source, template Arrays.sort(sources, (a, b) -> { diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java b/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java index 029b4d3..c98744d 100644 --- a/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java +++ b/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java @@ -64,7 +64,7 @@ public final class SourcesSupport { } public static void loadSources(Runtime runtime, String... routes) { - for (String route: routes) { + for (String route : routes) { if (ObjectHelper.isEmpty(route)) { continue; } @@ -80,7 +80,7 @@ public final class SourcesSupport { } public static void loadSources(Runtime runtime, SourceDefinition... definitions) { - for (SourceDefinition definition: definitions) { + for (SourceDefinition definition : definitions) { LOGGER.info("Loading routes from: {}", definition); load(runtime, Sources.fromDefinition(definition)); @@ -181,7 +181,7 @@ public final class SourcesSupport { f.setAccessible(true); ErrorHandlerBuilder privateErrorHandlerBuilder = (ErrorHandlerBuilder) f.get(builder); return privateErrorHandlerBuilder != null; - } catch (Exception e){ + } catch (Exception e) { throw new IllegalArgumentException("Something went wrong while checking the error handler builder", e); } } diff --git a/camel-k-core/support/src/test/java/org/apache/camel/k/listener/SourceConfigurerTest.java b/camel-k-core/support/src/test/java/org/apache/camel/k/listener/SourceConfigurerTest.java index ebdd22c..44f9f7c 100644 --- a/camel-k-core/support/src/test/java/org/apache/camel/k/listener/SourceConfigurerTest.java +++ b/camel-k-core/support/src/test/java/org/apache/camel/k/listener/SourceConfigurerTest.java @@ -111,4 +111,10 @@ public class SourceConfigurerTest { assertThat(configuration.getSources()[2].getName()).contains("source"); assertThat(configuration.getSources()[3].getName()).isEqualTo("template1"); } + + @Test + public void shouldNotFailOnEmptySources() { + SourcesConfigurer.sortSources(null); + SourcesConfigurer.checkUniqueErrorHandler(null); + } }