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

svn commit: r766319 - in /geronimo/sandbox/blueprint: itests/src/test/java/org/apache/felix/blueprint/itests/ org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/ sample/src/main/resources/OSGI-INF/blueprint/

Author: gnodet
Date: Sat Apr 18 13:07:57 2009
New Revision: 766319

URL: http://svn.apache.org/viewvc?rev=766319&view=rev
Log:
Simple support for OSGi exported services

Modified:
    geronimo/sandbox/blueprint/itests/src/test/java/org/apache/felix/blueprint/itests/Test.java
    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/Parser.java
    geronimo/sandbox/blueprint/sample/src/main/resources/OSGI-INF/blueprint/config.xml

Modified: geronimo/sandbox/blueprint/itests/src/test/java/org/apache/felix/blueprint/itests/Test.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/itests/src/test/java/org/apache/felix/blueprint/itests/Test.java?rev=766319&r1=766318&r2=766319&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/itests/src/test/java/org/apache/felix/blueprint/itests/Test.java (original)
+++ geronimo/sandbox/blueprint/itests/src/test/java/org/apache/felix/blueprint/itests/Test.java Sat Apr 18 13:07:57 2009
@@ -39,15 +39,19 @@
         ModuleContext moduleContext = getOsgiService(ModuleContext.class, 5000);
         assertNotNull(moduleContext);
 
-        Object obj = moduleContext.getComponent("foo");
-        assertNotNull(obj);
-        assertEquals(Foo.class, obj.getClass());
-        obj = moduleContext.getComponent("bar");
+        Object obj = moduleContext.getComponent("bar");
         assertNotNull(obj);
         assertEquals(Bar.class, obj.getClass());
+        obj = moduleContext.getComponent("foo");
+        assertNotNull(obj);
+        assertEquals(Foo.class, obj.getClass());
 
         // TODO: components properties
 
