camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1329365 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/converter/ camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/builder/ camel-core/src/test/java/org/apache/camel/converter/ ca...
Date Mon, 23 Apr 2012 18:38:20 GMT
Author: davsclaus
Date: Mon Apr 23 18:38:19 2012
New Revision: 1329365

URL: http://svn.apache.org/viewvc?rev=1329365&view=rev
Log:
CAMEL-5174: Do not use java beans property editor for type converter. CAMEL-5208: Optimize
type conversion with primitives to be faster.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java
      - copied, changed from r1329109, camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
Removed:
    camel/trunk/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/OIDListEditor.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
    camel/trunk/camel-core/src/test/resources/log4j.properties
    camel/trunk/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java?rev=1329365&r1=1329364&r2=1329365&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
Mon Apr 23 18:38:19 2012
@@ -63,7 +63,6 @@ public abstract class BaseTypeConverterR
     protected final PackageScanClassResolver resolver;
     protected Injector injector;
     protected final FactoryFinder factoryFinder;
-    protected final PropertyEditorTypeConverter propertyEditorTypeConverter = new PropertyEditorTypeConverter();
     protected final Statistics statistics = new UtilizationStatistics();
     protected final AtomicLong attemptCounter = new AtomicLong();
     protected final AtomicLong missCounter = new AtomicLong();
@@ -80,8 +79,6 @@ public abstract class BaseTypeConverterR
         // always convert something to a string so we want it only as the last resort
         // ToStringTypeConverter should NOT allow to be promoted
         addFallbackTypeConverter(new ToStringTypeConverter(), false);
-        // do not assume property editor as it has a String converter
-        addFallbackTypeConverter(propertyEditorTypeConverter, false);
         // enum is okay to be promoted
         addFallbackTypeConverter(new EnumTypeConverter(), true);
         // arrays is okay to be promoted
@@ -123,7 +120,11 @@ public abstract class BaseTypeConverterR
             }
 
             // error occurred during type conversion
