This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 8d4714d CAMEL-16732: camel-sql - DataSource should be autowired
8d4714d is described below
commit 8d4714d4b9cca87c947b8dd3813ab5c556583db5
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Jun 18 12:41:56 2021 +0200
CAMEL-16732: camel-sql - DataSource should be autowired
---
.../apache/camel/catalog/docs/elsql-component.adoc | 4 +-
.../apache/camel/catalog/docs/sql-component.adoc | 4 +-
.../component/elsql/ElsqlComponentConfigurer.java | 5 ++
.../component/elsql/ElsqlEndpointConfigurer.java | 5 ++
.../org/apache/camel/component/elsql/elsql.json | 4 +-
.../camel-elsql/src/main/docs/elsql-component.adoc | 4 +-
.../camel/component/elsql/ElsqlComponent.java | 48 ++++++++---------
.../camel/component/elsql/ElsqlEndpoint.java | 30 ++++++-----
.../component/sql/SqlComponentConfigurer.java | 5 ++
.../camel/component/sql/SqlEndpointConfigurer.java | 5 ++
.../org/apache/camel/component/sql/sql.json | 4 +-
.../camel-sql/src/main/docs/sql-component.adoc | 4 +-
.../camel/component/sql/DefaultSqlEndpoint.java | 7 +--
.../apache/camel/component/sql/SqlComponent.java | 61 ++++++++--------------
.../apache/camel/component/sql/SqlEndpoint.java | 6 +--
.../modules/ROOT/pages/elsql-component.adoc | 4 +-
.../modules/ROOT/pages/sql-component.adoc | 4 +-
17 files changed, 107 insertions(+), 97 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elsql-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elsql-component.adoc
index 1485a1a..0930fd6 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elsql-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elsql-component.adoc
@@ -69,7 +69,7 @@ The ElSQL component supports 7 options, which are listed
below.
|===
| Name | Description | Default | Type
| *databaseVendor* (common) | To use a vendor specific
com.opengamma.elsql.ElSqlConfig. There are 7 enums and the value can be one of:
Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca | |
ElSqlDatabaseVendor
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database. | | DataSource
| *resourceUri* (common) | The resource file which contains the elsql SQL
statements to use. You can specify multiple resources separated by comma. The
resources are loaded on the classpath by default, you can prefix with file: to
load from file system. Notice you can set this option on the component and then
you do not have to configure this on the endpoint. | | String
| *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 [...]
@@ -106,7 +106,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *allowNamedParameters* (common) | Whether to allow using named parameters in
the queries. | true | boolean
| *databaseVendor* (common) | To use a vendor specific
com.opengamma.elsql.ElSqlConfig. There are 7 enums and the value can be one of:
Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca | |
ElSqlDatabaseVendor
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database at endpoint level. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database at endpoint level. | | DataSource
| *outputClass* (common) | Specify the full package and class name to use as
conversion when outputType=SelectOne. | | String
| *outputHeader* (common) | Store the query result in a header instead of the
message body. By default, outputHeader == null and the query result is stored
in the message body, any existing content in the message body is discarded. If
outputHeader is set, the value is used as the name of the header to store the
query result and the original message body is preserved. | | String
| *outputType* (common) | Make the output of consumer or producer to
SelectList as List of Map, or SelectOne as single Java object in the following
way: a) If the query has only single column, then that JDBC Column object is
returned. (such as SELECT COUNT( ) FROM PROJECT will return a Long object. b)
If the query has more than one column, then it will return a Map of that
result. c) If the outputClass is set, then it will convert the query result
into an Java bean object by calling all [...]
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sql-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sql-component.adoc
index fa009e4..58bbb1e 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sql-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sql-component.adoc
@@ -124,7 +124,7 @@ The SQL component supports 5 options, which are listed
below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database. | | DataSource
| *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
@@ -161,7 +161,7 @@ with the following path and query parameters:
|===
| Name | Description | Default | Type
| *allowNamedParameters* (common) | Whether to allow using named parameters in
the queries. | true | boolean
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database at endpoint level. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database at endpoint level. | | DataSource
| *outputClass* (common) | Specify the full package and class name to use as
conversion when outputType=SelectOne. | | String
| *outputHeader* (common) | Store the query result in a header instead of the
message body. By default, outputHeader == null and the query result is stored
in the message body, any existing content in the message body is discarded. If
outputHeader is set, the value is used as the name of the header to store the
query result and the original message body is preserved. | | String
| *outputType* (common) | Make the output of consumer or producer to
SelectList as List of Map, or SelectOne as single Java object in the following
way: a) If the query has only single column, then that JDBC Column object is
returned. (such as SELECT COUNT( ) FROM PROJECT will return a Long object. b)
If the query has more than one column, then it will return a Map of that
result. c) If the outputClass is set, then it will convert the query result
into an Java bean object by calling all [...]
diff --git
a/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlComponentConfigurer.java
b/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlComponentConfigurer.java
index be25f86..7ef6bd2 100644
---
a/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlComponentConfigurer.java
+++
b/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlComponentConfigurer.java
@@ -40,6 +40,11 @@ public class ElsqlComponentConfigurer extends
PropertyConfigurerSupport implemen
}
@Override
+ public String[] getAutowiredNames() {
+ return new String[]{"dataSource"};
+ }
+
+ @Override
public Class<?> getOptionType(String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "autowiredenabled":
diff --git
a/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlEndpointConfigurer.java
b/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlEndpointConfigurer.java
index 327c806..58aad6b 100644
---
a/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlEndpointConfigurer.java
+++
b/components/camel-elsql/src/generated/java/org/apache/camel/component/elsql/ElsqlEndpointConfigurer.java
@@ -112,6 +112,11 @@ public class ElsqlEndpointConfigurer extends
PropertyConfigurerSupport implement
}
@Override
+ public String[] getAutowiredNames() {
+ return new String[]{"dataSource"};
+ }
+
+ @Override
public Class<?> getOptionType(String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "allownamedparameters":
diff --git
a/components/camel-elsql/src/generated/resources/org/apache/camel/component/elsql/elsql.json
b/components/camel-elsql/src/generated/resources/org/apache/camel/component/elsql/elsql.json
index 47f6ae5..a6e219a 100644
---
a/components/camel-elsql/src/generated/resources/org/apache/camel/component/elsql/elsql.json
+++
b/components/camel-elsql/src/generated/resources/org/apache/camel/component/elsql/elsql.json
@@ -23,7 +23,7 @@
},
"componentProperties": {
"databaseVendor": { "kind": "property", "displayName": "Database Vendor",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "org.apache.camel.component.elsql.ElSqlDatabaseVendor", "enum": [
"Default", "Postgres", "HSql", "MySql", "Oracle", "SqlServer2008", "Veritca" ],
"deprecated": false, "autowired": false, "secret": false, "description": "To
use a vendor specific com.opengamma.elsql.ElSqlConfig" },
- "dataSource": { "kind": "property", "displayName": "Data Source", "group":
"common", "label": "", "required": false, "type": "object", "javaType":
"javax.sql.DataSource", "deprecated": false, "autowired": false, "secret":
false, "description": "Sets the DataSource to use to communicate with the
database." },
+ "dataSource": { "kind": "property", "displayName": "Data Source", "group":
"common", "label": "", "required": false, "type": "object", "javaType":
"javax.sql.DataSource", "deprecated": false, "autowired": true, "secret":
false, "description": "Sets the DataSource to use to communicate with the
database." },
"resourceUri": { "kind": "property", "displayName": "Resource Uri",
"group": "common", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "The resource file which contains the elsql SQL
statements to use. You can specify multiple resources separated by comma. The
resources are loaded on the classpath by default, you can prefix with file: to
load from file system. Notice you can set [...]
"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 [...]
@@ -35,7 +35,7 @@
"resourceUri": { "kind": "path", "displayName": "Resource Uri", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"description": "The resource file which contains the elsql SQL statements to
use. You can specify multiple resources separated by comma. The resources are
loaded on the classpath by default, you can prefix with file: to load from file
system. Notice you can set thi [...]
"allowNamedParameters": { "kind": "parameter", "displayName": "Allow Named
Parameters", "group": "common", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to allow using
named parameters in the queries." },
"databaseVendor": { "kind": "parameter", "displayName": "Database Vendor",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "org.apache.camel.component.elsql.ElSqlDatabaseVendor", "enum": [
"Default", "Postgres", "HSql", "MySql", "Oracle", "SqlServer2008", "Veritca" ],
"deprecated": false, "autowired": false, "secret": false, "description": "To
use a vendor specific com.opengamma.elsql.ElSqlConfig" },
- "dataSource": { "kind": "parameter", "displayName": "Data Source",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "autowired": false,
"secret": false, "description": "Sets the DataSource to use to communicate with
the database at endpoint level." },
+ "dataSource": { "kind": "parameter", "displayName": "Data Source",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "deprecationNote": "",
"autowired": true, "secret": false, "description": "Sets the DataSource to use
to communicate with the database at endpoint level." },
"outputClass": { "kind": "parameter", "displayName": "Output Class",
"group": "common", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "Specify the full package and class name to use
as conversion when outputType=SelectOne." },
"outputHeader": { "kind": "parameter", "displayName": "Output Header",
"group": "common", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "Store the query result in a header instead of
the message body. By default, outputHeader == null and the query result is
stored in the message body, any existing content in the message body is
discarded. If outputHeader is set, the value is u [...]
"outputType": { "kind": "parameter", "displayName": "Output Type",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "org.apache.camel.component.sql.SqlOutputType", "enum": [
"SelectOne", "SelectList", "StreamList" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "SelectList", "description": "Make the
output of consumer or producer to SelectList as List of Map, or SelectOne as
single Java object in the following way: a) If th [...]
diff --git a/components/camel-elsql/src/main/docs/elsql-component.adoc
b/components/camel-elsql/src/main/docs/elsql-component.adoc
index 1485a1a..0930fd6 100644
--- a/components/camel-elsql/src/main/docs/elsql-component.adoc
+++ b/components/camel-elsql/src/main/docs/elsql-component.adoc
@@ -69,7 +69,7 @@ The ElSQL component supports 7 options, which are listed
below.
|===
| Name | Description | Default | Type
| *databaseVendor* (common) | To use a vendor specific
com.opengamma.elsql.ElSqlConfig. There are 7 enums and the value can be one of:
Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca | |
ElSqlDatabaseVendor
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database. | | DataSource
| *resourceUri* (common) | The resource file which contains the elsql SQL
statements to use. You can specify multiple resources separated by comma. The
resources are loaded on the classpath by default, you can prefix with file: to
load from file system. Notice you can set this option on the component and then
you do not have to configure this on the endpoint. | | String
| *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 [...]
@@ -106,7 +106,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *allowNamedParameters* (common) | Whether to allow using named parameters in
the queries. | true | boolean
| *databaseVendor* (common) | To use a vendor specific
com.opengamma.elsql.ElSqlConfig. There are 7 enums and the value can be one of:
Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca | |
ElSqlDatabaseVendor
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database at endpoint level. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database at endpoint level. | | DataSource
| *outputClass* (common) | Specify the full package and class name to use as
conversion when outputType=SelectOne. | | String
| *outputHeader* (common) | Store the query result in a header instead of the
message body. By default, outputHeader == null and the query result is stored
in the message body, any existing content in the message body is discarded. If
outputHeader is set, the value is used as the name of the header to store the
query result and the original message body is preserved. | | String
| *outputType* (common) | Make the output of consumer or producer to
SelectList as List of Map, or SelectOne as single Java object in the following
way: a) If the query has only single column, then that JDBC Column object is
returned. (such as SELECT COUNT( ) FROM PROJECT will return a Long object. b)
If the query has more than one column, then it will return a Map of that
result. c) If the outputClass is set, then it will convert the query result
into an Java bean object by calling all [...]
diff --git
a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlComponent.java
b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlComponent.java
index f49eb75..be2034b 100644
---
a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlComponent.java
+++
b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlComponent.java
@@ -36,10 +36,10 @@ public class ElsqlComponent extends DefaultComponent {
private static final Logger LOG =
LoggerFactory.getLogger(ElsqlComponent.class);
+ @Metadata(autowired = true)
+ private DataSource dataSource;
@Metadata
private ElSqlDatabaseVendor databaseVendor;
- @Metadata
- private DataSource dataSource;
@Metadata(label = "advanced")
private ElSqlConfig elSqlConfig;
@Metadata
@@ -50,26 +50,6 @@ public class ElsqlComponent extends DefaultComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) throws Exception {
- DataSource target = null;
-
- // endpoint options overrule component configured datasource
- DataSource ds = resolveAndRemoveReferenceParameter(parameters,
"dataSource", DataSource.class);
- if (ds != null) {
- target = ds;
- }
- if (target == null) {
- // fallback and use component
- target = getDataSource();
- }
- if (target == null) {
- throw new IllegalArgumentException("DataSource must be
configured");
- }
- LOG.trace("Using DataSource: {}", target);
-
- NamedParameterJdbcTemplate jdbcTemplate = new
NamedParameterJdbcTemplate(target);
- Map<String, Object> params =
PropertiesHelper.extractProperties(parameters, "template.");
- PropertyBindingSupport.bindProperties(getCamelContext(), jdbcTemplate,
params);
-
String elsqlName = remaining;
String resUri = resourceUri;
String[] part = remaining.split(":");
@@ -93,13 +73,33 @@ public class ElsqlComponent extends DefaultComponent {
onConsumeBatchComplete = getAndRemoveParameter(parameters,
"onConsumeBatchComplete", String.class);
}
- ElsqlEndpoint endpoint = new ElsqlEndpoint(uri, this, jdbcTemplate,
target, elsqlName, resUri);
+ // create endpoint
+ ElsqlEndpoint endpoint = new ElsqlEndpoint(uri, this, elsqlName,
resUri);
endpoint.setElSqlConfig(elSqlConfig);
endpoint.setDatabaseVendor(databaseVendor);
- endpoint.setDataSource(target);
endpoint.setOnConsume(onConsume);
endpoint.setOnConsumeFailed(onConsumeFailed);
endpoint.setOnConsumeBatchComplete(onConsumeBatchComplete);
+
+ // endpoint configured data source takes precedence
+ DataSource ds = dataSource;
+ if (endpoint.getDataSource() != null) {
+ ds = endpoint.getDataSource();
+ }
+ if (ds == null) {
+ throw new IllegalArgumentException("DataSource must be
configured");
+ }
+
+ // create template
+ NamedParameterJdbcTemplate jdbcTemplate = new
NamedParameterJdbcTemplate(ds);
+ Map<String, Object> templateOptions =
PropertiesHelper.extractProperties(parameters, "template.");
+ PropertyBindingSupport.bindProperties(getCamelContext(), jdbcTemplate,
templateOptions);
+
+ // set template on endpoint
+ endpoint.setNamedJdbcTemplate(jdbcTemplate);
+ endpoint.setDataSource(ds);
+ endpoint.setTemplateOptions(templateOptions);
+
return endpoint;
}
diff --git
a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlEndpoint.java
b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlEndpoint.java
index 88572cc..cc69d26 100644
---
a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlEndpoint.java
+++
b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlEndpoint.java
@@ -21,8 +21,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
-import javax.sql.DataSource;
-
import com.opengamma.elsql.ElSql;
import com.opengamma.elsql.ElSqlConfig;
import com.opengamma.elsql.SpringSqlParams;
@@ -57,11 +55,11 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
private static final Logger LOG =
LoggerFactory.getLogger(ElsqlEndpoint.class);
private ElSql elSql;
- private final NamedParameterJdbcTemplate namedJdbcTemplate;
+ private NamedParameterJdbcTemplate namedJdbcTemplate;
@UriPath
@Metadata(required = true)
- private final String elsqlName;
+ private String elsqlName;
@UriPath
private String resourceUri;
@UriParam
@@ -69,14 +67,10 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
@UriParam(label = "advanced")
private ElSqlConfig elSqlConfig;
- public ElsqlEndpoint(final String uri, final Component component, final
NamedParameterJdbcTemplate namedJdbcTemplate,
- final DataSource dataSource,
- final String elsqlName, final String resourceUri) {
- super(uri, component, null);
+ public ElsqlEndpoint(String endpointUri, Component component, String
elsqlName, String resourceUri) {
+ super(endpointUri, component);
this.elsqlName = elsqlName;
this.resourceUri = resourceUri;
- this.namedJdbcTemplate = namedJdbcTemplate;
- setDataSource(dataSource);
}
@Override
@@ -155,11 +149,23 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
}
}
+ public NamedParameterJdbcTemplate getNamedJdbcTemplate() {
+ return namedJdbcTemplate;
+ }
+
+ public void setNamedJdbcTemplate(NamedParameterJdbcTemplate
namedJdbcTemplate) {
+ this.namedJdbcTemplate = namedJdbcTemplate;
+ }
+
+ public String getElsqlName() {
+ return elsqlName;
+ }
+
/**
* The name of the elsql to use (is @NAMED in the elsql file)
*/
- public String getElsqlName() {
- return elsqlName;
+ public void setElsqlName(String elsqlName) {
+ this.elsqlName = elsqlName;
}
public ElSqlDatabaseVendor getDatabaseVendor() {
diff --git
a/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlComponentConfigurer.java
b/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlComponentConfigurer.java
index a2384f9..d9c7487 100644
---
a/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlComponentConfigurer.java
+++
b/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlComponentConfigurer.java
@@ -36,6 +36,11 @@ public class SqlComponentConfigurer extends
PropertyConfigurerSupport implements
}
@Override
+ public String[] getAutowiredNames() {
+ return new String[]{"dataSource"};
+ }
+
+ @Override
public Class<?> getOptionType(String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "autowiredenabled":
diff --git
a/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlEndpointConfigurer.java
b/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlEndpointConfigurer.java
index 75da018..cdee312 100644
---
a/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlEndpointConfigurer.java
+++
b/components/camel-sql/src/generated/java/org/apache/camel/component/sql/SqlEndpointConfigurer.java
@@ -108,6 +108,11 @@ public class SqlEndpointConfigurer extends
PropertyConfigurerSupport implements
}
@Override
+ public String[] getAutowiredNames() {
+ return new String[]{"dataSource"};
+ }
+
+ @Override
public Class<?> getOptionType(String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "allownamedparameters":
diff --git
a/components/camel-sql/src/generated/resources/org/apache/camel/component/sql/sql.json
b/components/camel-sql/src/generated/resources/org/apache/camel/component/sql/sql.json
index 3b9e04d..c572851 100644
---
a/components/camel-sql/src/generated/resources/org/apache/camel/component/sql/sql.json
+++
b/components/camel-sql/src/generated/resources/org/apache/camel/component/sql/sql.json
@@ -22,7 +22,7 @@
"lenientProperties": false
},
"componentProperties": {
- "dataSource": { "kind": "property", "displayName": "Data Source", "group":
"common", "label": "", "required": false, "type": "object", "javaType":
"javax.sql.DataSource", "deprecated": false, "autowired": false, "secret":
false, "description": "Sets the DataSource to use to communicate with the
database." },
+ "dataSource": { "kind": "property", "displayName": "Data Source", "group":
"common", "label": "", "required": false, "type": "object", "javaType":
"javax.sql.DataSource", "deprecated": false, "autowired": true, "secret":
false, "description": "Sets the DataSource to use to communicate with the
database." },
"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 [...]
@@ -31,7 +31,7 @@
"properties": {
"query": { "kind": "path", "displayName": "Query", "group": "common",
"label": "", "required": true, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "deprecationNote": "", "autowired":
false, "secret": false, "description": "Sets the SQL query to perform. You can
externalize the query by using file: or classpath: as prefix and specify the
location of the file." },
"allowNamedParameters": { "kind": "parameter", "displayName": "Allow Named
Parameters", "group": "common", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to allow using
named parameters in the queries." },
- "dataSource": { "kind": "parameter", "displayName": "Data Source",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "autowired": false,
"secret": false, "description": "Sets the DataSource to use to communicate with
the database at endpoint level." },
+ "dataSource": { "kind": "parameter", "displayName": "Data Source",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "deprecationNote": "",
"autowired": true, "secret": false, "description": "Sets the DataSource to use
to communicate with the database at endpoint level." },
"outputClass": { "kind": "parameter", "displayName": "Output Class",
"group": "common", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "Specify the full package and class name to use
as conversion when outputType=SelectOne." },
"outputHeader": { "kind": "parameter", "displayName": "Output Header",
"group": "common", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "Store the query result in a header instead of
the message body. By default, outputHeader == null and the query result is
stored in the message body, any existing content in the message body is
discarded. If outputHeader is set, the value is u [...]
"outputType": { "kind": "parameter", "displayName": "Output Type",
"group": "common", "label": "", "required": false, "type": "object",
"javaType": "org.apache.camel.component.sql.SqlOutputType", "enum": [
"SelectOne", "SelectList", "StreamList" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "SelectList", "description": "Make the
output of consumer or producer to SelectList as List of Map, or SelectOne as
single Java object in the following way: a) If th [...]
diff --git a/components/camel-sql/src/main/docs/sql-component.adoc
b/components/camel-sql/src/main/docs/sql-component.adoc
index fa009e4..58bbb1e 100644
--- a/components/camel-sql/src/main/docs/sql-component.adoc
+++ b/components/camel-sql/src/main/docs/sql-component.adoc
@@ -124,7 +124,7 @@ The SQL component supports 5 options, which are listed
below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database. | | DataSource
| *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
@@ -161,7 +161,7 @@ with the following path and query parameters:
|===
| Name | Description | Default | Type
| *allowNamedParameters* (common) | Whether to allow using named parameters in
the queries. | true | boolean
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database at endpoint level. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database at endpoint level. | | DataSource
| *outputClass* (common) | Specify the full package and class name to use as
conversion when outputType=SelectOne. | | String
| *outputHeader* (common) | Store the query result in a header instead of the
message body. By default, outputHeader == null and the query result is stored
in the message body, any existing content in the message body is discarded. If
outputHeader is set, the value is used as the name of the header to store the
query result and the original message body is preserved. | | String
| *outputType* (common) | Make the output of consumer or producer to
SelectList as List of Map, or SelectOne as single Java object in the following
way: a) If the query has only single column, then that JDBC Column object is
returned. (such as SELECT COUNT( ) FROM PROJECT will return a Long object. b)
If the query has more than one column, then it will return a Map of that
result. c) If the outputClass is set, then it will convert the query result
into an Java bean object by calling all [...]
diff --git
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
index 207ee0e..a31f961 100644
---
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
+++
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlEndpoint.java
@@ -27,6 +27,7 @@ import java.util.Map;
import javax.sql.DataSource;
import org.apache.camel.Component;
+import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriParam;
import org.apache.camel.support.DefaultPollingEndpoint;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
@@ -41,6 +42,7 @@ import
org.springframework.jdbc.core.RowMapperResultSetExtractor;
public abstract class DefaultSqlEndpoint extends DefaultPollingEndpoint {
private JdbcTemplate jdbcTemplate;
+ @Metadata(autowired = true)
@UriParam(description = "Sets the DataSource to use to communicate with
the database at endpoint level.")
private DataSource dataSource;
@UriParam(label = "consumer",
@@ -129,9 +131,8 @@ public abstract class DefaultSqlEndpoint extends
DefaultPollingEndpoint {
public DefaultSqlEndpoint() {
}
- public DefaultSqlEndpoint(String uri, Component component, JdbcTemplate
jdbcTemplate) {
- super(uri, component);
- this.jdbcTemplate = jdbcTemplate;
+ public DefaultSqlEndpoint(String endpointUri, Component component) {
+ super(endpointUri, component);
}
public JdbcTemplate getJdbcTemplate() {
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 cd7880f..2cd8614 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
@@ -17,7 +17,6 @@
package org.apache.camel.component.sql;
import java.util.Map;
-import java.util.Set;
import javax.sql.DataSource;
@@ -28,8 +27,6 @@ import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.util.PropertiesHelper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
/**
@@ -39,9 +36,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
@Component("sql")
public class SqlComponent extends DefaultComponent {
- private static final Logger LOG =
LoggerFactory.getLogger(SqlComponent.class);
-
- @Metadata
+ @Metadata(autowired = true)
private DataSource dataSource;
@Metadata(label = "advanced", defaultValue = "true")
private boolean usePlaceholder = true;
@@ -62,38 +57,8 @@ public class SqlComponent extends DefaultComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) throws Exception {
- DataSource target = null;
-
- // endpoint options overrule component configured datasource
- DataSource ds = resolveAndRemoveReferenceParameter(parameters,
"dataSource", DataSource.class);
- if (ds != null) {
- target = ds;
- }
- if (target == null) {
- // fallback and use component
- target = dataSource;
- }
- 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.iterator().next();
- }
- }
- if (target == null) {
- throw new IllegalArgumentException("DataSource must be
configured");
- }
- LOG.trace("Using DataSource: {}", target);
-
String parameterPlaceholderSubstitute =
getAndRemoveParameter(parameters, "placeholder", String.class, "#");
- JdbcTemplate jdbcTemplate = new JdbcTemplate(target);
- Map<String, Object> templateOptions =
PropertiesHelper.extractProperties(parameters, "template.");
- PropertyBindingSupport.bindProperties(getCamelContext(), jdbcTemplate,
templateOptions);
-
String query = remaining;
if (usePlaceholder) {
query = query.replaceAll(parameterPlaceholderSubstitute, "?");
@@ -121,15 +86,35 @@ public class SqlComponent extends DefaultComponent {
onConsumeBatchComplete =
onConsumeBatchComplete.replaceAll(parameterPlaceholderSubstitute, "?");
}
- SqlEndpoint endpoint = new SqlEndpoint(uri, this, jdbcTemplate, query);
+ // create endpoint
+ SqlEndpoint endpoint = new SqlEndpoint(uri, this);
+ endpoint.setQuery(query);
endpoint.setPlaceholder(parameterPlaceholderSubstitute);
endpoint.setUsePlaceholder(isUsePlaceholder());
endpoint.setOnConsume(onConsume);
endpoint.setOnConsumeFailed(onConsumeFailed);
endpoint.setOnConsumeBatchComplete(onConsumeBatchComplete);
+ setProperties(endpoint, parameters);
+
+ // endpoint configured data source takes precedence
+ DataSource ds = dataSource;
+ if (endpoint.getDataSource() != null) {
+ ds = endpoint.getDataSource();
+ }
+ if (ds == null) {
+ throw new IllegalArgumentException("DataSource must be
configured");
+ }
+
+ // create template
+ JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
+ Map<String, Object> templateOptions =
PropertiesHelper.extractProperties(parameters, "template.");
+ PropertyBindingSupport.bindProperties(getCamelContext(), jdbcTemplate,
templateOptions);
+
+ // set template on endpoint
+ endpoint.setJdbcTemplate(jdbcTemplate);
endpoint.setDataSource(ds);
endpoint.setTemplateOptions(templateOptions);
- setProperties(endpoint, parameters);
+
return endpoint;
}
diff --git
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
index 86b684b..c169686 100644
---
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
+++
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
@@ -25,7 +25,6 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriPath;
import org.apache.camel.util.UnsafeUriCharactersEncoder;
-import org.springframework.jdbc.core.JdbcTemplate;
/**
* Perform SQL queries using Spring JDBC.
@@ -41,9 +40,8 @@ public class SqlEndpoint extends DefaultSqlEndpoint {
public SqlEndpoint() {
}
- public SqlEndpoint(String uri, Component component, JdbcTemplate
jdbcTemplate, String query) {
- super(uri, component, jdbcTemplate);
- this.query = query;
+ public SqlEndpoint(String endpointUri, Component component) {
+ super(endpointUri, component);
}
@Override
diff --git a/docs/components/modules/ROOT/pages/elsql-component.adoc
b/docs/components/modules/ROOT/pages/elsql-component.adoc
index f53dbdf..183f198 100644
--- a/docs/components/modules/ROOT/pages/elsql-component.adoc
+++ b/docs/components/modules/ROOT/pages/elsql-component.adoc
@@ -71,7 +71,7 @@ The ElSQL component supports 7 options, which are listed
below.
|===
| Name | Description | Default | Type
| *databaseVendor* (common) | To use a vendor specific
com.opengamma.elsql.ElSqlConfig. There are 7 enums and the value can be one of:
Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca | |
ElSqlDatabaseVendor
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database. | | DataSource
| *resourceUri* (common) | The resource file which contains the elsql SQL
statements to use. You can specify multiple resources separated by comma. The
resources are loaded on the classpath by default, you can prefix with file: to
load from file system. Notice you can set this option on the component and then
you do not have to configure this on the endpoint. | | String
| *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 [...]
@@ -108,7 +108,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *allowNamedParameters* (common) | Whether to allow using named parameters in
the queries. | true | boolean
| *databaseVendor* (common) | To use a vendor specific
com.opengamma.elsql.ElSqlConfig. There are 7 enums and the value can be one of:
Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca | |
ElSqlDatabaseVendor
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database at endpoint level. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database at endpoint level. | | DataSource
| *outputClass* (common) | Specify the full package and class name to use as
conversion when outputType=SelectOne. | | String
| *outputHeader* (common) | Store the query result in a header instead of the
message body. By default, outputHeader == null and the query result is stored
in the message body, any existing content in the message body is discarded. If
outputHeader is set, the value is used as the name of the header to store the
query result and the original message body is preserved. | | String
| *outputType* (common) | Make the output of consumer or producer to
SelectList as List of Map, or SelectOne as single Java object in the following
way: a) If the query has only single column, then that JDBC Column object is
returned. (such as SELECT COUNT( ) FROM PROJECT will return a Long object. b)
If the query has more than one column, then it will return a Map of that
result. c) If the outputClass is set, then it will convert the query result
into an Java bean object by calling all [...]
diff --git a/docs/components/modules/ROOT/pages/sql-component.adoc
b/docs/components/modules/ROOT/pages/sql-component.adoc
index 7da723b..1512e26 100644
--- a/docs/components/modules/ROOT/pages/sql-component.adoc
+++ b/docs/components/modules/ROOT/pages/sql-component.adoc
@@ -126,7 +126,7 @@ The SQL component supports 5 options, which are listed
below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database. | | DataSource
| *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
@@ -163,7 +163,7 @@ with the following path and query parameters:
|===
| Name | Description | Default | Type
| *allowNamedParameters* (common) | Whether to allow using named parameters in
the queries. | true | boolean
-| *dataSource* (common) | Sets the DataSource to use to communicate with the
database at endpoint level. | | DataSource
+| *dataSource* (common) | *Autowired* Sets the DataSource to use to
communicate with the database at endpoint level. | | DataSource
| *outputClass* (common) | Specify the full package and class name to use as
conversion when outputType=SelectOne. | | String
| *outputHeader* (common) | Store the query result in a header instead of the
message body. By default, outputHeader == null and the query result is stored
in the message body, any existing content in the message body is discarded. If
outputHeader is set, the value is used as the name of the header to store the
query result and the original message body is preserved. | | String
| *outputType* (common) | Make the output of consumer or producer to
SelectList as List of Map, or SelectOne as single Java object in the following
way: a) If the query has only single column, then that JDBC Column object is
returned. (such as SELECT COUNT( ) FROM PROJECT will return a Long object. b)
If the query has more than one column, then it will return a Map of that
result. c) If the outputClass is set, then it will convert the query result
into an Java bean object by calling all [...]