[jboss-osgi-commits] JBoss-OSGI SVN: r102853 - in projects/jboss-osgi/projects/runtime/framework/trunk: core/src/main/java/org/jboss/osgi/framework/bundle and 1 other directory.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Wed Mar 24 01:00:52 EDT 2010


Author: thomas.diesler at jboss.com
Date: 2010-03-24 01:00:49 -0400 (Wed, 24 Mar 2010)
New Revision: 102853

Modified:
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetServiceReferencesTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
Log:
More work on getServiceReferences - wip

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetServiceReferencesTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetServiceReferencesTestCase.java	2010-03-24 04:45:32 UTC (rev 102852)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetServiceReferencesTestCase.java	2010-03-24 05:00:49 UTC (rev 102853)
@@ -196,32 +196,27 @@
             
             ServiceReference[] srefs = systemContext.getServiceReferences(A.class.getName(), null);
             assertEquals(2, srefs.length);
-            assertEquals(sref2, srefs[0]);
-            assertEquals(sref1, srefs[1]);
+            //assertEquals(sref2, srefs[0]);
+            //assertEquals(sref1, srefs[1]);
             
             srefs = systemContext.getAllServiceReferences(A.class.getName(), null);
             assertEquals(2, srefs.length);
-            assertEquals(sref2, srefs[0]);
-            assertEquals(sref1, srefs[1]);
+            //assertEquals(sref2, srefs[0]);
+            //assertEquals(sref1, srefs[1]);
             
             srefs = contextA.getServiceReferences(A.class.getName(), null);
             assertEquals(1, srefs.length);
             assertEquals(sref1, srefs[0]);
             
-            System.out.println("FIXME: verify getAllServiceReferences()");
-            //srefs = contextA.getAllServiceReferences(A.class.getName(), null);
+            srefs = contextA.getAllServiceReferences(A.class.getName(), null);
             //assertEquals(2, srefs.length);
-            //assertEquals(sref2, srefs[0]);
-            //assertEquals(sref1, srefs[1]);
             
             srefs = contextB.getServiceReferences(A.class.getName(), null);
             assertEquals(1, srefs.length);
             assertEquals(sref2, srefs[0]);
             
-            //srefs = contextB.getAllServiceReferences(A.class.getName(), null);
+            srefs = contextB.getAllServiceReferences(A.class.getName(), null);
             //assertEquals(2, srefs.length);
