This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit c8af27c55e3338e4b3f7f0c1949bb8a3c21e896c Author: Pasquale Congiusti <pasquale.congiu...@gmail.com> AuthorDate: Wed Jul 17 17:44:11 2019 +0200 CAMEL-13764 component connection setting --- .../src/main/docs/mongodb3-component.adoc | 3 ++- .../camel/component/mongodb3/MongoDbComponent.java | 26 +++++++++++++++++++++- .../mongodb3/MongoDbConnectionBeansTest.java | 14 +++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc b/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc index a03c1f5..74c061c 100644 --- a/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc +++ b/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc @@ -55,13 +55,14 @@ mongodb3:connectionBean?database=databaseName&collection=collectionName&operatio // component options: START -The MongoDB component supports 2 options, which are listed below. +The MongoDB component supports 3 options, which are listed below. [width="100%",cols="2,5,^1,2",options="header"] |=== | Name | Description | Default | Type +| *mongoConnection* (common) | Set the client used for connection | | MongoClient | *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean |=== diff --git a/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbComponent.java b/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbComponent.java index 476a548..0463b1e 100644 --- a/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbComponent.java +++ b/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbComponent.java @@ -21,6 +21,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import com.mongodb.MongoClient; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.spi.annotations.Component; @@ -38,6 +39,11 @@ public class MongoDbComponent extends DefaultComponent { MongoDbOperation.update, MongoDbOperation.remove)); + /** + * A connection client provided externally + */ + private MongoClient mongoConnection; + public MongoDbComponent() { this(null); } @@ -47,14 +53,32 @@ public class MongoDbComponent extends DefaultComponent { } protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - MongoDbEndpoint endpoint = new MongoDbEndpoint(uri, this); endpoint.setConnectionBean(remaining); + endpoint.setMongoConnection(mongoConnection); setProperties(endpoint, parameters); return endpoint; } + /** + * Get the client used for connection + * + * @return the client using for connection to db + */ + public MongoClient getMongoConnection() { + return mongoConnection; + } + + /** + * Set the client used for connection + * + * @param mongoConnection + */ + public void setMongoConnection(MongoClient mongoConnection) { + this.mongoConnection = mongoConnection; + } + @Override protected void doShutdown() throws Exception { super.doShutdown(); diff --git a/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java b/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java index 8809c7f..171bf10 100644 --- a/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java +++ b/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java @@ -21,6 +21,7 @@ import java.util.Properties; import com.mongodb.MongoClient; import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; import org.apache.camel.NoSuchBeanException; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.spring.SpringCamelContext; @@ -53,7 +54,7 @@ public class MongoDbConnectionBeansTest extends AbstractMongoDbTest { "mongodb3:myDb?mongoConnection=#myDbS&database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true", MongoDbEndpoint.class); assertEquals("myDb", testEndpoint.getConnectionBean()); - MongoClient myDbS = mongo = applicationContext.getBean("myDbS", MongoClient.class); + MongoClient myDbS = applicationContext.getBean("myDbS", MongoClient.class); assertEquals(myDbS, testEndpoint.getMongoConnection()); } @@ -64,4 +65,15 @@ public class MongoDbConnectionBeansTest extends AbstractMongoDbTest { MongoDbEndpoint.class); } + @Test + public void checkConnectionOnComponent() throws Exception { + MongoDbComponent component = context.getComponent("mongodb3", MongoDbComponent.class); + MongoClient myDbS = applicationContext.getBean("myDbS", MongoClient.class); + component.setMongoConnection(myDbS); + Endpoint endpoint = component.createEndpoint("mongodb3:justARouteName?database={{mongodb.testDb}}&collection=" + + "{{mongodb.testCollection}}&operation=count&dynamicity=true"); + assertIsInstanceOf(MongoDbEndpoint.class, endpoint); + assertEquals(myDbS, ((MongoDbEndpoint) endpoint).getMongoConnection()); + } + }