camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r832794 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/model/ main/java/org/apache/camel/spi/ test/java/org/apache/camel/processor/aggregator/
Date Wed, 04 Nov 2009 16:53:24 GMT
Author: davsclaus
Date: Wed Nov  4 16:53:24 2009
New Revision: 832794

URL: http://svn.apache.org/viewvc?rev=832794&view=rev
Log:
CAMEL-2131: Fixed issue with DataFormatResolver.

Removed:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorRouteNumberOfProcessorTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=832794&r1=832793&r2=832794&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Wed Nov  4 16:53:24
2009
@@ -460,20 +460,20 @@
     Map<String, DataFormatDefinition> getDataFormats();
     
     /**
-     * Resolve a data format given its definition
+     * Resolve a data format given a reference to lookup in {@link Registry}
      *
-     * @param def the data format definition
-     * @return the resolved data format
+     * @param name the data format name or a reference to it in the {@link Registry}
+     * @return the resolved data format, or <tt>null</tt> if not found
      */
-    DataFormat resolveDataFormat(DataFormatDefinition def);
+    DataFormat resolveDataFormatByRef(String name);
 
     /**
-     * Resolve a data format given its name
+     * Resolve a data format given its class name
      *
-     * @param name the data format name
-     * @return the resolved data format
+     * @param name the fully qualified class name
+     * @return the resolved data format, or <tt>null</tt> if not found
      */
