camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krass...@apache.org
Subject svn commit: r885767 - 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, 01 Dec 2009 14:28:19 GMT
Author: krasserm
Date: Tue Dec  1 14:28:18 2009
New Revision: 885767

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

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=885767&r1=885766&r2=885767&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 Dec
 1 14:28:18 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.impl;
 
 import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ScheduledExecutorService;
@@ -363,13 +365,13 @@
     /**
      * Resolves a reference parameter in the registry and removes it from the map. 
      * 
-     * @param <T>           type of object to lookup in th registry.
+     * @param <T>           type of object to lookup in the 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.
+     * @param type          type of object to lookup in the registry.
+     * @param defaultValue  default value to use if either the parameter map doesn't 
+     *                      contain the key or the registry doesn't contain an object
+     *                      of requested type.
      * @return the referenced object, the default value or <code>null</code>.
      */
     public <T> T resolveAndRemoveReferenceParameter(Map<String, Object> parameters,
String key, Class<T> type, T defaultValue) {
@@ -385,6 +387,54 @@
     }
     
     /**
+     * Resolves a reference list parameter in the registry and removes it from
+     * the map.
+     * 
+     * @param parameters
+     *            parameter map.
+     * @param key
+     *            parameter map key.
+     * @param elementType
+     *            result list element type.
+     * @return the list of referenced objects or an empty list, never
+     *         <code>null</code>.
+     * @see EndpointHelper#resolveReferenceListParameter(CamelContext, String, Class)
+     */
+    public <T> List<T> resolveAndRemoveReferenceListParameter(Map<String,
Object> parameters, String key, Class<T> elementType) {
+        return resolveAndRemoveReferenceListParameter(parameters, key, elementType, new ArrayList<T>(0));
+    }
+
+    /**
+     * Resolves a reference list parameter in the registry and removes it from
+     * the map.
+     * 
+     * @param parameters
+     *            parameter map.
+     * @param key
+     *            parameter map key.
+     * @param elementType
+     *            result list element type.
+     * @param defaultValue
+     *            default value to use if either the parameter map doesn't
+     *            contain the key or the lookup for none of the references
+     *            was successful.
+     * @return the list of referenced objects, the default value or an empty list, never
+     *         <code>null</code>.
+     * @see EndpointHelper#resolveReferenceListParameter(CamelContext, String, Class)
+     */
+    public <T> List<T> resolveAndRemoveReferenceListParameter(Map<String,
Object> parameters, String key, Class<T> elementType, List<T>  defaultValue)
{
+        String value = getAndRemoveParameter(parameters, key, String.class);
+        if (EndpointHelper.isReferenceParameter(value)) {
+            List<T> result = EndpointHelper.resolveReferenceListParameter(getCamelContext(),
value.toString(), elementType);
+            return result.isEmpty() ? defaultValue : result;
+        } else if (value == null) {
+            return defaultValue;
+        } else {
+            throw new IllegalArgumentException("Parameter value " + value + " is not a valid
reference (list)");
+        }
+    }
+    
+    /**
      * 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=885767&r1=885766&r2=885767&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 Dec
 1 14:28:18 2009
@@ -16,7 +16,11 @@
  */
 package org.apache.camel.util;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.regex.PatternSyntaxException;
 
@@ -163,7 +167,6 @@
      * @param parameters parameters
      * @throws Exception is thrown if setting property fails
      */
-    @SuppressWarnings("unchecked")
     public static void setReferenceProperties(CamelContext context, Object bean, Map<String,
Object> parameters) throws Exception {
         Iterator<Map.Entry<String, Object>> it = parameters.entrySet().iterator();
         while (it.hasNext()) {
@@ -194,7 +197,7 @@
      * @return <tt>true</tt> if its a reference parameter
      */
     public static boolean isReferenceParameter(String parameter) {
-        return parameter != null && parameter.startsWith("#");
+        return parameter != null && parameter.trim().startsWith("#");
     }
     
     /**
@@ -209,6 +212,56 @@
     public static <T> T resolveReferenceParameter(CamelContext context, String value,
Class<T> type) {
         assert isReferenceParameter(value);
         return context.getRegistry().lookup(value.substring(1), type);
+        
+    }
+
+    /**
+     * Resolves a reference list parameter by making lookups in the registry.
+     * The parameter value must be one of the following:
+     * <ul>
+     * <li>a comma-separated list of references to beans of type T</li>
+     * <li>a single reference to a bean type T</li>
+     * <li>a single reference to a bean of type java.util.List</li>
+     * </ul>
+     * Only bean lookup results that are not <code>null</code> are added to the
+     * result list.
+     * 
+     * @param context
+     *            Camel content to use for lookup.
+     * @param value
+     *            reference parameter value.
+     * @param elementType
+     *            result list element type.
+     * @return list of non-null lookup results or an empty list, never
+     *         <code>null</code>.
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> List<T> resolveReferenceListParameter(CamelContext context,
String value, Class<T> elementType) {
+        if (value == null) {
+            return Collections.emptyList();
+        }
+        List<String> elements = Arrays.asList(value.split(","));
+        if (elements.size() == 1) {
+            Object bean = resolveReferenceParameter(context, elements.get(0).trim(), Object.class);
+            if (bean instanceof List) {
+                // The bean is a list
+                return (List)bean;
+            } else if (elementType.isInstance(bean)) {
+                // The bean is a list element
+                return (List<T>)Arrays.asList(bean);
+            } else {
+                return Collections.emptyList();
+            }
+        } else { // more than one list element
+            ArrayList<T> result = new ArrayList<T>(elements.size());
+            for (String element : elements) {
+                T bean = resolveReferenceParameter(context, element.trim(), elementType);
+                if (bean != null) {
+                    result.add(bean);
+                }
+            }
+            return result;
+        }
     }
     
 }

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=885767&r1=885766&r2=885767&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
Dec  1 14:28:18 2009
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.impl;
 
+import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.CamelContext;
@@ -130,6 +132,54 @@
         }
     }
 
+    public void testResolveAndRemoveReferenceListParameterElement() {
+        Map<String, Object> parameters = new HashMap<String, Object>();
+        parameters.put("dates", "#bean1");
+        MyComponent my = new MyComponent(this.context);
+        List<Date> values = my.resolveAndRemoveReferenceListParameter(parameters, "dates",
Date.class);
+        assertEquals(1, values.size());
+        assertEquals(new Date(10), values.get(0));
+    }
+    
+    public void testResolveAndRemoveReferenceListParameterListComma() {
+        Map<String, Object> parameters = new HashMap<String, Object>();
+        parameters.put("dates", "#bean1,#bean2");
+        MyComponent my = new MyComponent(this.context);
+        List<Date> values = my.resolveAndRemoveReferenceListParameter(parameters, "dates",
Date.class);
+        assertEquals(2, values.size());
+        assertEquals(new Date(10), values.get(0));
+        assertEquals(new Date(11), values.get(1));
+    }
+
+    public void testResolveAndRemoveReferenceListParameterListCommaTrim() {
+        Map<String, Object> parameters = new HashMap<String, Object>();
+        parameters.put("dates", " #bean1 , #bean2 ");
+        MyComponent my = new MyComponent(this.context);
+        List<Date> values = my.resolveAndRemoveReferenceListParameter(parameters, "dates",
Date.class);
+        assertEquals(2, values.size());
+        assertEquals(new Date(10), values.get(0));
+        assertEquals(new Date(11), values.get(1));
+    }
+
+    public void testResolveAndRemoveReferenceListParameterListBean() {
+        Map<String, Object> parameters = new HashMap<String, Object>();
+        parameters.put("dates", "#listBean");
+        MyComponent my = new MyComponent(this.context);
+        List<Date> values = my.resolveAndRemoveReferenceListParameter(parameters, "dates",
Date.class);
+        assertEquals(2, values.size());
+        assertEquals(new Date(10), values.get(0));
+        assertEquals(new Date(11), values.get(1));
+    }
+
+    public void testResolveAndRemoveReferenceListParameterInvalidBean() {
+        Map<String, Object> parameters = new HashMap<String, Object>();
+        parameters.put("dates", "#bean1,#bean3");
+        MyComponent my = new MyComponent(this.context);
+        List<Date> values = my.resolveAndRemoveReferenceListParameter(parameters, "dates",
Date.class);
+        assertEquals(1, values.size());
+        assertEquals(new Date(10), values.get(0));
+    }
+
     public void testContextShouldBeSet() throws Exception {
         MyComponent my = new MyComponent(null);
         try {
@@ -141,8 +191,13 @@
     }
 
     protected JndiRegistry createRegistry() throws Exception {
+        Date bean1 = new Date(10);
+        Date bean2 = new Date(11);
         JndiRegistry jndiRegistry = super.createRegistry();
         jndiRegistry.bind("beginning", new Date(0));
+        jndiRegistry.bind("bean1", bean1);
+        jndiRegistry.bind("bean2", bean2);
+        jndiRegistry.bind("listBean", Arrays.asList(bean1, bean2));
         return jndiRegistry;
     }
 



Mime
View raw message