camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r738826 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/converter/ camel-core/src/main/java/org/apache/camel/converter/jaxp/ c...
Date Thu, 29 Jan 2009 11:42:43 GMT
Author: davsclaus
Date: Thu Jan 29 11:42:42 2009
New Revision: 738826

URL: http://svn.apache.org/viewvc?rev=738826&view=rev
Log:
CAMEL-1300: Add @FallbackConverter for registering a fallback converter. Used by GenericFileConverter
to allow us to delegte the from type to the wrapped body instead of the GenericFile that is
just the wrapper class.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java   (contents,
props changed)
      - copied, changed from r738760, camel/trunk/camel-core/src/main/java/org/apache/camel/Converter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java
  (contents, props changed)
      - copied, changed from r738760, camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
  (contents, props changed)
      - copied, changed from r738760, camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java
Removed:
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConverter.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ReportingTypeConverterRegistry.java
    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/DefaultTypeConverter.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/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java (from
r738760, camel/trunk/camel-core/src/main/java/org/apache/camel/Converter.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/Converter.java&r1=738760&r2=738826&rev=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Converter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java Thu Jan 29
11:42:42 2009
@@ -23,15 +23,23 @@
 import java.lang.annotation.Target;
 
 /**
- * An annotation used to mark classes and methods to indicate code capable of
- * converting from a type to another type which are then auto-discovered using
+ * An annotation used to mark methods to indicate code capable of being a
+ * fallback converter which are then auto-discovered using
  * the <a href="http://camel.apache.org/type-converter.html">Type
- * Conversion Support</a>
- * 
+ * Conversion Support</a>.
+ * <p/>
+ * The difference between a regular <tt>@Converter</tt> and a <tt>@FallbackConverter</tt>
+ * is that the fallback is resolved at last if no regular converter could be found.
+ * Also the method signautre is scoped to be generic to allow handling a broader range
+ * of types trying to be converted. The fallback converter can just return <tt>null</tt>
+ * if it can not handle the types to convert from/to.
+ *
+ * @see org.apache.camel.component.file.GenericFileConverter GenericFileConverter for an
example.
+ *
  * @version $Revision$
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Target({ElementType.TYPE, ElementType.METHOD })
-public @interface Converter {
-}
+public @interface FallbackConverter {
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/FallbackConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
Thu Jan 29 11:42:42 2009
@@ -16,19 +16,14 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
 import org.apache.camel.Converter;
-import org.apache.camel.converter.IOConverter;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.spi.TypeConverterRegistry;
 
 /**
- * A set of converter methods for working with remote file types
+ * A set of converter methods for working with generic file types
  */
 @Converter
 public final class GenericFileConverter {
@@ -37,34 +32,20 @@
         // Helper Class
     }
 
-    @Converter
-    public static InputStream toInputStream(GenericFile<File> file) throws FileNotFoundException
{
-        return IOConverter.toInputStream(file.getFile());
-    }
-
-    @Converter
-    public static BufferedReader toReader(GenericFile<File> file) throws FileNotFoundException
{
-        return IOConverter.toReader(file.getFile());
-    }
-
-    @Converter
-    public static OutputStream toOutputStream(GenericFile<File> file) throws FileNotFoundException
{
-        return IOConverter.toOutputStream(file.getFile());
-    }
-
-    @Converter
-    public static BufferedWriter toWriter(GenericFile<File> file) throws IOException
{
-        return IOConverter.toWriter(file.getFile());
-    }
-
-    @Converter
-    public static byte[] toByteArray(GenericFile<File> file) throws IOException {
-        return IOConverter.toByteArray(file.getFile());
-    }
-
-    @Converter
-    public static String toString(GenericFile<File> file) throws IOException {
-        return IOConverter.toString(file.getFile());
+    @FallbackConverter
+    public static <T> T convertTo(Class<T> type, Exchange exchange, Object value,
TypeConverterRegistry registry) {
+        // use a fallback type converter so we can convert the embedded body if the value
is GenericFile
+        if (GenericFile.class.isAssignableFrom(value.getClass())) {
+            GenericFile file = (GenericFile) value;
+            Class from = file.getBody().getClass();
+            TypeConverter tc = registry.lookup(type, from);
+            if (tc != null) {
+                Object body = file.getBody();
+                return tc.convertTo(type, exchange, body);
+            }
+        }
+        
+        return null;
     }
 
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java Thu Jan
29 11:42:42 2009
@@ -279,13 +279,29 @@
         return bos.toByteArray();
     }
 
+    @Converter
+    public static byte[] toByteArray(ByteArrayOutputStream os) {
+        return os.toByteArray();
+    }
+
+    @Converter
+    public static String toString(ByteArrayOutputStream os) {
+        return os.toString();
+    }
+
+    @Converter
+    public static InputStream toInputStream(ByteArrayOutputStream os) {
+        return new ByteArrayInputStream(os.toByteArray());
+    }
+
     public static void copy(InputStream stream, OutputStream os) throws IOException {
         byte[] data = new byte[4096];
-        int read = stream.read(data);        
-        while (read != -1) {            
+        int read = stream.read(data);
+        while (read != -1) {
             os.write(data, 0, read);
             read = stream.read(data);
         }
         os.flush();
     }
+
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java Thu
Jan 29 11:42:42 2009
@@ -26,7 +26,6 @@
 
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
-import org.apache.camel.component.file.GenericFile;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -62,11 +61,6 @@
     }
 
     @Converter
-    public static ByteBuffer toByteBuffer(GenericFile<File> file) throws IOException
{
-        return toByteBuffer(file.getFile());
-    }
-
-    @Converter
     public static ByteBuffer toByteBuffer(File file) throws IOException {
         InputStream in = null;
         try {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
Thu Jan 29 11:42:42 2009
@@ -53,7 +53,6 @@
 import org.xml.sax.XMLReader;
 
 import org.apache.camel.Converter;
-import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.converter.IOConverter;
 import org.apache.camel.converter.NIOConverter;
 import org.apache.camel.util.ObjectHelper;
@@ -310,11 +309,6 @@
     }
 
     @Converter
-    public StreamSource toStreamSource(GenericFile<File> in) throws TransformerException
{
-        return toStreamSource(in.getFile());
-    }
-
-    @Converter
     public StreamSource toStreamSource(byte[] in) throws TransformerException {
         if (in != null) {
             return new StreamSource(IOConverter.toInputStream(in));
@@ -535,18 +529,6 @@
     }
 
     /**
-     * Converts the given {@link File} to a DOM document
-     *
-     * @param file is the data to be parsed
-     * @return the parsed document
-     */
-    @Converter
-    public Document toDOMDocument(GenericFile<File> file) throws IOException, SAXException,
ParserConfigurationException {
-        return toDOMDocument(file.getFile());
-    }
-
-
-    /**
      * Create a DOM document from the given source.
      */
     @Converter

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ReportingTypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ReportingTypeConverterRegistry.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ReportingTypeConverterRegistry.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ReportingTypeConverterRegistry.java
Thu Jan 29 11:42:42 2009
@@ -41,6 +41,12 @@
         }
     }
 
+    public void addFallbackTypeConverter(TypeConverter typeConverter) {
+        if (errors.size() == 0) {
+            errors.add("Method should not be invoked.");
+        }
+    }
+
     public TypeConverter lookup(Class toType, Class fromType) {
         if (errors.size() == 0) {
             errors.add("Method should not be invoked.");

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=738826&r1=738825&r2=738826&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
Thu Jan 29 11:42:42 2009
@@ -19,7 +19,6 @@
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
-
 import java.lang.reflect.Method;
 
 import java.net.URL;
@@ -33,6 +32,7 @@
 
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
@@ -50,7 +50,7 @@
 public class AnnotationTypeConverterLoader implements TypeConverterLoader {
     public static final String META_INF_SERVICES = "META-INF/services/org/apache/camel/TypeConverter";
     private static final transient Log LOG = LogFactory.getLog(AnnotationTypeConverterLoader.class);
-    private ResolverUtil resolver;
+    protected ResolverUtil resolver;
     private Set<Class> visitedClasses = new HashSet<Class>();
 
     public AnnotationTypeConverterLoader() {
@@ -62,7 +62,7 @@
             resolver = new ResolverUtil();
         }
     }
-    
+
     public AnnotationTypeConverterLoader(ResolverUtil resolverUtil) {
         this.resolver = resolverUtil;
     }
@@ -122,7 +122,7 @@
      * Tokenizes the line from the META-IN/services file using commas and
      * ignoring whitespace between packages
      */
-    protected void tokenize(Set<String> packages, String line) {
+    private void tokenize(Set<String> packages, String line) {
         StringTokenizer iter = new StringTokenizer(line, ",");
         while (iter.hasMoreTokens()) {
             String name = iter.nextToken().trim();
@@ -148,34 +148,9 @@
                 // this may be prone to ClassLoader or packaging problems when the same class
is defined
                 // in two different jars (as is the case sometimes with specs).
                 if (ObjectHelper.hasAnnotation(method, Converter.class, true)) {
-                    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();
-                            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 = method.getParameterTypes()[0];
-                                if (isStatic(modifiers)) {
-                                    registerTypeConverter(registry, method, toType, fromType,
-                                            new StaticMethodTypeConverter(method));
-                                } else {
-                                    if (injector == null) {
-                                        injector = new CachingInjector(registry, type);
-                                    }
-                                    registerTypeConverter(registry, method, toType, fromType,
-                                            new InstanceMethodTypeConverter(injector, method));
-                                }
-                            }
-                        }
-                    } else {
-                        LOG.warn("Ignoring bad converter on type: " + type.getName() + "
method: " + method
-                                + " as a converter method should have one parameter");
-                    }
+                    injector = handleHasConverterAnnotation(registry, type, injector, method);
+                } else if (ObjectHelper.hasAnnotation(method, FallbackConverter.class, true))
{
+                    injector = handleHasFallbackConverterAnnotation(registry, type, injector,
method);
                 }
             }
 
@@ -188,15 +163,86 @@
         }
     }
 
