CAMEL-9162: camel-elsql component

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/913efcc1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/913efcc1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/913efcc1

Branch: refs/heads/master
Commit: 913efcc1ae852b86000b7709345be28cf6f12e56
Parents: 7241cad
Author: Claus Ibsen <davscl...@apache.org>
Authored: Mon Oct 5 11:50:34 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Mon Oct 5 11:50:34 2015 +0200

----------------------------------------------------------------------
 .../component/elsql/ElSqlDatabaseVendor.java    | 42 ++++++++++
 .../camel/component/elsql/ElsqlComponent.java   | 31 ++++---
 .../camel/component/elsql/ElsqlEndpoint.java    | 26 +++++-
 .../component/elsql/ElSqlComponentTest.java     | 88 ++++++++++++++++++++
 .../component/elsql/ElSqlDataSourceTest.java    |  2 +-
 .../src/test/resources/elsql/projects.elsql     |  2 +-
 6 files changed, 169 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/913efcc1/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElSqlDatabaseVendor.java
----------------------------------------------------------------------
diff --git 
a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElSqlDatabaseVendor.java
 
b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElSqlDatabaseVendor.java
new file mode 100644
index 0000000..28ce85a
--- /dev/null
+++ 
b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElSqlDatabaseVendor.java
@@ -0,0 +1,42 @@
+package org.apache.camel.component.elsql;
+
+import com.opengamma.elsql.ElSqlConfig;
+
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ */
+public enum ElSqlDatabaseVendor {
+
+    Default, Postgres, HSql, MySql, Oracle, SqlServer2008, Veritca;
+
+    ElSqlConfig asElSqlConfig() {
+        if (Postgres.equals(this)) {
+            return ElSqlConfig.POSTGRES;
+        } else if (HSql.equals(this)) {
+            return ElSqlConfig.HSQL;
+        } else if (MySql.equals(this)) {
+            return ElSqlConfig.MYSQL;
+        } else if (Oracle.equals(this)) {
+            return ElSqlConfig.ORACLE;
+        } else if (SqlServer2008.equals(this)) {
+            return ElSqlConfig.SQL_SERVER_2008;
+        } else if (Veritca.equals(this)) {
+            return ElSqlConfig.VERTICA;
+        } else {
+            return ElSqlConfig.DEFAULT;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/913efcc1/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlComponent.java
----------------------------------------------------------------------
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 51142e8..1c68612 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
@@ -28,6 +28,7 @@ import 
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 
 public class ElsqlComponent extends UriEndpointComponent {
 
+    private ElSqlDatabaseVendor databaseVendor;
     private DataSource dataSource;
     private ElSqlConfig elSqlConfig;
     private String resourceUri;
@@ -67,7 +68,7 @@ public class ElsqlComponent extends UriEndpointComponent {
             elsqlName = part[0];
             resUri = part[1];
         } else if (part.length > 2) {
-            throw new IllegalArgumentException("Invalid uri. Must by 
elsql:elsqlName/resourceUri, was: " + uri);
+            throw new IllegalArgumentException("Invalid uri. Must by 
elsql:elsqlName:resourceUri, was: " + uri);
         }
 
         String onConsume = getAndRemoveParameter(parameters, 
"consumer.onConsume", String.class);
@@ -84,27 +85,25 @@ public class ElsqlComponent extends UriEndpointComponent {
         }
 
         ElsqlEndpoint endpoint = new ElsqlEndpoint(uri, this, jdbcTemplate, 
elsqlName, resUri);
+        endpoint.setElSqlConfig(elSqlConfig);
+        endpoint.setDatabaseVendor(databaseVendor);
+        endpoint.setDataSource(ds);
+        endpoint.setDataSourceRef(dataSourceRef);
         endpoint.setOnConsume(onConsume);
         endpoint.setOnConsumeFailed(onConsumeFailed);
         endpoint.setOnConsumeBatchComplete(onConsumeBatchComplete);
-        endpoint.setDataSource(ds);
-        endpoint.setDataSourceRef(dataSourceRef);
-        endpoint.setElSqlConfig(elSqlConfig);
         return endpoint;
     }
 
-    @Override
-    protected void doStart() throws Exception {
-        super.doStart();
-
-        if (elSqlConfig == null) {
-            elSqlConfig = ElSqlConfig.DEFAULT;
-        }
+    public ElSqlDatabaseVendor getDatabaseVendor() {
+        return databaseVendor;
     }
 
-    @Override
-    protected void doStop() throws Exception {
-        super.doStop();
+    /**
+     * To use a vendor specific {@link com.opengamma.elsql.ElSqlConfig}
+     */
+    public void setDatabaseVendor(ElSqlDatabaseVendor databaseVendor) {
+        this.databaseVendor = databaseVendor;
     }
 
     /**
@@ -126,7 +125,7 @@ public class ElsqlComponent extends UriEndpointComponent {
     }
 
     /**
-     * To use the given ElSqlConfig as configuration
+     * To use a specific configured ElSqlConfig. It may be better to use the 
<tt>databaseVendor</tt> option instead.
      */
     public void setElSqlConfig(ElSqlConfig elSqlConfig) {
         this.elSqlConfig = elSqlConfig;
@@ -137,7 +136,7 @@ public class ElsqlComponent extends UriEndpointComponent {
     }
 
     /**
-     * The eqlsql resource tile which contains the elsql SQL statements to use
+     * The resource file which contains the elsql SQL statements to use
      */
     public void setResourceUri(String resourceUri) {
         this.resourceUri = resourceUri;

http://git-wip-us.apache.org/repos/asf/camel/blob/913efcc1/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlEndpoint.java
----------------------------------------------------------------------
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 d2b2cbf..62a00e7 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
@@ -45,7 +45,7 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(ElsqlEndpoint.class);
 
-    private volatile ElSql elSql;
+    private ElSql elSql;
     private NamedParameterJdbcTemplate namedJdbcTemplate;
 
     @UriPath
@@ -54,6 +54,8 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
     @UriPath
     private String resourceUri;
     @UriParam
+    private ElSqlDatabaseVendor databaseVendor;
+    @UriParam
     private ElSqlConfig elSqlConfig;
 
     public ElsqlEndpoint(String uri, Component component, 
NamedParameterJdbcTemplate namedJdbcTemplate, String elsqlName, String 
resourceUri) {
@@ -95,9 +97,14 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
     protected void doStart() throws Exception {
         super.doStart();
 
-        ObjectHelper.notNull(elSqlConfig, "elSqlConfig", this);
         ObjectHelper.notNull(resourceUri, "resourceUri", this);
 
+        if (elSqlConfig == null && databaseVendor != null) {
+            elSqlConfig = databaseVendor.asElSqlConfig();
+        } else if (elSqlConfig == null) {
+            elSqlConfig = ElSqlDatabaseVendor.Default.asElSqlConfig();
+        }
+
         URL url = 
ResourceHelper.resolveMandatoryResourceAsUrl(getCamelContext().getClassResolver(),
 resourceUri);
         elSql = ElSql.parse(elSqlConfig, url);
     }
@@ -109,12 +116,23 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
         return elsqlName;
     }
 
+    public ElSqlDatabaseVendor getDatabaseVendor() {
+        return databaseVendor;
+    }
+
+    /**
+     * To use a vendor specific {@link com.opengamma.elsql.ElSqlConfig}
+     */
+    public void setDatabaseVendor(ElSqlDatabaseVendor databaseVendor) {
+        this.databaseVendor = databaseVendor;
+    }
+
     public ElSqlConfig getElSqlConfig() {
         return elSqlConfig;
     }
 
     /**
-     * The elsql configuration to use
+     * To use a specific configured ElSqlConfig. It may be better to use the 
<tt>databaseVendor</tt> option instead.
      */
     public void setElSqlConfig(ElSqlConfig elSqlConfig) {
         this.elSqlConfig = elSqlConfig;
@@ -125,7 +143,7 @@ public class ElsqlEndpoint extends DefaultSqlEndpoint {
     }
 
     /**
-     * The eqlsql resource tile which contains the elsql SQL statements to use
+     * The resource file which contains the elsql SQL statements to use
      */
     public void setResourceUri(String resourceUri) {
         this.resourceUri = resourceUri;

http://git-wip-us.apache.org/repos/asf/camel/blob/913efcc1/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlComponentTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlComponentTest.java
 
b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlComponentTest.java
new file mode 100644
index 0000000..838f5a2
--- /dev/null
+++ 
b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlComponentTest.java
@@ -0,0 +1,88 @@
+/**
+ * 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.elsql;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+import org.junit.Test;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+public class ElSqlComponentTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+
+        // this is the database we create with some initial data for our unit 
test
+        db = new EmbeddedDatabaseBuilder()
+                
.setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase.sql").build();
+
+        jndi.bind("dataSource", db);
+
+        return jndi;
+    }
+
+    @Test
+    public void testSimpleBody() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:simple", "XXX");
+
+        mock.assertIsSatisfied();
+
+        // the result is a List
+        List<?> received = assertIsInstanceOf(List.class, 
mock.getReceivedExchanges().get(0).getIn().getBody());
+
+        // and each row in the list is a Map
+        Map<?, ?> row = assertIsInstanceOf(Map.class, received.get(0));
+
+        // and we should be able the get the project from the map that should 
be Linux
+        assertEquals("Linux", row.get("PROJECT"));
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        db.shutdown();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                getContext().getComponent("elsql", 
ElsqlComponent.class).setDataSource(db);
+                getContext().getComponent("elsql", 
ElsqlComponent.class).setResourceUri("elsql/projects.elsql");
+
+                from("direct:simple")
+                        .to("elsql:projectsById")
+                        .to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/913efcc1/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlDataSourceTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlDataSourceTest.java
 
b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlDataSourceTest.java
index 858daf4..e555808 100644
--- 
a/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlDataSourceTest.java
+++ 
b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlDataSourceTest.java
@@ -77,7 +77,7 @@ public class ElSqlDataSourceTest extends CamelTestSupport {
         return new RouteBuilder() {
             public void configure() {
                 from("direct:simple")
-                        
.to("elsql:projects:elsql/projects.elsql?dataSource=dataSource")
+                        
.to("elsql:projectsById:elsql/projects.elsql?dataSource=dataSource")
                         .to("mock:result");
             }
         };

http://git-wip-us.apache.org/repos/asf/camel/blob/913efcc1/components/camel-elsql/src/test/resources/elsql/projects.elsql
----------------------------------------------------------------------
diff --git a/components/camel-elsql/src/test/resources/elsql/projects.elsql 
b/components/camel-elsql/src/test/resources/elsql/projects.elsql
index 1d957ef..8cc1a8b 100644
--- a/components/camel-elsql/src/test/resources/elsql/projects.elsql
+++ b/components/camel-elsql/src/test/resources/elsql/projects.elsql
@@ -1,4 +1,4 @@
-@NAME(projects)
+@NAME(projectsById)
   SELECT *
   FROM projects
   WHERE license = :body

Reply via email to