Author: davsclaus
Date: Fri Aug 28 05:15:55 2009
New Revision: 808758

URL: http://svn.apache.org/viewvc?rev=808758&view=rev
Log:
CAMEL-1952: Added getIn(type) and getOut(type) to Exchange.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=808758&r1=808757&r2=808758&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Fri Aug 
28 05:15:55 2009
@@ -185,6 +185,14 @@
     Message getIn();
 
     /**
+     * Returns the inbound request message as the given type
+     *
+     * @param type the given type
+     * @return the message as the given type or <tt>null</tt> if not possible 
to covert to given type
+     */
+    <T> T getIn(Class<T> type);
+
+    /**
      * Sets the inbound message instance
      *
      * @param in the inbound message
@@ -202,6 +210,14 @@
     Message getOut();
 
     /**
+     * Returns the outbound request message as the given type
+     *
+     * @param type the given type
+     * @return the message as the given type or <tt>null</tt> if not possible 
to covert to given type
+     */
+    <T> T getOut(Class<T> type);
+
+    /**
      * Returns whether an OUT message has been set or not.
      *
      * @return <tt>true</tt> if an OUT message exists, <tt>false</tt> 
otherwise.
@@ -248,7 +264,8 @@
      *
      * @return true if this exchange failed due to either an exception or fault
      * @see Exchange#getException()
-     * @see Exchange#getFault()
+     * @see Message#setFault(boolean)
+     * @see Message#isFault()
      */
     boolean isFailed();
 

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=808758&r1=808757&r2=808758&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java 
Fri Aug 28 05:15:55 2009
@@ -168,6 +168,19 @@
         return in;
     }
 
+    public <T> T getIn(Class<T> type) {
+        Message in = getIn();
+
+        // eager same instance type test to avoid the overhead of invoking the 
type converter
+        // if already same type
+        if (type.isInstance(in)) {
+            return type.cast(in);
+        }
+
+        // fallback to use type converter
+        return context.getTypeConverter().convertTo(type, in);
+    }
+
     public void setIn(Message in) {
         this.in = in;
         configureMessage(in);
@@ -183,6 +196,23 @@
         return out;
     }
 
+    public <T> T getOut(Class<T> type) {
+        if (!hasOut()) {
+            return null;
+        }
+
+        Message out = getOut();
+
+        // eager same instance type test to avoid the overhead of invoking the 
type converter
+        // if already same type
+        if (type.isInstance(out)) {
+            return type.cast(out);
+        }
+
+        // fallback to use type converter
+        return context.getTypeConverter().convertTo(type, out);
+    }
+
     public boolean hasOut() {
         return out != null;
     }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java?rev=808758&r1=808757&r2=808758&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
 Fri Aug 28 05:15:55 2009
@@ -83,4 +83,22 @@
         assertEquals("123", exchange.getIn().getHeader("bar", String.class));
     }
 
+    public void testInType() throws Exception {
+        exchange.setIn(new MyMessage());
+
+        MyMessage my = exchange.getIn(MyMessage.class);
+        assertNotNull(my);
+    }
+
+    public void testOutType() throws Exception {
+        exchange.setOut(new MyMessage());
+
+        MyMessage my = exchange.getOut(MyMessage.class);
+        assertNotNull(my);
+    }
+
+    private class MyMessage extends DefaultMessage {
+
+    }
+
 }


Reply via email to