+    private CachingInjector handleHasConverterAnnotation(TypeConverterRegistry registry,
Class type, CachingInjector injector, Method method) {
+        if (isValidConverterMethod(method)) {
+            int modifiers = method.getModifiers();
+            if (isAbstract(modifiers) || !isPublic(modifiers)) {
+                LOG.warn("Ignoring bad converter on type: " + type.getCanonicalName() + "
method: " + method
+                        + " as a converter method is not a public and concrete method");
+            } else {
+                Class<?> toType = method.getReturnType();
+                if (toType.equals(Void.class)) {
+                    LOG.warn("Ignoring bad converter on type: " + type.getCanonicalName()
+ " method: "
+                            + method + " as a converter method returns a void method");
+                } else {
+                    Class<?> fromType = method.getParameterTypes()[0];
+                    if (isStatic(modifiers)) {
+                        registerTypeConverter(registry, method, toType, fromType,
+                                new StaticMethodTypeConverter(method));
+                    } else {
+                        if (injector == null) {
+                            injector = new CachingInjector(registry, type);
+                        }
+                        registerTypeConverter(registry, method, toType, fromType,
+                                new InstanceMethodTypeConverter(injector, method));
+                    }
+                }
+            }
+        } else {
+            LOG.warn("Ignoring bad converter on type: " + type.getCanonicalName() + " method:
" + method
+                    + " as a converter method should have one parameter");
+        }
+        return injector;
+    }
+
+    private CachingInjector handleHasFallbackConverterAnnotation(TypeConverterRegistry registry,
Class type, CachingInjector injector, Method method) {
+        if (isValidFallbackConverterMethod(method)) {
+            int modifiers = method.getModifiers();
+            if (isAbstract(modifiers) || !isPublic(modifiers)) {
+                LOG.warn("Ignoring bad fallback converter on type: " + type.getCanonicalName()
+ " method: " + method
+                        + " as a fallback converter method is not a public and concrete method");
+            } else {
+                Class<?> toType = method.getReturnType();
+                if (toType.equals(Void.class)) {
+                    LOG.warn("Ignoring bad fallback converter on type: " + type.getCanonicalName()
+ " method: "
+                            + method + " as a fallback converter method returns a void method");
+                } else {
+                    if (isStatic(modifiers)) {
+                        registerFallbackTypeConverter(registry, new StaticMethodFallbackTypeConverter(method,
registry));
+                    } else {
+                        if (injector == null) {
+                            injector = new CachingInjector(registry, type);
+                        }
+                        registerFallbackTypeConverter(registry, new InstanceMethodFallbackTypeConverter(injector,
method, registry));
+                    }
+                }
+            }
+        } else {
+            LOG.warn("Ignoring bad fallback converter on type: " + type.getCanonicalName()
+ " method: " + method
+                    + " as a fallback converter method should have one parameter");
+        }
+        return injector;
+    }
+
     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 && Exchange.class.isAssignableFrom(parameterTypes[1])));
