camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hadr...@apache.org
Subject svn commit: r687515 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/impl/converter/ came...
Date Thu, 21 Aug 2008 00:13:02 GMT
Author: hadrian
Date: Wed Aug 20 17:13:01 2008
New Revision: 687515

URL: http://svn.apache.org/viewvc?rev=687515&view=rev
Log:
CAMEL-115.

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceMethodWithExchangeTestConverter.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/MyBean.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticMethodWithExchangeTestConverter.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.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/ArrayTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.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/EnumTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionHelper.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
    activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
    activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
    activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
    activemq/camel/trunk/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
    activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java Wed
Aug 20 17:13:01 2008
@@ -33,4 +33,17 @@
      * @return the converted value or null if it can not be converted
      */
     <T> T convertTo(Class<T> type, Object value);
+
+    /**
+     * Converts the value to the specified type in the context of an exchange
+     * 
+     * @param type the requested type
+     * @param current exchange
+     * @param value the value to be converted
+     * @return the converted value or null if it can not be converted
+     * 
+     * Used when conversion requires extra information from the current 
+     * exchange (such as encoding).
+     */
+    <T> T convertTo(Class<T> type, Exchange exchange, Object value);
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
Wed Aug 20 17:13:01 2008
@@ -384,7 +384,7 @@
         return new Expression<E>() {
             public Object evaluate(E exchange) {
                 Object value = expression.evaluate(exchange);
-                return exchange.getContext().getTypeConverter().convertTo(type, value);
+                return exchange.getContext().getTypeConverter().convertTo(type, exchange,
value);
             }
 
             @Override
@@ -520,7 +520,7 @@
      */
     public static <E extends Exchange> String evaluateStringExpression(Expression<E>
expression, E exchange) {
         Object value = expression.evaluate(exchange);
-        return exchange.getContext().getTypeConverter().convertTo(String.class, value);
+        return exchange.getContext().getTypeConverter().convertTo(String.class, exchange,
value);
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
Wed Aug 20 17:13:01 2008
@@ -144,7 +144,7 @@
             validateExchangePropertyIsExpectedType(property, type, value);
         }
 
-        return getContext().getTypeConverter().convertTo(type, value);
+        return getContext().getTypeConverter().convertTo(type, this, value);
     }
 
     public void setProperty(String name, Object value) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
