activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r523881 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/converter/ main/java/org/apache/camel/impl/converter/ main/java/org/apache/camel/util/ test/java/org/apache/camel/converter/
Date Thu, 29 Mar 2007 23:47:55 GMT
Author: jstrachan
Date: Thu Mar 29 16:47:54 2007
New Revision: 523881

URL: http://svn.apache.org/viewvc?view=rev&rev=523881
Log:
added more test cases & fixes. also made the send() helpers on CamelClient return the
exchange

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelClient.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ProducerCache.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Thu Mar 29 16:47:54 2007
@@ -18,6 +18,8 @@
 package org.apache.camel.converter;
 
 import org.apache.camel.Converter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 import java.io.*;
 import java.nio.ByteBuffer;
@@ -29,6 +31,7 @@
  */
 @Converter
 public class IOConverter {
+    private static final transient Log log = LogFactory.getLog(IOConverter.class);
 
     @Converter
     public static InputStream toInputStream(File file) throws FileNotFoundException {
@@ -62,12 +65,17 @@
 
 
     @Converter
-    public static StringReader toInputStream(String text) {
+    public static StringReader toReader(String text) {
         // TODO could we automatically find this?
         return new StringReader(text);
     }
 
     @Converter
+    public static InputStream toInputStream(String text) {
+        return toInputStream(text.getBytes());
+    }
+
+    @Converter
     public static byte[] toByteArray(String text) {
         // TODO could we automatically find this?
         return text.getBytes();
@@ -76,6 +84,50 @@
     @Converter
     public static String toString(byte[] data) {
         return new String(data);
+    }
+
+    @Converter
+    public static String toString(Reader reader) throws IOException {
+        if (reader instanceof BufferedReader) {
+            return toString((BufferedReader) reader);
+        }
+        else {
+            return toString(new BufferedReader(reader));
+        }
+    }
+
+    @Converter
+    public static String toString(BufferedReader reader) throws IOException {
+        try {
+            StringBuilder builder = new StringBuilder();
+            boolean first = true;
+            while (true) {
+                String line = reader.readLine();
+                if (line == null) {
+                    return builder.toString();
+                }
+                if (first) {
+                    first = false;
+                }
+                else {
+                    builder.append("\n");
+                }
+                builder.append(line);
+            }
+        }
+        finally {
+            try {
+                reader.close();
+            }
+            catch (IOException e) {
+                log.warn("Failed to close stream: "+ e, e);
+            }
+        }
+    }
+    
+    @Converter
+    public static String toString(InputStream in) throws IOException {
+        return toString(toReader(in));
     }
 
     @Converter

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
Thu Mar 29 16:47:54 2007
@@ -147,13 +147,13 @@
                         else {
                             Class fromType = parameterTypes[0];
                             if (isStatic(modifiers)) {
-                                registry.addTypeConverter(fromType, toType, new StaticMethodTypeConverter(method));
+                                registry.addTypeConverter(toType, fromType, new StaticMethodTypeConverter(method));
                             }
                             else {
                                 if (injector == null) {
                                     injector = new CachingInjector(registry, type);
                                 }
-                                registry.addTypeConverter(fromType, toType, new InstanceMethodTypeConverter(injector,
method));
+                                registry.addTypeConverter(toType, fromType, new InstanceMethodTypeConverter(injector,
method));
                             }
                         }
                     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
Thu Mar 29 16:47:54 2007
@@ -47,7 +47,7 @@
             return toType.cast(value);
         }
         checkLoaded();
-        TypeConverter converter = getConverter(toType, value);
+        TypeConverter converter = getOrFindTypeConverter(toType, value);
         if (converter != null) {
             return converter.convertTo(toType, value);
         }
@@ -59,8 +59,8 @@
         return null;
     }
 
-    public void addTypeConverter(Class fromType, Class toType, TypeConverter typeConverter)
{
-        TypeMapping key = new TypeMapping(fromType, toType);
+    public void addTypeConverter(Class toType, Class fromType, TypeConverter typeConverter)
{
+        TypeMapping key = new TypeMapping(toType, fromType);
         synchronized (typeMappings) {
             TypeConverter converter = typeMappings.get(key);
             if (converter != null) {
@@ -70,8 +70,8 @@
         }
     }
 
-    public TypeConverter getTypeConverter(Class fromType, Class toType) {
-        TypeMapping key = new TypeMapping(fromType, toType);
+    public TypeConverter getTypeConverter(Class toType, Class fromType) {
+        TypeMapping key = new TypeMapping(toType, fromType);
         synchronized (typeMappings) {
             return typeMappings.get(key);
         }
@@ -88,12 +88,12 @@
         this.injector = injector;
     }
 
-    protected <T> TypeConverter getConverter(Class toType, Object value) {
+    protected <T> TypeConverter getOrFindTypeConverter(Class toType, Object value)
{
         Class fromType = null;
         if (value != null) {
             fromType = value.getClass();
         }
-        TypeMapping key = new TypeMapping(fromType, toType);
+        TypeMapping key = new TypeMapping(toType, fromType);
         TypeConverter converter;
         synchronized (typeMappings) {
             converter = typeMappings.get(key);
@@ -116,7 +116,7 @@
              toSuperClass != null && !toSuperClass.equals(Object.class);
              toSuperClass = toSuperClass.getSuperclass()) {
 
-            TypeConverter converter = getTypeConverter(fromType, toSuperClass);
+            TypeConverter converter = getTypeConverter(toSuperClass, fromType);
             if (converter != null) {
                 return converter;
             }
@@ -124,21 +124,30 @@
 
         // TODO should we filter out any interfaces which are super-interfaces?
         for (Class type : toType.getInterfaces()) {
-            TypeConverter converter = getTypeConverter(fromType, type);
+            TypeConverter converter = getTypeConverter(type, fromType);
             if (converter != null) {
                 return converter;
             }
         }
 
         // lets try the super classes of the from type
-        Class fromSuperClass = fromType.getSuperclass();
-        if (fromSuperClass != null && !fromSuperClass.equals(Object.class)) {
-            return findTypeConverter(toType, fromSuperClass, value);
-        }
-        for (Class type : fromType.getInterfaces()) {
-            TypeConverter converter = getTypeConverter(type, toType);
-            if (converter != null) {
-                return converter;
+        if (fromType != null) {
+            Class fromSuperClass = fromType.getSuperclass();
+            if (fromSuperClass != null && !fromSuperClass.equals(Object.class)) {
+
+                TypeConverter converter = getTypeConverter(toType, fromSuperClass);
+                if (converter == null) {
+                    converter = findTypeConverter(toType, fromSuperClass, value);
+                }
+                if (converter != null) {
+                    return converter;
+                }
+            }
+            for (Class type : fromType.getInterfaces()) {
+                TypeConverter converter = getTypeConverter(toType, type);
+                if (converter != null) {
+                    return converter;
+                }
             }
         }
         // TODO look at constructors of toType?
@@ -166,12 +175,12 @@
      * Represents a mapping from one type (which can be null) to another
      */
     protected static class TypeMapping {
-        Class fromType;
         Class toType;
+        Class fromType;
 
-        public TypeMapping(Class fromType, Class toType) {
-            this.fromType = fromType;
+        public TypeMapping(Class toType, Class fromType) {
             this.toType = toType;
+            this.fromType = fromType;
         }
 
         public Class getFromType() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
Thu Mar 29 16:47:54 2007
@@ -26,11 +26,11 @@
     /**
      * Allows a new type converter to be bregistered
      *
-     * @param fromType      the type to convert from
      * @param toType        the type to convert to
+     * @param fromType      the type to convert from
      * @param typeConverter the type converter to use
      */
-    void addTypeConverter(Class fromType, Class toType, TypeConverter typeConverter);
+    void addTypeConverter(Class toType, Class fromType, TypeConverter typeConverter);
 
     Injector getInjector();
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelClient.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelClient.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelClient.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelClient.java Thu
Mar 29 16:47:54 2007
@@ -45,9 +45,10 @@
      * @param endpointUri the endpoint URI to send the exchange to
      * @param exchange the exchange to send
      */
-    public void send(String endpointUri, E exchange) {
+    public E send(String endpointUri, E exchange) {
         Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
         send(endpoint, exchange);
+        return exchange;
     }
 
     /**
@@ -56,9 +57,9 @@
      * @param endpointUri the endpoint URI to send the exchange to
      * @param processor the transformer used to populate the new exchange
      */
-    public void send(String endpointUri, Processor<E> processor) {
+    public E send(String endpointUri, Processor<E> processor) {
         Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
-        send(endpoint,  processor);
+        return send(endpoint,  processor);
     }
 
     /**
@@ -67,8 +68,9 @@
      * @param endpoint the endpoint to send the exchange to
      * @param exchange the exchange to send
      */
-    public void send(Endpoint<E> endpoint, E exchange) {
+    public E send(Endpoint<E> endpoint, E exchange) {
         producerCache.send(endpoint, exchange);
+        return exchange;
     }
 
     /**
@@ -77,12 +79,16 @@
      * @param endpoint the endpoint to send the exchange to
      * @param processor the transformer used to populate the new exchange
      */
-    public void send(Endpoint<E> endpoint, Processor<E> processor) {
-        producerCache.send(endpoint, processor);
+    public E send(Endpoint<E> endpoint, Processor<E> processor) {
+        return producerCache.send(endpoint, processor);
     }
 
     public Producer<E> getProducer(Endpoint<E> endpoint) {
         return producerCache.getProducer(endpoint);
+    }
+
+    public CamelContext getContext() {
+        return context;
     }
 
     protected Endpoint resolveMandatoryEndpoint(String endpointUri) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ProducerCache.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ProducerCache.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ProducerCache.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ProducerCache.java
Thu Mar 29 16:47:54 2007
@@ -68,7 +68,7 @@
      * @param endpoint the endpoint to send the exchange to
      * @param processor the transformer used to populate the new exchange
      */
-    public void send(Endpoint<E> endpoint, Processor<E> processor) {
+    public E send(Endpoint<E> endpoint, Processor<E> processor) {
         Producer<E> producer = getProducer(endpoint);
         E exchange = producer.createExchange();
 
@@ -77,6 +77,7 @@
 
         // now lets dispatch
         producer.onExchange(exchange);
+        return exchange;
     }
 
     protected void doStop() throws Exception {

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java?view=diff&rev=523881&r1=523880&r2=523881
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
Thu Mar 29 16:47:54 2007
@@ -23,6 +23,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.InputStream;
+
 /**
  * @version $Revision$
  */
@@ -31,15 +33,21 @@
 
     protected TypeConverter converter = new DefaultTypeConverter();
     
-    public void testConvert() throws Exception {
+    public void testConvertStringAndBytes() throws Exception {
         byte[] array = converter.convertTo(byte[].class, "foo");
         assertNotNull(array);
 
         log.debug("Found array of size: " + array.length);
 
-        // lets now convert back again
-
         String text = converter.convertTo(String.class, array);
         assertEquals("Converted to String", "foo", text);
+    }
+
+    public void testConvertStringAndStreams() throws Exception {
+        InputStream inputStream = converter.convertTo(InputStream.class, "bar");
+        assertNotNull(inputStream);
+
+        String text = converter.convertTo(String.class, inputStream);
+        assertEquals("Converted to String", "bar", text);
     }
 }



Mime
View raw message