This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets-examples.git
The following commit(s) were added to refs/heads/main by this push:
new b8b5f8a Add legacy karaf blueprint example, that jbang can run.
b8b5f8a is described below
commit b8b5f8a1db0e19b8179aeef691f43389ef14945f
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Sep 27 16:35:23 2023 +0200
Add legacy karaf blueprint example, that jbang can run.
---
jbang/karaf-blueprint/.gitignore | 1 +
.../DatabaseInitializationBean.java | 97 +++++++++++++++++++++
jbang/karaf-blueprint/Order.java | 69 +++++++++++++++
jbang/karaf-blueprint/Order.xml | 63 +++++++++++++
jbang/karaf-blueprint/OrderService.java | 50 +++++++++++
jbang/karaf-blueprint/README.adoc | 48 ++++++++++
jbang/karaf-blueprint/SqlMapConfig.xml | 51 +++++++++++
jbang/karaf-blueprint/camel-mybatis.xml | 60 +++++++++++++
jbang/karaf-blueprint/derby-10.16.1.1.jar | Bin 0 -> 3538107 bytes
jbang/karaf-blueprint/derbyshared-10.16.1.1.jar | Bin 0 -> 88705 bytes
jbang/karaf-blueprint/derbytools-10.16.1.1.jar | Bin 0 -> 252754 bytes
11 files changed, 439 insertions(+)
diff --git a/jbang/karaf-blueprint/.gitignore b/jbang/karaf-blueprint/.gitignore
new file mode 100644
index 0000000..afe61e7
--- /dev/null
+++ b/jbang/karaf-blueprint/.gitignore
@@ -0,0 +1 @@
+derby.log
\ No newline at end of file
diff --git a/jbang/karaf-blueprint/DatabaseInitializationBean.java
b/jbang/karaf-blueprint/DatabaseInitializationBean.java
new file mode 100644
index 0000000..bb9836b
--- /dev/null
+++ b/jbang/karaf-blueprint/DatabaseInitializationBean.java
@@ -0,0 +1,97 @@
+/*
+ * 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 {
+ System.out.println(">>>>>>>>>>>>>>>>");
+
+ 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;
+ }
+}
diff --git a/jbang/karaf-blueprint/Order.java b/jbang/karaf-blueprint/Order.java
new file mode 100644
index 0000000..a285a62
--- /dev/null
+++ b/jbang/karaf-blueprint/Order.java
@@ -0,0 +1,69 @@
+/*
+ * 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;
+
+/**
+ * A pojo representing an Order.
+ */
+public class Order {
+
+ private int id;
+ private String item;
+ private int amount;
+ private String description;
+ private boolean processed;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getItem() {
+ return item;
+ }
+
+ public void setItem(String item) {
+ this.item = item;
+ }
+
+ public int getAmount() {
+ return amount;
+ }
+
+ public void setAmount(int amount) {
+ this.amount = amount;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public boolean isProcessed() {
+ return processed;
+ }
+
+ public void setProcessed(boolean processed) {
+ this.processed = processed;
+ }
+}
diff --git a/jbang/karaf-blueprint/Order.xml b/jbang/karaf-blueprint/Order.xml
new file mode 100644
index 0000000..b6d0c3e
--- /dev/null
+++ b/jbang/karaf-blueprint/Order.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<!-- START SNIPPET: e1 -->
+<!DOCTYPE mapper
+ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="Order">
+
+ <!-- Result maps describe the mapping between the columns returned
+ from a query, and the class properties. A result map isn't
+ necessary if the columns (or aliases) match to the properties
+ exactly. -->
+ <resultMap id="OrderResult" type="Order">
+ <result property="id" column="ORD_ID"/>
+ <result property="item" column="ITEM"/>
+ <result property="amount" column="ITEM_COUNT"/>
+ <result property="description" column="ITEM_DESC"/>
+ <result property="processed" column="ORD_DELETED"/>
+ </resultMap>
+
+ <!-- Select with no parameters using the result map for Order class. -->
+ <select id="selectOrders" resultMap="OrderResult">
+ select * from ORDERS where ORD_DELETED = false order by ORD_ID
+ </select>
+
+ <!-- Insert example, using the Order parameter class -->
+ <insert id="insertOrder" parameterType="Order">
+ insert into ORDERS (
+ ORD_ID,
+ ITEM,
+ ITEM_COUNT,
+ ITEM_DESC,
+ ORD_DELETED
+ )
+ values (
+ #{id}, #{item}, #{amount}, #{description}, false
+ )
+ </insert>
+
+ <update id="consumeOrder" parameterType="Order">
+ update ORDERS set ORD_DELETED = true where ORD_ID = #{id}
+ </update>
+
+</mapper>
+<!-- END SNIPPET: e1 -->
diff --git a/jbang/karaf-blueprint/OrderService.java
b/jbang/karaf-blueprint/OrderService.java
new file mode 100644
index 0000000..764917c
--- /dev/null
+++ b/jbang/karaf-blueprint/OrderService.java
@@ -0,0 +1,50 @@
+/*
+ * 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.util.Random;
+
+/**
+ * Bean that generates and process orders.
+ */
+public class OrderService {
+
+ private int counter;
+ private Random ran = new Random();
+
+ /**
+ * Generates a new order
+ */
+ public Order generateOrder() {
+ Order order = new Order();
+ order.setId(counter++);
+ order.setItem(counter % 2 == 0 ? "111" : "222");
+ order.setAmount(ran.nextInt(10) + 1);
+ order.setDescription(counter % 2 == 0 ? "Camel in Action" : "ActiveMQ
in Action");
+ return order;
+ }
+
+ /**
+ * Processes the order
+ *
+ * @param order the order
+ * @return the transformed order
+ */
+ public String processOrder(Order order) {
+ return "Processed order id " + order.getId() + " item " +
order.getItem() + " of " + order.getAmount() + " copies of " +
order.getDescription();
+ }
+}
diff --git a/jbang/karaf-blueprint/README.adoc
b/jbang/karaf-blueprint/README.adoc
new file mode 100644
index 0000000..fb073cc
--- /dev/null
+++ b/jbang/karaf-blueprint/README.adoc
@@ -0,0 +1,48 @@
+== Karaf Blueprint
+
+This example shows the migration of a legacy OSGi blueprint example:
+-
https://github.com/apache/camel-karaf-examples/tree/main/examples/camel-example-mybatis/
+
+Where the example source code has been copied to a flat structure, but
otherwise unchanged.
+
+=== Install JBang
+
+First install JBang according to https://www.jbang.dev
+
+When JBang is installed then you should be able to run from a shell:
+
+[source,sh]
+----
+$ jbang --version
+----
+
+This will output the version of JBang.
+
+To run this example you can either install Camel on JBang via:
+
+[source,sh]
+----
+$ jbang app install camel@apache/camel
+----
+
+Which allows to run CamelJBang with `camel` as shown below.
+
+=== How to run
+
+Then you can run this example using:
+
+[source,sh]
+----
+$ camel run *
+----
+
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/jbang/karaf-blueprint/SqlMapConfig.xml
b/jbang/karaf-blueprint/SqlMapConfig.xml
new file mode 100644
index 0000000..ca8ef65
--- /dev/null
+++ b/jbang/karaf-blueprint/SqlMapConfig.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<!DOCTYPE configuration
+ PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-config.dtd">
+
+<configuration>
+
+ <settings>
+ <setting name="useGeneratedKeys" value="false"/>
+ </settings>
+
+ <!-- Use type aliases to avoid typing the full classname every time. -->
+ <typeAliases>
+ <typeAlias alias="Order" type="org.apache.camel.example.mybatis.Order"/>
+ </typeAliases>
+
+ <!-- setup environment with JDBC data source -->
+ <environments default="development">
+ <environment id="development">
+ <transactionManager type="JDBC"/>
+ <dataSource type="POOLED">
+ <property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
+ <property name="url" value="jdbc:derby:memory:mybatis"/>
+ </dataSource>
+ </environment>
+ </environments>
+
+ <!-- mapping files -->
+ <mappers>
+ <mapper resource="Order.xml"/>
+ </mappers>
+
+</configuration>
\ No newline at end of file
diff --git a/jbang/karaf-blueprint/camel-mybatis.xml
b/jbang/karaf-blueprint/camel-mybatis.xml
new file mode 100644
index 0000000..1b0f912
--- /dev/null
+++ b/jbang/karaf-blueprint/camel-mybatis.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+
+ <!-- START SNIPPET: e1 -->
+ <!-- bean which creates/destroys the database table for this example -->
+ <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 -->
+
+ <!-- START SNIPPET: e2 -->
+ <!-- order service our business logic bean that creates new orders -->
+ <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"
depends-on="database-initializer">
+
+ <!-- route that generate new orders and insert them in the database -->
+ <route id="generateOrder-route">
+ <from uri="timer:foo?period=5000"/>
+ <transform>
+ <method ref="orderService" method="generateOrder"/>
+ </transform>
+ <to uri="mybatis:insertOrder?statementType=Insert"/>
+ <log message="Inserted new order ${body.id}"/>
+ </route>
+
+ <!-- route that process the orders by picking up new rows from the database
+ and when done processing then update the row to mark it as processed
-->
+ <route id="processOrder-route">
+ <from
uri="mybatis:selectOrders?statementType=SelectList&onConsume=consumeOrder"/>
+ <to uri="bean:orderService?method=processOrder"/>
+ <log message="${body}"/>
+ </route>
+
+ </camelContext>
+ <!-- END SNIPPET: e2 -->
+
+</blueprint>
diff --git a/jbang/karaf-blueprint/derby-10.16.1.1.jar
b/jbang/karaf-blueprint/derby-10.16.1.1.jar
new file mode 100644
index 0000000..beb0189
Binary files /dev/null and b/jbang/karaf-blueprint/derby-10.16.1.1.jar differ
diff --git a/jbang/karaf-blueprint/derbyshared-10.16.1.1.jar
b/jbang/karaf-blueprint/derbyshared-10.16.1.1.jar
new file mode 100644
index 0000000..360bc62
Binary files /dev/null and b/jbang/karaf-blueprint/derbyshared-10.16.1.1.jar
differ
diff --git a/jbang/karaf-blueprint/derbytools-10.16.1.1.jar
b/jbang/karaf-blueprint/derbytools-10.16.1.1.jar
new file mode 100644
index 0000000..72a0c84
Binary files /dev/null and b/jbang/karaf-blueprint/derbytools-10.16.1.1.jar
differ