camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1329366 - in /camel/branches/camel-2.9.x: ./ 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/c...
Date Mon, 23 Apr 2012 18:40:44 GMT
Author: davsclaus
Date: Mon Apr 23 18:40:43 2012
New Revision: 1329366

URL: http://svn.apache.org/viewvc?rev=1329366&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/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java
      - copied unchanged from r1329365, camel/trunk/camel-core/src/test/java/org/apache/camel/converter/PrimitiveTypeConverterIssueTest.java
Removed:
    camel/branches/camel-2.9.x/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/OIDListEditor.java
Modified:
    camel/branches/camel-2.9.x/   (props changed)
    camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
    camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java
    camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
    camel/branches/camel-2.9.x/camel-core/src/test/resources/log4j.properties
    camel/branches/camel-2.9.x/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1329365

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java?rev=1329366&r1=1329365&r2=1329366&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
(original)
+++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
Mon Apr 23 18:40:43 2012
@@ -61,7 +61,6 @@ public abstract class BaseTypeConverterR
     protected final PackageScanClassResolver resolver;
     protected Injector injector;
     protected final FactoryFinder factoryFinder;
-    protected final PropertyEditorTypeConverter propertyEditorTypeConverter = new PropertyEditorTypeConverter();
 
     public BaseTypeConverterRegistry(PackageScanClassResolver resolver, Injector injector,
FactoryFinder factoryFinder) {
         this.resolver = resolver;
@@ -73,8 +72,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
@@ -114,7 +111,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
@@ -141,7 +142,11 @@ public abstract class BaseTypeConverterR
             answer = doConvertTo(type, exchange, value, false);
         } catch (Exception e) {
             // 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
@@ -219,15 +224,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
@@ -256,14 +294,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
@@ -478,7 +508,6 @@ public abstract class BaseTypeConverterR
     protected void doStop() throws Exception {
         typeMappings.clear();
         misses.clear();
-        propertyEditorTypeConverter.clear();
     }
 
     /**

Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1329366&r1=1329365&r2=1329366&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
(original)
+++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
Mon Apr 23 18:40:43 2012
@@ -878,7 +878,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/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java?rev=1329366&r1=1329365&r2=1329366&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java
(original)
+++ camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java
Mon Apr 23 18:40:43 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));

Modified: camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java?rev=1329366&r1=1329365&r2=1329366&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
(original)
+++ camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/converter/PropertyEditorTypeConverterIssueTest.java
Mon Apr 23 18:40:43 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/branches/camel-2.9.x/camel-core/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/resources/log4j.properties?rev=1329366&r1=1329365&r2=1329366&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/test/resources/log4j.properties (original)
+++ camel/branches/camel-2.9.x/camel-core/src/test/resources/log4j.properties Mon Apr 23 18:40:43
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/branches/camel-2.9.x/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java?rev=1329366&r1=1329365&r2=1329366&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
Mon Apr 23 18:40:43 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