Wed Aug 20 17:13:01 2008
@@ -22,6 +22,7 @@
 
 import javax.activation.DataHandler;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 
 /**
@@ -44,7 +45,8 @@
 
     public <T> T getHeader(String name, Class<T> type) {
         Object value = getHeader(name);
-        return getExchange().getContext().getTypeConverter().convertTo(type, value);
+        Exchange e = getExchange();
+        return e.getContext().getTypeConverter().convertTo(type, e, value);
     }
 
     public void setHeader(String name, Object value) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
Wed Aug 20 17:13:01 2008
@@ -56,7 +56,7 @@
             CamelContext camelContext = e.getContext();
             if (camelContext != null) {
                 TypeConverter converter = camelContext.getTypeConverter();
-                T answer = converter.convertTo(type, body);
+                T answer = converter.convertTo(type, e, body);
                 if (answer == null) {
                     // lets first try converting the message itself first
                     // as for some types like InputStream v Reader its more efficient to
do the transformation
@@ -77,7 +77,7 @@
     public <T> void setBody(Object value, Class<T> type) {
         Exchange e = getExchange();
         if (e != null) {
-            T v = e.getContext().getTypeConverter().convertTo(type, value);
+            T v = e.getContext().getTypeConverter().convertTo(type, e, value);
             if (v != null) {
                 value = v;
             }

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?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- 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
Wed Aug 20 17:13:01 2008
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.impl.converter;
 
-
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -33,6 +32,7 @@
 import static java.lang.reflect.Modifier.isStatic;
 
 import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ResolverUtil;
@@ -40,7 +40,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-
 /**
  * A class which will auto-discover converter objects and methods to pre-load
  * the registry of converters on startup
@@ -141,25 +140,21 @@
             for (Method method : methods) {
                 Converter annotation = method.getAnnotation(Converter.class);
                 if (annotation != null) {
-                    Class<?>[] parameterTypes = method.getParameterTypes();
-                    if (parameterTypes == null || parameterTypes.length != 1) {
-                        LOG.warn("Ignoring bad converter on type: " + type.getName() + "
method: " + method
-                                + " as a converter method should have one parameter");
-                    } else {
+                    if (isValidConverterMethod(method)) {
                         int modifiers = method.getModifiers();
                         if (isAbstract(modifiers) || !isPublic(modifiers)) {
                             LOG.warn("Ignoring bad converter on type: " + type.getName()
+ " method: " + method
                                     + " as a converter method is not a public and concrete
method");
                         } else {
-                            Class toType = method.getReturnType();
+                            Class<?> toType = method.getReturnType();
                             if (toType.equals(Void.class)) {
                                 LOG.warn("Ignoring bad converter on type: " + type.getName()
+ " method: "
                                         + method + " as a converter method returns a void
method");
                             } else {
-                                Class fromType = parameterTypes[0];
+                                Class<?> fromType = method.getParameterTypes()[0];
                                 if (isStatic(modifiers)) {
                                     registerTypeConverter(registry, method, toType, fromType,
-                                                          new StaticMethodTypeConverter(method));
+                                            new StaticMethodTypeConverter(method));
                                 } else {
                                     if (injector == null) {
                                         injector = new CachingInjector(registry, type);
@@ -169,9 +164,13 @@
                                 }
                             }
                         }
+                    } else {
+                        LOG.warn("Ignoring bad converter on type: " + type.getName() + "
method: " + method
+                                + " as a converter method should have one parameter");
                     }
                 }
             }
+            
             Class superclass = type.getSuperclass();
             if (superclass != null && !superclass.equals(Object.class)) {
                 loadConverterMethods(registry, superclass);
@@ -181,9 +180,15 @@
         }
     }
 
-    protected void registerTypeConverter(TypeConverterRegistry registry, Method method,
-                                         Class toType, Class fromType, TypeConverter typeConverter)
{
+    protected void registerTypeConverter(TypeConverterRegistry registry, 
+    		Method method, Class toType, Class fromType, TypeConverter typeConverter) {
 
         registry.addTypeConverter(toType, fromType, typeConverter);
     }
+    
+    protected boolean isValidConverterMethod(Method method) {
+    	Class<?>[] parameterTypes = method.getParameterTypes();
+        return (parameterTypes != null) &&
+    	    (parameterTypes.length == 1 || (parameterTypes.length == 2 && parameterTypes[1]
== Exchange.class));
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -22,6 +22,7 @@
 import java.util.Collection;
 import java.util.List;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
 
 /**
@@ -71,4 +72,8 @@
         }
         return null;
     }
+    
+    public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+    	return convertTo(type, value);
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -65,6 +65,10 @@
         return null;
     }
 
+    public <T> T convertTo(Class<T> toType, Exchange exchange, Object value)
{
+    	return convertTo(toType, value);
+    }
+    
     public static AsyncProcessor convert(Processor value) {
         if (value instanceof AsyncProcessor) {
             return (AsyncProcessor)value;

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?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- 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
Wed Aug 20 17:13:01 2008
@@ -18,11 +18,12 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.Injector;
@@ -41,7 +42,7 @@
  */
 public class DefaultTypeConverter implements TypeConverter, TypeConverterRegistry {
     private static final transient Log LOG = LogFactory.getLog(DefaultTypeConverter.class);
-    private final Map<TypeMapping, TypeConverter> typeMappings = new HashMap<TypeMapping,
TypeConverter>();
+    private final Map<TypeMapping, TypeConverter> typeMappings = new ConcurrentHashMap<TypeMapping,
TypeConverter>();
     private Injector injector;
     private List<TypeConverterLoader> typeConverterLoaders = new ArrayList<TypeConverterLoader>();
     private List<TypeConverter> fallbackConverters = new ArrayList<TypeConverter>();
@@ -57,31 +58,36 @@
         addFallbackConverter(new EnumTypeConverter());
     }
 
