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 2008/11/12 20:41:26 UTC

svn commit: r713475 - in /activemq/camel/trunk/components/camel-jdbc/src: main/java/org/apache/camel/component/jdbc/ test/java/org/apache/camel/component/jdbc/

Author: davsclaus
Date: Wed Nov 12 11:41:26 2008
New Revision: 713475

URL: http://svn.apache.org/viewvc?rev=713475&view=rev
Log:
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/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java   (contents, props changed)
      - copied, changed from r713439, activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java
Modified:
    activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
    activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
    activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
    activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java
    activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java

Modified: activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java?rev=713475&r1=713474&r2=713475&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java Wed Nov 12 11:41:26 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;
@@ -26,6 +27,7 @@
  * @version $Revision:520964 $
  */
 public class JdbcComponent extends DefaultComponent {
+    private DataSource ds;
 
     public JdbcComponent() {
     }
@@ -36,6 +38,26 @@
 
     @Override
     protected Endpoint 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/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java?rev=713475&r1=713474&r2=713475&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java Wed Nov 12 11:41:26 2008
@@ -16,59 +16,39 @@
  */
 package org.apache.camel.component.jdbc;
 
-import java.net.URI;
 import java.net.URISyntaxException;
 
 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 javax.sql.DataSource;
+
 /**
  * @version $Revision:520964 $
  */
 public class JdbcEndpoint extends DefaultEndpoint {
+    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 {
+    protected JdbcEndpoint(String endpointUri, Component component, DataSource dataSource) throws URISyntaxException {
         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 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 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/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java?rev=713475&r1=713474&r2=713475&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java Wed Nov 12 11:41:26 2008
@@ -37,23 +37,18 @@
  * @version $Revision$
  */
 public class JdbcProducer extends DefaultProducer {
-
     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);
@@ -61,8 +56,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);
@@ -87,15 +85,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>();
@@ -105,7 +98,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;
@@ -115,6 +108,7 @@
             data.add(row);
             rowNumber++;
         }
+
         exchange.getOut().setBody(data);
     }
 

Modified: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java?rev=713475&r1=713474&r2=713475&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java Wed Nov 12 11:41:26 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");

Copied: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java (from r713439, activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java?p2=activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java&p1=activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java&r1=713439&r2=713475&rev=713475&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcAnotherRouteTest.java (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java Wed Nov 12 11:41:26 2008
@@ -19,28 +19,42 @@
 import javax.sql.DataSource;
 
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.JndiRegistry;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
-/**
- * Unit test based on user forum request about this component
- */
-public class JdbcAnotherRouteTest extends ContextTestSupport {
+import java.util.List;
+import java.util.ArrayList;
+
+public class JdbcOptionsTest extends ContextTestSupport {
     private String driverClass = "org.hsqldb.jdbcDriver";
     private String url = "jdbc:hsqldb:mem:camel_jdbc";
     private String user = "sa";
     private String password = "";
     private DataSource ds;
-    private JdbcTemplate jdbc;
 
-    public void testTimerInvoked() throws Exception {
+    public void testReadSize() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
 
+        template.sendBody("direct:start", "select * from customer");
+
         mock.assertIsSatisfied();
+
+        List list = mock.getExchanges().get(0).getIn().getBody(ArrayList.class);
+        assertEquals(1, list.size());
+    }
+
+    public void testNoDataSourceInRegistry() throws Exception {
+        try {
+            template.sendBody("jdbc:xxx", "Hello World");
+            fail("Should have thrown a ResolveEndpointFailedException");
+        } catch (ResolveEndpointFailedException e) {
+            assertEquals("DataSource xxx not found in registry", e.getCause().getMessage());
+        }
     }
 
     protected JndiRegistry createRegistry() throws Exception {
@@ -52,10 +66,7 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("timer://kickoff?period=10000").
-                    setBody(constant("select * from customer")).
-                    to("jdbc:testdb").
-                    to("mock:result");
+                from("direct:start").to("jdbc:testdb?readSize=1").to("mock:result");
             }
         };
     }
@@ -78,4 +89,4 @@
         jdbc.execute("drop table customer");
     }
 
-}
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcOptionsTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java?rev=713475&r1=713474&r2=713475&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java Wed Nov 12 11:41:26 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");