CAMEL-8226: sql-component should have the datasource options in the endpoint class so we have all supported options there.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0909802a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0909802a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0909802a Branch: refs/heads/master Commit: 0909802a841b068d0852585f291410811f8d3f99 Parents: 649b946 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Feb 10 09:30:47 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Feb 10 09:30:47 2015 +0100 ---------------------------------------------------------------------- .../camel/component/sql/SqlComponent.java | 32 ++++++++++++------ .../apache/camel/component/sql/SqlEndpoint.java | 35 ++++++++++++++++++-- 2 files changed, 54 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0909802a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java ---------------------------------------------------------------------- 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 2cdf72f..5cdbed8 100755 --- 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 @@ -22,6 +22,7 @@ import javax.sql.DataSource; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; 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.springframework.jdbc.core.JdbcTemplate; @@ -33,6 +34,7 @@ import org.springframework.jdbc.core.JdbcTemplate; */ public class SqlComponent extends UriEndpointComponent { private DataSource dataSource; + @Metadata(defaultValue = "true") private boolean usePlaceholder = true; public SqlComponent() { @@ -45,23 +47,28 @@ public class SqlComponent extends UriEndpointComponent { @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; + } String dataSourceRef = getAndRemoveParameter(parameters, "dataSourceRef", String.class); - if (ds == null && dataSourceRef != null) { - ds = CamelContextHelper.mandatoryLookup(getCamelContext(), dataSourceRef, DataSource.class); + if (target == null && dataSourceRef != null) { + target = CamelContextHelper.mandatoryLookup(getCamelContext(), dataSourceRef, DataSource.class); } - if (ds == null) { + if (target == null) { // fallback and use component - ds = dataSource; + target = dataSource; } - if (ds == null) { + if (target == null) { throw new IllegalArgumentException("DataSource must be configured"); } String parameterPlaceholderSubstitute = getAndRemoveParameter(parameters, "placeholder", String.class, "#"); - JdbcTemplate jdbcTemplate = new JdbcTemplate(ds); + JdbcTemplate jdbcTemplate = new JdbcTemplate(target); IntrospectionSupport.setProperties(jdbcTemplate, parameters, "template."); String query = remaining.replaceAll(parameterPlaceholderSubstitute, "?"); @@ -92,9 +99,14 @@ public class SqlComponent extends UriEndpointComponent { endpoint.setOnConsume(onConsume); endpoint.setOnConsumeFailed(onConsumeFailed); endpoint.setOnConsumeBatchComplete(onConsumeBatchComplete); + endpoint.setDataSource(ds); + endpoint.setDataSourceRef(dataSourceRef); return endpoint; } + /** + * Sets the DataSource to use to communicate with the database. + */ public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } @@ -103,10 +115,6 @@ public class SqlComponent extends UriEndpointComponent { return dataSource; } - public boolean isUsePlaceholder() { - return usePlaceholder; - } - /** * Sets whether to use placeholder and replace all placeholder characters with ? sign in the SQL queries. * <p/> @@ -115,4 +123,8 @@ public class SqlComponent extends UriEndpointComponent { public void setUsePlaceholder(boolean usePlaceholder) { this.usePlaceholder = usePlaceholder; } + + public boolean isUsePlaceholder() { + return usePlaceholder; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/0909802a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java ---------------------------------------------------------------------- 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 d44a584..b95351e 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 @@ -22,6 +22,8 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; +import javax.sql.DataSource; + import org.apache.camel.Component; import org.apache.camel.Consumer; import org.apache.camel.Processor; @@ -49,6 +51,11 @@ public class SqlEndpoint extends DefaultPollingEndpoint { @UriPath private String query; @UriParam + @Deprecated + private String dataSourceRef; + @UriParam + private DataSource dataSource; + @UriParam private boolean batch; @UriParam private int maxMessagesPerPoll; @@ -62,13 +69,13 @@ public class SqlEndpoint extends DefaultPollingEndpoint { private String onConsumeFailed; @UriParam private String onConsumeBatchComplete; - @UriParam + @UriParam(defaultValue = "true") private boolean allowNamedParameters = true; @UriParam private boolean alwaysPopulateStatement; - @UriParam + @UriParam(defaultValue = ",") private char separator = ','; - @UriParam + @UriParam(defaultValue = "SelectList") private SqlOutputType outputType = SqlOutputType.SelectList; @UriParam private String outputClass; @@ -310,6 +317,28 @@ public class SqlEndpoint extends DefaultPollingEndpoint { this.outputHeader = outputHeader; } + public String getDataSourceRef() { + return dataSourceRef; + } + + /** + * Sets the reference to a DataSource to lookup from the registry, to use for communicating with the database. + */ + public void setDataSourceRef(String dataSourceRef) { + this.dataSourceRef = dataSourceRef; + } + + public DataSource getDataSource() { + return dataSource; + } + + /** + * Sets the DataSource to use to communicate with the database. + */ + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; + } + @Override protected String createEndpointUri() { // Make sure it's properly encoded