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 2016/10/29 11:24:18 UTC

[1/3] camel git commit: CAMEL-10424: Bean should act like transform/setBody when setting result

Repository: camel
Updated Branches:
  refs/heads/master 2be65c116 -> f554c483e


CAMEL-10424: Bean should act like transform/setBody when setting result


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

Branch: refs/heads/master
Commit: 36bcdc87bdcb2e214b2150bf430f181d3c93458b
Parents: 2be65c1
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 29 12:31:15 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 29 12:31:15 2016 +0200

----------------------------------------------------------------------
 .../apache/camel/component/bean/MethodInfo.java | 56 ++++++++----
 .../BeanProcessorSpecializedMessageTest.java    | 93 ++++++++++++++++++++
 2 files changed, 133 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/36bcdc87/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
index 532046b..41fe76c 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
@@ -37,17 +37,20 @@ import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Expression;
 import org.apache.camel.ExpressionEvaluationException;
+import org.apache.camel.Message;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.Pattern;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeExchangeException;
 import org.apache.camel.StreamCache;
+import org.apache.camel.impl.DefaultMessage;
 import org.apache.camel.processor.DynamicRouter;
 import org.apache.camel.processor.RecipientList;
 import org.apache.camel.processor.RoutingSlip;
 import org.apache.camel.processor.aggregate.AggregationStrategy;
 import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.util.CamelContextHelper;