-    public <T> T convertTo(Class<T> toType, Object value) {
-        if (toType.isInstance(value)) {
-            return toType.cast(value);
+    public <T> T convertTo(Class<T> type, Object value) {
+        return convertTo(type, null, value);
+    }
+    
+	@SuppressWarnings("unchecked")
+	public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+        if (type.isInstance(value)) {
+            return type.cast(value);
         }
         checkLoaded();
-        TypeConverter converter = getOrFindTypeConverter(toType, value);
+        TypeConverter converter = getOrFindTypeConverter(type, value);
         if (converter != null) {
-            return converter.convertTo(toType, value);
+            return converter.convertTo(type, exchange, value);
         }
 
         for (TypeConverter fallback : fallbackConverters) {
-            T rc = fallback.convertTo(toType, value);
+            T rc = fallback.convertTo(type, exchange, value);
             if (rc != null) {
                 return rc;
             }
         }
 
         // lets avoid NullPointerException when converting to boolean for null values
-        if (boolean.class.isAssignableFrom(toType)) {
+        if (boolean.class.isAssignableFrom(type)) {
             return (T) Boolean.FALSE;
         }
-        if (toType.isPrimitive()) {
-            Class primitiveType = ObjectHelper.convertPrimitiveTypeToWrapperType(toType);
-            if (primitiveType != toType) {
-                return (T) convertTo(primitiveType, value);
+        if (type.isPrimitive()) {
+            Class primitiveType = ObjectHelper.convertPrimitiveTypeToWrapperType(type);
+            if (primitiveType != type) {
+                return (T) convertTo(primitiveType, exchange, value);
             }
         }
         return null;
@@ -108,9 +114,7 @@
 
     public TypeConverter getTypeConverter(Class toType, Class fromType) {
         TypeMapping key = new TypeMapping(toType, fromType);
-        synchronized (typeMappings) {
-            return typeMappings.get(key);
-        }
+        return typeMappings.get(key);
     }
 
     public Injector getInjector() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -18,6 +18,7 @@
 
 import java.lang.reflect.Method;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.util.ObjectHelper;
@@ -44,4 +45,9 @@
         }
         return null;
     }
+
+	public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+		// ignore exchange
+		return convertTo(type, value);
+	}
 }
\ No newline at end of file

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -18,6 +18,7 @@
 
 import java.lang.reflect.Method;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.util.ObjectHelper;
@@ -31,10 +32,12 @@
 public class InstanceMethodTypeConverter implements TypeConverter {
     private final CachingInjector injector;
     private final Method method;
+    private final boolean useExchange;
 
     public InstanceMethodTypeConverter(CachingInjector injector, Method method) {
         this.injector = injector;
         this.method = method;
+        this.useExchange = method.getParameterTypes().length == 2;
     }
 
     @Override
@@ -43,10 +46,16 @@
     }
 
     public <T> T convertTo(Class<T> type, Object value) {
+        return convertTo(type, null, value);
+    }
+
+	public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
         Object instance = injector.newInstance();
         if (instance == null) {
             throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getName());
         }
-        return (T) ObjectHelper.invokeMethod(method, instance, value);
-    }
+        return useExchange ? 
+            (T) ObjectHelper.invokeMethod(method, instance, value, exchange) :
+            (T) ObjectHelper.invokeMethod(method, instance, value);
+	}
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -19,6 +19,7 @@
 import java.beans.PropertyEditor;
 import java.beans.PropertyEditorManager;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.util.ObjectHelper;
 
@@ -59,4 +60,7 @@
         return null;
     }
 
+    public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+    	return convertTo(type, value);
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -18,6 +18,7 @@
 
 import java.lang.reflect.Method;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.util.ObjectHelper;
 
@@ -28,9 +29,11 @@
  */
 public class StaticMethodTypeConverter implements TypeConverter {
     private final Method method;
+    private final boolean useExchange;
 
     public StaticMethodTypeConverter(Method method) {
         this.method = method;
+        this.useExchange = method.getParameterTypes().length == 2;
     }
 
     @Override
@@ -39,6 +42,12 @@
     }
 
     public <T> T convertTo(Class<T> type, Object value) {
-        return (T) ObjectHelper.invokeMethod(method, null, value);
+        return convertTo(type, null, value);
     }
