camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krass...@apache.org
Subject svn commit: r832376 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/DefaultComponent.java main/java/org/apache/camel/util/EndpointHelper.java test/java/org/apache/camel/impl/DefaultComponentTest.java
Date Tue, 03 Nov 2009 10:23:22 GMT
Author: krasserm
Date: Tue Nov  3 10:23:06 2009
New Revision: 832376

URL: http://svn.apache.org/viewvc?rev=832376&view=rev
Log:
CAMEL-2127: DefaultComponent.resolveAndRemoveReferenceParameter as alternative to DefaultComponent.getAndRemoveParameter
for looking up reference parameters in the registry. 

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java?rev=832376&r1=832375&r2=832376&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java Tue Nov
 3 10:23:06 2009
@@ -296,25 +296,29 @@
     }
 
     /**
-     * Gets the parameter and remove it from the parameter map.
+     * Gets the parameter and remove it from the parameter map. This method doesn't resolve
+     * reference parameters in the registry.
      * 
      * @param parameters the parameters
      * @param key        the key
      * @param type       the requested type to convert the value from the parameter
      * @return  the converted value parameter, <tt>null</tt> if parameter does
not exists.
+     * @see #resolveAndRemoveReferenceParameter(Map, String, Class)
      */
     public <T> T getAndRemoveParameter(Map parameters, String key, Class<T> type)
{
         return getAndRemoveParameter(parameters, key, type, null);
     }
 
     /**
-     * Gets the parameter and remove it from the parameter map.
+     * Gets the parameter and remove it from the parameter map. This method doesn't resolve
+     * reference parameters in the registry.
      *
      * @param parameters    the parameters
      * @param key           the key
      * @param type          the requested type to convert the value from the parameter
      * @param defaultValue  use this default value if the parameter does not contain the
key
      * @return  the converted value parameter
+     * @see #resolveAndRemoveReferenceParameter(Map, String, Class, Object)
      */
     public <T> T getAndRemoveParameter(Map parameters, String key, Class<T> type,
T defaultValue) {
         Object value = parameters.remove(key);
@@ -329,6 +333,43 @@
     }
 
     /**
+     * Resolves a reference parameter in the registry and removes it from the map. 
+     * 
+     * @param <T>           type of object to lookup in th registry.
+     * @param parameters    parameter map.
+     * @param key           parameter map key.
+     * @param type          type of object to lookup in th registry.
+     * @return the referenced object or <code>null</code>.
+     */
+    public <T> T resolveAndRemoveReferenceParameter(Map parameters, String key, Class<T>
type) {
+        return resolveAndRemoveReferenceParameter(parameters, key, type, null); 
+    }
+
+    /**
+     * Resolves a reference parameter in the registry and removes it from the map. 
+     * 
+     * @param <T>           type of object to lookup in th registry.
+     * @param parameters    parameter map.
+     * @param key           parameter map key.
+     * @param type          type of object to lookup in th registry.
+     * @param defaultValue  default value to use if neither the parameter map contains
+     *                      the key nor the registry contains an object of requested
+     *                      type.
+     * @return the referenced object, the default value or <code>null</code>.
+     */
+    public <T> T resolveAndRemoveReferenceParameter(Map parameters, String key, Class<T>
type, T defaultValue) {
+        String value = getAndRemoveParameter(parameters, key, String.class);
+        if (EndpointHelper.isReferenceParameter(value)) {
+            T result = EndpointHelper.resolveReferenceParameter(getCamelContext(), value.toString(),
type);
+            return result == null ? defaultValue : result;
+        } else if (value == null) {
+            return defaultValue;
+        } else {
+            throw new IllegalArgumentException("Parameter value " + value + " is not a valid
reference");
+        }
+    }
+    
+    /**
      * Returns the reminder of the text if it starts with the prefix.
      * <p/>
      * Is useable for string parameters that contains commands.

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java?rev=832376&r1=832375&r2=832376&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java Tue Nov
 3 10:23:06 2009
@@ -172,7 +172,7 @@
             Object v = entry.getValue();
             String value = v != null ? v.toString() : null;
             if (value != null && isReferenceParameter(value)) {
-                Object ref = context.getRegistry().lookup(value.substring(1));
+                Object ref = resolveReferenceParameter(context, value, Object.class);
                 String name = key.toString();
                 if (ref != null) {
                     boolean hit = IntrospectionSupport.setProperty(context.getTypeConverter(),
bean, name, ref);
@@ -197,4 +197,19 @@
     public static boolean isReferenceParameter(String parameter) {
         return parameter != null && parameter.startsWith("#");
     }
+    
+    /**
+     * Resolves a reference parameter by making a lookup in the registry.
+     * 
+     * @param <T> type of object to lookup.
+     * @param context Camel content to use for lookup.
+     * @param value reference parameter value.
+     * @param type type of object to lookup.
+     * @return lookup result or <code>null</code>. 
+     */
+    public static <T> T resolveReferenceParameter(CamelContext context, String value,
Class<T> type) {
+        assert isReferenceParameter(value);
+        return context.getRegistry().lookup(value.substring(1), type);
+    }
+    
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java?rev=832376&r1=832375&r2=832376&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java Tue
Nov  3 10:23:06 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.impl;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -77,6 +78,58 @@
         assertEquals(value.intValue(), 4);
     }
 