+import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.camel.util.StringHelper;
@@ -116,15 +119,15 @@ public class MethodInfo {
         this.hasCustomAnnotation = hasCustomAnnotation;
         this.hasHandlerAnnotation = hasHandlerAnnotation;
         this.parametersExpression = createParametersExpression();
-        
+
         Map<Class<?>, Annotation> collectedMethodAnnotation = collectMethodAnnotations(type, method);
 
         Pattern oneway = findOneWayAnnotation(method);
         if (oneway != null) {
             pattern = oneway.value();
         }
-        
-        org.apache.camel.RoutingSlip routingSlipAnnotation = 
+
+        org.apache.camel.RoutingSlip routingSlipAnnotation =
             (org.apache.camel.RoutingSlip)collectedMethodAnnotation.get(org.apache.camel.RoutingSlip.class);
         if (routingSlipAnnotation != null && matchContext(routingSlipAnnotation.context())) {
             routingSlip = new RoutingSlip(camelContext);
@@ -138,7 +141,7 @@ public class MethodInfo {
             }
         }
 
-        org.apache.camel.DynamicRouter dynamicRouterAnnotation = 
+        org.apache.camel.DynamicRouter dynamicRouterAnnotation =
             (org.apache.camel.DynamicRouter)collectedMethodAnnotation.get(org.apache.camel.DynamicRouter.class);
         if (dynamicRouterAnnotation != null
                 && matchContext(dynamicRouterAnnotation.context())) {
@@ -153,7 +156,7 @@ public class MethodInfo {
             }
         }
 
-        org.apache.camel.RecipientList recipientListAnnotation = 
+        org.apache.camel.RecipientList recipientListAnnotation =
             (org.apache.camel.RecipientList)collectedMethodAnnotation.get(org.apache.camel.RecipientList.class);
         if (recipientListAnnotation != null
                 && matchContext(recipientListAnnotation.context())) {
@@ -201,7 +204,7 @@ public class MethodInfo {
         collectMethodAnnotations(c, method, annotations);
         return annotations;
     }
-    
+
     private void collectMethodAnnotations(Class<?> c, Method method, Map<Class<?>, Annotation> annotations) {
         for (Class<?> i : c.getInterfaces()) {
             collectMethodAnnotations(i, method, annotations);
@@ -337,16 +340,37 @@ public class MethodInfo {
     }
 
     private void fillResult(Exchange exchange, Object result) {
-        if (exchange.getPattern().isOutCapable()) {
-            // force out creating if not already created (as its lazy)
-            LOG.debug("Setting bean invocation result on the OUT message: {}", result);
-            exchange.getOut().setBody(result);
+        LOG.trace("Setting bean invocation result : {}", result);
+
+        // the bean component forces OUT if the MEP is OUT capable
+        boolean out = ExchangeHelper.isOutCapable(exchange) || exchange.hasOut();
+        Message old;
+        if (out) {
+            old = exchange.getOut();
             // propagate headers
             exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
+            // propagate attachments
+            if (exchange.getIn().hasAttachments()) {
+                exchange.getOut().getAttachments().putAll(exchange.getIn().getAttachments());
+            }
         } else {
-            // if not out then set it on the in
-            LOG.debug("Setting bean invocation result on the IN message: {}", result);
-            exchange.getIn().setBody(result);
+            old = exchange.getIn();
+        }
+
+        // create a new message container so we do not drag specialized message objects along
+        // but that is only needed if the old message is a specialized message
+        boolean copyNeeded = !(old.getClass().equals(DefaultMessage.class));
+
+        if (copyNeeded) {
+            Message msg = new DefaultMessage();
+            msg.copyFrom(old);
+            msg.setBody(result);
+
+            // replace message on exchange
+            ExchangeHelper.replaceMessage(exchange, msg, false);
+        } else {
+            // no copy needed so set replace value directly
+            old.setBody(result);
         }
     }
 
@@ -417,16 +441,16 @@ public class MethodInfo {
     public boolean isStaticMethod() {
         return Modifier.isStatic(method.getModifiers());
     }
-    
+
     /**
      * Returns true if this method is covariant with the specified method
      * (this method may above or below the specified method in the class hierarchy)
      */
     public boolean isCovariantWith(MethodInfo method) {
-        return 
+        return
             method.getMethod().getName().equals(this.getMethod().getName())
             && (method.getMethod().getReturnType().isAssignableFrom(this.getMethod().getReturnType())
-            || this.getMethod().getReturnType().isAssignableFrom(method.getMethod().getReturnType())) 
+            || this.getMethod().getReturnType().isAssignableFrom(method.getMethod().getReturnType()))
             && Arrays.deepEquals(method.getMethod().getParameterTypes(), this.getMethod().getParameterTypes());
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/36bcdc87/camel-core/src/test/java/org/apache/camel/processor/BeanProcessorSpecializedMessageTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/BeanProcessorSpecializedMessageTest.java b/camel-core/src/test/java/org/apache/camel/processor/BeanProcessorSpecializedMessageTest.java
new file mode 100644
index 0000000..6e86328
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/BeanProcessorSpecializedMessageTest.java
@@ -0,0 +1,93 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultMessage;
+
+/**
+ * @version 
+ */
+public class BeanProcessorSpecializedMessageTest extends ContextTestSupport {
+
+    public void testBeanSpecializedMessage() throws Exception {
+        MockEndpoint foo = getMockEndpoint("mock:foo");
+        foo.expectedBodiesReceived("Hello World");
+        foo.expectedHeaderReceived("foo", 123);
+        foo.message(0).predicate(new Predicate() {
+            public boolean matches(Exchange exchange) {
+                // this time we should have the specialized message
+                return exchange.getIn() instanceof MyMessage;
+            }
+        });
+
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.message(0).body().isNull();
+        result.expectedHeaderReceived("foo", 123);
+        result.message(0).predicate(new Predicate() {
+            public boolean matches(Exchange exchange) {
+                // this time we should have lost the specialized message
+                return !(exchange.getIn() instanceof MyMessage);
+            }
+        });
+
+        template.send("direct:start", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                MyMessage my = new MyMessage();
+                my.setBody("Hello World");
+                my.setHeader("foo", 123);
+                exchange.setIn(my);
+            }
+        });
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("mock:foo")
+                    .bean(MyBean.class, "empty")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    public static class MyMessage extends DefaultMessage {
+
+        @Override
+        public MyMessage newInstance() {
+            return new MyMessage();
+        }
+    }
+
+    public static class MyBean {
+
+        public static String empty(String body) {
+            // set null as body
+            return null;
+        }
+    }
+}


[3/3] camel git commit: Added test based on user forum issue

Posted by da...@apache.org.
Added test based on user forum issue


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

Branch: refs/heads/master
Commit: f554c483effb3342dde7ffcd04cf063f2e9887f8
Parents: 024898d
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 29 13:20:32 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 29 13:20:32 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/language/simple/SimpleTest.java    | 8 ++++++++
 1 file changed, 8 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f554c483/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 2e58195..357a903 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1467,6 +1467,14 @@ public class SimpleTest extends LanguageTestSupport {
         assertExpression("${body.length}", 3);
     }
 
+    public void testIntArrayLength() throws Exception {
+        exchange.getIn().setBody(new int[]{1, 20, 300});
+        assertExpression("${body[0]}", 1);
+        assertExpression("${body[1]}", 20);
+        assertExpression("${body[2]}", 300);
+        assertExpression("${body.length}", 3);
+    }
+
     public void testSimpleMapBoolean() throws Exception {
         Map<String, Object> map = new HashMap<String, Object>();
         exchange.getIn().setBody(map);


[2/3] camel git commit: Updated license header

Posted by da...@apache.org.
Updated license header


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

Branch: refs/heads/master
Commit: 024898db425ea35a5b153ca38fec29cacec48a39
Parents: 36bcdc8
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 29 12:37:13 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 29 12:37:13 2016 +0200

----------------------------------------------------------------------
 .../scala/converter/ScalaCollections.scala      | 29 ++++++++++---------
 .../camel/scala/converter/ScalaOption.scala     | 30 ++++++++++----------
 2 files changed, 30 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/024898db/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
----------------------------------------------------------------------
diff --git a/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala b/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
index 8b84f21..99f7b55 100644
--- a/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
+++ b/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
@@ -1,18 +1,19 @@
 /**
- * Copyright (C) 2011 Osinka <http://osinka.ru>
- *
- * Licensed 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.
- */
+  * 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.scala.converter
 
 import org.apache.camel.Converter

http://git-wip-us.apache.org/repos/asf/camel/blob/024898db/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
----------------------------------------------------------------------
diff --git a/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala b/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
index 7e2ba5f..449799e 100644
--- a/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
+++ b/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
@@ -1,19 +1,19 @@
 /**
- * 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.
- */
+  * 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.scala.converter
 
 import org.apache.camel.Converter