Esempio n. 1
0
 @Override
 protected void doExecute(List<Bundle> bundles) throws Exception {
   for (Bundle bundle : bundles) {
     ServiceReference<?>[] refs =
         (inUse) ? bundle.getServicesInUse() : bundle.getRegisteredServices();
     printServices(bundle, refs, showProperties);
   }
 }
  @Test
  public void testUnregister() throws Exception {
    Bundle bundle1 = installBundle(getBundleArchiveA());
    try {
      bundle1.start();
      BundleContext context1 = bundle1.getBundleContext();
      assertNotNull(context1);

      SimpleServiceFactory factory = new SimpleServiceFactory(context1, null);
      ServiceRegistration sreg1 =
          context1.registerService(BundleContext.class.getName(), factory, null);
      assertNotNull(sreg1);

      ServiceReference sref1 = sreg1.getReference();
      assertNotNull(sref1);

      ServiceReference sref2 = context1.getServiceReference(BundleContext.class.getName());
      assertEquals(sref1, sref2);

      ServiceReference[] inUse = bundle1.getServicesInUse();
      assertNull(inUse);

      context1.getService(sref1);
      inUse = bundle1.getServicesInUse();
      assertArrayEquals(new ServiceReference[] {sref1}, inUse);

      Bundle bundle2 = installBundle(getBundleArchiveB());
      try {
        bundle2.start();
        BundleContext context2 = bundle2.getBundleContext();
        assertNotNull(context2);
        context2.getService(sref1);
        inUse = bundle2.getServicesInUse();
        assertArrayEquals(new ServiceReference[] {sref1}, inUse);

        assertNull(factory.ungetBundle);
        assertNull(factory.ungetRegistration);
        assertNull(factory.ungetService);

        context1.addServiceListener(this);
        sreg1.unregister();

        sref2 = context1.getServiceReference(BundleContext.class.getName());
        assertNull("" + sref2, sref2);

        Object actual = context1.getService(sref1);
        assertNull("" + actual, actual);

        assertServiceEvent(ServiceEvent.UNREGISTERING, sref1);

        inUse = bundle1.getServicesInUse();
        assertNull(inUse);
        inUse = bundle2.getServicesInUse();
        assertNull(inUse);

        assertEquals(sreg1, factory.ungetRegistration);
        assertEquals(context1, factory.ungetService);
      } finally {
        bundle2.uninstall();
      }

      try {
        sreg1.unregister();
        fail("Should not be here!");
      } catch (IllegalStateException t) {
        // expected
      }
    } finally {
      bundle1.uninstall();
    }
  }
 public long[] getServicesInUse(long bid) {
   Bundle b = Activator.bc.getBundle(bid);
   return Util.referencesToLong(b.getServicesInUse());
 }