[jboss-osgi-commits] JBoss-OSGI SVN: r97341 - in projects/jboss-osgi/trunk/reactor/framework/src: test/java/org/jboss/test/osgi/service and 1 other directory.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Thu Dec 3 08:10:00 EST 2009


Author: alesj
Date: 2009-12-03 08:09:57 -0500 (Thu, 03 Dec 2009)
New Revision: 97341

Modified:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
   projects/jboss-osgi/trunk/reactor/framework/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java
Log:
Immutable empty dictonary.
OSGi service can have an alias via properties.
Test this alias.

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java	2009-12-03 12:57:01 UTC (rev 97340)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java	2009-12-03 13:09:57 UTC (rev 97341)
@@ -22,14 +22,16 @@
 package org.jboss.osgi.framework.bundle;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Dictionary;
-import java.util.Hashtable;
+import java.util.Enumeration;
 
 import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.logging.Logger;
 import org.jboss.metadata.spi.MetaData;
 import org.jboss.metadata.spi.scope.CommonLevels;
 import org.jboss.metadata.spi.scope.ScopeLevel;
+import org.jboss.util.collection.Iterators;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 
@@ -44,7 +46,7 @@
    private static final Logger log = Logger.getLogger(MDRUtils.class);
 
    /** The empty dictionary */
-   private static final Dictionary<String, Object> EMPTY = new Hashtable<String, Object>();
+   private static final Dictionary<String, Object> EMPTY = new EmptyDictonary<String, Object>();
 
    /**
     * Get metadata.
@@ -368,4 +370,43 @@
 
       return (thisServiceId > otherServiceId) ? -1 : 1;
    }
+
+   @SuppressWarnings({"unchecked"})
+   private static class EmptyDictonary<K, V> extends Dictionary<K, V>
+   {
+      public int size()
+      {
+         return 0;
+      }
+
+      public boolean isEmpty()
+      {
+         return true;
+      }
+
+      public Enumeration<K> keys()
+      {
+         return Iterators.toEnumeration(Collections.emptySet().iterator());
+      }
+
+      public Enumeration<V> elements()
+      {
+         return Iterators.toEnumeration(Collections.emptySet().iterator());
+      }
+
+      public V get(Object key)
+      {
+         return null;
+      }
+
+      public V put(K key, V value)
+      {
+         return null;
+      }
+
+      public V remove(Object key)
+      {
+         return null;
+      }
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2009-12-03 12:57:01 UTC (rev 97340)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2009-12-03 13:09:57 UTC (rev 97341)
@@ -49,6 +49,7 @@
 import org.jboss.osgi.framework.util.CaseInsensitiveDictionary;
 import org.jboss.osgi.spi.util.BundleClassLoader;
 import org.jboss.util.id.GUID;
+import org.jboss.util.collection.CollectionsFactory;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
@@ -68,6 +69,9 @@
  */
 public class OSGiServiceState extends AbstractControllerContext implements ServiceReference, ServiceRegistration, InvokeDispatchContext
 {
+   /** The alias constant */
+   private static final String SERVICE_ALIAS = "service.alias";
+
    /** The get classloader permission */
    private static final RuntimePermission GET_CLASSLOADER_PERMISSION = new RuntimePermission("getClassLoader");
 
@@ -114,7 +118,7 @@
    public OSGiServiceState(AbstractBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
    {
       // name is random / unique, we use aliases
-      super(GUID.asString(), OSGiControllerContextActions.ACTIONS, null, service);
+      super(GUID.asString(), getAlias(properties), OSGiControllerContextActions.ACTIONS, null, service);
 
       if (bundleState == null)
          throw new IllegalArgumentException("Null bundle state");
@@ -145,6 +149,35 @@
    }
 
    /**
+    * Check if there is an alias in properties.
+    *
+    * @param properties the properties
+    * @return alias or null
+    */
+   protected static Set<Object> getAlias(Dictionary<String, Object> properties)
+   {
+      if (properties != null)
+      {
+         Set<Object> aliases = null;
+         Enumeration<String> keys = properties.keys();
+         while (keys.hasMoreElements())
+         {
+            String key = keys.nextElement();
+            if (key.startsWith(SERVICE_ALIAS))
+            {
+               if (aliases == null)
+                  aliases = CollectionsFactory.createLazySet();
+
+               Object alias = properties.get(key);
+               aliases.add(alias);
+            }
+         }
+         return aliases;
+      }
+      return null;
+   }
+
+   /**
     * Get the serviceId.
     * 
     * @return the serviceId.

Modified: projects/jboss-osgi/trunk/reactor/framework/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java	2009-12-03 12:57:01 UTC (rev 97340)
+++ projects/jboss-osgi/trunk/reactor/framework/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java	2009-12-03 13:09:57 UTC (rev 97341)
@@ -168,6 +168,65 @@
       }
    }
 
+   public void testInjectionToMCNamedService() throws Throwable
+   {
+      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder("C", C.class.getName());
+      builder.addPropertyMetaData("a", builder.createInject("A"));
+      BeanMetaData bmd = builder.getBeanMetaData();
+      Deployment bean = addBean("beanA", C.class, bmd, A.class);
+      try
+      {
+         KernelControllerContext kcc = getControllerContext("C", null);
+
+         Bundle bundle1 = assembleBundle("simple2", "/bundles/service/service-bundle3");
+         try
+         {
+            bundle1.start();
+            BundleContext bundleContext1 = bundle1.getBundleContext();
+            assertNotNull(bundleContext1);
+
+            Class<?> aClass = bundle1.loadClass(A.class.getName());
+            Object a = aClass.newInstance();
+            Hashtable<String, Object> table = new Hashtable<String, Object>();
+            table.put("service.alias.1", "A");
+            ServiceRegistration reg1 = bundleContext1.registerService(A.class.getName(), a, table);
+            assertNotNull(reg1);
+            try
+            {
+               checkComplete();
+
+               Object c = getBean("C");
+               assertSame(a, invoke(c, "getA"));
+
+               ServiceReference ref1 = bundleContext1.getServiceReference(A.class.getName());
+               assertUsingBundles(ref1, LazyBundle.getBundle(getDeploymentUnit(bean)));
+
+               change(kcc, ControllerState.DESCRIBED);
+               // we did un-injection, should be removed now
+               assertUsingBundles(ref1);
+
+               change(kcc, ControllerState.INSTALLED);
+               assertEquals(ControllerState.INSTALLED, kcc.getState());
+            }
+            finally
+            {
+               reg1.unregister();
+            }
+
+            // check if the bean was unwinded as well
+            assertEquals(ControllerState.DESCRIBED, kcc.getState());
+         }
+         finally
+         {
+            uninstall(bundle1);
+         }
+      }
+      finally
+      {
+         undeploy(bean);
+      }
+   }
+
    public void testFiltering() throws Throwable
    {
       Deployment bean = addBean("beanA", A.class);



More information about the jboss-osgi-commits mailing list