geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r766041 - in /geronimo/sandbox/blueprint/org.apache.felix.blueprint/src: main/java/org/apache/felix/blueprint/context/ main/java/org/apache/felix/blueprint/convert/ test/java/org/apache/felix/blueprint/ test/java/org/apache/felix/blueprint/...
Date Fri, 17 Apr 2009 14:58:50 GMT
Author: gawor
Date: Fri Apr 17 14:58:49 2009
New Revision: 766041

URL: http://svn.apache.org/viewvc?rev=766041&view=rev
Log:
support for array injection

Modified:
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Instanciator.java
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/convert/ConversionServiceImpl.java
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/WiringTest.java
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/pojos/PojoA.java
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-wiring.xml

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Instanciator.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Instanciator.java?rev=766041&r1=766040&r2=766041&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Instanciator.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Instanciator.java
Fri Apr 17 14:58:49 2009
@@ -26,6 +26,7 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.xbean.recipe.ArrayRecipe;
 import org.apache.xbean.recipe.Option;
 import org.apache.xbean.recipe.Repository;
 import org.apache.xbean.recipe.DefaultRepository;
@@ -60,6 +61,19 @@
  */
 public class Instanciator {
 
+    private static Map<String, Class> primitiveClasses = new HashMap<String, Class>();
+    
+    static {
+        primitiveClasses.put("int", int.class);
+        primitiveClasses.put("short", short.class);
+        primitiveClasses.put("long", long.class);
+        primitiveClasses.put("byte", byte.class);
+        primitiveClasses.put("char", char.class);
+        primitiveClasses.put("float", float.class);
+        primitiveClasses.put("double", double.class);
+        primitiveClasses.put("boolean", boolean.class);
+    }
+    
     private ModuleContextImpl moduleContext;
     
     public Instanciator(ModuleContextImpl moduleContext) {
@@ -116,26 +130,17 @@
         if (v instanceof NullValue) {
             return null;
         } else if (v instanceof TypedStringValue) {
-            TypedStringValue stringValue = (TypedStringValue) v;            
-            Class type = getType(stringValue.getTypeName());
+            TypedStringValue stringValue = (TypedStringValue) v; 
             String value = stringValue.getStringValue();
-            if (type != null) {
-                if (hint == null || hint.isAssignableFrom(type)) {
-                    return convert(value, type);
-                } else {
-                    throw new Exception("" + type + " " + hint);
-                }
-            } else if (hint != null) {
-                return convert(value, hint);
-            } else {
-                return value;
-            }
+            Class type = loadType(stringValue.getTypeName());
+            type = determineType(type, hint);
+            return convert(value, type);
         } else if (v instanceof ReferenceValue) {
             String componentName = ((ReferenceValue) v).getComponentName();
             return new ReferenceRecipe(componentName);
         } else if (v instanceof ListValue) {
             ListValue listValue = (ListValue) v;
-            Class type = getType(listValue.getValueType());
+            Class type = loadType(listValue.getValueType());
             CollectionRecipe cr = new CollectionRecipe(ArrayList.class);
             for (Value lv : (List<Value>) listValue.getList()) {
                 cr.add(getValue(lv, type));
@@ -143,7 +148,7 @@
             return cr;
         } else if (v instanceof SetValue) {
             SetValue setValue = (SetValue) v;
-            Class type = getType(setValue.getValueType());
+            Class type = loadType(setValue.getValueType());
             CollectionRecipe cr = new CollectionRecipe(HashSet.class);
             for (Value lv : (Set<Value>) setValue.getSet()) {
                 cr.add(getValue(lv, type));
@@ -151,8 +156,8 @@
             return cr;
         } else if (v instanceof MapValue) {
             MapValue mapValue = (MapValue) v;
-            Class keyType = getType(mapValue.getKeyType());
-            Class valueType = getType(mapValue.getValueType());            
+            Class keyType = loadType(mapValue.getKeyType());
+            Class valueType = loadType(mapValue.getValueType());            
             MapRecipe mr = new MapRecipe(HashMap.class);
             for (Map.Entry<Value,Value> entry : ((Map<Value,Value>) mapValue.getMap()).entrySet())
{
                 Object key = getValue(entry.getKey(), keyType);
@@ -162,10 +167,13 @@
             return mr;
         } else if (v instanceof ArrayValue) {
             ArrayValue arrayValue = (ArrayValue) v;
-            Class type = getType(arrayValue.getValueType());
-            
-            // TODO
-            throw new IllegalStateException("Unsupported value: " + v.getClass().getName());
+            Class type = loadType(arrayValue.getValueType());
+            type = determineType(type, hint);
+            ArrayRecipe ar = new ArrayRecipe(type);
+            for (Value value : arrayValue.getArray()) {
+                ar.add(getValue(value, type));
+            }
+            return ar;
         } else if (v instanceof ComponentValue) {
             return createRecipe(((ComponentValue) v).getComponentMetadata());
         } else if (v instanceof PropertiesValue) {
@@ -177,19 +185,38 @@
         }
     }
     
+    private Class determineType(Class type, Class hint) throws Exception {
+        if (type != null) {
+            if (hint == null || hint.isAssignableFrom(type)) {
+                return type;
+            } else {
+                throw new Exception(type.getName() + " cannot be assigned to " + hint.getName());
+            }
+        } else if (hint != null) {
+            return hint;
+        } else {
+            return Object.class;
+        }
+    }
+    
     protected Object convert(Object source, Class type) throws Exception {
         return moduleContext.getConversionService().convert(source, type);
     }
     
-    private Class getType(String typeName) throws ClassNotFoundException {
+    private Class loadType(String typeName) throws ClassNotFoundException {
         if (typeName == null) {
             return null;
         }
-        if (moduleContext == null) {
-            return Class.forName(typeName);            
-        } else {
-            return moduleContext.getBundleContext().getBundle().loadClass(typeName);
+
+        Class clazz = primitiveClasses.get(typeName);
+        if (clazz == null) {
+            if (moduleContext == null) {
+                clazz = Class.forName(typeName);            
+            } else {
+                clazz = moduleContext.getBundleContext().getBundle().loadClass(typeName);
+            }
         }
+        return clazz;
     }
     
     private class BundleObjectRecipe extends ObjectRecipe {

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/convert/ConversionServiceImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/convert/ConversionServiceImpl.java?rev=766041&r1=766040&r2=766041&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/convert/ConversionServiceImpl.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/convert/ConversionServiceImpl.java
Fri Apr 17 14:58:49 2009
@@ -45,6 +45,9 @@
     }
 
     public Object convert(Object fromValue, Class toType) throws Exception {
+        if (Object.class == toType) {
+            return fromValue;
+        }
         Converter converter = lookupConverter(toType);
         if (converter == null) {
             return convertDefault(fromValue, toType);

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/WiringTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/WiringTest.java?rev=766041&r1=766040&r2=766041&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/WiringTest.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/WiringTest.java
Fri Apr 17 14:58:49 2009
@@ -60,6 +60,12 @@
         assertEquals(new Integer(55), pojoa.getList().get(2));
         assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getList().get(3));
         
+        assertNotNull(pojoa.getArray());
+        assertEquals("list value", pojoa.getArray()[0]);
+        assertEquals(pojob, pojoa.getArray()[1]);
+        assertEquals(new Integer(55), pojoa.getArray()[2]);
+        assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getArray()[3]);
+        
         assertNotNull(pojoa.getSet());
         assertTrue(pojoa.getSet().contains("set value"));
         assertTrue(pojoa.getSet().contains(pojob));
@@ -70,6 +76,18 @@
         
         assertNotNull(pojoa.getNumber());
         assertEquals(new BigInteger("10"), pojoa.getNumber());
+        
+        assertNotNull(pojoa.getIntArray());
+        assertEquals(3, pojoa.getIntArray().length);
+        assertEquals(1, pojoa.getIntArray()[0]);
+        assertEquals(50, pojoa.getIntArray()[1]);
+        assertEquals(100, pojoa.getIntArray()[2]);
+        
+        assertNotNull(pojoa.getNumberArray());
+        assertEquals(3, pojoa.getNumberArray().length);
+        assertEquals(new Integer(1), pojoa.getNumberArray()[0]);
+        assertEquals(new BigInteger("50"), pojoa.getNumberArray()[1]);
+        assertEquals(new Long(100), pojoa.getNumberArray()[2]);
     }
 
     protected Parser parse(String name) throws Exception {

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/pojos/PojoA.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/pojos/PojoA.java?rev=766041&r1=766040&r2=766041&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/pojos/PojoA.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/pojos/PojoA.java
Fri Apr 17 14:58:49 2009
@@ -29,6 +29,10 @@
     private Set set;
     private Map map;
     private Number number;
+    
+    private Object[] array;
+    private int[] intArray;
+    private Number[] numberArray;
 
     public PojoA() {
     }
@@ -77,6 +81,30 @@
         return number;
     }
     
+    public void setArray(Object[] array) {
+        this.array = array;
+    }
+    
+    public Object[] getArray() {
+        return array;
+    }
+    
+    public int[] getIntArray() {
+        return intArray;
+    }
+    
+    public void setIntArray(int[] array) {
+        intArray = array;
+    }
+    
+    public Number[] getNumberArray() {
+        return numberArray;
+    }
+    
+    public void setNumberArray(Number[] numberArray) {
+        this.numberArray = numberArray;
+    }
+    
     public void start() {
         System.out.println("Starting component " + this);
     }

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-wiring.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-wiring.xml?rev=766041&r1=766040&r2=766041&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-wiring.xml
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-wiring.xml
Fri Apr 17 14:58:49 2009
@@ -30,6 +30,28 @@
                 <value type="java.net.URI">http://geronimo.apache.org</value>
             </list>
         </property>
+        <property name="array">
+            <array>
+                <value>list value</value>
+                <ref component="pojoB" />
+                <value type="java.lang.Integer">55</value>
+                <value type="java.net.URI">http://geronimo.apache.org</value>
+            </array>
+        </property>
+        <property name="intArray">
+            <array value-type="int">
+                <value>1</value>
+                <value>50</value>
+                <value>100</value>
+            </array>
+        </property>
+        <property name="numberArray">
+            <array value-type="java.lang.Number">
+                <value type="java.lang.Integer">1</value>
+                <value type="java.math.BigInteger">50</value>
+                <value type="java.lang.Long">100</value>
+            </array>
+        </property>
         <property name="number">
             <value type="java.math.BigInteger">10</value>
         </property>



Mime
View raw message