svn commit: r901136 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/converter/ main/java/org/apache/camel/spi/ test/java/org/apache/camel/converter/ test/java/org/apache/camel/impl/

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r901136 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/converter/ main/java/org/apache/camel/spi/ test/java/org/apache/camel/converter/ test/java/org/apache/camel/impl/

davsclaus-2
Author: davsclaus
Date: Wed Jan 20 11:21:53 2010
New Revision: 901136

URL: http://svn.apache.org/viewvc?rev=901136&view=rev
Log:
CAMEL-2382: Instance based @Converter can now have parent type converter injected with TypeConverterAware. Fixed CS.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java?rev=901136&r1=901135&r2=901136&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java Wed Jan 20 11:21:53 2010
@@ -177,7 +177,7 @@
                             injector = new CachingInjector<Object>(registry, CastUtils.cast(type, Object.class));
                         }
                         registerTypeConverter(registry, method, toType, fromType,
-                                new InstanceMethodTypeConverter(injector, method));
+                                new InstanceMethodTypeConverter(injector, method, registry));
                     }
                 }
             }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java?rev=901136&r1=901135&r2=901136&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java Wed Jan 20 11:21:53 2010
@@ -21,6 +21,8 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
+import org.apache.camel.spi.TypeConverterAware;
+import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -33,11 +35,13 @@
     private final CachingInjector<?>injector;
     private final Method method;
     private final boolean useExchange;
+    private final TypeConverterRegistry registry;
 
-    public InstanceMethodTypeConverter(CachingInjector<?> injector, Method method) {
+    public InstanceMethodTypeConverter(CachingInjector<?> injector, Method method, TypeConverterRegistry registry) {
         this.injector = injector;
         this.method = method;
         this.useExchange = method.getParameterTypes().length == 2;
+        this.registry = registry;
     }
 
     @Override
@@ -55,6 +59,13 @@
         if (instance == null) {
             throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName());
         }
+        // inject parent type converter
+        if (instance instanceof TypeConverterAware) {
+            if (registry instanceof TypeConverter) {
+                TypeConverter parentTypeConverter = (TypeConverter) registry;
+                ((TypeConverterAware) instance).setTypeConverter(parentTypeConverter);
+            }
+        }
         return useExchange
             ? (T)ObjectHelper.invokeMethod(method, instance, value, exchange) : (T)ObjectHelper
                 .invokeMethod(method, instance, value);

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java?rev=901136&r1=901135&r2=901136&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java Wed Jan 20 11:21:53 2010
@@ -46,7 +46,7 @@
      *
      * @param toType        the type to convert to
      * @param fromType      the type to convert from