+                || (parameterTypes.length == 2 && Exchange.class.isAssignableFrom(parameterTypes[1])));
+    }
+
+    protected void registerFallbackTypeConverter(TypeConverterRegistry registry, TypeConverter
typeConverter) {
+        registry.addFallbackTypeConverter(typeConverter);
+    }
+
+    protected boolean isValidFallbackConverterMethod(Method method) {
+        Class<?>[] parameterTypes = method.getParameterTypes();
+        return (parameterTypes != null) && (parameterTypes.length == 3
+                || (parameterTypes.length == 4 && Exchange.class.isAssignableFrom(parameterTypes[1]))
+                && (TypeConverterRegistry.class.isAssignableFrom(parameterTypes[parameterTypes.length
- 1])));
     }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
Thu Jan 29 11:42:42 2009
@@ -34,7 +34,6 @@
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
 
 
@@ -55,11 +54,13 @@
     public DefaultTypeConverter(Injector injector) {
         typeConverterLoaders.add(new AnnotationTypeConverterLoader());
         this.injector = injector;
-        addFallbackConverter(new AsyncProcessorTypeConverter());
-        addFallbackConverter(new PropertyEditorTypeConverter());
-        addFallbackConverter(new ToStringTypeConverter());
-        addFallbackConverter(new ArrayTypeConverter());
-        addFallbackConverter(new EnumTypeConverter());
+        // add to string first as it will then be last in the last as to string can nearly
+        // always convert something to a string so we want it only as the last resort
+        addFallbackTypeConverter(new ToStringTypeConverter());
+        addFallbackTypeConverter(new EnumTypeConverter());
+        addFallbackTypeConverter(new ArrayTypeConverter());
+        addFallbackTypeConverter(new PropertyEditorTypeConverter());
+        addFallbackTypeConverter(new AsyncProcessorTypeConverter());
     }
 
     public List<TypeConverterLoader> getTypeConverterLoaders() {
@@ -123,6 +124,9 @@
     }
 
     public void addTypeConverter(Class toType, Class fromType, TypeConverter typeConverter)
{
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Adding type converter: " + typeConverter);
+        }
         TypeMapping key = new TypeMapping(toType, fromType);
         synchronized (typeMappings) {
             TypeConverter converter = typeMappings.get(key);
@@ -133,10 +137,15 @@
         }
     }
 
