Repository: camel
Updated Branches:
  refs/heads/master 69744b335 -> 624b8a735


CAMEL-10140: Java 8 type safe process DSL


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

Branch: refs/heads/master
Commit: e65d5cc3192bedbb97bc007d34cbb5e631b3197e
Parents: f7e603a
Author: lburgazzoli <lburgazz...@gmail.com>
Authored: Thu Sep 8 13:16:52 2016 +0200
Committer: lburgazzoli <lburgazz...@gmail.com>
Committed: Thu Sep 8 15:55:11 2016 +0200

----------------------------------------------------------------------
 .../org/apache/camel/builder/ProcessClause.java | 120 +++++++++++++++++++
 .../apache/camel/model/ProcessorDefinition.java |  16 +++
 examples/camel-example-dsl-java8/pom.xml        |  10 +-
 .../camel/example/dsl/java8/MyApplication.java  |  71 +++++++++--
 .../apache/camel/example/dsl/java8/MyRoute.java |  63 ----------
 5 files changed, 195 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e65d5cc3/camel-core/src/main/java/org/apache/camel/builder/ProcessClause.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/builder/ProcessClause.java 
b/camel-core/src/main/java/org/apache/camel/builder/ProcessClause.java
new file mode 100644
index 0000000..5235878
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/builder/ProcessClause.java
@@ -0,0 +1,120 @@
+/**
+ * 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.builder;
+
+import java.util.Map;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+
+public class ProcessClause<T> implements Processor {
+    private final T parent;
+    private Processor processor;
+
+    public ProcessClause(T parent) {
+        this.parent = parent;
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        if (processor != null) {
+            processor.process(exchange);
+        }
+    }
+
+    // *******************************
+    // Exchange
+    // *******************************
+
+    /**
+     * TODO: document
+     *
+     * Note: this is experimental and subject to changes in future releases.
+     */
+    public T exchange(final Consumer<Exchange> consumer) {
+        processor = consumer::accept;
+        return parent;
+    }
+
+
+    // *******************************
+    // Message
+    // *******************************
+
+    /**
+     * TODO: document
+     *
+     * Note: this is experimental and subject to changes in future releases.
+     */
+    public T message(final Consumer<Message> consumer) {
+        processor = e -> consumer.accept(e.getIn());
+        return parent;
+    }
+
+    // *******************************
+    // Body
+    // *******************************
+
+    /**
+     * TODO: document
+     *
+     * Note: this is experimental and subject to changes in future releases.
+     */
+    public T body(final Consumer<Object> consumer) {
+        processor = e -> consumer.accept(e.getIn().getBody());
+        return parent;
+    }
+
+    /**
+     * TODO: document
+     *
+     * Note: this is experimental and subject to changes in future releases.
+     */
+    public <B> T body(Class<B> type, final Consumer<B> consumer) {
+        processor = e -> consumer.accept(e.getIn().getBody(type));
+        return parent;
+    }
+
+    /**
+     * TODO: document
+     *
+     * Note: this is experimental and subject to changes in future releases.
+     */
+    public T body(final BiConsumer<Object, Map<String, Object>> consumer) {
+        processor = e -> consumer.accept(
+            e.getIn().getBody(),
+            e.getIn().getHeaders()
+        );
+        return parent;
+    }
+
+    /**
+     * TODO: document
+     *
+     * Note: this is experimental and subject to changes in future releases.
+     */
+    public <B> T body(Class<B> type, final BiConsumer<B, Map<String, Object>> 
consumer) {
+        processor = e -> consumer.accept(
+            e.getIn().getBody(type),
+            e.getIn().getHeaders()
+        );
+        return parent;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e65d5cc3/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java 
b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 47b014e..b7e8fc9 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -48,6 +48,7 @@ import org.apache.camel.Route;
 import org.apache.camel.builder.DataFormatClause;
 import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.builder.ProcessClause;
 import org.apache.camel.builder.ProcessorBuilder;
 import org.apache.camel.model.language.ConstantExpression;
 import org.apache.camel.model.language.ExpressionDefinition;
@@ -2664,6 +2665,21 @@ public abstract class ProcessorDefinition<Type extends 
ProcessorDefinition<Type>
     }
 
     /**
+     * TODO: document
+     * Note: this is experimental and subject to changes in future releases.
+     *
+     * @return the builder
+     */
+    @SuppressWarnings("unchecked")
+    public ProcessClause<ProcessorDefinition<Type>> process() {
+        ProcessClause<ProcessorDefinition<Type>> clause = new 
ProcessClause<>(this);
+        ProcessDefinition answer = new ProcessDefinition(clause);
+
+        addOutput(answer);
+        return clause;
+    }
+
+    /**
      * <a href="http://camel.apache.org/message-translator.html";>Message 
Translator EIP:</a>
      * Adds a bean which is invoked which could be a final destination, or 
could be a transformation in a pipeline
      *

http://git-wip-us.apache.org/repos/asf/camel/blob/e65d5cc3/examples/camel-example-dsl-java8/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-dsl-java8/pom.xml 
b/examples/camel-example-dsl-java8/pom.xml
index 417bd1d..436169f 100644
--- a/examples/camel-example-dsl-java8/pom.xml
+++ b/examples/camel-example-dsl-java8/pom.xml
@@ -37,14 +37,6 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-spring</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-spring-javaconfig</artifactId>
-    </dependency>
 
     <!-- logging -->
     <dependency>
@@ -78,7 +70,7 @@
         <artifactId>camel-maven-plugin</artifactId>
         <version>${project.version}</version>
         <configuration>
-          <basedPackages>org.apache.camel.example.dsl.java8</basedPackages>
+          
<mainClass>org.apache.camel.example.dsl.java8.MyApplication</mainClass>
         </configuration>
       </plugin>
     </plugins>

http://git-wip-us.apache.org/repos/asf/camel/blob/e65d5cc3/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyApplication.java
----------------------------------------------------------------------
diff --git 
a/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyApplication.java
 
b/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyApplication.java
index d6e2ba8..abaf561 100644
--- 
a/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyApplication.java
+++ 
b/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyApplication.java
@@ -16,20 +16,65 @@
  */
 package org.apache.camel.example.dsl.java8;
 
