Repository: camel
Updated Branches:
  refs/heads/master dd4b3236b -> b79471c7d


CAMEL-10513: Update camel-example-mybatis to break initialization dependency on 
Camel Context being started"


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

Branch: refs/heads/master
Commit: 36178cddd36c977a1118298749c6c8978a6d1223
Parents: dd8063e
Author: Quinn Stevenson <qu...@pronoia-solutions.com>
Authored: Fri Nov 25 10:36:18 2016 -0700
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Dec 4 13:17:55 2016 +0100

----------------------------------------------------------------------
 .../camel/example/mybatis/DatabaseBean.java     | 86 -----------------
 .../mybatis/DatabaseInitializationBean.java     | 98 ++++++++++++++++++++
 .../OSGI-INF/blueprint/camel-mybatis.xml        |  7 +-
 .../src/main/resources/SqlMapConfig.xml         |  2 +-
 4 files changed, 102 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/36178cdd/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseBean.java
----------------------------------------------------------------------
diff --git 
a/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseBean.java
 
b/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseBean.java
deleted file mode 100644
index 302d492..0000000
--- 
a/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseBean.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/**
- * 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.example.mybatis;
-
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.CamelContextAware;
-import org.apache.camel.component.mybatis.MyBatisComponent;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Bean that creates the database table
- */
-public class DatabaseBean implements CamelContextAware {
-
-    private static final Logger LOG = 
LoggerFactory.getLogger(DatabaseBean.class);
-    private CamelContext camelContext;
-
-    public CamelContext getCamelContext() {
-        return camelContext;
-    }
-
-    public void setCamelContext(CamelContext camelContext) {
-        this.camelContext = camelContext;
-    }
-
-    public void create() throws Exception {
-        String sql = "create table ORDERS (\n"
-              + "  ORD_ID integer primary key,\n"
-              + "  ITEM varchar(10),\n"
-              + "  ITEM_COUNT varchar(5),\n"
-              + "  ITEM_DESC varchar(30),\n"
-              + "  ORD_DELETED boolean\n"
-              + ")";
-
-        LOG.info("Creating table orders ...");
-
-        try {
-            execute("drop table orders");
-        } catch (Throwable e) {
-            // ignore
-        }
-
-        execute(sql);
-
-        LOG.info("... created table orders");
-    }
-
-    public void destroy() throws Exception {
-        try {
-            execute("drop table orders");
-        } catch (Throwable e) {
-            // ignore
-        }
-    }
-
-    private void execute(String sql) throws SQLException {
-        MyBatisComponent component = camelContext.getComponent("mybatis", 
MyBatisComponent.class);
-        Connection con = 
component.getSqlSessionFactory().getConfiguration().getEnvironment().getDataSource().getConnection();
-        Statement stm = con.createStatement();
-        stm.execute(sql);
-        // must commit connection
-        con.commit();
-        stm.close();
-        con.close();
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/36178cdd/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseInitializationBean.java
----------------------------------------------------------------------
diff --git 
a/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseInitializationBean.java
 
b/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseInitializationBean.java
new file mode 100644
index 0000000..873d8fa
--- /dev/null
+++ 
b/examples/camel-example-mybatis/src/main/java/org/apache/camel/example/mybatis/DatabaseInitializationBean.java
@@ -0,0 +1,98 @@
+/**
+ * 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.example.mybatis;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.derby.jdbc.EmbeddedDriver;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+/**
+ * Bean that creates the database table
+ */
+public class DatabaseInitializationBean {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DatabaseInitializationBean.class);
+
+    String url;
+
+    Connection connection;
+
+    public DatabaseInitializationBean() {
+    }
+
+    public void create() throws Exception {
+        LOG.info("Creating database tables ...");
+        if (connection == null) {
+            EmbeddedDriver driver = new EmbeddedDriver();
+            connection = driver.connect(url + ";create=true", null);
+        }
+
+        String sql = "create table ORDERS (\n"
+                + "  ORD_ID integer primary key,\n"
+                + "  ITEM varchar(10),\n"
+                + "  ITEM_COUNT varchar(5),\n"
+                + "  ITEM_DESC varchar(30),\n"
+                + "  ORD_DELETED boolean\n"
+                + ")";
+
+        try {
+            execute("drop table orders");
+        } catch (Throwable e) {
+            // ignore
+        }
+
+        execute(sql);
+
+        LOG.info("Database tables created");
+    }
+
+    public void drop() throws Exception {
+        LOG.info("Dropping database tables ...");
+
+        try {
+            execute("drop table orders");
+        } catch (Throwable e) {
+            // ignore
+        }
+        connection.close();
+
+        LOG.info("Database tables dropped");
+    }
+
+    private void execute(String sql) throws SQLException {
+        Statement stm = connection.createStatement();
+        stm.execute(sql);
+        // must commit connection
+        connection.commit();
+        stm.close();
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/36178cdd/examples/camel-example-mybatis/src/main/resources/OSGI-INF/blueprint/camel-mybatis.xml
----------------------------------------------------------------------
diff --git 
a/examples/camel-example-mybatis/src/main/resources/OSGI-INF/blueprint/camel-mybatis.xml
 
b/examples/camel-example-mybatis/src/main/resources/OSGI-INF/blueprint/camel-mybatis.xml
index 23742d7..8f74000 100644
--- 
a/examples/camel-example-mybatis/src/main/resources/OSGI-INF/blueprint/camel-mybatis.xml
+++ 
b/examples/camel-example-mybatis/src/main/resources/OSGI-INF/blueprint/camel-mybatis.xml
@@ -22,9 +22,8 @@
 
   <!-- START SNIPPET: e1 -->
   <!-- bean which creates/destroys the database table for this example -->
-  <bean id="initDatabase" class="org.apache.camel.example.mybatis.DatabaseBean"
-        init-method="create" destroy-method="destroy">
-    <property name="camelContext" ref="myBatisAndCamel"/>
+  <bean id="database-initializer" 
class="org.apache.camel.example.mybatis.DatabaseInitializationBean" 
init-method="create" destroy-method="drop">
+    <property name="url" value="jdbc:derby:memory:mybatis" />
   </bean>
   <!-- END SNIPPET: e1 -->
 
@@ -33,7 +32,7 @@
   <bean id="orderService" 
class="org.apache.camel.example.mybatis.OrderService"/>
 
   <!-- here is Camel configured with a number of routes -->
-  <camelContext id="myBatisAndCamel" 
xmlns="http://camel.apache.org/schema/blueprint";>
+  <camelContext id="myBatisAndCamel" 
xmlns="http://camel.apache.org/schema/blueprint"; 
depends-on="database-initializer">
 
     <!-- route that generate new orders and insert them in the database -->
     <route id="generateOrder-route">

http://git-wip-us.apache.org/repos/asf/camel/blob/36178cdd/examples/camel-example-mybatis/src/main/resources/SqlMapConfig.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-mybatis/src/main/resources/SqlMapConfig.xml 
b/examples/camel-example-mybatis/src/main/resources/SqlMapConfig.xml
index 9683925..796c2fc 100644
--- a/examples/camel-example-mybatis/src/main/resources/SqlMapConfig.xml
+++ b/examples/camel-example-mybatis/src/main/resources/SqlMapConfig.xml
@@ -37,7 +37,7 @@
       <transactionManager type="JDBC"/>
       <dataSource type="POOLED">
         <property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
-        <property name="url" value="jdbc:derby:memory:mybatis;create=true"/>
+        <property name="url" value="jdbc:derby:memory:mybatis"/>
       </dataSource>
     </environment>
   </environments>

Reply via email to