-    public void addFallbackConverter(TypeConverter converter) {
-        fallbackConverters.add(converter);
-        if (converter instanceof TypeConverterAware) {
-            TypeConverterAware typeConverterAware = (TypeConverterAware)converter;
+    public void addFallbackTypeConverter(TypeConverter typeConverter) {
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Adding fallback type converter: " + typeConverter);
+        }
+
+        // add in top of fallback as the toString() fallback will nearly always be able to
convert
+        fallbackConverters.add(0, typeConverter);
+        if (typeConverter instanceof TypeConverterAware) {
+            TypeConverterAware typeConverterAware = (TypeConverterAware)typeConverter;
             typeConverterAware.setTypeConverter(this);
         }
     }
@@ -268,7 +277,7 @@
         List<TypeConverter> converters = finder.newInstances("FallbackTypeConverter",
getInjector(),
                                                              TypeConverter.class);
         for (TypeConverter converter : converters) {
-            addFallbackConverter(converter);
+            addFallbackTypeConverter(converter);
         }
     }
 

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java
(from r738760, 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/InstanceMethodFallbackTypeConverter.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodTypeConverter.java&r1=738760&r2=738826&rev=738826&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/InstanceMethodFallbackTypeConverter.java
Thu Jan 29 11:42:42 2009
@@ -21,28 +21,31 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
+import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
 
 /**
- * A {@link TypeConverter} implementation which instantiates an object
- * so that an instance method can be used as a type converter
+ * A {@link org.apache.camel.TypeConverter} implementation which instantiates an object
+ * so that an instance method can be used as a fallback type converter
  *
  * @version $Revision$
  */
-public class InstanceMethodTypeConverter implements TypeConverter {
+public class InstanceMethodFallbackTypeConverter implements TypeConverter {
     private final CachingInjector injector;
     private final Method method;
     private final boolean useExchange;
+    private final TypeConverterRegistry registry;
 
-    public InstanceMethodTypeConverter(CachingInjector injector, Method method) {
+    public InstanceMethodFallbackTypeConverter(CachingInjector injector, Method method, TypeConverterRegistry
registry) {
         this.injector = injector;
         this.method = method;
-        this.useExchange = method.getParameterTypes().length == 2;
+        this.useExchange = method.getParameterTypes().length == 4;
+        this.registry = registry;
     }
 
     @Override
     public String toString() {
-        return "InstanceMethodTypeConverter: " + method;
+        return "InstanceMethodFallbackTypeConverter: " + method;
     }
 
     public <T> T convertTo(Class<T> type, Object value) {
@@ -52,10 +55,10 @@
     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());
+            throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName());
         }
         return useExchange
-            ? (T)ObjectHelper.invokeMethod(method, instance, value, exchange) : (T)ObjectHelper
-                .invokeMethod(method, instance, value);
+            ? (T)ObjectHelper.invokeMethod(method, instance, type, value, exchange, registry)
: (T)ObjectHelper
+                .invokeMethod(method, instance, type, value, registry);
     }
-}
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=738826&r1=738825&r2=738826&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
Thu Jan 29 11:42:42 2009
@@ -52,7 +52,7 @@
     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());