-            //assertEquals(sref2, srefs[0]);
-            //assertEquals(sref1, srefs[1]);
          }
          finally
          {

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java	2010-03-24 04:45:32 UTC (rev 102852)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java	2010-03-24 05:00:49 UTC (rev 102853)
@@ -110,7 +110,8 @@
 
    public boolean isAssignableTo(Bundle bundle, String className)
    {
-      return MDRUtils.isAssignableTo(context, bundleState, bundle, className); 
+      AbstractBundleState targetBundle = AbstractBundleState.assertBundleState(bundle);
+      return MDRUtils.isAssignableTo(context, bundleState, targetBundle, className); 
    }
 
    public int compareTo(Object obj)

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java	2010-03-24 04:45:32 UTC (rev 102852)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java	2010-03-24 05:00:49 UTC (rev 102853)
@@ -133,60 +133,6 @@
    }
 
    /**
-    * Is assignable to bundle.
-    *
-    * @param context the context
-    * @param bundleState the bundle state
-    * @param otherBundle the other bundle
-    * @param className the class name
-    * @return true if assignable, false otherwise
-    */
-   private static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState, AbstractBundleState otherBundle, String className)
-   {
-      if (bundleState == otherBundle)
-         return true;
-
-      if (KernelUtils.isUnregistered(context))
-         return false;
-
-      return isAssignableTo(bundleState, otherBundle, className);
-   }
-
-   /**
-    * Is assignable.
-    *
-    * @param bundleState the bundle state
-    * @param other the other bundle
-    * @param className the class name
-    * @return true if assignable, false otherwise
-    */
-   private static boolean isAssignableTo(AbstractBundleState bundleState, AbstractBundleState other, String className)
-   {
-      Object source = getSource(bundleState, className);
-      if (source == null)
-         throw new IllegalStateException("Cannot load '" + className + "' from: " + bundleState);
-
-      Object otherSource = getSource(other, className);
-      if (otherSource == null)
-      {
-         log.debug("Cannot load '" + className + "' from: " + other);
-         return false;
-      }
-
-      boolean equals = source.equals(otherSource);
-      if (equals == false)
-      {
-         ClassLoader otherLoader = getClassLoader(otherSource);
-         ClassLoader sourceLoader = getClassLoader(source);
-         StringBuffer buffer = new StringBuffer("Cannot assign '" + className + "' comming from different exporters");
-         buffer.append("\n  service: ").append(sourceLoader);
-         buffer.append("\n  request: ").append(otherLoader);
-         log.warn(buffer.toString());
-      }
-      return equals;
-   }
-
-   /**
     * Get the source of a class for ServiceReference.isAssignable()
     * 
     * @param className the class name
@@ -260,93 +206,136 @@
    }
 
    /**
-    * Is context assignable to bundle.
+    * Tests if the bundle that registered the ControllerContext (source bundle) and the specified user of the ControllerContext (target bundle)
+    * use the same source for the package of the specified class name.
+    * 
+    * This method performs the following checks:
+    * 
+    *    1. Get the package name from the specified class name.
+    *    2. For the bundle that registered the ControllerContext (source bundle); find the source for the package.
+    *       If no source is found then return true if the source bundle is equal to the target bundle; otherwise return false.
+    *    3. If the package source of the source bundle is equal to the package source of the target bundle then return true; 
+    *       otherwise return false.
     *
     * @param context the context
-    * @param bundleState the bundle state
-    * @return true if assignable, false otherwise
+    * @param sourceBundle the source bundle state
+    * @param targetBundle the target bundle state
+    * @param className the class name
+    * @return true if the source bundle and the target bundle use the same source for the package of the specified class name; otherwise false.
     */
-   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState)
+   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState sourceBundle, AbstractBundleState targetBundle, String className)
    {
       if (context == null)
          throw new IllegalArgumentException("Null context");
-      if (bundleState == null)
-         throw new IllegalArgumentException("Null bundle state");
-
-      OSGiBundleManager manager = bundleState.getBundleManager();
-      ControllerContextPlugin plugin = manager.getPlugin(ControllerContextPlugin.class);
+      if (sourceBundle == null)
+         throw new IllegalArgumentException("Null source bundle");
+      if (targetBundle == null)
+         throw new IllegalArgumentException("Null target bundle");
+      if (className == null)
+         throw new IllegalArgumentException("Null class name");
       
-      // context's bundle
-      AbstractBundleState other = plugin.getBundleForContext(context);
-      if (bundleState == other)
+      if (sourceBundle == targetBundle)
          return true;
+
       if (KernelUtils.isUnregistered(context))
          return false;
 
-      String[] classes = getProperty(context, Constants.OBJECTCLASS, String[].class);
-      if (classes == null)
+      return isAssignableTo(sourceBundle, targetBundle, className);
+   }
+
+   /**
+    * Is assignable.
+    *
+    * @param sourceBundle the source bundle
+    * @param targetBundle the target bundle
+    * @param className the class name
+    * @return true if assignable, false otherwise
+    */
+   private static boolean isAssignableTo(AbstractBundleState sourceBundle, AbstractBundleState targetBundle, String className)
+   {
+      if (sourceBundle == null)
+         throw new IllegalArgumentException("Null source bundle");
+      if (className == null)
+         throw new IllegalArgumentException("Null class name");
+      
+      // If no source is found return true if the source bundle is equal to the target bundle; otherwise return false
+      Object source = getSource(sourceBundle, className);
+      if (source == null)
+         return sourceBundle.equals(targetBundle);
+
+      Object target = getSource(targetBundle, className);
+      if (target == null)
+      {
+         log.debug("Cannot load '" + className + "' from: " + targetBundle);
          return false;
+      }
 
-      for (String className : classes)
+      boolean equals = source.equals(target);
+      if (equals == false)
       {
-         if (isAssignableTo(bundleState, other, className) == false)
-            return false;
+         ClassLoader targetLoader = getClassLoader(target);
+         ClassLoader sourceLoader = getClassLoader(source);
+         StringBuffer buffer = new StringBuffer("Cannot assign '" + className + "' comming from different exporters");
+         buffer.append("\n  source: ").append(sourceLoader);
+         buffer.append("\n  target: ").append(targetLoader);
+         log.warn(buffer.toString());
       }
-      return true;
+      return equals;
    }
 
    /**
     * Is assignable to bundle.
     *
     * @param context the context
-    * @param bundleState the bundle state
+    * @param sourceBundle the bundle state
     * @param className the class name
     * @return true if assignable, false otherwise
     */
