You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ma...@apache.org on 2010/08/30 14:42:03 UTC

svn commit: r990789 - in /incubator/aries/trunk/blueprint/blueprint-core/src: main/java/org/apache/aries/blueprint/container/AggregateConverter.java test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java

Author: mahrwald
Date: Mon Aug 30 12:42:03 2010
New Revision: 990789

URL: http://svn.apache.org/viewvc?rev=990789&view=rev
Log:
ARIES-389: Committing patch from Wolfgang Glas

Modified:
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java
    incubator/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java?rev=990789&r1=990788&r2=990789&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java Mon Aug 30 12:42:03 2010
@@ -73,6 +73,17 @@ public class AggregateConverter implemen
         Object convert(ReifiedType type) throws Exception;
     }
 
+    private static class ConversionResult {
+
+        public final Converter converter;
+        public final Object value;
+
+        public ConversionResult(Converter converter, Object value) {
+            this.converter = converter;
+            this.value = value;
+        }
+    }
+
     private ExtendedBlueprintContainer blueprintContainer;
     private List<Converter> converters = new ArrayList<Converter>();
 
@@ -133,18 +144,18 @@ public class AggregateConverter implemen
         if (isAssignable(fromValue, type)) {
             return fromValue;
         }
-        Object value = null;
+        ConversionResult result = null;
         AccessControlContext acc = blueprintContainer.getAccessControlContext();
         if (acc == null) {
-            value = convertWithConverters(fromValue, type);
+            result = convertWithConverters(fromValue, type);
         } else {
-            value = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
-                public Object run() throws Exception {
+            result = AccessController.doPrivileged(new PrivilegedExceptionAction<ConversionResult>() {
+                public ConversionResult run() throws Exception {
                     return convertWithConverters(fromValue, type);
                 }            
             }, acc);
         }
-        if (value == null) {
+        if (result == null) {
             if (fromValue instanceof Number && Number.class.isAssignableFrom(unwrap(toClass(type)))) {
                 return convertToNumber((Number) fromValue, toClass(type));
             } else if (fromValue instanceof String) {
@@ -161,31 +172,32 @@ public class AggregateConverter implemen
                 throw new Exception("Unable to convert value " + fromValue + " to type " + type);
             }
         }
-        return value;
+        return result.value;
     }
 
-    private boolean canConvertWithConverters(Object source, ReifiedType type) {
+    private Converter selectMatchingConverter(Object source, ReifiedType type) {
         for (Converter converter : converters) {
             if (converter.canConvert(source, type)) {
-                return true;
+                return converter;
             }
         }
-        return false;
+        return null;
     }
-    
-    private Object convertWithConverters(Object source, ReifiedType type) throws Exception {
-        Object value = null;
-        for (Converter converter : converters) {
-            if (converter.canConvert(source, type)) {
-                value = converter.convert(source, type);
-                if (value != null) {
-                    return value;
-                }
-            }
-        }
-        return value;
+
+    private boolean canConvertWithConverters(Object source, ReifiedType type) {
+        return selectMatchingConverter(source,type) != null;
     }
+    
+    private ConversionResult convertWithConverters(Object source, ReifiedType type) throws Exception {
 
+        Converter converter = selectMatchingConverter(source,type);
+
+        if (converter == null)  return null;
+
+        Object value = converter.convert(source, type);
+        return new ConversionResult(converter,value);
+    }
+    
     public Object convertToNumber(Number value, Class toType) throws Exception {
         toType = unwrap(toType);
         if (AtomicInteger.class == toType) {

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java?rev=990789&r1=990788&r2=990789&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java Mon Aug 30 12:42:03 2010
@@ -180,10 +180,13 @@ public class AggregateConverterTest exte
         s = new AggregateConverter(new TestBlueprintContainer(null));
         s.registerConverter(new AsianRegionConverter());
         s.registerConverter(new EuRegionConverter());
+        s.registerConverter(new NullMarkerConverter());
         
         result = s.convert(new Object(), Region.class);
         // TODO: check with the spec about the result
         //assertTrue(result instanceof AsianRegion || result instanceof EuRegion);
+        result = s.convert(new Object(), NullMarker.class);
+        assertNull(result);
     }
     
     private interface Region {} 
@@ -191,6 +194,8 @@ public class AggregateConverterTest exte
     private interface EuRegion extends Region {}
     
     private interface AsianRegion extends Region {}
+
+    private interface NullMarker {}
     
     private static class RegionConverter implements Converter {
         public boolean canConvert(Object fromValue, ReifiedType toType) {
@@ -219,4 +224,13 @@ public class AggregateConverterTest exte
         }
     }
 
+    private static class NullMarkerConverter implements Converter {
+        public boolean canConvert(Object fromValue, ReifiedType toType) {
+            return toType.getRawClass().isAssignableFrom(NullMarker.class);
+        }
+        public Object convert(Object source, ReifiedType toType) throws Exception {
+            return null;
+        }
+    }
+
 }