-     * @return the type converter or null if not found.
+     * @return the type converter or <tt>null</tt> if not found.
      */
     TypeConverter lookup(Class<?> toType, Class<?> fromType);
 

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java?rev=901136&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java Wed Jan 20 11:21:53 2010
@@ -0,0 +1,51 @@
+/**
+ * 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.converter;
+
+import java.math.BigDecimal;
+
+/**
+ * @version $Revision$
+ */
+public class PurchaseOrder {
+
+    private final String name;
+    private final BigDecimal price;
+    private final int amount;
+
+    public PurchaseOrder(String name, BigDecimal price, int amount) {
+        this.name = name;
+        this.price = price;
+        this.amount = amount;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public BigDecimal getPrice() {
+        return price;
+    }
+
+    public int getAmount() {
+        return amount;
+    }
+
+    public String toString() {
+        return "Ordering " + amount + " of " + name + " at total " + price;
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrder.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java?rev=901136&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java Wed Jan 20 11:21:53 2010
@@ -0,0 +1,61 @@
+/**
+ * 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.converter;
+
+import java.math.BigDecimal;
+
+import org.apache.camel.Converter;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.spi.TypeConverterAware;
+
+/**
+ * @version $Revision$
+ */
+@Converter
+public class PurchaseOrderConverter implements TypeConverterAware {
+
+    private TypeConverter converter;
+
+    public void setTypeConverter(TypeConverter parentTypeConverter) {
+        this.converter = parentTypeConverter;
+    }
+
+    @Converter
+    public PurchaseOrder toPurchaseOrder(byte[] data) {
+        String s = converter.convertTo(String.class, data);
+
+        if (s == null || s.length() < 30) {
+            throw new IllegalArgumentException("data is invalid");
+        }
+
+        s = s.replaceAll("##START##", "");
+        s = s.replaceAll("##END##", "");
+
+        String name = s.substring(0, 9).trim();
+        String s2 = s.substring(10, 19).trim();
+        String s3 = s.substring(20).trim();
+
+        BigDecimal price = new BigDecimal(s2);
+        price.setScale(2);
+
+        Integer amount = converter.convertTo(Integer.class, s3);
+
+        PurchaseOrder order = new PurchaseOrder(name, price, amount);
+        return order;
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PurchaseOrderConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java?rev=901136&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java Wed Jan 20 11:21:53 2010
@@ -0,0 +1,38 @@
+/**
+ * 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.converter;
+
+import org.apache.camel.ContextTestSupport;
+
+/**
+ * @version $Revision$
+ */
+public class TypeConverterAwareTest extends ContextTestSupport {
+
+    public void testPurchaseOrderConverter() throws Exception {
+        byte[] data = "##START##AKC4433   179       3##END##".getBytes();
+        PurchaseOrder order = context.getTypeConverter().convertTo(PurchaseOrder.class, data);
+        assertNotNull(order);
+
+        System.out.println(order);
+
+        assertEquals("AKC4433", order.getName());
+        assertEquals("179", order.getPrice().toString());
+        assertEquals(3, order.getAmount());
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TypeConverterAwareTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java?rev=901136&r1=901135&r2=901136&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java Wed Jan 20 11:21:53 2010
@@ -42,7 +42,7 @@
  */
 public class DefaultProducerTemplateAsyncTest extends ContextTestSupport {
 
-    private static final AtomicInteger order = new AtomicInteger(0);
+    private static final AtomicInteger ORDER = new AtomicInteger(0);
 
     public void testRequestAsync() throws Exception {
         Exchange exchange = new DefaultExchange(context);
@@ -307,7 +307,7 @@
     }
 
     public void testAsyncCallbackExchangeInOnly() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
@@ -319,22 +319,22 @@
         template.asyncCallback("direct:start", exchange, new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("Hello World", exchange.getIn().getBody());
                 latch.countDown();
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         latch.await(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
         assertMockEndpointsSatisfied();
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
     }
 
     public void testAsyncCallbackExchangeInOut() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         final CountDownLatch latch = new CountDownLatch(1);
 
@@ -345,21 +345,21 @@
         template.asyncCallback("direct:echo", exchange, new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("HelloHello", exchange.getOut().getBody());
                 latch.countDown();
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         latch.await(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
     }
 
     public void testAsyncCallbackExchangeInOnlyGetResult() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
@@ -369,22 +369,22 @@
         Future<Exchange> future = template.asyncCallback("direct:start", exchange, new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("Hello World", exchange.getIn().getBody());
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         Exchange reply = future.get(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
         assertMockEndpointsSatisfied();
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
         assertNotNull(reply);
     }
 
     public void testAsyncCallbackExchangeInOutGetResult() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         Exchange exchange = context.getEndpoint("direct:start").createExchange();
         exchange.getIn().setBody("Hello");
@@ -393,22 +393,22 @@
         Future<Exchange> future = template.asyncCallback("direct:echo", exchange, new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("HelloHello", exchange.getOut().getBody());
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         Exchange reply = future.get(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
         assertNotNull(reply);
         assertEquals("HelloHello", reply.getOut().getBody());
     }
 
     public void testAsyncCallbackBodyInOnly() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
@@ -417,85 +417,85 @@
         template.asyncCallbackSendBody("direct:start", "Hello", new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("Hello World", exchange.getIn().getBody());
                 latch.countDown();
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         latch.await(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
         assertMockEndpointsSatisfied();
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
     }
 
     public void testAsyncCallbackBodyInOut() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         final CountDownLatch latch = new CountDownLatch(1);
 
         template.asyncCallbackRequestBody("direct:echo", "Hello", new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("HelloHello", exchange.getOut().getBody());
                 latch.countDown();
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         latch.await(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
     }
 
     public void testAsyncCallbackBodyInOnlyGetResult() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
         Future<Object> future = template.asyncCallbackSendBody("direct:start", "Hello", new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("Hello World", exchange.getIn().getBody());
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         Object reply = future.get(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
         assertMockEndpointsSatisfied();
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
         // no reply when in only
         assertEquals(null, reply);
     }
 
     public void testAsyncCallbackBodyInOutGetResult() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         Future<Object> future = template.asyncCallbackRequestBody("direct:echo", "Hello", new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("HelloHello", exchange.getOut().getBody());
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         Object reply = future.get(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
         assertEquals("HelloHello", reply);
     }
 
     public void testAsyncCallbackInOnlyProcessor() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
@@ -508,22 +508,22 @@
         }, new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("Hello World", exchange.getIn().getBody());
                 latch.countDown();
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         latch.await(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
         assertMockEndpointsSatisfied();
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
     }
 
     public void testAsyncCallbackInOutProcessor() throws Exception {
-        order.set(0);
+        ORDER.set(0);
 
         final CountDownLatch latch = new CountDownLatch(1);
 
@@ -535,17 +535,17 @@
         }, new SynchronizationAdapter() {
             @Override
             public void onDone(Exchange exchange) {
-                order.addAndGet(2);
+                ORDER.addAndGet(2);
                 assertEquals("HelloHello", exchange.getOut().getBody());
                 latch.countDown();
             }
         });
 
-        order.addAndGet(1);
+        ORDER.addAndGet(1);
         latch.await(10, TimeUnit.SECONDS);
-        order.addAndGet(4);
+        ORDER.addAndGet(4);
 
-        assertEquals(7, order.get());
+        assertEquals(7, ORDER.get());
     }
 
     @Override