svn commit: r713480 - in /activemq/camel/branches/camel-1.x: ./ components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/ components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r713480 - in /activemq/camel/branches/camel-1.x: ./ components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/ components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/

davsclaus-2
Author: davsclaus
Date: Wed Nov 12 11:58:14 2008
New Revision: 713480

URL: http://svn.apache.org/viewvc?rev=713480&view=rev
Log:
Merged revisions 713475 via svnmerge from
https://svn.apache.org/repos/asf/activemq/camel/trunk

........
  r713475 | davsclaus | 2008-11-12 20:41:26 +0100 (on, 12 nov 2008) | 1 line
 
  CAMEL-1081: Polished camel-jdbc to be aligned with camel-sql. option readSize is now default 0 to support getting all data out-of-the-box.
........

Added:
    activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java
      - copied unchanged from r713475, activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java
Modified:
    activemq/camel/branches/camel-1.x/   (props changed)
    activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
    activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
    activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
    activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java
    activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java

Propchange: activemq/camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Nov 12 11:58:14 2008
@@ -1 +1 @@
-/activemq/camel/trunk:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713273,713290,713292,713295,713314
+/activemq/camel/trunk:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713273,713290,713292,713295,713314,713475

Propchange: activemq/camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java?rev=713480&r1=713479&r2=713480&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java Wed Nov 12 11:58:14 2008
@@ -17,6 +17,7 @@
 package org.apache.camel.component.jdbc;
 
 import java.util.Map;
+import javax.sql.DataSource;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
@@ -27,6 +28,7 @@
  * @version $Revision:520964 $
  */
 public class JdbcComponent extends DefaultComponent<DefaultExchange> {
+    private DataSource ds;
 
     public JdbcComponent() {
     }
@@ -37,7 +39,26 @@
 
     @Override
     protected Endpoint<DefaultExchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
-        return new JdbcEndpoint(uri, remaining, this);
+        DataSource dataSource;
+
+        if (ds != null) {
+            // use data source set by setter
+            dataSource = ds;
+        } else {
+            // lookup in registry instead
+            dataSource = getCamelContext().getRegistry().lookup(remaining, DataSource.class);
+            if (dataSource == null) {
+                throw new IllegalArgumentException("DataSource " + remaining + " not found in registry");
+            }
+        }
+
+        JdbcEndpoint jdbc = new JdbcEndpoint(uri, this, dataSource);
+        setProperties(jdbc, parameters);
+        return jdbc;
+    }
+
+    public void setDataSource(DataSource dataSource) {
+        this.ds = dataSource;
     }
 
 }

Modified: activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java?rev=713480&r1=713479&r2=713480&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java Wed Nov 12 11:58:14 2008
@@ -16,13 +16,13 @@
  */
 package org.apache.camel.component.jdbc;
 
-import java.net.URI;
-import java.net.URISyntaxException;
+import javax.sql.DataSource;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.Component;
+import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.impl.DefaultExchange;
 
