You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2009/04/17 21:15:25 UTC

svn commit: r766122 - in /geronimo/sandbox/blueprint/org.apache.felix.blueprint/src: main/java/org/apache/felix/blueprint/context/ main/java/org/apache/felix/blueprint/convert/ main/java/org/apache/felix/blueprint/namespace/ test/resources/

Author: gawor
Date: Fri Apr 17 19:15:24 2009
New Revision: 766122

URL: http://svn.apache.org/viewvc?rev=766122&view=rev
Log:
get type-converters working

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/context/ModuleContextImpl.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/main/java/org/apache/felix/blueprint/namespace/ComponentDefinitionRegistryImpl.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=766122&r1=766121&r2=766122&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 19:15:24 2009
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.blueprint.context;
 
+import java.lang.reflect.Type;
 import java.util.Set;
 import java.util.Collection;
 import java.util.ArrayList;
@@ -26,6 +27,8 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.felix.blueprint.namespace.ComponentDefinitionRegistryImpl;
+import org.apache.xbean.recipe.AbstractRecipe;
 import org.apache.xbean.recipe.ArrayRecipe;
 import org.apache.xbean.recipe.Option;
 import org.apache.xbean.recipe.Repository;
@@ -37,7 +40,6 @@
 import org.apache.xbean.recipe.ReferenceRecipe;
 import org.apache.xbean.recipe.Recipe;
 import org.osgi.framework.Bundle;
-import org.osgi.service.blueprint.namespace.ComponentDefinitionRegistry;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.osgi.service.blueprint.reflect.LocalComponentMetadata;
 import org.osgi.service.blueprint.reflect.PropertyInjectionMetadata;
@@ -89,10 +91,24 @@
         }
     }
     