-            throw new TypeConversionException(value, type, e);
+            if (e instanceof TypeConversionException) {
+                throw (TypeConversionException) e;
+            } else {
+                throw new TypeConversionException(value, type, e);
+            }
         }
         if (answer == Void.TYPE) {
             // Could not find suitable conversion
@@ -155,7 +156,11 @@ public abstract class BaseTypeConverterR
         } catch (Exception e) {
             failedCounter.incrementAndGet();
             // error occurred during type conversion
-            throw new TypeConversionException(value, type, e);
+            if (e instanceof TypeConversionException) {
+                throw (TypeConversionException) e;
+            } else {
+                throw new TypeConversionException(value, type, e);
+            }
         }
         if (answer == Void.TYPE || value == null) {
             // Could not find suitable conversion
@@ -240,15 +245,48 @@ public abstract class BaseTypeConverterR
         TypeConverter converter = getOrFindTypeConverter(type, value);
         if (converter != null) {
             log.trace("Using converter: {} to convert {}", converter, key);
-            Object rc = converter.convertTo(type, exchange, value);
+            Object rc;
+            if (tryConvert) {
+                rc = converter.tryConvertTo(type, exchange, value);
+            } else {
+                rc = converter.convertTo(type, exchange, value);
+            }
             if (rc != null) {
                 return rc;
             }
         }
 
+        // not found with that type then if it was a primitive type then try again with the
wrapper type
+        if (type.isPrimitive()) {
+            Class<?> primitiveType = ObjectHelper.convertPrimitiveTypeToWrapperType(type);
+            if (primitiveType != type) {
+                Class<?> fromType = value.getClass();
+                TypeConverter tc = getOrFindTypeConverter(primitiveType, value);
+                if (tc != null) {
+                    // add the type as a known type converter as we can convert from primitive
to object converter
+                    addTypeConverter(type, fromType, tc);
+                    Object rc;
+                    if (tryConvert) {
+                        rc = tc.tryConvertTo(primitiveType, exchange, value);
+                    } else {
+                        rc = tc.convertTo(primitiveType, exchange, value);
+                    }
+                    if (rc != null) {
+                        return rc;
+                    }
+                }
+            }
+        }
+
         // fallback converters
         for (FallbackTypeConverter fallback : fallbackConverters) {
-            Object rc = fallback.getFallbackTypeConverter().convertTo(type, exchange, value);
+            TypeConverter tc = fallback.getFallbackTypeConverter();
+            Object rc;
+            if (tryConvert) {
+                rc = tc.tryConvertTo(type, exchange, value);
+            } else {
+                rc = tc.convertTo(type, exchange, value);
+            }
 
             if (Void.TYPE.equals(rc)) {
                 // it cannot be converted so give up
@@ -277,14 +315,6 @@ public abstract class BaseTypeConverterR
             }
         }
 
-        // not found with that type then if it was a primitive type then try again with the
wrapper type
-        if (type.isPrimitive()) {
-            Class<?> primitiveType = ObjectHelper.convertPrimitiveTypeToWrapperType(type);
-            if (primitiveType != type) {
-                return convertTo(primitiveType, exchange, value);
-            }
-        }
-
         if (!tryConvert) {
             // Could not find suitable conversion, so remember it
             // do not register misses for try conversions
@@ -492,7 +522,6 @@ public abstract class BaseTypeConverterR
 
         typeMappings.clear();
         misses.clear();
-        propertyEditorTypeConverter.clear();
         statistics.reset();
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1329365&r1=1329364&r2=1329365&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Mon Apr 23
18:38:19 2012
@@ -909,7 +909,7 @@ public final class ObjectHelper {
         } catch (IllegalAccessException e) {
             throw new RuntimeCamelException(e);
         } catch (InvocationTargetException e) {
-            throw new RuntimeCamelException(e.getCause());
+            throw ObjectHelper.wrapRuntimeCamelException(e.getCause());
         }
     }
 

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java?rev=1329365&r1=1329364&r2=1329365&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java Mon
Apr 23 18:38:19 2012
@@ -53,7 +53,7 @@ public class SimpleBuilderTest extends T
             SimpleBuilder.simple("${body}", int.class).evaluate(exchange, Object.class);
             fail("Should have thrown exception");
         } catch (TypeConversionException e) {
-            assertIsInstanceOf(NumberFormatException.class, e.getCause());
+            assertIsInstanceOf(NumberFormatException.class, e.getCause().getCause());
         }
 
         assertEquals(true, SimpleBuilder.simple("${header.cool}", boolean.class).evaluate(exchange,
Object.class));

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java
(from r1329109, camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java&r1=1329109&r2=1329365&rev=1329365&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java
Mon Apr 23 18:38:19 2012
@@ -22,18 +22,13 @@ import org.apache.camel.util.StopWatch;
 /**
  * @version 
  */
-public class PropertyEditorTypeConverterIssueTest extends ContextTestSupport {
-
-    public void testPropertyEditorTypeConverter() throws Exception {
-        // test that converters a custom object (MyBean) to a String which causes
-        // PropertyEditorTypeConverter to be used. And this test times how fast
-        // this is. As we want to optimize PropertyEditorTypeConverter to be faster
-        MyBean bean = new MyBean();
-        bean.setBar("Hello");
+public class PrimitiveTypeConverterIssueTest extends ContextTestSupport {
 
+    public void testPrimitiveTypeConverter() throws Exception {
         StopWatch watch = new StopWatch();
-        for (int i = 0; i < 500; i++) {
-            assertNotNull(context.getTypeConverter().convertTo(String.class, bean));
+        for (int i = 0; i < 10000; i++) {
+            int num = context.getTypeConverter().convertTo(int.class, "123");
+            assertEquals(123, num);
         }
         log.info("Time taken: " + watch.stop());
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java?rev=1329365&r1=1329364&r2=1329365&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
Mon Apr 23 18:38:19 2012
@@ -33,7 +33,9 @@ public class PropertyEditorTypeConverter
 
         StopWatch watch = new StopWatch();
         for (int i = 0; i < 500; i++) {
-            assertNotNull(context.getTypeConverter().convertTo(String.class, bean));
+            String s = context.getTypeConverter().convertTo(String.class, bean);
+            log.debug(s);
+            assertNotNull(s);
         }
         log.info("Time taken: " + watch.stop());
     }

Modified: camel/trunk/camel-core/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/log4j.properties?rev=1329365&r1=1329364&r2=1329365&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/resources/log4j.properties (original)
+++ camel/trunk/camel-core/src/test/resources/log4j.properties Mon Apr 23 18:38:19 2012
@@ -24,7 +24,6 @@ log4j.rootLogger=INFO, file
 log4j.logger.org.apache.camel.management=WARN
 log4j.logger.org.apache.camel.impl.DefaultPackageScanClassResolver=WARN
 #log4j.logger.org.apache.camel.impl.converter.DefaultTypeConverter=TRACE
-#log4j.logger.org.apache.camel.impl.converter.PropertyEditorTypeConverter=TRACE
 #log4j.logger.org.apache.camel.impl.converter=DEBUG
 
 #log4j.logger.org.apache.camel=DEBUG

Modified: camel/trunk/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java?rev=1329365&r1=1329364&r2=1329365&view=diff
==============================================================================
--- camel/trunk/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
(original)
+++ camel/trunk/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
Mon Apr 23 18:38:19 2012
@@ -16,8 +16,12 @@
  */
 package org.apache.camel.component.snmp;
 
+import java.util.StringTokenizer;
+
 import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
 import org.snmp4j.PDU;
+import org.snmp4j.smi.OID;
 import org.snmp4j.smi.VariableBinding;
 
 @Converter
@@ -40,6 +44,29 @@ public final class SnmpConverters {
         //Utility Class
     }
 
+    @Converter
+    public static OIDList toOIDList(String s, Exchange exchange) {
+        OIDList list = new OIDList();
+
+        if (s != null && s.indexOf(",") != -1) {
+            // seems to be a comma separated oid list
+            StringTokenizer strTok = new StringTokenizer(s, ",");
+            while (strTok.hasMoreTokens()) {
+                String tok = strTok.nextToken();
+                if (tok != null && tok.trim().length() > 0) {
+                    list.add(new OID(tok.trim()));
+                } else {
+                    // empty token - skip
+                }
+            }
+        } else if (s != null) {
+            // maybe a single oid
+            list.add(new OID(s.trim()));
+        }
+
+        return list;
+    }
+
     /**
      * Converts the given snmp pdu to a String body.
      *
@@ -50,10 +77,10 @@ public final class SnmpConverters {
     public static String toString(PDU pdu) {
      // the output buffer
         StringBuilder sb = new StringBuilder();
-        
+
         // prepare the header
         sb.append(SNMP_TAG_OPEN);
-                
+
         // now loop all variables of the response
         for (Object o : pdu.getVariableBindings()) {
             VariableBinding b = (VariableBinding)o;
@@ -67,13 +94,13 @@ public final class SnmpConverters {
             sb.append(VALUE_TAG_CLOSE);
             sb.append(ENTRY_TAG_CLOSE);
         }
-        
+
         // prepare the footer
         sb.append(SNMP_TAG_CLOSE);
-        
+
         return sb.toString();
     }
-    
+
     private static String getXmlSafeString(String string) {
         return string.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("&",
"&amp;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;");
     }



Mime
View raw message