+        Foo foo = getOsgiService(Foo.class, 5000);
+        assertNotNull(foo);
+        assertSame(foo, obj);
+
         bundle.stop();
         try {
             moduleContext = getOsgiService(ModuleContext.class, 1);

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=766319&r1=766318&r2=766319&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 Sat Apr 18 13:07:57 2009
@@ -25,8 +25,12 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.lang.reflect.Type;
 
 import org.apache.felix.blueprint.namespace.ComponentDefinitionRegistryImpl;
+import org.apache.felix.blueprint.reflect.ServiceExportComponentMetadataImpl;
 import org.apache.xbean.recipe.ArrayRecipe;
 import org.apache.xbean.recipe.CollectionRecipe;
 import org.apache.xbean.recipe.ConstructionException;
@@ -37,6 +41,8 @@
 import org.apache.xbean.recipe.Recipe;
 import org.apache.xbean.recipe.ReferenceRecipe;
 import org.apache.xbean.recipe.Repository;
+import org.apache.xbean.recipe.AbstractRecipe;
+import org.apache.xbean.recipe.RecipeHelper;
 import org.osgi.service.blueprint.convert.ConversionService;
 import org.osgi.service.blueprint.reflect.ArrayValue;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
@@ -52,7 +58,11 @@
 import org.osgi.service.blueprint.reflect.SetValue;
 import org.osgi.service.blueprint.reflect.TypedStringValue;
 import org.osgi.service.blueprint.reflect.Value;
+import org.osgi.service.blueprint.reflect.ServiceExportComponentMetadata;
 import org.osgi.service.blueprint.namespace.ComponentDefinitionRegistry;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 
 /**
  * TODO: javadoc
@@ -134,6 +144,10 @@
             // TODO: factory-method
             // TODO: factory-component
             return recipe;
+        } else if (component instanceof ServiceExportComponentMetadata) {
+            ExportedServiceRecipe recipe = new ExportedServiceRecipe((ServiceExportComponentMetadata) component);
+            recipe.setName(component.getName());
+            return recipe;
         } else {
             // TODO
             throw new IllegalStateException("Unsupported component " + component.getClass());
@@ -238,5 +252,80 @@
             }
         }
     }
+
+    /**
+     * TODO: section 5.4.1.5 : expose a proxy to the ServiceRegistration
+     */
+    private class ExportedServiceRecipe extends AbstractRecipe {
+
+        private final ServiceExportComponentMetadata metadata;
+        private ServiceRegistration serviceRegistration;
+
+        public ExportedServiceRecipe(ServiceExportComponentMetadata metadata) {
+            this.metadata = metadata;
+        }
+
+        public boolean canCreate(Type type) {
+            return true;
+        }
+
+        protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
+            // TODO: metadata.getRegistrationListeners()
+            try {
+                Object service = getValue(metadata.getExportedComponent(),  null);
+                service = RecipeHelper.convert(Object.class, service, false);
+                Set<String> classes;
+                switch (metadata.getAutoExportMode()) {
+                    case ServiceExportComponentMetadata.EXPORT_MODE_INTERFACES:
+                        classes = getImplementedInterfaces(new HashSet<String>(), service.getClass());
+                        break;
+                    case ServiceExportComponentMetadata.EXPORT_MODE_CLASS_HIERARCHY:
+                        classes = getSuperClasses(new HashSet<String>(), service.getClass());
+                        break;
+                    case ServiceExportComponentMetadata.EXPORT_MODE_ALL:
+                        classes = getSuperClasses(new HashSet<String>(), service.getClass());
+                        classes = getImplementedInterfaces(classes, service.getClass());
+                        break;
+                    default:
+                        classes = metadata.getInterfaceNames();
+                        break;
+                }
+                Map map = metadata.getServiceProperties();
+                if (map == null && metadata instanceof ServiceExportComponentMetadataImpl) {
+                    Object val = getValue(((ServiceExportComponentMetadataImpl) metadata).getServicePropertiesValue(), null);
+                    map = (Map) RecipeHelper.convert(Map.class, val, false); 
+                }
+                if (map == null) {
+                    map = new HashMap();
+                }
+                map.put(Constants.SERVICE_RANKING, metadata.getRanking());
+                String[] classesArray = classes.toArray(new String[classes.size()]);
+                serviceRegistration = moduleContext.getBundleContext().registerService(classesArray, service, new Hashtable(map));
+                return serviceRegistration;
+            } catch (Exception e) {
+                throw new ConstructionException(e);
+            }
+        }
+
+        private Set<String> getImplementedInterfaces(Set<String> classes, Class clazz) {
+            if (clazz != null && clazz != Object.class) {
+                for (Class itf : clazz.getInterfaces()) {
+                    classes.add(itf.getName());
+                    getImplementedInterfaces(classes, itf);
+                }
+                getImplementedInterfaces(classes, clazz.getSuperclass());
+            }
+            return classes;
+        }
+
+        private Set<String> getSuperClasses(Set<String> classes, Class clazz) {
+            if (clazz != null && clazz != Object.class) {
+                classes.add(clazz.getName());
+                getSuperClasses(classes, clazz.getSuperclass());
+            }
+            return classes;
+        }
+
+    }
             
 }

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Parser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Parser.java?rev=766319&r1=766318&r2=766319&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Parser.java (original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/context/Parser.java Sat Apr 18 13:07:57 2009
@@ -166,8 +166,8 @@
 
     public static final String AUTO_EXPORT_DISABLED = "disabled";
     public static final String AUTO_EXPORT_INTERFACES = "interfaces";
-    public static final String AUTO_EXPORT_CLASS_HIERARCHY = "class-hierachy";
-    public static final String AUTO_EXPORT_ALL = "all";
+    public static final String AUTO_EXPORT_CLASS_HIERARCHY = "class-hierarchy";
+    public static final String AUTO_EXPORT_ALL = "all-classes";
     public static final String AUTO_EXPORT_DEFAULT = AUTO_EXPORT_DISABLED;
     public static final String RANKING_DEFAULT = "0";
     public static final String AVAILABILITY_MANDATORY = "mandatory";
@@ -445,7 +445,7 @@
         } else if (AUTO_EXPORT_ALL.equals(autoExport)) {
             service.setAutoExportMode(ServiceExportComponentMetadata.EXPORT_MODE_ALL);
         } else {
-            throw new ComponentDefinitionException("Illegal value for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
+            throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
         }
         String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
         try {

Modified: geronimo/sandbox/blueprint/sample/src/main/resources/OSGI-INF/blueprint/config.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/sample/src/main/resources/OSGI-INF/blueprint/config.xml?rev=766319&r1=766318&r2=766319&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/sample/src/main/resources/OSGI-INF/blueprint/config.xml (original)
+++ geronimo/sandbox/blueprint/sample/src/main/resources/OSGI-INF/blueprint/config.xml Sat Apr 18 13:07:57 2009
@@ -33,6 +33,12 @@
     </component>
 
     <component id="converter2" class="org.apache.geronimo.osgi.example.CurrencyTypeConverter"/>
+
+    <service ref="foo" auto-export="all-classes">
+        <service-properties>
+            <entry key="key" value="value"/>
+        </service-properties>
+    </service>
     
 </components>