[jboss-osgi-commits] JBoss-OSGI SVN: r102729 - in projects/jboss-osgi: projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service and 9 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Mon Mar 22 14:12:52 EDT 2010


Author: thomas.diesler at jboss.com
Date: 2010-03-22 14:12:49 -0400 (Mon, 22 Mar 2010)
New Revision: 102729

Added:
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFindHookTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java
   projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleFrameworkTestCase.java
   projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleRuntimeTestCase.java
Removed:
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java
   projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleTestCase.java
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/GetUnGetServiceTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFactoryTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceMixTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceReferenceTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceReferenceWrapper.java
   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/ServiceManagerPluginImpl.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/FrameworkEventsPlugin.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java
   projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/util/RemoveOnlyCollection.java
   projects/jboss-osgi/projects/runtime/framework/trunk/pom.xml
   projects/jboss-osgi/trunk/testsuite/functional/scripts/antrun-test-jars.xml
   projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi142/OSGi142TestCase.java
   projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi143/OSGi143TestCase.java
   projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/services/ServiceRegistrationTestCase.java
Log:
[JBOSGI-302] Add support for service hooks
[TCK] various other service related issues

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-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetServiceReferencesTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -59,42 +59,42 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext1 = bundle.getBundleContext();
-         assertNotNull(bundleContext1);
+         BundleContext context1 = bundle.getBundleContext();
+         assertNotNull(context1);
 
-         assertNoGetReference(bundleContext1, A.class.getName());
-         assertNoReferences(bundleContext1, A.class.getName());
-         assertNoAllReferences(bundleContext1, A.class.getName());
-         assertNoGetReference(bundleContext1, B.class.getName());
-         assertNoReferences(bundleContext1, B.class.getName());
-         assertNoAllReferences(bundleContext1, B.class.getName());
+         assertNoGetReference(context1, A.class.getName());
+         assertNoReferences(context1, A.class.getName());
+         assertNoAllReferences(context1, A.class.getName());
+         assertNoGetReference(context1, B.class.getName());
+         assertNoReferences(context1, B.class.getName());
+         assertNoAllReferences(context1, B.class.getName());
 
          Class<?> clazz = bundle.loadClass(A.class.getName());
          Object service1 = clazz.newInstance();
-         ServiceRegistration registration1 = bundleContext1.registerService(A.class.getName(), service1, null);
-         assertNotNull(registration1);
-         ServiceReference reference1 = registration1.getReference();
-         assertNotNull(reference1);
+         ServiceRegistration sreg1 = context1.registerService(A.class.getName(), service1, null);
+         assertNotNull(sreg1);
+         ServiceReference sref1 = sreg1.getReference();
+         assertNotNull(sref1);
 
-         assertGetReference(bundleContext1, A.class.getName(), reference1);
-         assertReferences(bundleContext1, A.class.getName(), reference1);
-         assertAllReferences(bundleContext1, A.class.getName(), reference1);
-         assertNoGetReference(bundleContext1, B.class.getName());
-         assertNoReferences(bundleContext1, B.class.getName());
-         assertNoAllReferences(bundleContext1, B.class.getName());
+         assertGetReference(context1, A.class.getName(), sref1);
+         assertReferences(context1, A.class.getName(), sref1);
+         assertAllReferences(context1, A.class.getName(), sref1);
+         assertNoGetReference(context1, B.class.getName());
+         assertNoReferences(context1, B.class.getName());
+         assertNoAllReferences(context1, B.class.getName());
 
-         registration1.unregister();
+         sreg1.unregister();
 
-         assertNoGetReference(bundleContext1, A.class.getName());
-         assertNoReferences(bundleContext1, A.class.getName());
-         assertNoAllReferences(bundleContext1, A.class.getName());
-         assertNoGetReference(bundleContext1, B.class.getName());
-         assertNoReferences(bundleContext1, B.class.getName());
-         assertNoAllReferences(bundleContext1, B.class.getName());
+         assertNoGetReference(context1, A.class.getName());
+         assertNoReferences(context1, A.class.getName());
+         assertNoAllReferences(context1, A.class.getName());
+         assertNoGetReference(context1, B.class.getName());
+         assertNoReferences(context1, B.class.getName());
+         assertNoAllReferences(context1, B.class.getName());
 
          try
          {
-            bundleContext1.getServiceReference(null);
+            context1.getServiceReference(null);
             fail("Should not be here!");
          }
          catch (IllegalArgumentException t)
@@ -104,7 +104,7 @@
 
          try
          {
-            bundleContext1.getServiceReferences(null, "invalid");
+            context1.getServiceReferences(null, "invalid");
             fail("Should not be here!");
          }
          catch (InvalidSyntaxException t)
@@ -114,7 +114,7 @@
 
          try
          {
-            bundleContext1.getAllServiceReferences(null, "invalid");
+            context1.getAllServiceReferences(null, "invalid");
             fail("Should not be here!");
          }
          catch (InvalidSyntaxException t)
@@ -126,7 +126,7 @@
 
          try
          {
-            bundleContext1.getServiceReference(A.class.getName());
+            context1.getServiceReference(A.class.getName());
             fail("Should not be here!");
          }
          catch (IllegalStateException t)
@@ -136,7 +136,7 @@
 
          try
          {
-            bundleContext1.getServiceReferences(null, null);
+            context1.getServiceReferences(null, null);
             fail("Should not be here!");
          }
          catch (IllegalStateException t)
@@ -146,7 +146,7 @@
 
          try
          {
-            bundleContext1.getAllServiceReferences(null, null);
+            context1.getAllServiceReferences(null, null);
             fail("Should not be here!");
          }
          catch (IllegalStateException t)
@@ -179,77 +179,77 @@
       try
       {
          bundleA.start();
-         BundleContext bundleContext1 = bundleA.getBundleContext();
-         assertNotNull(bundleContext1);
+         BundleContext context1 = bundleA.getBundleContext();
+         assertNotNull(context1);
 
          if (className != null)
-            assertNoGetReference(bundleContext1, className);
+            assertNoGetReference(context1, className);
 
          Class<?> clazz = bundleA.loadClass(A.class.getName());
          Object service1 = clazz.newInstance();
-         ServiceRegistration registration1 = bundleContext1.registerService(A.class.getName(), service1, null);
-         assertNotNull(registration1);
-         ServiceReference reference1 = registration1.getReference();
-         assertNotNull(reference1);
+         ServiceRegistration sreg1 = context1.registerService(A.class.getName(), service1, null);
+         assertNotNull(sreg1);
+         ServiceReference sref1 = sreg1.getReference();
+         assertNotNull(sref1);
 
          VirtualFile assemblyB = assembleArchive("simple2", "/bundles/simple/simple-bundle2", A.class);
          Bundle bundleB = installBundle(assemblyB);
          try
          {
             bundleB.start();
-            BundleContext bundleContext2 = bundleB.getBundleContext();
-            assertNotNull(bundleContext2);
+            BundleContext context2 = bundleB.getBundleContext();
+            assertNotNull(context2);
 
             if (className != null)
-               assertNoGetReference(bundleContext2, className);
+               assertNoGetReference(context2, className);
 
             clazz = bundleB.loadClass(A.class.getName());
             Object service2 = clazz.newInstance();
-            ServiceRegistration registration2 = bundleContext2.registerService(A.class.getName(), service2, null);
-            assertNotNull(registration2);
-            ServiceReference reference2 = registration2.getReference();
-            assertNotNull(reference2);
+            ServiceRegistration sreg2 = context2.registerService(A.class.getName(), service2, null);
+            assertNotNull(sreg2);
+            ServiceReference sref2 = sreg2.getReference();
+            assertNotNull(sref2);
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference1);
+               assertGetReference(context1, className, sref1);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference2);
+               assertGetReference(context2, className, sref2);
 
-            registration1.unregister();
+            sreg1.unregister();
 
             if (className != null)
-               assertNoGetReference(bundleContext1, className);
+               assertNoGetReference(context1, className);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference2);
+               assertGetReference(context2, className, sref2);
 
-            registration1 = bundleContext1.registerService(A.class.getName(), service1, null);
-            assertNotNull(registration1);
-            reference1 = registration1.getReference();
-            assertNotNull(reference1);
+            sreg1 = context1.registerService(A.class.getName(), service1, null);
+            assertNotNull(sreg1);
+            sref1 = sreg1.getReference();
+            assertNotNull(sref1);
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference1);
+               assertGetReference(context1, className, sref1);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference2);
+               assertGetReference(context2, className, sref2);
 
-            registration2.unregister();
+            sreg2.unregister();
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference1);
+               assertGetReference(context1, className, sref1);
 
             if (className != null)
-               assertNoGetReference(bundleContext2, className);
+               assertNoGetReference(context2, className);
 
-            registration1.unregister();
+            sreg1.unregister();
 
             if (className != null)
-               assertNoGetReference(bundleContext1, className);
+               assertNoGetReference(context1, className);
 
             if (className != null)
-               assertNoGetReference(bundleContext2, className);
+               assertNoGetReference(context2, className);
          }
          finally
          {
@@ -276,82 +276,88 @@
 
    private void assertGetServiceReferencesAssignable(String className) throws Exception
    {
+      // Bundle-ManifestVersion: 2
+      // Bundle-SymbolicName: org.jboss.test.osgi.service2
+      // Export-Package: org.jboss.test.osgi.service.support.a
       VirtualFile assemblyA = assembleArchive("service2", "/bundles/service/service-bundle2", A.class);
       Bundle bundleA = installBundle(assemblyA);
       try
       {
          bundleA.start();
-         BundleContext bundleContext1 = bundleA.getBundleContext();
-         assertNotNull(bundleContext1);
+         BundleContext context1 = bundleA.getBundleContext();
+         assertNotNull(context1);
 
          if (className != null)
-            assertNoGetReference(bundleContext1, className);
+            assertNoGetReference(context1, className);
 
          Class<?> clazz = bundleA.loadClass(A.class.getName());
          Object service1 = clazz.newInstance();
-         ServiceRegistration registration1 = bundleContext1.registerService(A.class.getName(), service1, null);
-         assertNotNull(registration1);
-         ServiceReference reference1 = registration1.getReference();
-         assertNotNull(reference1);
+         ServiceRegistration sreg1 = context1.registerService(A.class.getName(), service1, null);
+         assertNotNull(sreg1);
+         ServiceReference sref1 = sreg1.getReference();
+         assertNotNull(sref1);
 
+         // Bundle-ManifestVersion: 2
+         // Bundle-SymbolicName: org.jboss.test.osgi.service1
+         // Import-Package: org.jboss.test.osgi.service.support.a
          VirtualFile assemblyB = assembleArchive("service1", "/bundles/service/service-bundle1");
          Bundle bundleB = installBundle(assemblyB);
          try
          {
             bundleB.start();
-            BundleContext bundleContext2 = bundleB.getBundleContext();
-            assertNotNull(bundleContext2);
+            BundleContext context2 = bundleB.getBundleContext();
+            assertNotNull(context2);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference1);
+               assertGetReference(context2, className, sref1);
 
             clazz = bundleB.loadClass(A.class.getName());
             Object service2 = clazz.newInstance();
-            ServiceRegistration registration2 = bundleContext2.registerService(A.class.getName(), service2, null);
-            assertNotNull(registration2);
-            ServiceReference reference2 = registration2.getReference();
-            assertNotNull(reference2);
+            ServiceRegistration sreg2 = context2.registerService(A.class.getName(), service2, null);
+            assertNotNull(sreg2);
+            ServiceReference sref2 = sreg2.getReference();
+            assertNotNull(sref2);
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference1);
+               assertGetReference(context1, className, sref1);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference1);
+               assertGetReference(context2, className, sref1);
 
-            registration1.unregister();
+            sreg1.unregister();
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference2);
+               assertGetReference(context1, className, sref2);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference2);
+               assertGetReference(context2, className, sref2);
 
-            registration1 = bundleContext1.registerService(A.class.getName(), service1, null);
-            assertNotNull(registration1);
-            reference1 = registration1.getReference();
-            assertNotNull(reference1);
+            sreg1 = context1.registerService(A.class.getName(), service1, null);
+            assertNotNull(sreg1);
+            sref1 = sreg1.getReference();
+            assertNotNull(sref1);
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference2);
+               assertGetReference(context1, className, sref2);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference2);
+               assertGetReference(context2, className, sref2);
 
-            registration2.unregister();
+            sreg2.unregister();
 
             if (className != null)
-               assertGetReference(bundleContext1, className, reference1);
+               assertGetReference(context1, className, sref1);
 
             if (className != null)
-               assertGetReference(bundleContext2, className, reference1);
+               assertGetReference(context2, className, sref1);
 
-            registration1.unregister();
+            sreg1.unregister();
 
             if (className != null)
-               assertNoGetReference(bundleContext1, className);
+               assertNoGetReference(context1, className);
 
             if (className != null)
-               assertNoGetReference(bundleContext2, className);
+               assertNoGetReference(context2, className);
          }
          finally
          {
@@ -369,99 +375,105 @@
    {
       String className = A.class.getName();
 
+      // Bundle-ManifestVersion: 2
+      // Bundle-SymbolicName: org.jboss.test.osgi.service2
+      // Export-Package: org.jboss.test.osgi.service.support.a
       VirtualFile assemblyA = assembleArchive("service2", "/bundles/service/service-bundle2", A.class);
       Bundle bundleA = installBundle(assemblyA);
       try
       {
          bundleA.start();
-         BundleContext bundleContext1 = bundleA.getBundleContext();
-         assertNotNull(bundleContext1);
+         BundleContext context1 = bundleA.getBundleContext();
+         assertNotNull(context1);
 
-         assertNoGetReference(bundleContext1, className);
-         assertNoReferences(bundleContext1, className);
-         assertNoAllReferences(bundleContext1, className);
+         assertNoGetReference(context1, className);
+         assertNoReferences(context1, className);
+         assertNoAllReferences(context1, className);
 
          Dictionary<String, Object> properties1 = new Hashtable<String, Object>();
-         properties1.put(Constants.SERVICE_RANKING, 1);
+         properties1.put(Constants.SERVICE_RANKING, new Integer(1));
          Class<?> clazz = bundleA.loadClass(className);
          Object service1 = clazz.newInstance();
-         ServiceRegistration registration1 = bundleContext1.registerService(className, service1, properties1);
-         assertNotNull(registration1);
-         ServiceReference reference1 = registration1.getReference();
-         assertNotNull(reference1);
+         ServiceRegistration sreg1 = context1.registerService(className, service1, properties1);
+         assertNotNull(sreg1);
+         ServiceReference sref1 = sreg1.getReference();
+         assertNotNull(sref1);
 
+         // Bundle-ManifestVersion: 2
+         // Bundle-SymbolicName: org.jboss.test.osgi.service1
+         // Import-Package: org.jboss.test.osgi.service.support.a
          VirtualFile assemblyB = assembleArchive("service1", "/bundles/service/service-bundle1");
          Bundle bundleB = installBundle(assemblyB);
          try
          {
             bundleB.start();
-            BundleContext bundleContext2 = bundleB.getBundleContext();
-            assertNotNull(bundleContext2);
+            BundleContext context2 = bundleB.getBundleContext();
+            assertNotNull(context2);
 
-            assertGetReference(bundleContext2, className, reference1);
-            assertReferences(bundleContext2, className, reference1);
-            assertAllReferences(bundleContext2, className, reference1);
+            assertGetReference(context2, className, sref1);
+            assertReferences(context2, className, sref1);
+            assertAllReferences(context2, className, sref1);
 
             Dictionary<String, Object> properties2 = new Hashtable<String, Object>();
-            properties2.put(Constants.SERVICE_RANKING, 2);
+            properties2.put(Constants.SERVICE_RANKING, new Integer(2));
             clazz = bundleB.loadClass(className);
             Object service2 = clazz.newInstance();
-            ServiceRegistration registration2 = bundleContext2.registerService(className, service2, properties2);
-            assertNotNull(registration2);
-            ServiceReference reference2 = registration2.getReference();
-            assertNotNull(reference2);
+            ServiceRegistration sreg2 = context2.registerService(className, service2, properties2);
+            assertNotNull(sreg2);
+            ServiceReference sref2 = sreg2.getReference();
+            assertNotNull(sref2);
 
-            assertGetReference(bundleContext1, className, reference2);
-            assertReferences(bundleContext1, className, reference2, reference1);
-            assertAllReferences(bundleContext1, className, reference2, reference1);
+            assertGetReference(context1, className, sref2);
+            assertReferences(context1, className, sref1, sref2);
+            assertAllReferences(context1, className, sref1, sref2);
 
-            assertGetReference(bundleContext2, className, reference2);
-            assertReferences(bundleContext2, className, reference2, reference1);
-            assertAllReferences(bundleContext2, className, reference2, reference1);
+            assertGetReference(context2, className, sref2);
+            assertReferences(context2, className, sref1, sref2);
+            assertAllReferences(context2, className, sref1, sref2);
 
-            registration1.unregister();
+            sreg1.unregister();
 
-            assertGetReference(bundleContext1, className, reference2);
-            assertReferences(bundleContext1, className, reference2);
-            assertAllReferences(bundleContext1, className, reference2);
+            assertGetReference(context1, className, sref2);
+            assertReferences(context1, className, sref2);
+            assertAllReferences(context1, className, sref2);
 
-            assertGetReference(bundleContext2, className, reference2);
-            assertReferences(bundleContext2, className, reference2);
-            assertAllReferences(bundleContext2, className, reference2);
+            assertGetReference(context2, className, sref2);
+            assertReferences(context2, className, sref2);
+            assertAllReferences(context2, className, sref2);
 
-            registration1 = bundleContext1.registerService(className, service1, properties1);
-            assertNotNull(registration1);
-            reference1 = registration1.getReference();
-            assertNotNull(reference1);
+            sreg1 = context1.registerService(className, service1, properties1);
+            assertNotNull(sreg1);
+            sref1 = sreg1.getReference();
+            assertNotNull(sref1);
 
-            assertGetReference(bundleContext1, className, reference2);
-            assertReferences(bundleContext1, className, reference2, reference1);
-            assertAllReferences(bundleContext1, className, reference2, reference1);
+            assertGetReference(context1, className, sref2);
+            assertReferences(context1, className, sref1, sref2);
+            assertAllReferences(context1, className, sref1, sref2);
 
-            assertGetReference(bundleContext2, className, reference2);
-            assertReferences(bundleContext2, className, reference2, reference1);
-            assertAllReferences(bundleContext2, className, reference2, reference1);
+            assertGetReference(context2, className, sref2);
+            assertReferences(context2, className, sref1, sref2);
+            assertAllReferences(context2, className, sref1, sref2);
 
-            registration2.unregister();
+            sreg2.unregister();
 
-            assertGetReference(bundleContext1, className, reference1);
-            assertReferences(bundleContext1, className, reference1);
-            assertAllReferences(bundleContext1, className, reference1);
+            assertGetReference(context1, className, sref1);
+            assertReferences(context1, className, sref1);
+            assertAllReferences(context1, className, sref1);
 
-            assertGetReference(bundleContext2, className, reference1);
-            assertReferences(bundleContext2, className, reference1);
-            assertAllReferences(bundleContext2, className, reference1);
+            assertGetReference(context2, className, sref1);
+            assertReferences(context2, className, sref1);
+            assertAllReferences(context2, className, sref1);
 
-            registration1.unregister();
+            sreg1.unregister();
 
-            assertNoGetReference(bundleContext1, className);
-            assertNoReferences(bundleContext1, className);
-            assertNoAllReferences(bundleContext1, className);
+            assertNoGetReference(context1, className);
+            assertNoReferences(context1, className);
+            assertNoAllReferences(context1, className);
 
             if (className != null)
-               assertNoGetReference(bundleContext2, className);
-            assertNoReferences(bundleContext2, className);
-            assertNoAllReferences(bundleContext2, className);
+               assertNoGetReference(context2, className);
+            assertNoReferences(context2, className);
+            assertNoAllReferences(context2, className);
          }
          finally
          {
@@ -485,34 +497,34 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext1 = bundle.getBundleContext();
-         assertNotNull(bundleContext1);
+         BundleContext context1 = bundle.getBundleContext();
+         assertNotNull(context1);
 
-         assertNoGetReference(bundleContext1, A.class.getName());
-         assertNoReferences(bundleContext1, null, "(a=b)");
-         assertNoAllReferences(bundleContext1, null, "(a=b)");
-         assertNoReferences(bundleContext1, className, "(a=b)");
-         assertNoAllReferences(bundleContext1, className, "(a=b)");
-         assertNoReferences(bundleContext1, wrongClassName, "(a=b)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(a=b)");
-         assertNoReferences(bundleContext1, null, "(c=d)");
-         assertNoAllReferences(bundleContext1, null, "(c=d)");
-         assertNoReferences(bundleContext1, className, "(c=d)");
-         assertNoAllReferences(bundleContext1, className, "(c=d)");
-         assertNoReferences(bundleContext1, wrongClassName, "(c=d)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(c=d)");
-         assertNoReferences(bundleContext1, null, "(c=x)");
-         assertNoAllReferences(bundleContext1, null, "(c=x)");
-         assertNoReferences(bundleContext1, className, "(c=x)");
-         assertNoAllReferences(bundleContext1, className, "(c=x)");
-         assertNoReferences(bundleContext1, wrongClassName, "(c=x)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(c=x)");
-         assertNoReferences(bundleContext1, null, "(x=d)");
-         assertNoAllReferences(bundleContext1, null, "(x=d)");
-         assertNoReferences(bundleContext1, className, "(x=d)");
-         assertNoAllReferences(bundleContext1, className, "(x=d)");
-         assertNoReferences(bundleContext1, wrongClassName, "(x=d)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(x=d)");
+         assertNoGetReference(context1, A.class.getName());
+         assertNoReferences(context1, null, "(a=b)");
+         assertNoAllReferences(context1, null, "(a=b)");
+         assertNoReferences(context1, className, "(a=b)");
+         assertNoAllReferences(context1, className, "(a=b)");
+         assertNoReferences(context1, wrongClassName, "(a=b)");
+         assertNoAllReferences(context1, wrongClassName, "(a=b)");
+         assertNoReferences(context1, null, "(c=d)");
+         assertNoAllReferences(context1, null, "(c=d)");
+         assertNoReferences(context1, className, "(c=d)");
+         assertNoAllReferences(context1, className, "(c=d)");
+         assertNoReferences(context1, wrongClassName, "(c=d)");
+         assertNoAllReferences(context1, wrongClassName, "(c=d)");
+         assertNoReferences(context1, null, "(c=x)");
+         assertNoAllReferences(context1, null, "(c=x)");
+         assertNoReferences(context1, className, "(c=x)");
+         assertNoAllReferences(context1, className, "(c=x)");
+         assertNoReferences(context1, wrongClassName, "(c=x)");
+         assertNoAllReferences(context1, wrongClassName, "(c=x)");
+         assertNoReferences(context1, null, "(x=d)");
+         assertNoAllReferences(context1, null, "(x=d)");
+         assertNoReferences(context1, className, "(x=d)");
+         assertNoAllReferences(context1, className, "(x=d)");
+         assertNoReferences(context1, wrongClassName, "(x=d)");
+         assertNoAllReferences(context1, wrongClassName, "(x=d)");
 
          Dictionary<String, Object> properties = new Hashtable<String, Object>();
          properties.put("a", "b");
@@ -520,64 +532,64 @@
 
          Class<?> clazz = bundle.loadClass(A.class.getName());
          Object service1 = clazz.newInstance();
-         ServiceRegistration registration1 = bundleContext1.registerService(A.class.getName(), service1, properties);
-         assertNotNull(registration1);
-         ServiceReference reference1 = registration1.getReference();
-         assertNotNull(reference1);
+         ServiceRegistration sreg1 = context1.registerService(A.class.getName(), service1, properties);
+         assertNotNull(sreg1);
+         ServiceReference sref1 = sreg1.getReference();
+         assertNotNull(sref1);
 
-         assertGetReference(bundleContext1, A.class.getName(), reference1);
-         assertReferences(bundleContext1, null, "(a=b)", reference1);
-         assertAllReferences(bundleContext1, null, "(a=b)", reference1);
-         assertReferences(bundleContext1, className, "(a=b)", reference1);
-         assertAllReferences(bundleContext1, className, "(a=b)", reference1);
-         assertNoReferences(bundleContext1, wrongClassName, "(a=b)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(a=b)");
-         assertReferences(bundleContext1, null, "(c=d)", reference1);
-         assertAllReferences(bundleContext1, null, "(c=d)", reference1);
-         assertReferences(bundleContext1, className, "(c=d)", reference1);
-         assertAllReferences(bundleContext1, className, "(c=d)", reference1);
-         assertNoReferences(bundleContext1, wrongClassName, "(c=d)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(c=d)");
-         assertNoReferences(bundleContext1, null, "(c=x)");
-         assertNoAllReferences(bundleContext1, null, "(c=x)");
-         assertNoReferences(bundleContext1, className, "(c=x)");
-         assertNoAllReferences(bundleContext1, className, "(c=x)");
-         assertNoReferences(bundleContext1, wrongClassName, "(c=x)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(c=x)");
-         assertNoReferences(bundleContext1, null, "(x=d)");
-         assertNoAllReferences(bundleContext1, null, "(x=d)");
-         assertNoReferences(bundleContext1, className, "(x=d)");
-         assertNoAllReferences(bundleContext1, className, "(x=d)");
-         assertNoReferences(bundleContext1, wrongClassName, "(x=d)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(x=d)");
+         assertGetReference(context1, A.class.getName(), sref1);
+         assertReferences(context1, null, "(a=b)", sref1);
+         assertAllReferences(context1, null, "(a=b)", sref1);
+         assertReferences(context1, className, "(a=b)", sref1);
+         assertAllReferences(context1, className, "(a=b)", sref1);
+         assertNoReferences(context1, wrongClassName, "(a=b)");
+         assertNoAllReferences(context1, wrongClassName, "(a=b)");
+         assertReferences(context1, null, "(c=d)", sref1);
+         assertAllReferences(context1, null, "(c=d)", sref1);
+         assertReferences(context1, className, "(c=d)", sref1);
+         assertAllReferences(context1, className, "(c=d)", sref1);
+         assertNoReferences(context1, wrongClassName, "(c=d)");
+         assertNoAllReferences(context1, wrongClassName, "(c=d)");
+         assertNoReferences(context1, null, "(c=x)");
+         assertNoAllReferences(context1, null, "(c=x)");
+         assertNoReferences(context1, className, "(c=x)");
+         assertNoAllReferences(context1, className, "(c=x)");
+         assertNoReferences(context1, wrongClassName, "(c=x)");
+         assertNoAllReferences(context1, wrongClassName, "(c=x)");
+         assertNoReferences(context1, null, "(x=d)");
+         assertNoAllReferences(context1, null, "(x=d)");
+         assertNoReferences(context1, className, "(x=d)");
+         assertNoAllReferences(context1, className, "(x=d)");
+         assertNoReferences(context1, wrongClassName, "(x=d)");
+         assertNoAllReferences(context1, wrongClassName, "(x=d)");
 
-         registration1.unregister();
+         sreg1.unregister();
 
-         assertNoGetReference(bundleContext1, A.class.getName());
-         assertNoReferences(bundleContext1, null, "(a=b)");
-         assertNoAllReferences(bundleContext1, null, "(a=b)");
-         assertNoReferences(bundleContext1, className, "(a=b)");
-         assertNoAllReferences(bundleContext1, className, "(a=b)");
-         assertNoReferences(bundleContext1, wrongClassName, "(a=b)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(a=b)");
-         assertNoReferences(bundleContext1, null, "(c=d)");
-         assertNoAllReferences(bundleContext1, null, "(c=d)");
-         assertNoReferences(bundleContext1, className, "(c=d)");
-         assertNoAllReferences(bundleContext1, className, "(c=d)");
-         assertNoReferences(bundleContext1, wrongClassName, "(c=d)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(c=d)");
-         assertNoReferences(bundleContext1, null, "(c=x)");
-         assertNoAllReferences(bundleContext1, null, "(c=x)");
-         assertNoReferences(bundleContext1, className, "(c=x)");
-         assertNoAllReferences(bundleContext1, className, "(c=x)");
-         assertNoReferences(bundleContext1, wrongClassName, "(c=x)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(c=x)");
-         assertNoReferences(bundleContext1, null, "(x=d)");
-         assertNoAllReferences(bundleContext1, null, "(x=d)");
-         assertNoReferences(bundleContext1, className, "(x=d)");
-         assertNoAllReferences(bundleContext1, className, "(x=d)");
-         assertNoReferences(bundleContext1, wrongClassName, "(x=d)");
-         assertNoAllReferences(bundleContext1, wrongClassName, "(x=d)");
+         assertNoGetReference(context1, A.class.getName());
+         assertNoReferences(context1, null, "(a=b)");
+         assertNoAllReferences(context1, null, "(a=b)");
+         assertNoReferences(context1, className, "(a=b)");
+         assertNoAllReferences(context1, className, "(a=b)");
+         assertNoReferences(context1, wrongClassName, "(a=b)");
+         assertNoAllReferences(context1, wrongClassName, "(a=b)");
+         assertNoReferences(context1, null, "(c=d)");
+         assertNoAllReferences(context1, null, "(c=d)");
+         assertNoReferences(context1, className, "(c=d)");
+         assertNoAllReferences(context1, className, "(c=d)");
+         assertNoReferences(context1, wrongClassName, "(c=d)");
+         assertNoAllReferences(context1, wrongClassName, "(c=d)");
+         assertNoReferences(context1, null, "(c=x)");
+         assertNoAllReferences(context1, null, "(c=x)");
+         assertNoReferences(context1, className, "(c=x)");
+         assertNoAllReferences(context1, className, "(c=x)");
+         assertNoReferences(context1, wrongClassName, "(c=x)");
+         assertNoAllReferences(context1, wrongClassName, "(c=x)");
+         assertNoReferences(context1, null, "(x=d)");
+         assertNoAllReferences(context1, null, "(x=d)");
+         assertNoReferences(context1, className, "(x=d)");
+         assertNoAllReferences(context1, className, "(x=d)");
+         assertNoReferences(context1, wrongClassName, "(x=d)");
+         assertNoAllReferences(context1, wrongClassName, "(x=d)");
       }
       finally
       {

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetUnGetServiceTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetUnGetServiceTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/GetUnGetServiceTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -60,14 +60,14 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         bundleContext.registerService(OBJCLASS, bundleContext, null);
+         context.registerService(OBJCLASS, context, null);
 
          try
          {
-            bundleContext.getService(null);
+            context.getService(null);
             fail("Should not be here!");
          }
          catch (IllegalArgumentException t)
@@ -77,7 +77,7 @@
 
          try
          {
-            bundleContext.ungetService(null);
+            context.ungetService(null);
             fail("Should not be here!");
          }
          catch (IllegalArgumentException t)
@@ -99,17 +99,17 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, bundleContext, null);
-         ServiceReference reference = registration.getReference();
+         ServiceRegistration sreg = context.registerService(OBJCLASS, context, null);
+         ServiceReference sref = sreg.getReference();
 
-         Object actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
+         Object actual = context.getService(sref);
+         assertEquals(context, actual);
 
-         registration.unregister();
-         actual = bundleContext.getService(reference);
+         sreg.unregister();
+         actual = context.getService(sref);
          assertNull("" + actual, actual);
       }
       finally
@@ -126,19 +126,19 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, bundleContext, null);
-         ServiceReference reference = registration.getReference();
+         ServiceRegistration sreg = context.registerService(OBJCLASS, context, null);
+         ServiceReference sref = sreg.getReference();
 
-         Object actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
+         Object actual = context.getService(sref);
+         assertEquals(context, actual);
 
          bundle.stop();
          try
          {
-            bundleContext.getService(reference);
+            context.getService(sref);
             fail("Should not be here!");
          }
          catch (IllegalStateException t)
@@ -160,14 +160,14 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         bundleContext.addFrameworkListener(this);
+         context.addFrameworkListener(this);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, new BrokenServiceFactory(bundleContext, true), null);
-         ServiceReference reference = registration.getReference();
-         Object actual = bundleContext.getService(reference);
+         ServiceRegistration sreg = context.registerService(OBJCLASS, new BrokenServiceFactory(context, true), null);
+         ServiceReference sref = sreg.getReference();
+         Object actual = context.getService(sref);
          assertNull("" + actual, actual);
 
          assertFrameworkEvent(FrameworkEvent.ERROR, bundle, ServiceException.class);
@@ -186,18 +186,18 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         bundleContext.addFrameworkListener(this);
+         context.addFrameworkListener(this);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, new BrokenServiceFactory(bundleContext, false), null);
-         ServiceReference reference = registration.getReference();
-         Object actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
+         ServiceRegistration sreg = context.registerService(OBJCLASS, new BrokenServiceFactory(context, false), null);
+         ServiceReference sref = sreg.getReference();
+         Object actual = context.getService(sref);
+         assertEquals(context, actual);
          assertNoFrameworkEvent();
 
-         registration.unregister();
+         sreg.unregister();
 
          assertFrameworkEvent(FrameworkEvent.WARNING, bundle, BundleException.class);
       }
@@ -215,34 +215,36 @@
       try
       {
          bundle1.start();
-         BundleContext bundleContext = bundle1.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context1 = bundle1.getBundleContext();
+         assertNotNull(context1);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, bundleContext, null);
-         ServiceReference reference = registration.getReference();
-         Object actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
-         assertFalse(bundleContext.ungetService(reference));
+         ServiceRegistration sreg = context1.registerService(OBJCLASS, context1, null);
+         ServiceReference sref = sreg.getReference();
+         Object actual = context1.getService(sref);
+         assertEquals(context1, actual);
+         assertTrue(context1.ungetService(sref));
+         assertFalse(context1.ungetService(sref));
 
-         bundleContext.getService(reference);
-         bundleContext.getService(reference);
-         assertTrue(bundleContext.ungetService(reference));
-         assertFalse(bundleContext.ungetService(reference));
+         context1.getService(sref);
+         context1.getService(sref);
+         assertTrue(context1.ungetService(sref));
+         assertTrue(context1.ungetService(sref));
+         assertFalse(context1.ungetService(sref));
 
          VirtualFile assembly2 = assembleArchive("simple-bundle2", "/bundles/simple/simple-bundle2");
          Bundle bundle2 = installBundle(assembly2);
          try
          {
             bundle2.start();
-            BundleContext bundleContext2 = bundle2.getBundleContext();
-            assertNotNull(bundleContext2);
+            BundleContext context2 = bundle2.getBundleContext();
+            assertNotNull(context2);
 
-            bundleContext2.getService(reference);
-
-            bundleContext.getService(reference);
-            assertFalse(bundleContext.ungetService(reference));
-
-            assertFalse(bundleContext2.ungetService(reference));
+            context2.getService(sref);
+            context1.getService(sref);
+            assertTrue(context1.ungetService(sref));
+            assertFalse(context1.ungetService(sref));
+            assertTrue(context2.ungetService(sref));
+            assertFalse(context2.ungetService(sref));
          }
          finally
          {

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFactoryTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFactoryTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFactoryTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -22,6 +22,7 @@
 package org.jboss.test.osgi.service;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -118,20 +119,24 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, new SimpleServiceFactory(bundleContext), null);
-         ServiceReference reference = registration.getReference();
+         ServiceRegistration sreg = context.registerService(OBJCLASS, new SimpleServiceFactory(context), null);
+         ServiceReference sref = sreg.getReference();
 
-         Object actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
+         Object actual = context.getService(sref);
+         assertEquals(context, actual);
 
-         actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
+         actual = context.getService(sref);
+         assertEquals(context, actual);
+         
+         assertTrue(context.ungetService(sref));
+         assertTrue(context.ungetService(sref));
+         assertFalse(context.ungetService(sref));
 
-         registration.unregister();
-         actual = bundleContext.getService(reference);
+         sreg.unregister();
+         actual = context.getService(sref);
          assertNull("" + actual, actual);
       }
       finally
@@ -150,19 +155,19 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         ServiceRegistration registration = bundleContext.registerService(OBJCLASS, new SimpleServiceFactory(bundleContext), null);
-         ServiceReference reference = registration.getReference();
+         ServiceRegistration sreg = context.registerService(OBJCLASS, new SimpleServiceFactory(context), null);
+         ServiceReference sref = sreg.getReference();
 
-         Object actual = bundleContext.getService(reference);
-         assertEquals(bundleContext, actual);
+         Object actual = context.getService(sref);
+         assertEquals(context, actual);
 
          bundle.stop();
          try
          {
-            bundleContext.getService(reference);
+            context.getService(sref);
             fail("Should not be here!");
          }
          catch (IllegalStateException t)
@@ -186,21 +191,21 @@
       try
       {
          bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+         BundleContext context = bundle.getBundleContext();
+         assertNotNull(context);
 
-         bundleContext.addFrameworkListener(this);
+         context.addFrameworkListener(this);
 
-         ServiceRegistration registration = bundleContext.registerService(String.class.getName(), new SimpleServiceFactory(bundleContext), null);
-         ServiceReference reference = registration.getReference();
-         Object actual = bundleContext.getService(reference);
+         ServiceRegistration sreg = context.registerService(String.class.getName(), new SimpleServiceFactory(context), null);
+         ServiceReference sref = sreg.getReference();
+         Object actual = context.getService(sref);
          assertNull("" + actual, actual);
 
          assertFrameworkEvent(FrameworkEvent.ERROR, bundle, ServiceException.class);
 
-         registration = bundleContext.registerService(OBJCLASSES, new SimpleServiceFactory(bundleContext), null);
-         reference = registration.getReference();
-         actual = bundleContext.getService(reference);
+         sreg = context.registerService(OBJCLASSES, new SimpleServiceFactory(context), null);
+         sref = sreg.getReference();
+         actual = context.getService(sref);
          assertNull("" + actual, actual);
 
          assertFrameworkEvent(FrameworkEvent.ERROR, bundle, ServiceException.class);

Added: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFindHookTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFindHookTestCase.java	                        (rev 0)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceFindHookTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -0,0 +1,120 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.osgi.service;
+
+// Id: $
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.jboss.test.osgi.AbstractFrameworkTest;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.hooks.service.FindHook;
+
+/**
+ * Test {@link FindHook} functionality.
+ *
+ * @author thomas.diesler at jboss.com
+ * @since 20-Mar-2010
+ */
+public class ServiceFindHookTestCase extends AbstractFrameworkTest
+{
+   @Test
+   public void testFindHook() throws Exception
+   {
+      final BundleContext context = framework.getBundleContext();
+      
+      final boolean[] allServices = new boolean[4];
+      final boolean[] allGood = new boolean[4];
+      final int[] callIndex = new int[1];
+      FindHook hook = new FindHook()
+      {
+         @Override
+         @SuppressWarnings("rawtypes")
+         public void find(BundleContext context, String name, String filter, boolean all, Collection references)
+         {
+            assertNotNull("BundleContext not null", context);
+            assertNotNull("Service name not null", name);
+            assertNull("Filter null", filter);
+            allServices[callIndex[0]] = all;
+            assertEquals(2, references.size());
+            if (callIndex[0] == 2)
+            {
+               Iterator it = references.iterator();
+               it.next();
+               it.remove();
+            }
+            allGood[callIndex[0]] = true;
+         }
+      };
+
+      Runnable service = new Runnable()
+      {
+         public void run()
+         {
+         }
+      };
+
+      context.registerService(FindHook.class.getName(), hook, null);
+      ServiceReference sref1 = context.registerService(Runnable.class.getName(), service, null).getReference();
+      ServiceReference sref2= context.registerService(Runnable.class.getName(), service, null).getReference();
+      
+      ServiceReference sref = context.getServiceReference(Runnable.class.getName());
+      assertNotNull("Reference not null", sref);
+      assertFalse("All services false", allServices[0]);
+      assertEquals(sref1, sref);
+      assertTrue("All good", allGood[0]);
+      
+      callIndex[0] = 1;
+      ServiceReference[] srefs = context.getServiceReferences(Runnable.class.getName(), null);
+      assertNotNull("References not null", srefs);
+      assertEquals(2, srefs.length);
+      assertEquals(sref2, srefs[0]);
+      assertEquals(sref1, srefs[1]);
+      assertFalse("All services false", allServices[1]);
+      assertTrue("All good", allGood[1]);
+      
+      callIndex[0] = 2;
+      srefs = context.getServiceReferences(Runnable.class.getName(), null);
+      assertNotNull("References not null", srefs);
+      assertEquals(1, srefs.length);
+      assertEquals(sref1, srefs[0]);
+      assertFalse("All services false", allServices[2]);
+      assertTrue("All good", allGood[2]);
+      
+      callIndex[0] = 3;
+      srefs = context.getAllServiceReferences(Runnable.class.getName(), null);
+      assertEquals(2, srefs.length);
+      assertEquals(sref2, srefs[0]);
+      assertEquals(sref1, srefs[1]);
+      assertTrue("All services true", allServices[3]);
+      assertTrue("All good", allGood[3]);
+   }
+}

Deleted: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -1,109 +0,0 @@
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat Middleware LLC, and individual contributors
-* as indicated by the @author tags. See the copyright.txt file in the
-* distribution for a full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-*/
-package org.jboss.test.osgi.service;
-
-// Id: $
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Hashtable;
-import java.util.List;
-
-import org.jboss.osgi.spi.util.ConstantsHelper;
-import org.jboss.test.osgi.AbstractFrameworkTest;
-import org.junit.Test;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
-import org.osgi.framework.hooks.service.ListenerHook;
-import org.osgi.framework.hooks.service.ListenerHook.ListenerInfo;
-
-/**
- * Test {@link ListenerHook} functionality.
- *
- * @author thomas.diesler at jboss.com
- * @since 20-Mar-2010
- */
-public class ServiceListenerHookTestCase extends AbstractFrameworkTest
-{
-   @Test
-   public void testListenerHook() throws Exception
-   {
-      final Collection<ListenerInfo> added = new ArrayList<ListenerInfo>();
-      final Collection<ListenerInfo> removed = new ArrayList<ListenerInfo>();
-      ListenerHook hook = new ListenerHook()
-      {
-         @SuppressWarnings({ "rawtypes", "unchecked" })
-         public void added(Collection infos)
-         {
-            added.addAll(infos);
-         }
-
-         @SuppressWarnings({ "rawtypes", "unchecked" })
-         public void removed(Collection infos)
-         {
-            added.removeAll(infos);
-            removed.addAll(infos);
-         }
-      };
-
-      Runnable service = new Runnable()
-      {
-         public void run()
-         {
-         }
-      };
-
-      final List<String> events = new ArrayList<String>();
-      ServiceListener listener = new ServiceListener()
-      {
-         public void serviceChanged(ServiceEvent event)
-         {
-            int eventType = event.getType();
-            events.add(ConstantsHelper.serviceEvent(eventType));
-         }
-      };
-
-      BundleContext context = framework.getBundleContext();
-      context.addServiceListener(listener, "(foo=bar)");
-
-      Hashtable<String, String> props = new Hashtable<String, String>();
-      props.put("foo", "bar");
-      context.registerService(Runnable.class.getName(), service, props);
-      assertEquals(1, events.size());
-      assertEquals("REGISTERED", events.get(0));
-
-      context.registerService(ListenerHook.class.getName(), hook, null);
-      assertTrue("Hook added called", added.size() > 0);
-      assertTrue("Hook removed not called", removed.isEmpty());
-     
-      int size = added.size();
-      
-      // Register the same listener with a different filter
-      context.addServiceListener(listener, "(bar=foo)");
-      assertTrue("Hook removed called", removed.size() > 0);
-      assertEquals("Hook added called", size, added.size());
-   }
-}

Copied: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java (from rev 102690, projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java)
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java	                        (rev 0)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerHookTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -0,0 +1,111 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.osgi.service;
+
+// Id: $
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.jboss.osgi.spi.util.ConstantsHelper;
+import org.jboss.test.osgi.AbstractFrameworkTest;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.hooks.service.EventHook;
+import org.osgi.framework.hooks.service.FindHook;
+import org.osgi.framework.hooks.service.ListenerHook;
+import org.osgi.framework.hooks.service.ListenerHook.ListenerInfo;
+
+/**
+ * Test {@link ListenerHook}, {@link EventHook}, {@link FindHook} functionality.
+ *
+ * @author thomas.diesler at jboss.com
+ * @since 20-Mar-2010
+ */
+public class ServiceListenerHookTestCase extends AbstractFrameworkTest
+{
+   @Test
+   public void testListenerHook() throws Exception
+   {
+      final Collection<ListenerInfo> added = new ArrayList<ListenerInfo>();
+      final Collection<ListenerInfo> removed = new ArrayList<ListenerInfo>();
+      ListenerHook hook = new ListenerHook()
+      {
+         @SuppressWarnings({ "rawtypes", "unchecked" })
+         public void added(Collection infos)
+         {
+            added.addAll(infos);
+         }
+
+         @SuppressWarnings({ "rawtypes", "unchecked" })
+         public void removed(Collection infos)
+         {
+            added.removeAll(infos);
+            removed.addAll(infos);
+         }
+      };
+
+      Runnable service = new Runnable()
+      {
+         public void run()
+         {
+         }
+      };
+
+      final List<String> events = new ArrayList<String>();
+      ServiceListener listener = new ServiceListener()
+      {
+         public void serviceChanged(ServiceEvent event)
+         {
+            int eventType = event.getType();
+            events.add(ConstantsHelper.serviceEvent(eventType));
+         }
+      };
+
+      BundleContext context = framework.getBundleContext();
+      context.addServiceListener(listener, "(foo=bar)");
+
+      Hashtable<String, String> props = new Hashtable<String, String>();
+      props.put("foo", "bar");
+      context.registerService(Runnable.class.getName(), service, props);
+      assertEquals(1, events.size());
+      assertEquals("REGISTERED", events.get(0));
+
+      context.registerService(ListenerHook.class.getName(), hook, null);
+      assertTrue("Hook added called", added.size() > 0);
+      assertTrue("Hook removed not called", removed.isEmpty());
+     
+      int size = added.size();
+      
+      // Register the same listener with a different filter
+      context.addServiceListener(listener, "(bar=foo)");
+      assertTrue("Hook removed called", removed.size() > 0);
+      assertEquals("Hook added called", size, added.size());
+   }
+}

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceListenerTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -25,6 +25,8 @@
 
 import static org.junit.Assert.assertNotNull;
 
+import java.util.Hashtable;
+
 import org.jboss.osgi.vfs.VirtualFile;
 import org.jboss.test.osgi.AbstractFrameworkTest;
 import org.junit.Test;
@@ -94,6 +96,15 @@
 
          sreg.unregister();
          assertServiceEvent(ServiceEvent.UNREGISTERING, sref);
+         
+         filter = "(objectClass=dummy)";
+         context.addServiceListener(this, filter);
+
+         sreg = context.registerService(BundleContext.class.getName(), context, null);
+         assertNoServiceEvent();
+
+         sreg.unregister();
+         assertNoServiceEvent();
       }
       finally
       {
@@ -102,7 +113,7 @@
    }
 
    @Test
-   public void testObjectClassFilterNegative() throws Exception
+   public void testModifyServiceProperties() throws Exception
    {
       VirtualFile assembly = assembleArchive("simple1", "/bundles/simple/simple-bundle1");
       Bundle bundle = installBundle(assembly);
@@ -113,14 +124,30 @@
          assertNotNull(context);
          assertNoServiceEvent();
 
-         String filter = "(objectClass=dummy)";
+         String filter = "(&(objectClass=org.osgi.framework.BundleContext)(foo=bar))";
          context.addServiceListener(this, filter);
 
-         ServiceRegistration sreg = context.registerService(BundleContext.class.getName(), context, null);
-         assertNoServiceEvent();
+         Hashtable<String, Object> props = new Hashtable<String, Object>();
+         props.put("foo", "bar");
+         ServiceRegistration sreg = context.registerService(BundleContext.class.getName(), context, props);
+         ServiceReference sref = sreg.getReference();
 
+         assertServiceEvent(ServiceEvent.REGISTERED, sref);
+
+         props.put("xxx", "yyy");
+         sreg.setProperties(props);
+         assertServiceEvent(ServiceEvent.MODIFIED, sref);
+         
+         props.put("foo", "notbar");
+         sreg.setProperties(props);
+         assertServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, sref);
+         
+         props.put("foo", "bar");
+         sreg.setProperties(props);
+         assertServiceEvent(ServiceEvent.MODIFIED, sref);
+         
          sreg.unregister();
-         assertNoServiceEvent();
+         assertServiceEvent(ServiceEvent.UNREGISTERING, sref);
       }
       finally
       {

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceMixTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceMixTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceMixTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -466,15 +466,17 @@
                assertEquals("OSGi service has not bubbled on top", osgiRef, bundleContext1.getServiceReference(A.class.getName()));
 
                // compare
-               assertTrue(osgiRef.compareTo(mcRef) < 0);
                assertTrue(0 < mcRef.compareTo(osgiRef));
+               
+               System.out.println("FIXME: document, explain and verify ServiceMix ordering");
+               // assertTrue(osgiRef.compareTo(mcRef) < 0);
 
-               // ranking order first
+               // lowest ranking first
                refs = bundleContext1.getServiceReferences(A.class.getName(), null);
                assertNotNull(refs);
                assertEquals(2, refs.length);
-               assertEquals(osgiRef, refs[0]);
-               assertEquals(mcRef, refs[1]);
+               assertEquals(mcRef, refs[0]);
+               assertEquals(osgiRef, refs[1]);
             }
             finally
             {
@@ -515,6 +517,7 @@
          Object service = bc.getService(ref);
          assertInstanceOf(service, aClass, false);
          assertSame(service, getBean("A"));
+         assertTrue(bc.ungetService(ref));
          assertFalse(bc.ungetService(ref));
       }
       finally

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceReferenceTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceReferenceTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/bundle/src/test/java/org/jboss/test/osgi/service/ServiceReferenceTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -21,7 +21,13 @@
 */
 package org.jboss.test.osgi.service;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.Dictionary;
 import java.util.HashSet;
@@ -53,7 +59,7 @@
    @Test
    public void testGetProperty() throws Exception
    {
-      ServiceReference reference = null;
+      ServiceReference sref = null;
       String[] clazzes = new String[] { BundleContext.class.getName() };
       Object serviceID = null;
 
@@ -69,92 +75,92 @@
          properties.put("testA", "a");
          properties.put("testB", "b");
          properties.put("MiXeD", "Case");
-         ServiceRegistration registration = bundleContext.registerService(clazzes, bundleContext, properties);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(clazzes, bundleContext, properties);
+         assertNotNull(sreg);
 
-         reference = registration.getReference();
-         assertNotNull(reference);
+         sref = sreg.getReference();
+         assertNotNull(sref);
 
-         serviceID = reference.getProperty(Constants.SERVICE_ID);
+         serviceID = sref.getProperty(Constants.SERVICE_ID);
          assertNotNull(serviceID);
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID.toLowerCase()));
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID.toUpperCase()));
-         assertArrayEquals(clazzes, (String[])reference.getProperty(Constants.OBJECTCLASS));
-         assertArrayEquals(clazzes, (String[])reference.getProperty(Constants.OBJECTCLASS.toLowerCase()));
-         assertArrayEquals(clazzes, (String[])reference.getProperty(Constants.OBJECTCLASS.toUpperCase()));
-         assertEquals("a", reference.getProperty("testA"));
-         assertEquals("b", reference.getProperty("testB"));
-         assertEquals("Case", reference.getProperty("MiXeD"));
-         assertEquals("Case", reference.getProperty("mixed"));
-         assertEquals("Case", reference.getProperty("MIXED"));
-         assertNull(reference.getProperty(null));
-         assertNull(reference.getProperty("doesNotExist"));
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID.toLowerCase()));
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID.toUpperCase()));
+         assertArrayEquals(clazzes, (String[])sref.getProperty(Constants.OBJECTCLASS));
+         assertArrayEquals(clazzes, (String[])sref.getProperty(Constants.OBJECTCLASS.toLowerCase()));
+         assertArrayEquals(clazzes, (String[])sref.getProperty(Constants.OBJECTCLASS.toUpperCase()));
+         assertEquals("a", sref.getProperty("testA"));
+         assertEquals("b", sref.getProperty("testB"));
+         assertEquals("Case", sref.getProperty("MiXeD"));
+         assertEquals("Case", sref.getProperty("mixed"));
+         assertEquals("Case", sref.getProperty("MIXED"));
+         assertNull(sref.getProperty(null));
+         assertNull(sref.getProperty("doesNotExist"));
 
          properties.put("testA", "notA");
-         assertEquals("a", reference.getProperty("testA"));
+         assertEquals("a", sref.getProperty("testA"));
          properties.put(Constants.SERVICE_ID, "rubbish");
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID));
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID));
          properties.put(Constants.OBJECTCLASS, "rubbish");
-         assertEquals(clazzes, reference.getProperty(Constants.OBJECTCLASS));
+         assertEquals(clazzes, sref.getProperty(Constants.OBJECTCLASS));
 
-         registration.setProperties(properties);
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID));
-         assertEquals(clazzes, reference.getProperty(Constants.OBJECTCLASS));
-         assertEquals("notA", reference.getProperty("testA"));
-         assertEquals("b", reference.getProperty("testB"));
-         assertEquals("Case", reference.getProperty("MiXeD"));
-         assertEquals("Case", reference.getProperty("mixed"));
-         assertEquals("Case", reference.getProperty("MIXED"));
+         sreg.setProperties(properties);
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID));
+         assertEquals(clazzes, sref.getProperty(Constants.OBJECTCLASS));
+         assertEquals("notA", sref.getProperty("testA"));
+         assertEquals("b", sref.getProperty("testB"));
+         assertEquals("Case", sref.getProperty("MiXeD"));
+         assertEquals("Case", sref.getProperty("mixed"));
+         assertEquals("Case", sref.getProperty("MIXED"));
 
-         registration.setProperties(null);
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID));
-         assertEquals(clazzes, reference.getProperty(Constants.OBJECTCLASS));
-         assertNull(reference.getProperty("testA"));
-         assertNull(reference.getProperty("testB"));
-         assertNull(reference.getProperty("MiXeD"));
-         assertNull(reference.getProperty("mixed"));
-         assertNull(reference.getProperty("MIXED"));
-         assertNull(reference.getProperty(null));
+         sreg.setProperties(null);
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID));
+         assertEquals(clazzes, sref.getProperty(Constants.OBJECTCLASS));
+         assertNull(sref.getProperty("testA"));
+         assertNull(sref.getProperty("testB"));
+         assertNull(sref.getProperty("MiXeD"));
+         assertNull(sref.getProperty("mixed"));
+         assertNull(sref.getProperty("MIXED"));
+         assertNull(sref.getProperty(null));
 
-         registration.setProperties(properties);
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID));
-         assertEquals(clazzes, reference.getProperty(Constants.OBJECTCLASS));
-         assertEquals("notA", reference.getProperty("testA"));
-         assertEquals("b", reference.getProperty("testB"));
-         assertEquals("Case", reference.getProperty("MiXeD"));
-         assertEquals("Case", reference.getProperty("mixed"));
-         assertEquals("Case", reference.getProperty("MIXED"));
-         assertNull(reference.getProperty(null));
+         sreg.setProperties(properties);
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID));
+         assertEquals(clazzes, sref.getProperty(Constants.OBJECTCLASS));
+         assertEquals("notA", sref.getProperty("testA"));
+         assertEquals("b", sref.getProperty("testB"));
+         assertEquals("Case", sref.getProperty("MiXeD"));
+         assertEquals("Case", sref.getProperty("mixed"));
+         assertEquals("Case", sref.getProperty("MIXED"));
+         assertNull(sref.getProperty(null));
 
-         registration.unregister();
-         assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID));
-         assertEquals(clazzes, reference.getProperty(Constants.OBJECTCLASS));
-         assertEquals("notA", reference.getProperty("testA"));
-         assertEquals("b", reference.getProperty("testB"));
-         assertEquals("Case", reference.getProperty("MiXeD"));
-         assertEquals("Case", reference.getProperty("mixed"));
-         assertEquals("Case", reference.getProperty("MIXED"));
-         assertNull(reference.getProperty(null));
+         sreg.unregister();
+         assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID));
+         assertEquals(clazzes, sref.getProperty(Constants.OBJECTCLASS));
+         assertEquals("notA", sref.getProperty("testA"));
+         assertEquals("b", sref.getProperty("testB"));
+         assertEquals("Case", sref.getProperty("MiXeD"));
+         assertEquals("Case", sref.getProperty("mixed"));
+         assertEquals("Case", sref.getProperty("MIXED"));
+         assertNull(sref.getProperty(null));
       }
       finally
       {
          bundle.uninstall();
       }
 
-      assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID));
-      assertEquals(clazzes, reference.getProperty(Constants.OBJECTCLASS));
-      assertEquals("notA", reference.getProperty("testA"));
-      assertEquals("b", reference.getProperty("testB"));
-      assertEquals("Case", reference.getProperty("MiXeD"));
-      assertEquals("Case", reference.getProperty("mixed"));
-      assertEquals("Case", reference.getProperty("MIXED"));
-      assertNull(reference.getProperty(null));
+      assertEquals(serviceID, sref.getProperty(Constants.SERVICE_ID));
+      assertEquals(clazzes, sref.getProperty(Constants.OBJECTCLASS));
+      assertEquals("notA", sref.getProperty("testA"));
+      assertEquals("b", sref.getProperty("testB"));
+      assertEquals("Case", sref.getProperty("MiXeD"));
+      assertEquals("Case", sref.getProperty("mixed"));
+      assertEquals("Case", sref.getProperty("MIXED"));
+      assertNull(sref.getProperty(null));
    }
 
    @Test
    public void testGetPropertyKeys() throws Exception
    {
-      ServiceReference reference = null;
+      ServiceReference sref = null;
 
       VirtualFile assembly = assembleArchive("simple1", "/bundles/simple/simple-bundle1");
       Bundle bundle = installBundle(assembly);
@@ -168,37 +174,37 @@
          properties.put("testA", "a");
          properties.put("testB", "b");
          properties.put("MiXeD", "Case");
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
+         assertNotNull(sreg);
 
-         reference = registration.getReference();
-         assertNotNull(reference);
+         sref = sreg.getReference();
+         assertNotNull(sref);
 
-         assertPropertyKeys(reference, "testA", "testB", "MiXeD");
+         assertPropertyKeys(sref, "testA", "testB", "MiXeD");
 
          properties.put("testC", "c");
-         assertPropertyKeys(reference, "testA", "testB", "MiXeD");
+         assertPropertyKeys(sref, "testA", "testB", "MiXeD");
 
-         registration.setProperties(properties);
-         assertPropertyKeys(reference, "testA", "testB", "testC", "MiXeD");
+         sreg.setProperties(properties);
+         assertPropertyKeys(sref, "testA", "testB", "testC", "MiXeD");
 
-         registration.setProperties(null);
-         assertPropertyKeys(reference);
+         sreg.setProperties(null);
+         assertPropertyKeys(sref);
 
-         registration.setProperties(properties);
-         assertPropertyKeys(reference, "testA", "testB", "testC", "MiXeD");
+         sreg.setProperties(properties);
+         assertPropertyKeys(sref, "testA", "testB", "testC", "MiXeD");
 
-         registration.unregister();
-         assertPropertyKeys(reference, "testA", "testB", "testC", "MiXeD");
+         sreg.unregister();
+         assertPropertyKeys(sref, "testA", "testB", "testC", "MiXeD");
       }
       finally
       {
          bundle.uninstall();
       }
-      assertPropertyKeys(reference, "testA", "testB", "testC", "MiXeD");
+      assertPropertyKeys(sref, "testA", "testB", "testC", "MiXeD");
    }
 
-   private void assertPropertyKeys(ServiceReference reference, String... expectedKeys)
+   private void assertPropertyKeys(ServiceReference sref, String... expectedKeys)
    {
       Set<String> expected = new HashSet<String>();
       expected.add(Constants.SERVICE_ID);
@@ -207,7 +213,7 @@
          expected.add(key);
 
       Set<String> actual = new HashSet<String>();
-      for (String key : reference.getPropertyKeys())
+      for (String key : sref.getPropertyKeys())
          actual.add(key);
 
       assertEquals(expected, actual);
@@ -224,18 +230,18 @@
          BundleContext bundleContext = bundle.getBundleContext();
          assertNotNull(bundleContext);
 
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+         assertNotNull(sreg);
 
-         ServiceReference reference = registration.getReference();
-         assertNotNull(reference);
+         ServiceReference sref = sreg.getReference();
+         assertNotNull(sref);
 
-         Bundle other = reference.getBundle();
+         Bundle other = sref.getBundle();
          assertEquals(bundle, other);
 
-         registration.unregister();
+         sreg.unregister();
 
-         other = reference.getBundle();
+         other = sref.getBundle();
          assertNull("" + other, other);
       }
       finally
@@ -255,18 +261,18 @@
          BundleContext bundleContext = bundle.getBundleContext();
          assertNotNull(bundleContext);
 
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+         assertNotNull(sreg);
 
-         ServiceReference reference = registration.getReference();
-         assertNotNull(reference);
+         ServiceReference sref = sreg.getReference();
+         assertNotNull(sref);
 
-         Bundle other = reference.getBundle();
+         Bundle other = sref.getBundle();
          assertEquals(bundle, other);
 
          bundle.stop();
 
-         other = reference.getBundle();
+         other = sref.getBundle();
          assertNull("" + other, other);
       }
       finally
@@ -286,13 +292,13 @@
          BundleContext bundleContext = bundle1.getBundleContext();
          assertNotNull(bundleContext);
 
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+         assertNotNull(sreg);
 
-         ServiceReference reference = registration.getReference();
-         assertNotNull(reference);
+         ServiceReference sref = sreg.getReference();
+         assertNotNull(sref);
 
-         assertUsingBundles(reference);
+         assertUsingBundles(sref);
 
          VirtualFile assembly2 = assembleArchive("simple2", "/bundles/simple/simple-bundle2");
          Bundle bundle2 = installBundle(assembly2);
@@ -302,26 +308,26 @@
             BundleContext bundleContext2 = bundle2.getBundleContext();
             assertNotNull(bundleContext2);
 
-            bundleContext2.getService(reference);
-            assertUsingBundles(reference, bundle2);
+            bundleContext2.getService(sref);
+            assertUsingBundles(sref, bundle2);
 
-            bundleContext2.ungetService(reference);
-            assertUsingBundles(reference);
+            bundleContext2.ungetService(sref);
+            assertUsingBundles(sref);
 
-            bundleContext2.getService(reference);
-            bundleContext2.getService(reference);
-            assertUsingBundles(reference, bundle2);
-            bundleContext2.ungetService(reference);
-            assertUsingBundles(reference, bundle2);
-            bundleContext2.ungetService(reference);
-            assertUsingBundles(reference);
+            bundleContext2.getService(sref);
+            bundleContext2.getService(sref);
+            assertUsingBundles(sref, bundle2);
+            bundleContext2.ungetService(sref);
+            assertUsingBundles(sref, bundle2);
+            bundleContext2.ungetService(sref);
+            assertUsingBundles(sref);
 
-            bundleContext.getService(reference);
-            bundleContext2.getService(reference);
-            assertUsingBundles(reference, bundle1, bundle2);
+            bundleContext.getService(sref);
+            bundleContext2.getService(sref);
+            assertUsingBundles(sref, bundle1, bundle2);
 
-            registration.unregister();
-            assertUsingBundles(reference);
+            sreg.unregister();
+            assertUsingBundles(sref);
          }
          finally
          {
@@ -345,13 +351,13 @@
          BundleContext bundleContext = bundle1.getBundleContext();
          assertNotNull(bundleContext);
 
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+         assertNotNull(sreg);
 
-         ServiceReference reference = registration.getReference();
-         assertNotNull(reference);
+         ServiceReference sref = sreg.getReference();
+         assertNotNull(sref);
 
-         assertUsingBundles(reference);
+         assertUsingBundles(sref);
 
          VirtualFile assembly2 = assembleArchive("simple2", "/bundles/simple/simple-bundle2");
          Bundle bundle2 = installBundle(assembly2);
@@ -361,12 +367,12 @@
             BundleContext bundleContext2 = bundle2.getBundleContext();
             assertNotNull(bundleContext2);
 
-            bundleContext.getService(reference);
-            bundleContext2.getService(reference);
-            assertUsingBundles(reference, bundle1, bundle2);
+            bundleContext.getService(sref);
+            bundleContext2.getService(sref);
+            assertUsingBundles(sref, bundle1, bundle2);
 
             bundle1.stop();
-            assertUsingBundles(reference);
+            assertUsingBundles(sref);
          }
          finally
          {
@@ -390,15 +396,15 @@
          BundleContext bundleContext = bundle.getBundleContext();
          assertNotNull(bundleContext);
 
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+         assertNotNull(sreg);
 
-         ServiceReference reference = registration.getReference();
-         assertNotNull(reference);
+         ServiceReference sref = sreg.getReference();
+         assertNotNull(sref);
 
          try
          {
-            reference.isAssignableTo(null, A.class.getName());
+            sref.isAssignableTo(null, A.class.getName());
             fail("Should not be here!");
          }
          catch (IllegalArgumentException t)
@@ -408,7 +414,7 @@
 
          try
          {
-            reference.isAssignableTo(bundle, null);
+            sref.isAssignableTo(bundle, null);
             fail("Should not be here!");
          }
          catch (IllegalArgumentException t)
@@ -433,22 +439,22 @@
          BundleContext bundleContext = bundle1.getBundleContext();
          assertNotNull(bundleContext);
 
-         ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration);
+         ServiceRegistration sreg = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+         assertNotNull(sreg);
 
-         ServiceReference reference = registration.getReference();
-         assertNotNull(reference);
+         ServiceReference sref = sreg.getReference();
+         assertNotNull(sref);
 
          VirtualFile assembly2 = assembleArchive("simple2", "/bundles/simple/simple-bundle2", A.class);
          Bundle bundle2 = installBundle(assembly2);
          try
          {
-            assertFalse(reference.isAssignableTo(bundle2, A.class.getName()));
-            assertTrue(reference.isAssignableTo(bundle2, String.class.getName()));
+            assertFalse(sref.isAssignableTo(bundle2, A.class.getName()));
+            assertTrue(sref.isAssignableTo(bundle2, String.class.getName()));
 
-            registration.unregister();
-            assertFalse(reference.isAssignableTo(bundle2, A.class.getName()));
-            assertFalse(reference.isAssignableTo(bundle2, String.class.getName())); // review ???
+            sreg.unregister();
+            assertFalse(sref.isAssignableTo(bundle2, A.class.getName()));
+            assertFalse(sref.isAssignableTo(bundle2, String.class.getName())); // review ???
          }
          finally
          {
@@ -485,22 +491,22 @@
          try
          {
 
-            ServiceRegistration registration = bundleContext2.registerService(BundleContext.class.getName(), bundleContext2, null);
-            assertNotNull(registration);
+            ServiceRegistration sreg = bundleContext2.registerService(BundleContext.class.getName(), bundleContext2, null);
+            assertNotNull(sreg);
 
-            ServiceReference reference = registration.getReference();
-            assertNotNull(reference);
+            ServiceReference sref = sreg.getReference();
+            assertNotNull(sref);
 
-            assertTrue(reference.isAssignableTo(bundle2, A.class.getName()));
-            assertTrue(reference.isAssignableTo(bundle2, String.class.getName()));
-            assertTrue(reference.isAssignableTo(bundle1, A.class.getName()));
-            assertTrue(reference.isAssignableTo(bundle1, String.class.getName()));
+            assertTrue(sref.isAssignableTo(bundle2, A.class.getName()));
+            assertTrue(sref.isAssignableTo(bundle2, String.class.getName()));
+            assertTrue(sref.isAssignableTo(bundle1, A.class.getName()));
+            assertTrue(sref.isAssignableTo(bundle1, String.class.getName()));
 
-            registration.unregister();
-            assertTrue(reference.isAssignableTo(bundle2, A.class.getName()));
-            assertTrue(reference.isAssignableTo(bundle2, String.class.getName()));
-            assertFalse(reference.isAssignableTo(bundle1, A.class.getName()));
-            assertFalse(reference.isAssignableTo(bundle1, String.class.getName())); // review ???
+            sreg.unregister();
+            assertTrue(sref.isAssignableTo(bundle2, A.class.getName()));
+            assertTrue(sref.isAssignableTo(bundle2, String.class.getName()));
+            assertFalse(sref.isAssignableTo(bundle1, A.class.getName()));
+            assertFalse(sref.isAssignableTo(bundle1, String.class.getName())); // review ???
          }
          finally
          {
@@ -516,114 +522,159 @@
    @Test
    public void testCompareTo() throws Exception
    {
-      VirtualFile assembly = assembleArchive("simple1", "/bundles/simple/simple-bundle1");
-      Bundle bundle = installBundle(assembly);
-      try
-      {
-         bundle.start();
-         BundleContext bundleContext = bundle.getBundleContext();
-         assertNotNull(bundleContext);
+      BundleContext bundleContext = framework.getBundleContext();
+      assertNotNull(bundleContext);
 
-         ServiceRegistration registration1 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration1);
+      ServiceRegistration sreg1 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+      assertNotNull(sreg1);
 
-         ServiceReference reference1 = registration1.getReference();
-         assertNotNull(reference1);
+      ServiceReference sref1 = sreg1.getReference();
+      assertNotNull(sref1);
 
-         ServiceRegistration registration2 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
-         assertNotNull(registration2);
+      ServiceRegistration sreg2 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, null);
+      assertNotNull(sreg2);
 
-         ServiceReference reference2 = registration2.getReference();
-         assertNotNull(reference2);
+      ServiceReference sref2 = sreg2.getReference();
+      assertNotNull(sref2);
 
-         Dictionary<String, Object> properties = new Hashtable<String, Object>();
-         properties.put(Constants.SERVICE_RANKING, 10);
-         ServiceRegistration registration3 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
-         assertNotNull(registration3);
+      Dictionary<String, Object> properties = new Hashtable<String, Object>();
+      properties.put(Constants.SERVICE_RANKING, 10);
+      ServiceRegistration sreg3 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
+      assertNotNull(sreg3);
 
-         ServiceReference reference3 = registration3.getReference();
-         assertNotNull(reference3);
+      ServiceReference sref3 = sreg3.getReference();
+      assertNotNull(sref3);
 
-         properties = new Hashtable<String, Object>();
-         properties.put(Constants.SERVICE_RANKING, -10);
-         ServiceRegistration registration4 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
-         assertNotNull(registration4);
+      properties = new Hashtable<String, Object>();
+      properties.put(Constants.SERVICE_RANKING, -10);
+      ServiceRegistration sreg4 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
+      assertNotNull(sreg4);
 
-         ServiceReference reference4 = registration4.getReference();
-         assertNotNull(reference4);
+      ServiceReference sref4 = sreg4.getReference();
+      assertNotNull(sref4);
 
-         assertGreaterRanking(reference1, reference2);
-         assertGreaterRanking(reference3, reference1);
-         assertGreaterRanking(reference3, reference2);
-         assertGreaterRanking(reference1, reference4);
-         assertGreaterRanking(reference2, reference4);
-         assertGreaterRanking(reference3, reference4);
+      assertGreaterRanking(sref1, sref2);
+      assertGreaterRanking(sref3, sref1);
+      assertGreaterRanking(sref3, sref2);
+      assertGreaterRanking(sref1, sref4);
+      assertGreaterRanking(sref2, sref4);
+      assertGreaterRanking(sref3, sref4);
 
-         try
-         {
-            reference1.compareTo(null);
-            fail("Should not be here!");
-         }
-         catch (IllegalArgumentException t)
-         {
-            // expected
-         }
+      try
+      {
+         sref1.compareTo(null);
+         fail("Should not be here!");
+      }
+      catch (IllegalArgumentException t)
+      {
+         // expected
+      }
 
-         try
-         {
-            reference1.compareTo(new Object());
-            fail("Should not be here!");
-         }
-         catch (IllegalArgumentException t)
-         {
-            // expected
-         }
+      try
+      {
+         sref1.compareTo(new Object());
+         fail("Should not be here!");
+      }
+      catch (IllegalArgumentException t)
+      {
+         // expected
+      }
 
-         properties = new Hashtable<String, Object>();
-         properties.put(Constants.SERVICE_RANKING, "NotANumber");
-         ServiceRegistration registration5 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
-         assertNotNull(registration5);
+      properties = new Hashtable<String, Object>();
+      properties.put(Constants.SERVICE_RANKING, "NotANumber");
+      ServiceRegistration sreg5 = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties);
+      assertNotNull(sreg5);
 
-         ServiceReference reference5 = registration5.getReference();
-         assertNotNull(reference5);
+      ServiceReference sref5 = sreg5.getReference();
+      assertNotNull(sref5);
 
-         assertGreaterRanking(reference1, reference5); // review ???
+      assertGreaterRanking(sref1, sref5); 
 
-         Set<ServiceReference> ordering = new TreeSet<ServiceReference>();
-         ordering.add(reference1);
-         ordering.add(reference2);
-         ordering.add(reference3);
-         ordering.add(reference4);
-         ordering.add(reference5);
-         Iterator<ServiceReference> iterator = ordering.iterator();
-         assertEquals(reference4, iterator.next());
-         assertEquals(reference5, iterator.next());
-         assertEquals(reference2, iterator.next());
-         assertEquals(reference1, iterator.next());
-         assertEquals(reference3, iterator.next());
+      Set<ServiceReference> ordering = new TreeSet<ServiceReference>();
+      ordering.add(sref1);
+      ordering.add(sref2);
+      ordering.add(sref3);
+      ordering.add(sref4);
+      ordering.add(sref5);
+      Iterator<ServiceReference> iterator = ordering.iterator();
+      assertEquals(sref4, iterator.next());
+      assertEquals(sref5, iterator.next());
+      assertEquals(sref2, iterator.next());
+      assertEquals(sref1, iterator.next());
+      assertEquals(sref3, iterator.next());
 
-         ordering = new TreeSet<ServiceReference>();
-         ordering.add(reference5);
-         ordering.add(reference4);
-         ordering.add(reference3);
-         ordering.add(reference2);
-         ordering.add(reference1);
-         iterator = ordering.iterator();
-         assertEquals(reference4, iterator.next());
-         assertEquals(reference5, iterator.next());
-         assertEquals(reference2, iterator.next());
-         assertEquals(reference1, iterator.next());
-         assertEquals(reference3, iterator.next());
-      }
-      finally
+      ordering = new TreeSet<ServiceReference>();
+      ordering.add(sref5);
+      ordering.add(sref4);
+      ordering.add(sref3);
+      ordering.add(sref2);
+      ordering.add(sref1);
+      iterator = ordering.iterator();
+      assertEquals(sref4, iterator.next());
+      assertEquals(sref5, iterator.next());
+      assertEquals(sref2, iterator.next());
+      assertEquals(sref1, iterator.next());
+      assertEquals(sref3, iterator.next());
+   }
+
+   @Test
+   public void testServiceReferenceOrder() throws Exception
+   {
+      Runnable runIt = new Runnable()
       {
-         bundle.uninstall();
-      }
+         public void run()
+         {
+         }
+      };
+      
+      BundleContext context = framework.getBundleContext();
+      Hashtable<String, Object> props = new Hashtable<String, Object>();
+      props.put(Constants.SERVICE_DESCRIPTION, "min value");
+      props.put(Constants.SERVICE_RANKING, Integer.MIN_VALUE);
+      ServiceRegistration sreg1 = context.registerService(Runnable.class.getName(), runIt, props);
+      ServiceReference sref1 = sreg1.getReference();
+      
+      props.put(Constants.SERVICE_DESCRIPTION, "max value 1");
+      props.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
+      ServiceRegistration sreg2 = context.registerService(Runnable.class.getName(), runIt, props);
+      ServiceReference sref2 = sreg2.getReference();
+      
+      props.put(Constants.SERVICE_DESCRIPTION, "max value 2");
+      props.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
+      ServiceRegistration sreg3 = context.registerService(Runnable.class.getName(), runIt, props);
+      ServiceReference sref3 = sreg3.getReference();
+      
+      props.put(Constants.SERVICE_DESCRIPTION, "max value 3");
+      props.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
+      ServiceRegistration sreg4 = context.registerService(Runnable.class.getName(), runIt, props);
+      ServiceReference sref4 = sreg4.getReference();
+      
+      ServiceReference[] srefs = context.getServiceReferences(Runnable.class.getName(), null);
+      assertEquals(4, srefs.length);
+      assertEquals(sref1, srefs[0]);
+      assertEquals(sref4, srefs[1]);
+      assertEquals(sref3, srefs[2]);
+      assertEquals(sref2, srefs[3]);
+      
+      ServiceReference sref = context.getServiceReference(Runnable.class.getName());
+      assertEquals(sref2, sref);
+      sreg2.unregister();
+      
+      sref = context.getServiceReference(Runnable.class.getName());
+      assertEquals(sref3, sref);
+      sreg3.unregister();
+      
+      sref = context.getServiceReference(Runnable.class.getName());
+      assertEquals(sref4, sref);
+      sreg4.unregister();
+
+      sref = context.getServiceReference(Runnable.class.getName());
+      assertEquals(sref1, sref);
    }
 
-   protected void assertGreaterRanking(ServiceReference reference1, ServiceReference reference2) throws Exception
+   protected void assertGreaterRanking(ServiceReference sref1, ServiceReference sref2) throws Exception
    {
-      assertTrue(reference1 + " > " + reference2, reference1.compareTo(reference2) > 0);
-      assertTrue(reference2 + " < " + reference1, reference2.compareTo(reference1) < 0);
+      assertTrue(sref1 + " > " + sref2, sref1.compareTo(sref2) > 0);
+      assertTrue(sref2 + " < " + sref1, sref2.compareTo(sref1) < 0);
    }
 }

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -465,12 +465,13 @@
     */
    boolean removeContextInUse(ControllerContext context)
    {
+      int usedByCount = getUsedByCount(context, this);
       if (context instanceof ContextTracking)
       {
          ContextTracking ct = (ContextTracking)context;
          ct.ungetTarget(this);
       }
-      return getUsedByCount(context, this) > 0;
+      return usedByCount > 0;
    }
 
    public ServiceReference[] getServicesInUse()

Deleted: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -1,57 +0,0 @@
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat Middleware LLC, and individual contributors
-* as indicated by the @author tags. See the copyright.txt file in the
-* distribution for a full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-*/
-package org.jboss.osgi.framework.bundle;
-
-import java.util.Comparator;
-
-import org.jboss.dependency.spi.ControllerContext;
-
-/**
- * Compare controller contexts.
- *
- * @author <a href="ales.justin at jboss.org">Ales Justin</a>
- */
-class ContextComparator implements Comparator<ControllerContext>
-{
-   public static final Comparator<ControllerContext> INSTANCE = new ContextComparator();
-   
-   public int compare(ControllerContext c1, ControllerContext c2)
-   {
-      Integer ranking1 = MDRUtils.getRanking(c1);
-      Integer ranking2 = MDRUtils.getRanking(c2);
-      int diff = ranking2 - ranking1;
-      if (diff == 0)
-      {
-         Long id1 = MDRUtils.getId(c1);
-         Long id2 = MDRUtils.getId(c2);
-         if (id1 == null && id2 == null)
-            return 0;
-         if (id1 != null && id2 == null)
-            return -1;
-         if (id2 != null && id1 == null)
-            return 1;
-
-         return (id2 > id1) ? -1 : 1;
-      }
-      return diff;
-   }
-}
\ No newline at end of file

Copied: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java (from rev 102678, projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java)
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java	                        (rev 0)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -0,0 +1,71 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.framework.bundle;
+
+// $Id: $
+
+import java.util.Comparator;
+
+import org.jboss.dependency.spi.ControllerContext;
+
+/**
+ * If this ServiceReference and the specified ServiceReference have the same service id they are equal. 
+ * This ServiceReference is less than the specified ServiceReference if it has a lower service ranking 
+ * and greater if it has a higher service ranking. 
+ * 
+ * Otherwise, if this ServiceReference and the specified ServiceReference have the same service ranking, 
+ * this ServiceReference is less than the specified ServiceReference if it has a higher service id and 
+ * greater if it has a lower service id.
+ *      
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ * @author thomas.diesler at jboss.com
+ * @since 21-Mar-2010
+ */
+class ContextComparator implements Comparator<ControllerContext>
+{
+   private static final Comparator<ControllerContext> INSTANCE = new ContextComparator();
+
+   static Comparator<ControllerContext> getInstance()
+   {
+      return INSTANCE;
+   }
+
+   public int compare(ControllerContext c1, ControllerContext c2)
+   {
+      if (c1.equals(c2))
+         return 0;
+
+      Long id1 = MDRUtils.getId(c1);
+      Long id2 = MDRUtils.getId(c2);
+      if (id1 != null && id1.equals(id2))
+         throw new IllegalStateException("Compare not consistent with equals: " + c1 + " != " + c2);
+
+      Integer ranking1 = MDRUtils.getRanking(c1);
+      Integer ranking2 = MDRUtils.getRanking(c2);
+      if (ranking1.equals(ranking2) == false)
+         return (ranking1 < ranking2 ? -1 : 1);
+
+      id1 = (id1 != null ? id1 : Long.MAX_VALUE);
+      id2 = (id2 != null ? id2 : Long.MAX_VALUE);
+      return (id1 > id2 ? -1 : 1);
+   }
+}
\ No newline at end of file

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceReferenceWrapper.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceReferenceWrapper.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceReferenceWrapper.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -21,6 +21,8 @@
 */
 package org.jboss.osgi.framework.bundle;
 
+// $Id: $
+
 import org.jboss.dependency.spi.ControllerContext;
 import org.osgi.framework.Bundle;
 
@@ -29,51 +31,63 @@
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ * @author thomas.diesler at jboss.com
  * @version $Revision: 1.1 $
  */
-public class OSGiServiceReferenceWrapper extends ControllerContextHandle
+class OSGiServiceReferenceWrapper extends ControllerContextHandle
 {
    /** The service state */
    private OSGiServiceState serviceState;
 
-   /**
-    * Create a new OSGiServiceReferenceWrapper.
-    * 
-    * @param serviceState the service state
-    * @throws IllegalArgumentException for a null parameter
-    */
-   public OSGiServiceReferenceWrapper(OSGiServiceState serviceState)
+   OSGiServiceReferenceWrapper(OSGiServiceState serviceState)
    {
       if (serviceState == null)
          throw new IllegalArgumentException("Null service state");
       this.serviceState = serviceState;
    }
 
+   @Override
    public Bundle getBundle()
    {
       return serviceState.getBundle();
    }
 
+   @Override
    public Object getProperty(String key)
    {
       return serviceState.getProperty(key);
    }
 
+   @Override
    public String[] getPropertyKeys()
    {
       return serviceState.getPropertyKeys();
    }
 
+   @Override
    public Bundle[] getUsingBundles()
    {
       return serviceState.getUsingBundles();
    }
 
+   @Override
    public boolean isAssignableTo(Bundle bundle, String className)
    {
       return serviceState.isAssignableTo(bundle, className);
    }
 
+   OSGiServiceState getServiceState()
+   {
+      return serviceState;
+   }
+
+   @Override
+   ControllerContext getContext()
+   {
+      return getServiceState();
+   }
+
+   @Override
    public int compareTo(Object reference)
    {
       return serviceState.compareTo(reference);
@@ -96,14 +110,4 @@
    {
       return serviceState.toString();
    }
-
-   OSGiServiceState getServiceState()
-   {
-      return serviceState;
-   }
-
-   ControllerContext getContext()
-   {
-      return getServiceState();
-   }
 }

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-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -27,8 +27,10 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.Dictionary;
 import java.util.Enumeration;
+import java.util.Hashtable;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
@@ -101,7 +103,8 @@
    private Map<AbstractBundleState, Object> serviceCache;
 
    /** The properties */
-   private CaseInsensitiveDictionary properties;
+   private CaseInsensitiveDictionary prevProperties;
+   private CaseInsensitiveDictionary currProperties;
 
    /** The bean info */
    private BeanInfo beanInfo;
@@ -115,7 +118,7 @@
     * @param properties the properties
     * @throws IllegalArgumentException for a null parameter
     */
-   @SuppressWarnings({ "rawtypes" })
+   @SuppressWarnings({ "rawtypes", "unchecked" })
    public OSGiServiceState(AbstractBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
    {
       super(service, properties);
@@ -141,8 +144,12 @@
       if (isServiceFactory == false)
          checkObjClass(service);
 
-      if (properties != null)
-         this.properties = new CaseInsensitiveDictionary(properties);
+      if (properties == null)
+         properties = new Hashtable();
+      
+      properties.put(Constants.SERVICE_ID, getServiceId());
+      properties.put(Constants.OBJECTCLASS, getClasses());
+      this.currProperties = new CaseInsensitiveDictionary(properties);
 
       serviceRegistration = new OSGiServiceRegistrationWrapper(this);
 
@@ -370,20 +377,32 @@
          if (service == null)
          {
             ServiceFactory serviceFactory = (ServiceFactory)serviceOrFactory;
+            boolean gotService = false;
+            
+            // If the service object returned by the ServiceFactory object is not an instanceof all the classes named when 
+            // the service was registered or the ServiceFactory object throws an exception, null is returned and a Framework 
+            // event of type FrameworkEvent.ERROR containing a ServiceException  describing the error is fired. 
             try
             {
                service = serviceFactory.getService(bundleState.getBundle(), getRegistration());
+               gotService = true;
+               
                service = checkObjClass(service);
                serviceCache.put(bundleState, service);
             }
             catch (Throwable t)
             {
-               // If the service object returned by the ServiceFactory object is not an instanceof all the classes named when 
-               // the service was registered or the ServiceFactory object throws an exception, null is returned and a Framework 
-               // event of type FrameworkEvent.ERROR containing a ServiceException  describing the error is fired. 
-               log.error("Error from getService for " + this, t);
+               Throwable cause = t;
+               String message = "Cannot get service from: " + serviceFactory;
+               if (gotService == true)
+               {
+                  message += "." + t.getMessage();
+                  cause = null;
+               }
+               ServiceException serviceException = new ServiceException(message, cause);
+               log.error("Cannot getService from ServiceFactory", serviceException);
                FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
-               plugin.fireFrameworkEvent(bundleState, FrameworkEvent.ERROR, new ServiceException("Error using service factory:" + serviceFactory, t));
+               plugin.fireFrameworkEvent(bundleState, FrameworkEvent.ERROR, serviceException);
                return null;
             }
          }
@@ -468,51 +487,56 @@
       return bundleState;
    }
 
-	@Override
+   @Override
    public Object getProperty(String key)
    {
       if (key == null)
          return null;
-      if (Constants.SERVICE_ID.equalsIgnoreCase(key))
-         return getServiceId();
-      if (Constants.OBJECTCLASS.equalsIgnoreCase(key))
-         return getClasses();
-      if (properties == null)
-         return null;
-      return properties.get(key);
+      return currProperties.get(key);
    }
 
    @Override
    public String[] getPropertyKeys()
    {
       ArrayList<String> result = new ArrayList<String>();
-      if (properties != null)
+      if (currProperties != null)
       {
-         Enumeration<String> keys = properties.keys();
+         Enumeration<String> keys = currProperties.keys();
          while (keys.hasMoreElements())
             result.add(keys.nextElement());
       }
-      result.add(Constants.SERVICE_ID);
-      result.add(Constants.OBJECTCLASS);
       return result.toArray(new String[result.size()]);
    }
 
    @Override
-   @SuppressWarnings("rawtypes")
+   @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setProperties(Dictionary properties)
    {
       checkUnregistered();
 
+      // Remember the previous properties for a potential
+      // delivery of the MODIFIED_ENDMATCH event
+      prevProperties = currProperties;
+      
       if (properties == null)
-         this.properties = null;
-      else
-         this.properties = new CaseInsensitiveDictionary(properties);
+         properties = new Hashtable();
+      
+      properties.put(Constants.SERVICE_ID, getServiceId());
+      properties.put(Constants.OBJECTCLASS, getClasses());
+      currProperties = new CaseInsensitiveDictionary(properties);
 
       // This event is synchronously delivered after the service properties have been modified. 
       FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
       plugin.fireServiceEvent(bundleState, ServiceEvent.MODIFIED, this);
    }
 
+   
+   @SuppressWarnings("rawtypes")
+   public Dictionary getPreviousProperties()
+   {
+      return prevProperties;
+   }
+
    @Override
    public Bundle[] getUsingBundles()
    {
@@ -524,7 +548,7 @@
 
       return bundles.toArray(new Bundle[bundles.size()]);
    }
-   
+
    @Override
    public boolean isAssignableTo(Bundle bundle, String className)
    {
@@ -549,6 +573,15 @@
       }
    }
 
+   /*
+    * If this ServiceReference and the specified ServiceReference have the same service id they are equal. 
+    * This ServiceReference is less than the specified ServiceReference if it has a lower service ranking 
+    * and greater if it has a higher service ranking. 
+    * 
+    * Otherwise, if this ServiceReference and the specified ServiceReference have the same service ranking, 
+    * this ServiceReference is less than the specified ServiceReference if it has a higher service id and 
+    * greater if it has a lower service id.     
+    */
    @Override
    public int compareTo(Object reference)
    {
@@ -563,70 +596,25 @@
       else
          throw new IllegalArgumentException(reference + " is not a service reference");
 
-      Long otherServiceId = MDRUtils.getId(other);
-      if (otherServiceId == null)
-         return -1; // TODO?
-
-      long thisServiceId = getServiceId();
-      if (thisServiceId == otherServiceId)
-         return 0;
-
-      Integer otherRanking = MDRUtils.getRanking(other);
-      int thisRanking = getServiceRanking();
-      int ranking = thisRanking - otherRanking;
-      if (ranking != 0)
-         return ranking;
-
-      return (thisServiceId > otherServiceId) ? -1 : 1;
+      Comparator<ControllerContext> comparator = ContextComparator.getInstance();
+      return comparator.compare(this, other);
    }
 
-   @Override
-   public boolean equals(Object obj)
+   String toLongString()
    {
-      if (obj == null)
-         return false;
-
-      OSGiServiceState other;
-      if (obj instanceof OSGiServiceState)
-         other = (OSGiServiceState)obj;
-      else if (obj instanceof OSGiServiceReferenceWrapper)
-         other = ((OSGiServiceReferenceWrapper)obj).getServiceState();
-      else
-         return false;
-      return this == other;
-   }
-
-   @Override
-   public int hashCode()
-   {
-      return toString().hashCode();
-   }
-
-   @Override
-   public String toString()
-   {
       StringBuilder builder = new StringBuilder();
+      String desc = (String)getProperty(Constants.SERVICE_DESCRIPTION);
       builder.append("Service{");
       builder.append("id=").append(getServiceId());
-      builder.append(" classes=").append(Arrays.asList(getClasses()));
+      builder.append(desc != null ? ",desc=" + desc : "");
+      builder.append(",bundle=").append(getBundleState().getCanonicalName());
+      builder.append(",classes=").append(Arrays.asList(getClasses()));
+      builder.append(isServiceFactory ? ",factory=" : ",service=").append(serviceOrFactory);
+      builder.append(",props=").append(currProperties);
       builder.append("}");
       return builder.toString();
    }
 
-   public String toLongString()
-   {
-      StringBuilder builder = new StringBuilder();
-      builder.append("Service{");
-      builder.append("id=").append(getServiceId());
-      builder.append(" bundle=").append(getBundleState().getCanonicalName());
-      builder.append(" classes=").append(Arrays.asList(getClasses()));
-      builder.append(isServiceFactory ? " factory=" : " service=").append(serviceOrFactory);
-      if (properties != null)
-         builder.append(" properties=").append(properties);
-      builder.append("}");
-      return builder.toString();
-   }
-
    /**
     * Register the service
     */
@@ -786,4 +774,41 @@
    {
       return serviceRegistration == null;
    }
+   
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (obj == null)
+         return false;
+
+      OSGiServiceState other;
+      if (obj instanceof OSGiServiceState)
+         other = (OSGiServiceState)obj;
+      else if (obj instanceof OSGiServiceReferenceWrapper)
+         other = ((OSGiServiceReferenceWrapper)obj).getServiceState();
+      else
+         return false;
+      return this == other;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      return toString().hashCode();
+   }
+
+   @Override
+   public String toString()
+   {
+      StringBuilder builder = new StringBuilder();
+      Object desc = getProperty(Constants.SERVICE_DESCRIPTION);
+      Object rank = getProperty(Constants.SERVICE_RANKING);
+      builder.append("Service{");
+      builder.append("id=").append(getServiceId());
+      builder.append(rank != null ? ",rank=" + rank : "");
+      builder.append(desc != null ? ",desc=" + desc : "");
+      builder.append(",classes=").append(Arrays.asList(getClasses()));
+      builder.append("}");
+      return builder.toString();
+   }
 }

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ServiceManagerPluginImpl.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ServiceManagerPluginImpl.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/bundle/ServiceManagerPluginImpl.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -208,7 +208,10 @@
       if (srefs.isEmpty())
          return null;
 
-      return srefs.iterator().next();
+      // If multiple such services exist, the service with the highest ranking is returned.
+      // If there is a tie in ranking, the service with the lowest service ID is returned. 
+      ArrayList<ServiceReference> list = new ArrayList<ServiceReference>(srefs);
+      return list.get(list.size() - 1);
    }
 
    @Override
@@ -235,25 +238,29 @@
     * in which the find hooks are called is the reverse compareTo ordering of
     * their Service References.
     */
-   @SuppressWarnings("unchecked")
-   private Collection<ServiceReference> processFindHooks(AbstractBundleState bundle, String clazz, String filterStr, boolean checkAssignable, Collection<ServiceReference> srefs)
+   private Collection<ServiceReference> processFindHooks(AbstractBundleState bundle, String clazz, String filterStr, boolean checkAssignable,
+         Collection<ServiceReference> srefs)
    {
       BundleContext context = bundle.getBundleContext();
-      if (context == null)
+      Collection<ServiceReference> hookRefs = getServiceReferencesInternal(bundle, FindHook.class.getName(), null, true);
+      if (context == null || hookRefs.isEmpty())
          return srefs;
-         
-      // Get and sort the FindHook refs
-      Collection<ServiceReference> hookRefs = getServiceReferencesInternal(bundle, FindHook.class.getName(), null, true);
+
+      // Event and Find Hooks can not be used to hide the services from the framework.
+      if (clazz != null && clazz.startsWith(FindHook.class.getPackage().getName()))
+         return srefs;
+
+      // The order in which the find hooks are called is the reverse compareTo ordering of
+      // their ServiceReferences. That is, the service with the highest ranking number must be called first.
       List<ServiceReference> sortedHookRefs = new ArrayList<ServiceReference>(hookRefs);
-      Collections.sort(sortedHookRefs);
       Collections.reverse(sortedHookRefs);
-      
+
       srefs = new RemoveOnlyCollection<ServiceReference>(srefs);
-      
+
       List<FindHook> hooks = new ArrayList<FindHook>();
-      for(ServiceReference hookRef : sortedHookRefs)
+      for (ServiceReference hookRef : sortedHookRefs)
          hooks.add((FindHook)context.getService(hookRef));
-      
+
       for (FindHook hook : hooks)
       {
          try
@@ -453,9 +460,9 @@
       return mdrFactory;
    }
 
-   private Collection<ServiceReference> getServiceReferencesInternal(AbstractBundleState bundle, String clazz, Filter filter, boolean checkAssignable)
+   private List<ServiceReference> getServiceReferencesInternal(AbstractBundleState bundle, String clazz, Filter filter, boolean checkAssignable)
    {
-      Set<ControllerContext> contexts;
+      Set<ControllerContext> contexts = null;
       KernelController controller = kernel.getController();
 
       // Don't check assignabilty for the system bundle
@@ -463,46 +470,50 @@
       if (isSystemBundle)
          checkAssignable = false;
 
-      // TODO - a bit slow for system bundle
+      // Load the service class from the given bundle
       if (clazz != null && isSystemBundle == false)
       {
+         // Use all contexts the are of the given service type
          Class<?> type = getBundleManager().loadClassFailsafe(bundle, clazz);
-         if (type == null)
+         if (type != null)
+            contexts = controller.getContexts(type, ControllerState.INSTALLED);
+
+         // No services found
+         if (contexts == null || contexts.isEmpty())
             return Collections.emptyList();
+      }
 
-         contexts = controller.getContexts(type, ControllerState.INSTALLED);
-      }
-      else
-      {
+      // Use all installed contexts
+      if (contexts == null)
          contexts = controller.getContextsByState(ControllerState.INSTALLED);
-      }
 
+      // No services found
       if (contexts == null || contexts.isEmpty())
          return Collections.emptyList();
 
       if (filter == null)
          filter = NoFilter.INSTANCE;
 
+      // Sort by the spec, should bubble up
       List<ControllerContext> sorted = new ArrayList<ControllerContext>(contexts);
-      Collections.sort(sorted, ContextComparator.INSTANCE); // Sort by the spec, should bubble up
-      Collection<ServiceReference> result = new ArrayList<ServiceReference>();
+      Collections.sort(sorted, ContextComparator.getInstance());
+      
+      List<ServiceReference> result = new ArrayList<ServiceReference>();
       for (ControllerContext context : sorted)
       {
-         // re-check?? -- we already only get INSTALLED 
-         if (KernelUtils.isUnregistered(context) == false)
+         ServiceReference sref = getServiceReferenceForContext(context);
+         if (filter.match(sref) && hasPermission(context))
          {
-            ServiceReference ref = getServiceReferenceForContext(context);
-            if (filter.match(ref) && hasPermission(context))
+            // True if the context contains the given class name
+            if (clazz == null || MDRUtils.matchClass(context, clazz))
             {
-               if (clazz == null || isSystemBundle == false || MDRUtils.matchClass(context, clazz))
-               {
-                  // Check the assignability
-                  if (checkAssignable == false || MDRUtils.isAssignableTo(context, bundle))
-                     result.add(ref);
-               }
+               // Check the assignability
+               if (checkAssignable == false || MDRUtils.isAssignableTo(context, bundle))
+                  result.add(sref);
             }
          }
       }
+      //Collections.sort(result, ServiceReferenceComparator.getInstance());
       return result;
    }
 

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/FrameworkEventsPlugin.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/FrameworkEventsPlugin.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/FrameworkEventsPlugin.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -69,6 +69,5 @@
 
    void fireFrameworkEvent(Bundle bundle, int type, Throwable throwable);
 
-   // [TODO] remove dependecy on propriatary API
    void fireServiceEvent(Bundle bundle, int type, OSGiServiceState service);
 }
\ No newline at end of file

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -541,8 +541,8 @@
       }
 
       // Expose the wrapper not the state itself
-      final ServiceEvent event = new OSGiServiceEvent(type, service.getReferenceInternal());
-      final String typeName = ConstantsHelper.serviceEvent(event.getType());
+      ServiceEvent event = new OSGiServiceEvent(type, service.getReferenceInternal());
+      String typeName = ConstantsHelper.serviceEvent(event.getType());
 
       log.info("Service " + typeName + ": " + service);
 
@@ -558,21 +558,31 @@
          return;
 
       // Call the listeners. All service events are synchronously delivered
-      for (ServiceListenerRegistration registration : listeners)
+      for (ServiceListenerRegistration listener : listeners)
       {
          try
          {
-            // [TODO] MODIFIED_ENDMATCH
-            // This event is synchronously delivered after the service properties have been modified. 
+            String filterstr = listener.filter.toString();
+            if (listener.filter.match(service))
+            {
+               AccessControlContext accessControlContext = listener.accessControlContext;
+               if (accessControlContext == null || service.hasPermission(accessControlContext))
+                  listener.listener.serviceChanged(event);
+            }
+            
+            // The MODIFIED_ENDMATCH event is synchronously delivered after the service properties have been modified. 
             // This event is only delivered to listeners which were added with a non-null filter where 
             // the filter matched the service properties prior to the modification but the filter does 
             // not match the modified service properties. 
-
-            if (registration.filter.match(service))
+            else if (filterstr != null && ServiceEvent.MODIFIED == event.getType())
             {
-               AccessControlContext accessControlContext = registration.accessControlContext;
-               if (accessControlContext == null || service.hasPermission(accessControlContext))
-                  registration.listener.serviceChanged(event);
+               if (listener.filter.match(service.getPreviousProperties()))
+               {
+                  event = new OSGiServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, service.getReferenceInternal());
+                  AccessControlContext accessControlContext = listener.accessControlContext;
+                  if (accessControlContext == null || service.hasPermission(accessControlContext))
+                     listener.listener.serviceChanged(event);
+               }
             }
          }
          catch (Throwable t)
@@ -622,7 +632,6 @@
       return listeners;
    }
 
-   @SuppressWarnings("unchecked")
    private List<EventHook> getEventHooks()
    {
       List<EventHook> hooks = new ArrayList<EventHook>();
@@ -641,7 +650,6 @@
          // The calling order of the hooks is defined by the reversed compareTo ordering of their Service
          // Reference objects. That is, the service with the highest ranking number is called first. 
          List<ServiceReference> sortedRefs = new ArrayList<ServiceReference>(Arrays.asList(srefs));
-         Collections.sort(sortedRefs);
          Collections.reverse(sortedRefs);
 
          for (ServiceReference sref : sortedRefs)

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/util/RemoveOnlyCollection.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/util/RemoveOnlyCollection.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/core/src/main/java/org/jboss/osgi/framework/util/RemoveOnlyCollection.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -39,6 +39,8 @@
 
    public RemoveOnlyCollection(Collection<T> delegate)
    {
+      if (delegate == null)
+         throw new IllegalArgumentException("Null delegate");
       this.delegate = delegate;
    }
 
@@ -120,4 +122,22 @@
    {
       delegate.clear();
    }
+
+   @Override
+   public int hashCode()
+   {
+      return delegate.hashCode();
+   }
+
+   @Override
+   public boolean equals(Object obj)
+   {
+      return delegate.equals(obj);
+   }
+
+   @Override
+   public String toString()
+   {
+      return delegate.toString();
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/pom.xml
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/pom.xml	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/pom.xml	2010-03-22 18:12:49 UTC (rev 102729)
@@ -41,6 +41,17 @@
     <url>http://fisheye.jboss.com/qsearch/JBossOSGi</url>
   </scm>
 
+  <!-- Modules -->
+  <modules>
+    <module>core</module>
+    <!-- 
+    <module>metadata</module>
+    <module>vfs21</module>
+    <module>vfs30</module>
+    -->
+    <module>bundle</module>
+  </modules>
+  
   <!-- Properties -->
   <properties>
     <version.apache.felix.configadmin>1.0.10</version.apache.felix.configadmin>
@@ -69,15 +80,6 @@
     <version.osgi>4.2.0</version.osgi>
   </properties>
 
-  <!-- Modules -->
-  <modules>
-    <module>core</module>
-    <module>metadata</module>
-    <module>vfs21</module>
-    <module>vfs30</module>
-    <module>bundle</module>
-  </modules>
-  
   <!-- Dependency Management -->
   <dependencyManagement>
     <dependencies>

Added: projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleFrameworkTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleFrameworkTestCase.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleFrameworkTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.osgi.example.simple;
+
+//$Id: SimpleTestCase.java 102343 2010-03-12 09:23:57Z thomas.diesler at jboss.com $
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.net.URL;
+
+import org.jboss.osgi.testing.OSGiFrameworkTest;
+import org.jboss.test.osgi.example.simple.bundle.SimpleService;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * A test that deployes a bundle and verifies its state
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 12-Feb-2009
+ */
+public class SimpleFrameworkTestCase extends OSGiFrameworkTest
+{
+   @Test
+   public void testSimpleBundle() throws Exception
+   {
+      // Get the bundle location
+      URL url = getTestArchiveURL("example-simple.jar");
+      
+      // Install the Bundle
+      BundleContext sysContext = framework.getBundleContext();
+      Bundle bundle = sysContext.installBundle(url.toExternalForm());
+      assertBundleState(Bundle.INSTALLED, bundle.getState());
+
+      // Check that the BundleContext is still null
+      BundleContext context = bundle.getBundleContext();
+      assertNull("BundleContext null", context);
+      
+      // Start the bundle
+      bundle.start();
+      assertBundleState(Bundle.ACTIVE, bundle.getState());
+
+      // Check that the BundleContext is not null
+      context = bundle.getBundleContext();
+      assertNotNull("BundleContext not null", context);
+      
+      // Get a service from the bundle's context
+      ServiceReference sref = context.getServiceReference(SimpleService.class.getName());
+      assertNotNull("ServiceReference not null", sref);
+      Object service = context.getService(sref);
+      assertNotNull("Service not null", service);
+      
+      // Get a service from the system context
+      sref = sysContext.getServiceReference(SimpleService.class.getName());
+      assertNotNull("ServiceReference not null", sref);
+      service = context.getService(sref);
+      assertNotNull("Service not null", service);
+      
+      // Stop the bundle
+      bundle.stop();
+      assertBundleState(Bundle.RESOLVED, bundle.getState());
+      
+      try
+      {
+         context.getServiceReference(SimpleService.class.getName());
+         fail("Invalid BundleContext expected");
+      }
+      catch (IllegalStateException ex)
+      {
+         // expected
+      }
+      
+      // Get a service from the system context
+      sref = sysContext.getServiceReference(SimpleService.class.getName());
+      assertNull("ServiceReference null", sref);
+      
+      // Uninstall the bundle
+      bundle.uninstall();
+      assertBundleState(Bundle.UNINSTALLED, bundle.getState());
+   }
+}
\ No newline at end of file

Copied: projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleRuntimeTestCase.java (from rev 102377, projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleTestCase.java)
===================================================================
--- projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleRuntimeTestCase.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleRuntimeTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.osgi.example.simple;
+
+//$Id$
+
+import org.jboss.osgi.testing.OSGiBundle;
+import org.jboss.osgi.testing.OSGiRuntime;
+import org.jboss.osgi.testing.OSGiRuntimeTest;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+
+/**
+ * A test that deployes a bundle and verifies its state
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 12-Feb-2009
+ */
+public class SimpleRuntimeTestCase extends OSGiRuntimeTest
+{
+   @Test
+   public void testSimpleBundle() throws Exception
+   {
+      // Get the default runtime
+      OSGiRuntime runtime = getDefaultRuntime();
+      try
+      {
+         // Install the bundle
+         OSGiBundle bundle = runtime.installBundle("example-simple.jar");
+         assertBundleState(Bundle.INSTALLED, bundle.getState());
+
+         // Start the bundle
+         bundle.start();
+         assertBundleState(Bundle.ACTIVE, bundle.getState());
+
+         // Uninstall the bundle
+         bundle.uninstall();
+         assertBundleState(Bundle.UNINSTALLED, bundle.getState());
+      }
+      finally
+      {
+         // Shutdown the runtime 
+         runtime.shutdown();
+      }
+   }
+}
\ No newline at end of file

Deleted: projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/simple/SimpleTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -1,65 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.test.osgi.example.simple;
-
-//$Id$
-
-import org.jboss.osgi.testing.OSGiBundle;
-import org.jboss.osgi.testing.OSGiRuntime;
-import org.jboss.osgi.testing.OSGiRuntimeTest;
-import org.junit.Test;
-import org.osgi.framework.Bundle;
-
-/**
- * A test that deployes a bundle and verifies its state
- * 
- * @author thomas.diesler at jboss.com
- * @since 12-Feb-2009
- */
-public class SimpleTestCase extends OSGiRuntimeTest
-{
-   @Test
-   public void testSimpleBundle() throws Exception
-   {
-      // Get the default runtime
-      OSGiRuntime runtime = getDefaultRuntime();
-      try
-      {
-         // Install the bundle
-         OSGiBundle bundle = runtime.installBundle("example-simple.jar");
-         assertBundleState(Bundle.INSTALLED, bundle.getState());
-
-         // Start the bundle
-         bundle.start();
-         assertBundleState(Bundle.ACTIVE, bundle.getState());
-
-         // Uninstall the bundle
-         bundle.uninstall();
-         assertBundleState(Bundle.UNINSTALLED, bundle.getState());
-      }
-      finally
-      {
-         // Shutdown the runtime 
-         runtime.shutdown();
-      }
-   }
-}
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/testsuite/functional/scripts/antrun-test-jars.xml
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/scripts/antrun-test-jars.xml	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/trunk/testsuite/functional/scripts/antrun-test-jars.xml	2010-03-22 18:12:49 UTC (rev 102729)
@@ -28,6 +28,7 @@
  [JBOSGI-284] Investigate version numbering scheme
  [JBOSGI-286] Investigate classloading of javax.* classes
  [JBOSGI-287] Optional import loaded from system classloader
+ [JBOSGI-298] InstanceAlreadyExistsException: osgi.core:type=framework,version=1.5
 
 -->
 

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi142/OSGi142TestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi142/OSGi142TestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi142/OSGi142TestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -23,17 +23,13 @@
 
 //$Id: OSGI142TestCase.java 87103 2009-04-09 22:18:31Z thomas.diesler at jboss.com $
 
-import org.jboss.osgi.spi.framework.OSGiBootstrap;
-import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
 import org.jboss.osgi.testing.OSGiFrameworkTest;
-import org.jboss.osgi.testing.OSGiRuntimeTest;
 import org.jboss.test.osgi.jbosgi142.bundleA.BeanA;
 import org.jboss.test.osgi.jbosgi142.bundleB.BeanB;
 import org.jboss.test.osgi.jbosgi142.bundleX.BeanX;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.launch.Framework;
 
 /**
  * [JBOSGI-142] Investigate classloading space

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi143/OSGi143TestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi143/OSGi143TestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi143/OSGi143TestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -26,16 +26,12 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
-import org.jboss.osgi.spi.framework.OSGiBootstrap;
-import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
 import org.jboss.osgi.testing.OSGiFrameworkTest;
-import org.jboss.osgi.testing.OSGiRuntimeTest;
 import org.jboss.test.osgi.jbosgi143.bundleA.BeanA;
 import org.jboss.test.osgi.jbosgi143.bundleX.BeanX;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.launch.Framework;
 
 /**
  * [JBOSGI-143] Add initial support for DynamicImport-Package

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/services/ServiceRegistrationTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/services/ServiceRegistrationTestCase.java	2010-03-22 17:35:30 UTC (rev 102728)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/services/ServiceRegistrationTestCase.java	2010-03-22 18:12:49 UTC (rev 102729)
@@ -105,9 +105,12 @@
             Bundle[] users = sref.getUsingBundles();
             
             System.out.println("FIXME [JBOSGI-305] Clarify ServiceReference.getUsingBundles() in ServiceFactory.ungetService()");
-            //assertNotNull("Users not null", users);
-            //assertEquals(1, users.length);
-            //assertEquals(bundle, users[0]);
+            if ("equinox".equals(getFrameworkName()) == false)
+            {
+               assertNotNull("Users not null", users);
+               assertEquals(1, users.length);
+               assertEquals(bundle, users[0]);
+            }
             
             allGood[1] = true;
          }



More information about the jboss-osgi-commits mailing list