[jboss-osgi-commits] JBoss-OSGI SVN: r97235 - projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Tue Dec 1 09:23:16 EST 2009


Author: alesj
Date: 2009-12-01 09:23:16 -0500 (Tue, 01 Dec 2009)
New Revision: 97235

Modified:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/KernelDictionaryFactory.java
Log:
Dynamic OBJECTCLASS creation.

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/KernelDictionaryFactory.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/KernelDictionaryFactory.java	2009-12-01 14:22:49 UTC (rev 97234)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/KernelDictionaryFactory.java	2009-12-01 14:23:16 UTC (rev 97235)
@@ -21,10 +21,15 @@
 */
 package org.jboss.osgi.framework.bundle;
 
+import java.util.Collections;
 import java.util.Dictionary;
-import java.util.Hashtable;
+import java.util.Enumeration;
+import java.util.Set;
+import java.util.HashSet;
 
+import org.jboss.beans.info.spi.BeanInfo;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.util.collection.Iterators;
 import org.osgi.framework.Constants;
 
 /**
@@ -34,6 +39,8 @@
  */
 public class KernelDictionaryFactory implements DictionaryFactory<KernelControllerContext>
 {
+   private static final String[] EMPTY = new String[0];
+
    public Class<KernelControllerContext> getContextType()
    {
       return KernelControllerContext.class;
@@ -41,8 +48,97 @@
 
    public Dictionary<String, Object> getDictionary(KernelControllerContext context)
    {
-      Dictionary<String, Object> table = new Hashtable<String, Object>();
-      table.put(Constants.OBJECTCLASS, new String[0]);
-      return table;
+      return new KernelDictionary(context);
    }
+
+   private static class KernelDictionary extends Dictionary<String, Object>
+   {
+      private KernelControllerContext context;
+      private String[] classes = EMPTY;
+
+      private KernelDictionary(KernelControllerContext context)
+      {
+         this.context = context;
+      }
+
+      public int size()
+      {
+         return 1;
+      }
+
+      public boolean isEmpty()
+      {
+         return size() == 0;
+      }
+
+      @SuppressWarnings({"unchecked"})
+      public Enumeration<String> keys()
+      {
+         return Iterators.toEnumeration(Collections.singleton(Constants.OBJECTCLASS).iterator());
+      }
+
+      @SuppressWarnings({"unchecked"})
+      public Enumeration<Object> elements()
+      {
+         return Iterators.toEnumeration(Collections.singleton(classes).iterator());
+      }
+
+      public Object get(Object key)
+      {
+         if (Constants.OBJECTCLASS.equals(key))
+         {
+            if (classes == EMPTY)
+            {
+               Class<?> clazz = null;
+               BeanInfo info = context.getBeanInfo();
+               if (info != null)
+               {
+                  clazz = info.getClassInfo().getType();
+               }
+               else if (context.getTarget() != null)
+               {
+                  clazz = context.getTarget().getClass();
+               }
+
+               if (clazz != null)
+               {
+                  Set<String> clazzes = new HashSet<String>();
+                  traverseClass(clazz, clazzes);
+                  classes = clazzes.toArray(new String[clazzes.size()]);
+               }
+            }
+            return classes;
+         }
+         return null;
+      }
+
+      public Object put(String key, Object value)
+      {
+         return null;
+      }
+
+      public Object remove(Object key)
+      {
+         return null;
+      }
+
+      protected static void traverseClass(Class<?> clazz, Set<String> classes)
+      {
+         if (clazz == null || clazz == Object.class)
+         {
+            return;
+         }
+
+         classes.add(clazz.getName());
+
+         // traverse superclass
+         traverseClass(clazz.getSuperclass(), classes);
+         Class<?>[] interfaces = clazz.getInterfaces();
+         // traverse interfaces
+         for(Class<?> intface : interfaces)
+         {
+            traverseClass(intface, classes);
+         }
+      }
+   }
 }
\ No newline at end of file



More information about the jboss-osgi-commits mailing list