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 2018/07/12 08:11:53 UTC
[camel] branch master updated: CAMEL-12645: camel-jdbc - Can now
use default datasource from registry if using dataSource or default as the
name. This is a bit similar to camel-sql and makes it easier to use in
Spring Boot.
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 63df502 CAMEL-12645: camel-jdbc - Can now use default datasource from registry if using dataSource or default as the name. This is a bit similar to camel-sql and makes it easier to use in Spring Boot.
63df502 is described below
commit 63df502b91503bc54e08b76f7d8f3e4c48a95e7f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jul 12 10:11:39 2018 +0200
CAMEL-12645: camel-jdbc - Can now use default datasource from registry if using dataSource or default as the name. This is a bit similar to camel-sql and makes it easier to use in Spring Boot.
---
.../camel-jdbc/src/main/docs/jdbc-component.adoc | 2 +-
.../apache/camel/component/jdbc/JdbcComponent.java | 41 ++++++++++++-----
.../apache/camel/component/jdbc/JdbcEndpoint.java | 4 +-
.../component/jdbc/JdbcDefaultDataSourceTest.java | 53 ++++++++++++++++++++++
.../apache/camel/component/sql/SqlComponent.java | 5 ++
5 files changed, 91 insertions(+), 14 deletions(-)
diff --git a/components/camel-jdbc/src/main/docs/jdbc-component.adoc b/components/camel-jdbc/src/main/docs/jdbc-component.adoc
index b930bc6..1fdb4ad 100644
--- a/components/camel-jdbc/src/main/docs/jdbc-component.adoc
+++ b/components/camel-jdbc/src/main/docs/jdbc-component.adoc
@@ -75,7 +75,7 @@ with the following path and query parameters:
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *dataSourceName* | *Required* Name of DataSource to lookup in the Registry. | | String
+| *dataSourceName* | *Required* Name of DataSource to lookup in the Registry. If the name is dataSource or default, then Camel will attempt to lookup a default DataSource from the registry, meaning if there is a only one instance of DataSource found, then this DataSource will be used. | | String
|===
diff --git a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
index a43616c..e5f65a2 100644
--- a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
+++ b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
@@ -17,27 +17,24 @@
package org.apache.camel.component.jdbc;
import java.util.Map;
+import java.util.Set;
import javax.sql.DataSource;
-import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
-import org.apache.camel.impl.UriEndpointComponent;
+import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.impl.DefaultComponent;
import org.apache.camel.util.CamelContextHelper;
import org.apache.camel.util.IntrospectionSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @version
*/
-public class JdbcComponent extends UriEndpointComponent {
- private DataSource dataSource;
-
- public JdbcComponent() {
- super(JdbcEndpoint.class);
- }
+public class JdbcComponent extends DefaultComponent {
- public JdbcComponent(CamelContext context) {
- super(context, JdbcEndpoint.class);
- }
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcComponent.class);
+ private DataSource dataSource;
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
@@ -49,7 +46,23 @@ public class JdbcComponent extends UriEndpointComponent {
dataSource = this.dataSource;
dataSourceRef = "component";
} else {
- dataSource = CamelContextHelper.mandatoryLookup(getCamelContext(), remaining, DataSource.class);
+ DataSource target = CamelContextHelper.lookup(getCamelContext(), remaining, DataSource.class);
+ if (target == null && !isDefaultDataSourceName(remaining)) {
+ throw new NoSuchBeanException(remaining, DataSource.class.getName());
+ } else if (target == null) {
+ // check if the registry contains a single instance of DataSource
+ Set<DataSource> dataSources = getCamelContext().getRegistry().findByType(DataSource.class);
+ if (dataSources.size() > 1) {
+ throw new IllegalArgumentException("Multiple DataSources found in the registry and no explicit configuration provided");
+ } else if (dataSources.size() == 1) {
+ target = dataSources.stream().findFirst().orElse(null);
+ }
+ if (target == null) {
+ throw new IllegalArgumentException("No default DataSource found in the registry");
+ }
+ LOG.debug("Using default DataSource discovered from registry: {}", target);
+ }
+ dataSource = target;
dataSourceRef = remaining;
}
@@ -69,4 +82,8 @@ public class JdbcComponent extends UriEndpointComponent {
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
+
+ private static boolean isDefaultDataSourceName(String remaining) {
+ return "dataSource".equalsIgnoreCase(remaining) || "default".equalsIgnoreCase(remaining);
+ }
}
diff --git a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
index d117c52..2d14575 100644
--- a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
+++ b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
@@ -90,7 +90,9 @@ public class JdbcEndpoint extends DefaultEndpoint {
}
/**
- * Name of DataSource to lookup in the Registry.
+ * Name of DataSource to lookup in the Registry. If the name is dataSource or default, then Camel
+ * will attempt to lookup a default DataSource from the registry, meaning if there is a only
+ * one instance of DataSource found, then this DataSource will be used.
*/
public void setDataSourceName(String dataSourceName) {
this.dataSourceName = dataSourceName;
diff --git a/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcDefaultDataSourceTest.java b/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcDefaultDataSourceTest.java
new file mode 100644
index 0000000..543669e
--- /dev/null
+++ b/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcDefaultDataSourceTest.java
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jdbc;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class JdbcDefaultDataSourceTest extends AbstractJdbcTestSupport {
+
+ @EndpointInject(uri = "mock:result")
+ private MockEndpoint mock;
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void testReadSize() throws Exception {
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", "select * from customer");
+
+ assertMockEndpointsSatisfied();
+
+ List list = mock.getExchanges().get(0).getIn().getBody(ArrayList.class);
+ assertEquals(1, list.size());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:start").to("jdbc:dataSource?readSize=1").to("mock:result");
+ }
+ };
+ }
+}
\ No newline at end of file
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
index d26511a..d995363 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
@@ -26,6 +26,8 @@ import org.apache.camel.impl.UriEndpointComponent;
import org.apache.camel.spi.Metadata;
import org.apache.camel.util.CamelContextHelper;
import org.apache.camel.util.IntrospectionSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
/**
@@ -34,6 +36,8 @@ import org.springframework.jdbc.core.JdbcTemplate;
*/
public class SqlComponent extends UriEndpointComponent {
+ private static final Logger LOG = LoggerFactory.getLogger(SqlComponent.class);
+
private DataSource dataSource;
@Metadata(label = "advanced", defaultValue = "true")
private boolean usePlaceholder = true;
@@ -83,6 +87,7 @@ public class SqlComponent extends UriEndpointComponent {
if (target == null) {
throw new IllegalArgumentException("DataSource must be configured");
}
+ LOG.debug("Using default DataSource discovered from registry: {}", target);
String parameterPlaceholderSubstitute = getAndRemoveParameter(parameters, "placeholder", String.class, "#");