You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/02/11 16:38:25 UTC

[camel] branch master updated: CAMEL-16179: prevent a NPE on the MongoDB component if unable to resolve the DB connection (#5073)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e53235b  CAMEL-16179: prevent a NPE on the MongoDB component if unable to resolve the DB connection (#5073)
e53235b is described below

commit e53235ba80bb6ad43e786380edb207f526b105bc
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Thu Feb 11 17:33:09 2021 +0100

    CAMEL-16179: prevent a NPE on the MongoDB component if unable to resolve the DB connection (#5073)
---
 .../camel/catalog/docs/mongodb-component.adoc       |  2 +-
 .../mongodb/MongoDbComponentConfigurer.java         |  5 +++++
 .../org/apache/camel/component/mongodb/mongodb.json |  2 +-
 .../src/main/docs/mongodb-component.adoc            |  2 +-
 .../camel/component/mongodb/MongoDbComponent.java   |  2 +-
 .../camel/component/mongodb/MongoDbEndpoint.java    | 21 ++++++++++++++++++---
 .../modules/ROOT/pages/mongodb-component.adoc       |  2 +-
 7 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
index 7d1f627..3946a2e 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
@@ -73,7 +73,7 @@ The MongoDB component supports 4 options, which are listed below.
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *mongoConnection* (common) | Shared client used for connection. All endpoints generated from the component will share this connection client. |  | MongoClient
+| *mongoConnection* (common) | *Autowired* Shared client used for connection. All endpoints generated from the component will share this connection client. |  | MongoClient
 | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *autowiredEnabled* (advanced) | Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean
diff --git a/components/camel-mongodb/src/generated/java/org/apache/camel/component/mongodb/MongoDbComponentConfigurer.java b/components/camel-mongodb/src/generated/java/org/apache/camel/component/mongodb/MongoDbComponentConfigurer.java
index 43967c7..42a45fc 100644
--- a/components/camel-mongodb/src/generated/java/org/apache/camel/component/mongodb/MongoDbComponentConfigurer.java
+++ b/components/camel-mongodb/src/generated/java/org/apache/camel/component/mongodb/MongoDbComponentConfigurer.java
@@ -34,6 +34,11 @@ public class MongoDbComponentConfigurer extends PropertyConfigurerSupport implem
     }
 
     @Override
+    public String[] getAutowiredNames() {
+        return new String[]{"mongoConnection"};
+    }
+
+    @Override
     public Class<?> getOptionType(String name, boolean ignoreCase) {
         switch (ignoreCase ? name.toLowerCase() : name) {
         case "autowiredenabled":
diff --git a/components/camel-mongodb/src/generated/resources/org/apache/camel/component/mongodb/mongodb.json b/components/camel-mongodb/src/generated/resources/org/apache/camel/component/mongodb/mongodb.json
index c93cef7..7c9e47e 100644
--- a/components/camel-mongodb/src/generated/resources/org/apache/camel/component/mongodb/mongodb.json
+++ b/components/camel-mongodb/src/generated/resources/org/apache/camel/component/mongodb/mongodb.json
@@ -22,7 +22,7 @@
     "lenientProperties": false
   },
   "componentProperties": {
-    "mongoConnection": { "kind": "property", "displayName": "Mongo Connection", "group": "common", "label": "", "required": false, "type": "object", "javaType": "com.mongodb.client.MongoClient", "deprecated": false, "autowired": false, "secret": false, "description": "Shared client used for connection. All endpoints generated from the component will share this connection client." },
+    "mongoConnection": { "kind": "property", "displayName": "Mongo Connection", "group": "common", "label": "", "required": false, "type": "object", "javaType": "com.mongodb.client.MongoClient", "deprecated": false, "autowired": true, "secret": false, "description": "Shared client used for connection. All endpoints generated from the component will share this connection client." },
     "bridgeErrorHandler": { "kind": "property", "displayName": "Bridge Error Handler", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a me [...]
     "lazyStartProducer": { "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during star [...]
     "autowiredEnabled": { "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which t [...]
diff --git a/components/camel-mongodb/src/main/docs/mongodb-component.adoc b/components/camel-mongodb/src/main/docs/mongodb-component.adoc
index 7d1f627..3946a2e 100644
--- a/components/camel-mongodb/src/main/docs/mongodb-component.adoc
+++ b/components/camel-mongodb/src/main/docs/mongodb-component.adoc
@@ -73,7 +73,7 @@ The MongoDB component supports 4 options, which are listed below.
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *mongoConnection* (common) | Shared client used for connection. All endpoints generated from the component will share this connection client. |  | MongoClient
+| *mongoConnection* (common) | *Autowired* Shared client used for connection. All endpoints generated from the component will share this connection client. |  | MongoClient
 | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *autowiredEnabled* (advanced) | Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java
index d7bce69..de62f28 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java
@@ -40,7 +40,7 @@ public class MongoDbComponent extends DefaultComponent {
                     MongoDbOperation.update,
                     MongoDbOperation.remove));
 
-    @Metadata
+    @Metadata(autowired = true)
     private MongoClient mongoConnection;
 
     public MongoDbComponent() {
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
index 7269465..76ddc9a 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
@@ -232,6 +232,15 @@ public class MongoDbEndpoint extends DefaultEndpoint {
         if (database == null || (collection == null && !(getDbStats.equals(operation) || command.equals(operation)))) {
             throw new CamelMongoDbException("Missing required endpoint configuration: database and/or collection");
         }
+
+        if (mongoConnection == null) {
+            mongoConnection = resolveMongoConnection();
+            if (mongoConnection == null) {
+                throw new CamelMongoDbException(
+                        "Could not initialise MongoDbComponent. Could not resolve the mongo connection.");
+            }
+        }
+
         mongoDatabase = mongoConnection.getDatabase(database);
         if (mongoDatabase == null) {
             throw new CamelMongoDbException("Could not initialise MongoDbComponent. Database " + database + " does not exist.");
@@ -322,15 +331,21 @@ public class MongoDbEndpoint extends DefaultEndpoint {
     @Override
     protected void doStart() throws Exception {
         if (mongoConnection == null) {
-            mongoConnection = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, MongoClient.class);
-            LOG.debug("Resolved the connection provided by {} context reference as {}", connectionBean,
-                    mongoConnection);
+            mongoConnection = resolveMongoConnection();
         } else {
             LOG.debug("Resolved the connection provided by mongoConnection property parameter as {}", mongoConnection);
         }
         super.doStart();
     }
 
+    private MongoClient resolveMongoConnection() {
+        MongoClient mongoClient = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, MongoClient.class);
+        LOG.debug("Resolved the connection provided by {} context reference as {}", connectionBean,
+                mongoConnection);
+
+        return mongoClient;
+    }
+
     public String getConnectionBean() {
         return connectionBean;
     }
diff --git a/docs/components/modules/ROOT/pages/mongodb-component.adoc b/docs/components/modules/ROOT/pages/mongodb-component.adoc
index 94460be..fb5791f 100644
--- a/docs/components/modules/ROOT/pages/mongodb-component.adoc
+++ b/docs/components/modules/ROOT/pages/mongodb-component.adoc
@@ -75,7 +75,7 @@ The MongoDB component supports 4 options, which are listed below.
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *mongoConnection* (common) | Shared client used for connection. All endpoints generated from the component will share this connection client. |  | MongoClient
+| *mongoConnection* (common) | *Autowired* Shared client used for connection. All endpoints generated from the component will share this connection client. |  | MongoClient
 | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *autowiredEnabled* (advanced) | Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean