@Test
  public void testOwnerCannotSeeServiceClass() throws Exception {
    final Bundle bundleA = installBundle(getBundleArchiveA());
    final Bundle bundleB = installBundle(getBundleArchiveB());
    try {
      bundleA.start();
      bundleB.start();

      BundleContext contextA = bundleA.getBundleContext();
      BundleContext contextB = bundleB.getBundleContext();

      final CountDownLatch latch = new CountDownLatch(1);
      ServiceListener listener =
          new ServiceListener() {
            public void serviceChanged(ServiceEvent event) {
              latch.countDown();
            }
          };
      contextB.addServiceListener(listener);

      Object service = bundleB.loadClass(B.class.getName()).newInstance();
      ServiceRegistration reg = contextA.registerService(B.class.getName(), service, null);

      ServiceReference ref = reg.getReference();
      assertTrue(ref.isAssignableTo(bundleA, B.class.getName()));
      assertTrue(ref.isAssignableTo(bundleB, B.class.getName()));

      if (latch.await(5, TimeUnit.SECONDS) == false) throw new TimeoutException();

    } finally {
      bundleA.uninstall();
      bundleB.uninstall();
    }
  }
  @Test
  public void testRegBundleIsRefBundle() throws Exception {
    Bundle bundle = installBundle(getBundleArchiveA());
    try {
      bundle.start();

      final CountDownLatch latch = new CountDownLatch(1);
      ServiceListener listener =
          new ServiceListener() {
            public void serviceChanged(ServiceEvent event) {
              latch.countDown();
            }
          };
      BundleContext context = bundle.getBundleContext();
      context.addServiceListener(listener);

      Object service = bundle.loadClass(A.class.getName()).newInstance();
      ServiceRegistration reg = context.registerService(A.class.getName(), service, null);

      ServiceReference ref = reg.getReference();
      assertTrue(ref.isAssignableTo(bundle, A.class.getName()));

      if (latch.await(5, TimeUnit.SECONDS) == false) throw new TimeoutException();

    } finally {
      bundle.uninstall();
    }
  }
  @Test
  public void testOwnerAndRegBundleUseDifferentSource() throws Exception {
    final Bundle bundleA = installBundle(getBundleArchiveA());
    final Bundle bundleB = installBundle(getBundleArchiveC());
    try {
      bundleA.start();
      bundleB.start();

      BundleContext contextA = bundleA.getBundleContext();

      Object service = bundleA.loadClass(A.class.getName()).newInstance();
      ServiceRegistration reg = contextA.registerService(A.class.getName(), service, null);

      ServiceReference ref = reg.getReference();
      assertTrue(ref.isAssignableTo(bundleA, A.class.getName()));
      assertFalse(ref.isAssignableTo(bundleB, A.class.getName()));
    } finally {
      bundleA.uninstall();
      bundleB.uninstall();
    }
  }
예제 #4
0
 <T> T getService(URI bundleURI, Class<T> svcClazz) {
   Bundle bundle = get().get(bundleURI);
   if (bundle == null) {
     synchronized (this) {
       bundle = startBundle(bundleURI);
     }
   }
   BundleContext ctx = bundle.getBundleContext();
   for (ServiceReference<?> ref : bundle.getRegisteredServices()) {
     if (ref.isAssignableTo(bundle, svcClazz.getName())) {
       return svcClazz.cast(ctx.getService(ref));
     }
   }
   return null;
 }
  @Test
  public void testRegValueIsServiceFactory() throws Exception {
    final Bundle bundleA = installBundle(getBundleArchiveA());
    final Bundle bundleB = installBundle(getBundleArchiveB());
    try {
      bundleA.start();
      bundleB.start();

      BundleContext contextA = bundleA.getBundleContext();
      BundleContext contextB = bundleB.getBundleContext();

      final CountDownLatch latch = new CountDownLatch(1);
      ServiceListener listener =
          new ServiceListener() {
            public void serviceChanged(ServiceEvent event) {
              latch.countDown();
            }
          };
      contextB.addServiceListener(listener);

      Object service =
          new ServiceFactory() {
            public Object getService(Bundle bundle, ServiceRegistration registration) {
              try {
                return bundleA.loadClass(A.class.getName()).newInstance();
              } catch (Exception e) {
                throw new IllegalStateException(e);
              }
            }

            public void ungetService(
                Bundle bundle, ServiceRegistration registration, Object service) {}
          };
      ServiceRegistration reg = contextA.registerService(A.class.getName(), service, null);

      ServiceReference ref = reg.getReference();
      assertTrue(ref.isAssignableTo(bundleA, A.class.getName()));
      assertTrue(ref.isAssignableTo(bundleB, A.class.getName()));

      if (latch.await(5, TimeUnit.SECONDS) == false) throw new TimeoutException();

    } finally {
      bundleA.uninstall();
      bundleB.uninstall();
    }
  }