-    public Repository createRepository(ComponentDefinitionRegistry registry) throws Exception {
+    public Repository createRepository(ComponentDefinitionRegistryImpl registry) throws Exception {
         Repository repository = new DefaultRepository();
         addBuiltinComponents(repository);
-        // Create recipes
+        
+        // Create type-converter recipes
+        for (Value value : registry.getTypeConverters()) {
+            if (value instanceof ComponentValue) {
+                Recipe recipe = (Recipe) getValue(value, null);
+                repository.add(recipe.getName(), recipe);
+            } else if (value instanceof ReferenceValue) {
+                ReferenceRecipe recipe = (ReferenceRecipe) getValue(value, null);
+                repository.add(recipe.getReferenceName(), recipe);
+            } else {
+                throw new RuntimeException("Unexpected converter type: " + value);
+            }
+        }
+        
+        // Create component recipes
         for (String name : (Set<String>) registry.getComponentDefinitionNames()) {
             ComponentMetadata component = registry.getComponentDefinition(name);
             Recipe recipe = createRecipe(component);
@@ -133,8 +149,7 @@
             TypedStringValue stringValue = (TypedStringValue) v; 
             String value = stringValue.getStringValue();
             Class type = loadType(stringValue.getTypeName());
-            type = determineType(type, hint);
-            return convert(value, type);
+            return new ValueRecipe(value, type, hint);
         } else if (v instanceof ReferenceValue) {
             String componentName = ((ReferenceValue) v).getComponentName();
             return new ReferenceRecipe(componentName);
@@ -185,12 +200,12 @@
         }
     }
     
-    private Class determineType(Class type, Class hint) throws Exception {
+    private static Class determineType(Class type, Class hint) throws RuntimeException {
         if (type != null) {
             if (hint == null || hint.isAssignableFrom(type)) {
                 return type;
             } else {
-                throw new Exception(type.getName() + " cannot be assigned to " + hint.getName());
+                throw new RuntimeException(type.getName() + " cannot be assigned to " + hint.getName());
             }
         } else if (hint != null) {
             return hint;
@@ -240,5 +255,34 @@
             }
         }
     }
+    
+    private class ValueRecipe extends AbstractRecipe {
+
+        private String value;
+        private Class type;
+        private Class hint;
+
+        private ValueRecipe(String value, Class type, Class hint) {
+            this.value = value;
+            this.type = type;
+            this.hint = hint;
+        }
+        
+        @Override
+        protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
+            Class myType = determineType(type, hint);
+            //System.out.println("create: " + expectedType + " " + type + " " + hint + " " + myType);
+            try {
+                return convert(value, myType);
+            } catch (Exception e) {
+                throw new ConstructionException(e);
+            }
+        }
+
+        public boolean canCreate(Type expectedType) {
+            return true;
+        }
+        
+    }
 
 }

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/ModuleContextImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/ModuleContextImpl.java?rev=766122&r1=766121&r2=766122&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/ModuleContextImpl.java (original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/ModuleContextImpl.java Fri Apr 17 19:15:24 2009
@@ -22,7 +22,9 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.net.URL;
 
@@ -42,10 +44,14 @@
 import org.osgi.service.blueprint.context.ModuleContext;
 import org.osgi.service.blueprint.context.NoSuchComponentException;
 import org.osgi.service.blueprint.convert.ConversionService;
+import org.osgi.service.blueprint.convert.Converter;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
+import org.osgi.service.blueprint.reflect.ComponentValue;
 import org.osgi.service.blueprint.reflect.LocalComponentMetadata;
+import org.osgi.service.blueprint.reflect.ReferenceValue;
 import org.osgi.service.blueprint.reflect.ServiceExportComponentMetadata;
 import org.osgi.service.blueprint.reflect.ServiceReferenceComponentMetadata;
+import org.osgi.service.blueprint.reflect.Value;
 
 /**
  * TODO: javadoc
@@ -60,7 +66,7 @@
     private final NamespaceHandlerRegistry handlers;
     private final List<URL> urls;
     private ComponentDefinitionRegistryImpl componentDefinitionRegistry;
-    private ConversionService conversionService;
+    private ConversionServiceImpl conversionService;
 
     public ModuleContextImpl(BundleContext bundleContext, ModuleContextEventSender sender, NamespaceHandlerRegistry handlers, List<URL> urls) {
         this.bundleContext = bundleContext;
@@ -99,7 +105,11 @@
             Instanciator i = new Instanciator(this);
             Repository repository = i.createRepository(componentDefinitionRegistry);
             ObjectGraph graph = new ObjectGraph(repository);
-            System.out.println(graph.createAll(new ArrayList<String>(componentDefinitionRegistry.getComponentDefinitionNames())));                    
+
+            registerTypeConverters(graph);
+            
+            System.out.println(graph.createAll(new ArrayList<String>(componentDefinitionRegistry.getComponentDefinitionNames())));
+                        
             sender.sendCreated(this);
         } catch (WaitForDependencyException e) {
             sender.sendWaiting(this, null, null); // TODO: give correct args
@@ -111,10 +121,25 @@
         }
     }
 
+    private void registerTypeConverters(ObjectGraph graph) {
+        List<String> typeConvertersNames = componentDefinitionRegistry.getTypeConverterNames();
+        Map<String, Object> typeConverters = graph.createAll(typeConvertersNames);
+        System.out.println(typeConverters);
+        for (String name : typeConvertersNames) {
+            Object typeConverterInstance = typeConverters.get(name);
+            if (typeConverterInstance instanceof Converter) {
+                Converter converter = (Converter) typeConverterInstance;
+                conversionService.registerConverter(converter);
+            } else {
+                // TODO: throw exception or log
+            }
+        }
+    }
+    
     public Set<String> getComponentNames() {
         return componentDefinitionRegistry.getComponentDefinitionNames();
     }
-
+    
     public Object getComponent(String name) throws NoSuchComponentException {
         ComponentMetadata metadata = getComponentMetadata(name);
         // TODO: get the component instance

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=766122&r1=766121&r2=766122&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 19:15:24 2009
@@ -44,6 +44,14 @@
         converters.add(converter);
     }
 
+    public void unregisterConverter(Converter converter) {
+        Class type = converter.getTargetClass();
+        List<Converter> converters = convertersMap.get(type);
+        if (converters != null) {
+            converters.remove(converter);
+        }
+    }
+    
     public Object convert(Object fromValue, Class toType) throws Exception {
         if (Object.class == toType) {
             return fromValue;

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/namespace/ComponentDefinitionRegistryImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/namespace/ComponentDefinitionRegistryImpl.java?rev=766122&r1=766121&r2=766122&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/namespace/ComponentDefinitionRegistryImpl.java (original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/namespace/ComponentDefinitionRegistryImpl.java Fri Apr 17 19:15:24 2009
@@ -28,6 +28,8 @@
 import org.osgi.service.blueprint.namespace.ComponentDefinitionRegistry;
 import org.osgi.service.blueprint.namespace.ComponentNameAlreadyInUseException;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
+import org.osgi.service.blueprint.reflect.ComponentValue;
+import org.osgi.service.blueprint.reflect.ReferenceValue;
 import org.osgi.service.blueprint.reflect.Value;
 
 /**
@@ -73,5 +75,25 @@
     public void registerTypeConverter(Value typeConverter) {
         typeConverters.add(typeConverter);
     }
+    
+    public List<Value> getTypeConverters() {
+        return Collections.unmodifiableList(typeConverters);
+    }
+    
+    public List<String> getTypeConverterNames() {
+        List<String> names = new ArrayList<String>();
+        for (Value value : typeConverters) {
+            if (value instanceof ComponentValue) {
+                ComponentValue componentValue = (ComponentValue) value;
+                names.add(componentValue.getComponentMetadata().getName());
+            } else if (value instanceof ReferenceValue) {
+                ReferenceValue referenceValue = (ReferenceValue) value;
+                names.add(referenceValue.getComponentName());
+            } else {
+                throw new RuntimeException("Unexpected converter type: " + value);
+            }
+        }
+        return names;
+    }
 
 }

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=766122&r1=766121&r2=766122&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 19:15:24 2009
@@ -4,6 +4,12 @@
             xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 ../../main/resources/org/apache/felix/blueprint/blueprint.xsd"
             default-availability="mandatory">
 
+    <type-converters>
+            <component id="converter1" class="org.apache.felix.blueprint.convert.TestConverter">
+                <property name="bundle" ref="bundle" />
+            </component>
+    </type-converters>
+    
     <component id="pojoB" class="org.apache.felix.blueprint.pojos.PojoB">
         <property name="uri" value="urn:myuri" />
     </component>