You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/05/01 19:24:38 UTC

svn commit: r534163 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/impl/DefaultEndpoint.java test/java/org/apache/camel/impl/ProducerTest.java

Author: jstrachan
Date: Tue May  1 10:24:32 2007
New Revision: 534163

URL: http://svn.apache.org/viewvc?view=rev&rev=534163
Log:
avoid the copy of an exchange if it is of the correct type already

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ProducerTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java?view=diff&rev=534163&r1=534162&r2=534163
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java Tue May  1 10:24:32 2007
@@ -24,6 +24,9 @@
 import org.apache.camel.Service;
 import org.apache.camel.util.ObjectHelper;
 
+import java.lang.reflect.TypeVariable;
+import java.lang.reflect.Type;
+import java.lang.reflect.ParameterizedType;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 
@@ -110,6 +113,12 @@
     }
 
     public E createExchange(Exchange exchange) {
+        Class<E> exchangeType = getExchangeType();
+        if (exchangeType != null) {
+            if (exchangeType.isInstance(exchange)) {
+                return exchangeType.cast(exchange);
+            }
+        }
         E answer = createExchange();
         answer.copyFrom(exchange);
         return answer;
@@ -118,6 +127,24 @@
     public E toExchangeType(Exchange exchange) {
         // TODO avoid cloning exchanges if E == Exchange!
         return createExchange(exchange);
+    }
+
+    /**
+     * Returns the type of the exchange which is generated by this component
+     */
+    public Class<E> getExchangeType() {
+        Type type = getClass().getGenericSuperclass();
+        if (type instanceof ParameterizedType) {
+            ParameterizedType parameterizedType = (ParameterizedType) type;
+            Type[] arguments = parameterizedType.getActualTypeArguments();
+            if (arguments.length > 0) {
+                Type argumentType = arguments[0];
+                if (argumentType instanceof Class) {
+                    return (Class<E>) argumentType;
+                }
+            }
+        }
+        return null;
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ProducerTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ProducerTest.java?view=diff&rev=534163&r1=534162&r2=534163
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ProducerTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ProducerTest.java Tue May  1 10:24:32 2007
@@ -33,7 +33,7 @@
     private CamelContext context = new DefaultCamelContext();
 
     public void testUsingADerivedExchange() throws Exception {
-        Endpoint<MyExchange> endpoint = new DefaultEndpoint<MyExchange>("foo", new DefaultComponent()) {
+        DefaultEndpoint<MyExchange> endpoint = new DefaultEndpoint<MyExchange>("foo", new DefaultComponent()) {
             public Consumer<MyExchange> createConsumer(Processor processor) throws Exception {
                 return null;
             }
@@ -60,5 +60,19 @@
         // now lets try send in a normal exchange
         Exchange exchange = new DefaultExchange(context);
         producer.process(exchange);
+
+        Class type = endpoint.getExchangeType();
+        assertEquals("exchange type", MyExchange.class, type);
+
+        MyExchange actual = endpoint.toExchangeType(exchange);
+        assertNotNull(actual);
+        assertTrue("Not same exchange", actual != exchange);
+
+
+        MyExchange expected = new MyExchange(context);
+        actual = endpoint.toExchangeType(expected);
+
+        assertSame("Should not copy an exchange when of the correct type", expected, actual);
+
     }
 }