-import org.apache.camel.spring.javaconfig.CamelConfiguration;
-import org.apache.camel.spring.javaconfig.Main;
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-@ComponentScan
-public class MyApplication extends CamelConfiguration {
-    /**
-     * Allow this route to be run as an application
-     */
+import java.util.Date;
+import java.util.Objects;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.main.Main;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class MyApplication {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(MyApplication.class);
+
+    private MyApplication() {
+    }
+
     public static void main(String[] args) throws Exception {
         Main main = new Main();
-        main.setConfigClass(MyApplication.class);
-        main.run();
+        main.addRouteBuilder(new MyRouteBuilder());
+        main.run(args);
+    }
+
+    private static class MyRouteBuilder extends RouteBuilder {
+        @Override
+        public void configure() throws Exception {
+            from("timer:simple?period=503")
+                .id("simple-route")
+                .transform()
+                    .exchange(this::dateToTime)
+                .process()
+                    .message(this::log)
+                .process()
+                    .body(this::log)
+                .choice()
+                    .when()
+                        .body(Integer.class, b -> (b & 1) == 0)
+                        .log("Received even number")
+                    .when()
+                        .body(Integer.class, (b, h) -> h.containsKey("skip") ? 
false : (b & 1) == 0)
+                        .log("Received odd number")
+                    .when()
+                        .body(Objects::isNull)
+                        .log("Received null body")
+                    .when()
+                        .body(Integer.class, b -> (b & 1) != 0)
+                        .log("Received odd number")
+                .endChoice();
+        }
+
+        private Long dateToTime(Exchange e) {
+            return e.getProperty(Exchange.TIMER_FIRED_TIME, 
Date.class).getTime();
+        }
+
+        private void log(Object b) {
+            LOGGER.info("body is: {}", b);
+        }
+
+        private void log(Message m) {
+            LOGGER.info("message is: {}", m);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e65d5cc3/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyRoute.java
----------------------------------------------------------------------
diff --git 
a/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyRoute.java
 
b/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyRoute.java
deleted file mode 100644
index bcfb294..0000000
--- 
a/examples/camel-example-dsl-java8/src/main/java/org/apache/camel/example/dsl/java8/MyRoute.java
+++ /dev/null
@@ -1,63 +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.dsl.java8;
-
-import java.util.Date;
-import java.util.Objects;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.builder.RouteBuilder;
-import org.springframework.stereotype.Component;
-
-@Component
-public class MyRoute extends RouteBuilder {
-    @Override
-    public void configure() throws Exception {
-        from("timer:simple?period=503")
-            .id("simple-route")
-            .transform()
-                .exchange(this::dateToTime)
-            .choice()
-                .when()
-                    .body(Integer.class, b ->  (b & 1) == 0)
-                    .log("Received even number")
-                .when()
-                    .body(Integer.class, (b, h) -> h.containsKey("skip") ? 
false : (b & 1) == 0)
-                    .log("Received odd number")
-                .when()
-                    .body(Objects::isNull)
-                    .log("Received null body")
-                .when()
-                    .body(Integer.class, b ->  (b & 1) != 0)
-                    .log("Received odd number")
-                .endChoice();
-
-
-         //   .transform(this::dateToTime2)
-         //   .transform(function(
-         //       (Exchange e) -> e.getProperty(Exchange.TIMER_FIRED_TIME, 
Date.class).getTime()
-         //   ))
-    }
-
-    private Long dateToTime(Exchange e) {
-        return e.getProperty(Exchange.TIMER_FIRED_TIME, Date.class).getTime();
-    }
-
-    private <T> T dateToTime2(Exchange exchange, Class<T> type) {
-        return null;
-    }
-}

Reply via email to