+            throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName());
         }
         return useExchange
             ? (T)ObjectHelper.invokeMethod(method, instance, value, exchange) : (T)ObjectHelper

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
(from r738760, camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java&r1=738760&r2=738826&rev=738826&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodTypeConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
Thu Jan 29 11:42:42 2009
@@ -20,25 +20,29 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.TypeConverter;
+import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
 
 /**
- * A {@link TypeConverter} implementation which invokes a static method to convert from a
type to another type
+ * A {@link org.apache.camel.TypeConverter} implementation which invokes a static method
+ * as a fallback type converter from a type to another type
  *
  * @version $Revision$
  */
-public class StaticMethodTypeConverter implements TypeConverter {
+public class StaticMethodFallbackTypeConverter implements TypeConverter {
     private final Method method;
     private final boolean useExchange;
+    private final TypeConverterRegistry registry;
 
-    public StaticMethodTypeConverter(Method method) {
+    public StaticMethodFallbackTypeConverter(Method method, TypeConverterRegistry registry)
{
         this.method = method;
-        this.useExchange = method.getParameterTypes().length == 2;
+        this.useExchange = method.getParameterTypes().length == 4;
+        this.registry = registry;
     }
 
     @Override
     public String toString() {
-        return "StaticMethodTypeConverter: " + method;
+        return "StaticMethodFallbackTypeConverter: " + method;
     }
 
     public <T> T convertTo(Class<T> type, Object value) {
@@ -46,7 +50,7 @@
     }
 
     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);
+        return useExchange ? (T)ObjectHelper.invokeMethod(method, null, type, exchange, value,
registry)
+            : (T)ObjectHelper.invokeMethod(method, null, type, value, registry);
     }
-}
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/StaticMethodFallbackTypeConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=738826&r1=738825&r2=738826&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 Thu
Jan 29 11:42:42 2009
@@ -35,6 +35,13 @@
     void addTypeConverter(Class toType, Class fromType, TypeConverter typeConverter);
 
     /**
+     * Registers a new fallback type converter
+     *
+     * @param typeConverter the type converter to use
+     */
+    void addFallbackTypeConverter(TypeConverter typeConverter);
+
+    /**
      * Performs a lookup for a given type converter.
      *
      * @param toType        the type to convert to

Modified: camel/trunk/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java?rev=738826&r1=738825&r2=738826&view=diff
==============================================================================
--- camel/trunk/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
(original)
+++ camel/trunk/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
Thu Jan 29 11:42:42 2009
@@ -32,7 +32,6 @@
 
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
-import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.converter.IOConverter;
 import org.apache.camel.converter.NIOConverter;
 import org.apache.camel.converter.jaxp.XmlConverter;
@@ -51,11 +50,6 @@
     private XmlConverter xmlConverter = new XmlConverter();
 
     @Converter
-    public static XmlObject toXmlObject(GenericFile<File> value) throws IOException,
XmlException {
-        return toXmlObject(value.getFile());
-    }
-
-    @Converter
     public static XmlObject toXmlObject(File value) throws IOException, XmlException {
         return XmlObject.Factory.parse(value);
     }



Mime
View raw message