Skip to content

Commit

Permalink
camel: fix null pointer exceptions
Browse files Browse the repository at this point in the history
- when camel context is provided
- when looking bean by name
- Add more constructors to CamelModule
  • Loading branch information
jknack committed May 6, 2023
1 parent 6875b8c commit 96f5834
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion modules/jooby-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.36.0</version>
<version>2.27.2</version>
<configuration>
<skip>true</skip>
</configuration>
Expand Down
51 changes: 43 additions & 8 deletions modules/jooby-camel/src/main/java/io/jooby/camel/CamelModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ public class CamelModule implements Extension {

private CamelContext camel;

/** Creates module using the {@link CamelContext}. */
public CamelModule() {}

/**
* Creates module using the {@link CamelContext}.
*
* @param camel Camel context.
*/
public CamelModule(CamelContext camel) {
public CamelModule(@NonNull CamelContext camel) {
this.camel = camel;
}

Expand All @@ -76,7 +79,19 @@ public CamelModule(CamelContext camel) {
* @param route Route configuration.
* @param routes Optional route configuration.
*/
public CamelModule(RouteBuilder route, RouteBuilder... routes) {
public CamelModule(@NonNull RouteBuilder route, RouteBuilder... routes) {
this(null, route, routes);
}

/**
* Creates a new camel module adding one or more routes.
*
* @param route Route configuration.
* @param routes Optional route configuration.
*/
public CamelModule(
@NonNull CamelContext camel, @NonNull RouteBuilder route, RouteBuilder... routes) {
this.camel = camel;
this.routes = registry -> concat(route, routes).collect(Collectors.toList());
}

Expand All @@ -88,11 +103,29 @@ public CamelModule(RouteBuilder route, RouteBuilder... routes) {
* @param route Route configuration.
* @param routes Optional route configuration.
*/
public CamelModule(Class<? extends RouteBuilder> route, Class<? extends RouteBuilder>... routes) {
public CamelModule(
@NonNull Class<? extends RouteBuilder> route,
@NonNull Class<? extends RouteBuilder>... routes) {
this(null, route, routes);
}

/**
* Creates a new camel module adding one or more routes. Route provisioning is delegated to
* Dependency Injection framework (if any), otherwise camel does basic/minimal injection using
* {@link org.apache.camel.impl.engine.DefaultInjector}.
*
* @param route Route configuration.
* @param routes Optional route configuration.
*/
public CamelModule(
@NonNull CamelContext camel,
@NonNull Class<? extends RouteBuilder> route,
Class<? extends RouteBuilder>... routes) {
this.camel = camel;
this.routes =
camel ->
context ->
concat(route, routes)
.map(type -> camel.getInjector().newInstance(type))
.map(type -> context.getInjector().newInstance(type))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -130,9 +163,11 @@ public void install(@NonNull Jooby application) throws Exception {
main.init();

// Do routes
List<RouteBuilder> routeList = routes.apply(camel);
for (RouteBuilder route : routeList) {
camel.addRoutes(route);
if (routes != null) {
List<RouteBuilder> routeList = routes.apply(camel);
for (RouteBuilder route : routeList) {
camel.addRoutes(route);
}
}
// Start camel:
main.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ private <T> Object getBean(ServiceKey<T> key) {
}

private Provider beanByName(String beanId) {
return findBean(
var entry =
findBean(
key ->
ofNullable(key.getName())
.orElseGet(() -> camelBeanId(key.getType()))
.equals(beanId))
.getValue();
.equals(beanId));
return entry == null ? null : entry.getValue();
}

private Map.Entry<ServiceKey<?>, Provider<?>> beanByType(Class type) {
Expand Down

0 comments on commit 96f5834

Please sign in to comment.