-    DataFormat resolveDataFormat(String name);
+    DataFormat resolveDataFormatByClassName(String name);
 
     /**
      * Sets the properties that can be referenced in the camel context

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=832794&r1=832793&r2=832794&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Wed
Nov  4 16:53:24 2009
@@ -1361,12 +1361,12 @@
         this.dataFormatResolver = dataFormatResolver;
     }
 
-    public DataFormat resolveDataFormat(DataFormatDefinition def) {
-        return dataFormatResolver.resolveDataFormat(def, this);
+    public DataFormat resolveDataFormatByRef(String ref) {
+        return dataFormatResolver.resolveDataFormatByRef(ref, this);
     }
 
-    public DataFormat resolveDataFormat(String ref) {
-        return dataFormatResolver.resolveDataFormat(ref, this);
+    public DataFormat resolveDataFormatByClassName(String name) {
+        return dataFormatResolver.resolveDataFormatByClassName(name, this);
     }
 
     protected String getEndpointKey(String uri, Endpoint endpoint) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java?rev=832794&r1=832793&r2=832794&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
Wed Nov  4 16:53:24 2009
@@ -20,6 +20,7 @@
 import org.apache.camel.model.DataFormatDefinition;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.DataFormatResolver;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * Default data format resolver
@@ -28,32 +29,39 @@
  */
 public class DefaultDataFormatResolver implements DataFormatResolver {
 
-    @SuppressWarnings("unchecked")
-    public DataFormat resolveDataFormat(DataFormatDefinition definition, CamelContext context)
{
-        Class type = context.getClassResolver().resolveClass(definition.getDataFormatName());
-        if (type == null) {
-            throw new IllegalArgumentException("The class " + definition.getDataFormatName()
-                + " is not on the classpath! Cannot use the dataFormat " + this);
+    public DataFormat resolveDataFormatByClassName(String name, CamelContext context) {
+        if (name != null) {
+            Class type = context.getClassResolver().resolveClass(name);
+            if (type == null) {
+                throw new IllegalArgumentException("The class " + name + " is not on the
classpath! Cannot use the dataFormat " + this);
+            }
+            return (DataFormat) ObjectHelper.newInstance(type);
         }
-        return (DataFormat) context.getInjector().newInstance(type);
+        return null;
     }
 
-    public DataFormat resolveDataFormat(String ref, CamelContext context) {
-        DataFormat dataFormat = context.getRegistry().lookup(ref, DataFormat.class);
+    public DataFormat resolveDataFormatByRef(String ref, CamelContext context) {
+        DataFormat dataFormat = lookup(context, ref, DataFormat.class);
         if (dataFormat == null) {
             // lookup type and create the data format from it
-            DataFormatDefinition type = context.getRegistry().lookup(ref, DataFormatDefinition.class);
+            DataFormatDefinition type = lookup(context, ref, DataFormatDefinition.class);
             if (type == null && context.getDataFormats() != null) {
                 type = context.getDataFormats().get(ref);
             }
             if (type != null) {
-                dataFormat = resolveDataFormat(type, context);
+                dataFormat = type.getDataFormat();
             }
         }
-        if (dataFormat == null) {
-            throw new IllegalArgumentException("Cannot find data format in registry with
ref: " + ref);
-        }
         return dataFormat;
     }
 
+    private static <T> T lookup(CamelContext context, String ref, Class<T> type)
{
+        try {
+            return context.getRegistry().lookup(ref, type);
+        } catch (Exception e) {
+            // need to ignore not same type and return it as null
+            return null;
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java?rev=832794&r1=832793&r2=832794&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
Wed Nov  4 16:53:24 2009
@@ -61,9 +61,39 @@
     public static DataFormat getDataFormat(RouteContext routeContext, DataFormatDefinition
type, String ref) {
         if (type == null) {
             ObjectHelper.notNull(ref, "ref or dataFormat");
-            return routeContext.getCamelContext().resolveDataFormat(ref);
+
+            // try to let resolver see if it can resolve it, its not always possible
+            DataFormat dataFormat = routeContext.getCamelContext().resolveDataFormatByRef(ref);
+
+            if (dataFormat == null) {
+                // resolver could not then do a bit more leg work using the route context
+                // which can help instantiate data formats
+
+                type = lookup(routeContext, ref, DataFormatDefinition.class);
+                if (type == null) {
+                    type = routeContext.getDataFormat(ref);
+                }
+                if (type != null) {
+                    dataFormat = type.getDataFormat(routeContext);
+                }
+            }
+
+            if (dataFormat == null) {
+                throw new IllegalArgumentException("Cannot find data format in registry with
ref: " + ref);
+            }
+
+            return dataFormat;
         } else {
-            return type.createDataFormat(routeContext);
+            return type.getDataFormat(routeContext);
+        }
+    }
+
+    private static <T> T lookup(RouteContext routeContext, String ref, Class<T>
type) {
+        try {
+            return routeContext.lookup(ref, type);
+        } catch (Exception e) {
+            // need to ignore not same type and return it as null
+            return null;
         }
     }
 
@@ -82,7 +112,7 @@
     @SuppressWarnings("unchecked")
     protected DataFormat createDataFormat(RouteContext routeContext) {
         if (dataFormatName != null) {
-            return routeContext.getCamelContext().resolveDataFormat(this);
+            return routeContext.getCamelContext().resolveDataFormatByClassName(dataFormatName);
         }
         return null;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java?rev=832794&r1=832793&r2=832794&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java Wed
Nov  4 16:53:24 2009
@@ -27,21 +27,21 @@
 public interface DataFormatResolver {
 
     /**
-     * Resolves the given data format by its definition.
+     * Resolves the given data format by a reference.
      *
-     * @param definition the definition
+     * @param name the fully qualified classname
      * @param context the camel context
-     * @return the data format
+     * @return the data format or <tt>null</tt> if not possible to resolve
      */
-    DataFormat resolveDataFormat(DataFormatDefinition definition, CamelContext context);
+    DataFormat resolveDataFormatByClassName(String name, CamelContext context);
 
     /**
-     * Resolves the given data format by its name.
+     * Resolves the given data format by a reference.
      *
-     * @param name the name
+     * @param ref the reference to lookup in {@link org.apache.camel.spi.Registry}
      * @param context the camel context
-     * @return the data format
+     * @return the data format or <tt>null</tt> if not possible to resolve
      */
-    DataFormat resolveDataFormat(String name, CamelContext context);
+    DataFormat resolveDataFormatByRef(String ref, CamelContext context);
 
 }



Mime
View raw message