Author: ningjiang
Date: Sun Mar 14 05:15:29 2010
New Revision: 922763

URL: http://svn.apache.org/viewvc?rev=922763&view=rev
Log:
CAMEL-2547 Bean parameter binding annotation for attachments

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java   
(with props)
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java
   (with props)
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java?rev=922763&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java 
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java Sun 
Mar 14 05:15:29 2010
@@ -0,0 +1,34 @@
+/**
+ * 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;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marks a parameter as being Map of attachments of an inbound {...@link 
Message}
+ * 
+ * @version $Revision$
+ */
+...@retention(RetentionPolicy.RUNTIME)
+...@documented
+...@target({ElementType.PARAMETER })
+public @interface Attachments {
+}

Propchange: 
camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/main/java/org/apache/camel/Attachments.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=922763&r1=922762&r2=922763&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
 Sun Mar 14 05:15:29 2010
@@ -56,6 +56,24 @@ public final class ExpressionBuilder {
      */
     private ExpressionBuilder() {
     }
+    
+    /**
+     * Returns an expression for the inbound message attachments
+     *
+     * @return an expression object which will return the inbound message 
attachments
+     */
+    public static Expression attachmentsExpression() {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                return exchange.getIn().getAttachments();
+            }
+
+            @Override
+            public String toString() {
+                return "attachments";
+            }
+        };
+    }
 
     /**
      * Returns an expression for the header value with the given name

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=922763&r1=922762&r2=922763&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
 Sun Mar 14 05:15:29 2010
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.camel.Attachments;
 import org.apache.camel.Body;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
@@ -50,7 +51,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import static org.apache.camel.util.ExchangeHelper.convertToType;
-
 /**
  * Represents the metadata about a bean type created via a combination of
  * introspection and annotations together with some useful sensible defaults
@@ -605,8 +605,9 @@ public class BeanInfo {
 
     private Expression 
createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method, 
             Class<?> parameterType, Annotation annotation) {
-
-        if (annotation instanceof Property) {
+        if (annotation instanceof Attachments) {
+            return ExpressionBuilder.attachmentsExpression();
+        } else if (annotation instanceof Property) {
             Property propertyAnnotation = (Property)annotation;
             return 
ExpressionBuilder.propertyExpression(propertyAnnotation.value());
         } else if (annotation instanceof Properties) {

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java?rev=922763&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java
 Sun Mar 14 05:15:29 2010
@@ -0,0 +1,84 @@
+/**
+ * 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.bean;
+
+import java.util.Map;
+
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+import javax.naming.Context;
+
+import org.apache.camel.Attachments;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.util.jndi.JndiContext;
+
+public class BeanWithAttachmentAnnotationTest extends ContextTestSupport {
+    
+    public void testBeanWithAnnotationAndExchangeTest() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("attachment");
+        
+        template.send("direct:in", new Processor() {
+
+            public void process(Exchange exchange) throws Exception {
+                exchange.setPattern(ExchangePattern.InOut);
+                Message m = exchange.getIn();
+                m.addAttachment("attachment", new DataHandler(new 
FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
+            }
+            
+        });
+
+        mock.assertIsSatisfied();
+    }
+
+    protected Context createJndiContext() throws Exception {
+        JndiContext answer = new JndiContext();
+        answer.bind("processor", new AttachmentProcessor());
+        return answer;
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:in").to("bean:processor").to("mock:result");
+            }
+        };
+    }
+
+    public static class AttachmentProcessor {
+        // START SNIPPET: e1
+        public String doSomething(@Attachments Map<String, DataHandler> 
attachments) {
+            assertNotNull(attachments);
+            assertEquals("The attache size is wrong", 1, attachments.size());
+            String key = attachments.keySet().iterator().next();
+            assertNotNull(key);
+            assertNotNull(attachments.get(key));
+            DataHandler handler = attachments.get(key);
+            assertNotNull(handler);
+            assertTrue("The data source should be a instance of 
FileDataSource", handler.getDataSource() instanceof FileDataSource);     
+            return key;
+        }
+        // END SNIPPET: e1
+    }
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to