+
+	public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+        return useExchange ? 
+            (T) ObjectHelper.invokeMethod(method, null, value, exchange) :
+            (T) ObjectHelper.invokeMethod(method, null, value);
+	}
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.impl.converter;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
 
 /**
@@ -35,4 +36,7 @@
         return null;
     }
 
+    public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+    	return convertTo(type, value);
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
Wed Aug 20 17:13:01 2008
@@ -172,7 +172,7 @@
      * not be converted
      */
     public static <T> T convertToType(Exchange exchange, Class<T> type, Object
value) {
-        return exchange.getContext().getTypeConverter().convertTo(type, value);
+        return exchange.getContext().getTypeConverter().convertTo(type, exchange, value);
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionHelper.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionHelper.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionHelper.java
Wed Aug 20 17:13:01 2008
@@ -55,6 +55,6 @@
     public static <T, E extends Exchange> T evaluateAsType(Expression<E> expression,
E exchange,
                                                            Class<T> resultType) {
         Object value = expression.evaluate(exchange);
-        return exchange.getContext().getTypeConverter().convertTo(resultType, value);
+        return exchange.getContext().getTypeConverter().convertTo(resultType, exchange, value);
     }
 }

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?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- 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
Wed Aug 20 17:13:01 2008
@@ -27,7 +27,12 @@
 import java.util.Set;
 
 import junit.framework.TestCase;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.impl.converter.DefaultTypeConverter;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ReflectionInjector;
@@ -53,6 +58,11 @@
         }
     }
 
+    @Override
+    protected void setUp() throws Exception {
+        PropertyEditorManager.registerEditor(Integer.class, IntegerPropertyEditor.class);
+    }
+
     public void testIntegerPropertyEditorConversion() throws Exception {
         Integer value = converter.convertTo(Integer.class, "1000");
         assertNotNull(value);
@@ -181,22 +191,24 @@
         value = converter.convertTo(Boolean.class, null);
         assertEquals("converted boolean value", null, value);
     }