+    public void testResolveAndRemoveReferenceParameter() {
+        Map parameters = new HashMap();
+        parameters.put("date", "#beginning");
+        MyComponent my = new MyComponent(this.context);
+        Date value = my.resolveAndRemoveReferenceParameter(parameters, "date", Date.class);
+        assertEquals(new Date(0), value);
+    }
+
+    public void testResolveAndRemoveReferenceParameterNotInRegistryDefault() {
+        Map parameters = new HashMap();
+        parameters.put("date", "#somewhen");
+        MyComponent my = new MyComponent(this.context);
+        Date value = my.resolveAndRemoveReferenceParameter(parameters, "date", Date.class,
new Date(1));
+        assertEquals(new Date(1), value);
+    }
+
+    public void testResolveAndRemoveReferenceParameterNotInRegistryNull() {
+        Map parameters = new HashMap();
+        parameters.put("date", "#somewhen");
+        MyComponent my = new MyComponent(this.context);
+        Date value = my.resolveAndRemoveReferenceParameter(parameters, "date", Date.class);
+        assertNull(value);
+    }
+
+    public void testResolveAndRemoveReferenceParameterNotInMapDefault() {
+        Map parameters = new HashMap();
+        parameters.put("date", "#beginning");
+        MyComponent my = new MyComponent(this.context);
+        Date value = my.resolveAndRemoveReferenceParameter(parameters, "wrong", Date.class,
new Date(1));
+        assertEquals(new Date(1), value);
+    }
+
+    public void testResolveAndRemoveReferenceParameterNotInMapNull() {
+        Map parameters = new HashMap();
+        parameters.put("date", "#beginning");
+        MyComponent my = new MyComponent(this.context);
+        Date value = my.resolveAndRemoveReferenceParameter(parameters, "wrong", Date.class);
+        assertNull(value);
+    }
+
+    public void testResolveAndRemoveInvalidReferenceParameter() {
+        Map parameters = new HashMap();
+        parameters.put("date", "beginning");
+        MyComponent my = new MyComponent(this.context);
+        try {
+            my.resolveAndRemoveReferenceParameter(parameters, "date", Date.class);
+            fail("usage of invalid reference");
+        } catch (IllegalArgumentException e) {
+            // test passed
+        }
+    }
+
     public void testContextShouldBeSet() throws Exception {
         MyComponent my = new MyComponent(null);
         try {
@@ -87,4 +140,10 @@
         }
     }
 
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndiRegistry = super.createRegistry();
+        jndiRegistry.bind("beginning", new Date(0));
+        return jndiRegistry;
+    }
+
 }



Mime
View raw message