Skip to content
Permalink
Browse files
fix(core): NPE on error handler source sorting
  • Loading branch information
squakez authored and lburgazzoli committed Apr 13, 2021
1 parent 36a1179 commit 42d86cc741df881e6cda77564643643f9f3e3209
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
@@ -86,8 +86,8 @@ private void checkUniqueErrorHandler() {
}

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 @@ private void sortSources() {
}

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) -> {
@@ -64,7 +64,7 @@ protected void accept(Runtime runtime) {
}

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 static void loadSources(Runtime runtime, String... routes) {
}

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 @@ static boolean existErrorHandler(RouteBuilder builder) {
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);
}
}
@@ -111,4 +111,10 @@ public void shouldOrderSourcesByType() {
assertThat(configuration.getSources()[2].getName()).contains("source");
assertThat(configuration.getSources()[3].getName()).isEqualTo("template1");
}

@Test
public void shouldNotFailOnEmptySources() {
SourcesConfigurer.sortSources(null);
SourcesConfigurer.checkUniqueErrorHandler(null);
}
}

0 comments on commit 42d86cc

Please sign in to comment.