@@ -30,46 +30,24 @@
  * @version $Revision:520964 $
  */
 public class JdbcEndpoint extends DefaultEndpoint<DefaultExchange> {
+    private int readSize;
+    private DataSource dataSource;
 
-    private URI uri;
-    private String remaining;
-    /** The maximum size for reading a result set <code>readSize</code> */
-    private int readSize = 20000;
-
-    protected JdbcEndpoint(String endpointUri, String remaining, JdbcComponent component) throws URISyntaxException {
+    public JdbcEndpoint(String endpointUri, Component component, DataSource dataSource) {
         super(endpointUri, component);
-        this.uri = new URI(endpointUri);
-        this.remaining = remaining;
-    }
-
-    public JdbcEndpoint(String endpointUri, String remaining) throws URISyntaxException {
-        super(endpointUri);
-        this.remaining = remaining;
-        this.uri = new URI(endpointUri);
+        this.dataSource = dataSource;
     }
 
     public boolean isSingleton() {
-        return false;
+        return true;
     }
 
     public Consumer<DefaultExchange> createConsumer(Processor processor) throws Exception {
-        throw new RuntimeCamelException("A JDBC Consumer would be the server side of database! No such support here");
+        throw new UnsupportedOperationException("Not supported");
     }
 
     public Producer<DefaultExchange> createProducer() throws Exception {
-        return new JdbcProducer(this, remaining, readSize);
-    }
-
-    public String getName() {
-        String path = uri.getPath();
-        if (path == null) {
-            path = uri.getSchemeSpecificPart();
-        }
-        return path;
-    }
-
-    public int getReadSize() {
-        return this.readSize;
+        return new JdbcProducer(this, dataSource, readSize);
     }
 
     public void setReadSize(int readSize) {

Modified: activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java?rev=713480&r1=713479&r2=713480&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java Wed Nov 12 11:58:14 2008
@@ -38,23 +38,18 @@
  * @version $Revision$
  */
 public class JdbcProducer extends DefaultProducer<DefaultExchange> {
-
     private static final transient Log LOG = LogFactory.getLog(JdbcProducer.class);
-    private DataSource source;
-
-    /** The maximum size for reading a result set <code>readSize</code> */
-    private int readSize = 2000;
+    private DataSource dataSource;
+    private int readSize;
 
-    public JdbcProducer(JdbcEndpoint endpoint, String remaining, int readSize) throws Exception {
+    public JdbcProducer(JdbcEndpoint endpoint, DataSource dataSource, int readSize) throws Exception {
         super(endpoint);
+        this.dataSource = dataSource;
         this.readSize = readSize;
-        source = (DataSource) getEndpoint().getCamelContext().getRegistry().lookup(remaining);
     }
 
     /**
      * Execute sql of exchange and set results on output
-     *
-     * @see org.apache.camel.Processor#process(org.apache.camel.Exchange)
      */
     public void process(Exchange exchange) throws Exception {
         String sql = exchange.getIn().getBody(String.class);
@@ -62,8 +57,11 @@
         Statement stmt = null;
         ResultSet rs = null;
         try {
-            conn = source.getConnection();
+            conn = dataSource.getConnection();
             stmt = conn.createStatement();
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Executing JDBC statement: " + sql);
+            }
             if (stmt.execute(sql)) {
                 rs = stmt.getResultSet();
                 setResultSet(exchange, rs);
@@ -88,15 +86,10 @@
         }
     }
 
-    public int getReadSize() {
-        return this.readSize;
-    }
-
-    public void setReadSize(int readSize) {
-        this.readSize = readSize;
-    }
-
-    public void setResultSet(Exchange exchange, ResultSet rs) throws SQLException {
+    /**
+     * Sets the result from the ResultSet to the Exchange as its OUT body.
+     */
+    protected void setResultSet(Exchange exchange, ResultSet rs) throws SQLException {
         ResultSetMetaData meta = rs.getMetaData();
 
         HashMap<String, Object> props = new HashMap<String, Object>();
@@ -106,7 +99,7 @@
         int count = meta.getColumnCount();
         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
         int rowNumber = 0;
-        while (rs.next() && rowNumber < readSize) {
+        while (rs.next() && (readSize == 0 || rowNumber < readSize)) {
             HashMap<String, Object> row = new HashMap<String, Object>();
             for (int i = 0; i < count; i++) {
                 int columnNumber = i + 1;
@@ -116,6 +109,7 @@
             data.add(row);
             rowNumber++;
         }
+
         exchange.getOut().setBody(data);
     }
 

Modified: activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java?rev=713480&r1=713479&r2=713480&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java Wed Nov 12 11:58:14 2008
@@ -34,7 +34,6 @@
     private String user = "sa";
     private String password = "";
     private DataSource ds;
-    private JdbcTemplate jdbc;
 
     public void testTimerInvoked() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");

Modified: activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java?rev=713480&r1=713479&r2=713480&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java Wed Nov 12 11:58:14 2008
@@ -37,9 +37,8 @@
     private String user = "sa";
     private String password = "";
     private DataSource ds;
-    private JdbcTemplate jdbc;
 
-    public void testPojoRoutes() throws Exception {
+    public void testJdbcRoutes() throws Exception {
         // START SNIPPET: invoke
         // first we create our exchange using the endpoint
         Endpoint endpoint = context.getEndpoint("direct:hello");