-
-    public static class MyBean {
-        private int foo;
-
-        public int getFoo() {
-            return foo;
-        }
-
-        public void setFoo(int foo) {
-            this.foo = foo;
-        }
-    }
-
-    @Override
-    protected void setUp() throws Exception {
-        PropertyEditorManager.registerEditor(Integer.class, IntegerPropertyEditor.class);
+    
+    public void testStaticMethodConversionWithExchange() throws Exception {
+    	CamelContext camel = new DefaultCamelContext();
+    	Exchange e = new DefaultExchange(camel);
+    	e.setProperty("prefix", "foo-");
+    	MyBean bean = converter.convertTo(MyBean.class, e, "5:bar");
+        assertEquals("converted using exchange", 5, bean.getFoo(), 5);
+        assertEquals("converted using exchange", "foo-bar", bean.getBar());
+    }
+
+    public void testInstanceMethodConversionWithExchange() throws Exception {
+        String[] values = new String[]{"5", "bar"};
+
+    	CamelContext camel = new DefaultCamelContext();
+    	Exchange e = new DefaultExchange(camel);
+    	e.setProperty("prefix", "foo-");
+    	MyBean bean = converter.convertTo(MyBean.class, e, values);
+        assertEquals("converted using exchange", 5, bean.getFoo(), 5);
+        assertEquals("converted using exchange", "foo-bar", bean.getBar());
     }
-
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceMethodWithExchangeTestConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceMethodWithExchangeTestConverter.java?rev=687515&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceMethodWithExchangeTestConverter.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceMethodWithExchangeTestConverter.java
Wed Aug 20 17:13:01 2008
@@ -0,0 +1,30 @@
+/**
+ * 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.Converter;
+import org.apache.camel.Exchange;
+
+@Converter
+public class InstanceMethodWithExchangeTestConverter {
+
+    @Converter
+    public MyBean fromArray(String[] values, Exchange exchange) {
+        return new MyBean(Integer.parseInt(values[0]), 
+            exchange.getProperty("prefix", String.class) + values[1]);
+    }
+}

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
Wed Aug 20 17:13:01 2008
@@ -57,8 +57,7 @@
     }
 
     public void testConvertToSource() throws Exception {
-        Source source = converter
-                .convertTo(Source.class, "<hello>world!</hello>");
+        Source source = converter.convertTo(Source.class, "<hello>world!</hello>");
         assertNotNull(source);
 
         LOG.debug("Found document: " + source);

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/MyBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/MyBean.java?rev=687515&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/MyBean.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/MyBean.java Wed
Aug 20 17:13:01 2008
@@ -0,0 +1,47 @@
+/**
+ * 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;
+
+public class MyBean {
+    private int foo;
+    private String bar;
+
+    public MyBean() {
+    	this(0, "");
+    }
+    
+    public MyBean(int foo, String bar) {
+    	this.foo = foo;
+    	this.bar = bar;
+    }
+    
+    public int getFoo() {
+        return foo;
+    }
+
+    public void setFoo(int foo) {
+        this.foo = foo;
+    }
+    
+    public String getBar() {
+    	return bar;
+    }
+    
+    public void setBar(String bar) {
+    	this.bar = bar;
+    }
+}

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticMethodWithExchangeTestConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticMethodWithExchangeTestConverter.java?rev=687515&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticMethodWithExchangeTestConverter.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticMethodWithExchangeTestConverter.java
Wed Aug 20 17:13:01 2008
@@ -0,0 +1,31 @@
+/**
+ * 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.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.util.ObjectHelper;
+
+@Converter
+public final class StaticMethodWithExchangeTestConverter {
+
+    @Converter
+    public static MyBean fromString(String text, Exchange exchange) {
+        String[] values = ObjectHelper.splitOnCharacter(text, ":", 2);
+        return new MyBean(Integer.parseInt(values[0]), exchange.getProperty("prefix", String.class)
+ values[1]);
+    }
+}

Modified: activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
(original)
+++ activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
Wed Aug 20 17:13:01 2008
@@ -42,7 +42,7 @@
 
     @Converter
     public static Message toMessage(String body) throws HL7Exception {
-        // replace \n with \r as HL7 uses 0x0d = \r as segment termninators
+        // replace \n with \r as HL7 uses 0x0d = \r as segment terminators
         body = body.replace('\n', '\r');
 
         Parser parser = new PipeParser();

Modified: activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
(original)
+++ activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
Wed Aug 20 17:13:01 2008
@@ -29,6 +29,7 @@
 import javax.xml.bind.util.JAXBSource;
 import javax.xml.transform.Source;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.TypeConverterAware;
@@ -71,6 +72,10 @@
         }
     }
 
+    public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+    	return convertTo(type, value);
+    }
+
     protected <T> boolean isJaxbType(Class<T> type) {
         XmlRootElement element = type.getAnnotation(XmlRootElement.class);
         boolean jaxbType = element != null;

Modified: activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
(original)
+++ activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
Wed Aug 20 17:13:01 2008
@@ -30,7 +30,8 @@
     protected TypeConverter converter = context.getTypeConverter();
 
     public void testConverter() throws Exception {
-        PurchaseOrder purchaseOrder = converter.convertTo(PurchaseOrder.class, "<purchaseOrder
name='foo' amount='123.45' price='2.22'/>");
+        PurchaseOrder purchaseOrder = converter.convertTo(PurchaseOrder.class, 
+            "<purchaseOrder name='foo' amount='123.45' price='2.22'/>");
 
         assertNotNull("Purchase order should not be null!", purchaseOrder);
         assertEquals("name", "foo", purchaseOrder.getName());

Modified: activemq/camel/trunk/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
(original)
+++ activemq/camel/trunk/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
Wed Aug 20 17:13:01 2008
@@ -41,8 +41,8 @@
             Node node = base.addNode(getNodeName(exchange));
             TypeConverter converter = exchange.getContext().getTypeConverter();
             for (String key : exchange.getProperties().keySet()) {
-                Value value = converter.convertTo(Value.class, exchange
-                        .getProperty(key));
+                Value value = converter.convertTo(Value.class, 
+                    exchange, exchange.getProperty(key));
                 node.setProperty(key, value);
             }
             node.addMixin("mix:referenceable");

Modified: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=687515&r1=687514&r2=687515&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
(original)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Wed Aug 20 17:13:01 2008
@@ -314,7 +314,7 @@
     }
 
     private static String asString(Exchange exchange, Object value) {
-        return exchange.getContext().getTypeConverter().convertTo(String.class, value);
+        return exchange.getContext().getTypeConverter().convertTo(String.class, exchange,
value);
     }
 
 }



Mime
View raw message