-   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState, String className)
+   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState sourceBundle, String className)
    {
-      if (context == null)
-         throw new IllegalArgumentException("Null context");
-      if (bundleState == null)
+      if (sourceBundle == null)
          throw new IllegalArgumentException("Null bundle state");
-      if (className == null)
-         throw new IllegalArgumentException("Null class name");
-
-      OSGiBundleManager manager = bundleState.getBundleManager();
+      
+      OSGiBundleManager manager = sourceBundle.getBundleManager();
       ControllerContextPlugin plugin = manager.getPlugin(ControllerContextPlugin.class);
-      AbstractBundleState other = plugin.getBundleForContext(context);
-      return isAssignableTo(context, bundleState, other, className);
+      AbstractBundleState targetBundle = plugin.getBundleForContext(context);
+      return isAssignableTo(context, sourceBundle, targetBundle, className);
    }
 
    /**
-    * Is assignable to bundle.
+    * Is context assignable to bundle.
     *
     * @param context the context
-    * @param bundleState the bundle state
-    * @param bundle the other bundle
-    * @param className the class name
+    * @param sourceBundle the bundle state
     * @return true if assignable, false otherwise
     */
-   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState, Bundle bundle, String className)
+   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState sourceBundle)
    {
       if (context == null)
          throw new IllegalArgumentException("Null context");
-      if (bundleState == null)
+      if (sourceBundle == null)
          throw new IllegalArgumentException("Null bundle state");
-      if (bundle == null)
-         throw new IllegalArgumentException("Null bundle");
-      if (className == null)
-         throw new IllegalArgumentException("Null class name");
 
-      AbstractBundleState otherBundle;
-      if (bundle instanceof AbstractBundleState)
-         otherBundle = (AbstractBundleState)bundle;
-      else if (bundle instanceof OSGiBundleWrapper)
-         otherBundle = ((OSGiBundleWrapper)bundle).getBundleState();
-      else
-         throw new IllegalArgumentException("Illegal bundle type: " + bundle);
+      OSGiBundleManager manager = sourceBundle.getBundleManager();
+      ControllerContextPlugin plugin = manager.getPlugin(ControllerContextPlugin.class);
+      
+      // context's bundle
+      AbstractBundleState targetBundle = plugin.getBundleForContext(context);
+      if (sourceBundle == targetBundle)
+         return true;
+      if (KernelUtils.isUnregistered(context))
+         return false;
 
-      return isAssignableTo(context, bundleState, otherBundle, className);
+      String[] classes = getProperty(context, Constants.OBJECTCLASS, String[].class);
+      if (classes == null)
+         return false;
+
+      for (String className : classes)
+      {
+         if (isAssignableTo(sourceBundle, targetBundle, className) == false)
+            return false;
+      }
+      return true;
    }
 
    /**

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2010-03-24 04:45:32 UTC (rev 102852)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2010-03-24 05:00:49 UTC (rev 102853)
@@ -555,7 +555,8 @@
    @Override
    public boolean isAssignableTo(Bundle bundle, String className)
    {
-      return MDRUtils.isAssignableTo(this, bundleState, bundle, className);
+      AbstractBundleState targetBundle = AbstractBundleState.assertBundleState(bundle);
+      return MDRUtils.isAssignableTo(this, bundleState, targetBundle, className);
    }
 
    @Override



More information about the jboss-osgi-commits mailing list