Java Code Examples for org.osgi.util.tracker.ServiceTracker#getService()

The following examples show how to use org.osgi.util.tracker.ServiceTracker#getService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogReaderServiceTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testExtendedLogReaderServiceAvailable() throws Exception {
    Framework f = IntegrationTest.findFramework();
    BundleContext bc = f.getBundleContext();
    
    ServiceTracker logReaderTracker = new ServiceTracker(bc, ExtendedLogReaderService.class.getName(), null);
    logReaderTracker.open();
            
    LogReaderService logReader = (ExtendedLogReaderService) logReaderTracker.getService();
    assertNotNull(logReader);
        
}
 
Example 2
Source File: Activator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the preference service, or <code>null</code> if not available.
 */
public static IPreferencesService getPreferenceService() {
	// protect against concurrent shutdown
	Activator a = singleton;
	if (a == null)
		return null;
	ServiceTracker<IPreferencesService, IPreferencesService> tracker = a.getPrefTracker();
	if (tracker == null)
		return null;
	return tracker.getService();
}
 
Example 3
Source File: ServiceUtil.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
public static <S, T> T getNotNull(ServiceTracker<S, T> serviceTracker) {
    T service = serviceTracker.getService();
    if (service == null)
        throw new IllegalStateException("Service is null");

    return service;
}
 
Example 4
Source File: ComponentTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test that SCR handles factory CM pids with target filters.
 * 
 */

public void runTest() {
  Bundle b1 = null;
  ServiceTracker<ConfigurationAdmin, ConfigurationAdmin> cmt = null;
  try {
    b1 = Util.installBundle(bc, "componentC_test-1.0.0.jar");
    b1.start();
    final String b1loc = b1.getLocation();

    cmt = new ServiceTracker<ConfigurationAdmin,ConfigurationAdmin>(bc, ConfigurationAdmin.class.getName(), null);
    cmt.open();
    
    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.service.componentC_test.ComponentU");
    assertNull("Should not get serviceRef U", ref);

    ConfigurationAdmin ca = cmt.getService();
    Configuration c = ca.createFactoryConfiguration("componentC_test.U", b1loc);

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("vRef.target", "(vnum=v0)");
    c.update(props);
    
    Thread.sleep(SLEEP_TIME);

    ref = bc.getServiceReference("org.knopflerfish.service.componentC_test.ComponentU");
    assertNull("Should not get serviceRef U", ref);

    props.put("vRef.target", "(vnum=v1)");
    c.update(props);
    
    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> [] refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", null);
    assertTrue("Should get one serviceRef to ComponentU", refs != null && refs.length == 1);

    Configuration c2 = ca.createFactoryConfiguration("componentC_test.U", b1loc);
    props = new Hashtable<String, Object>();
    props.put("vRef.target", "(vnum=v2)");
    c2.update(props);
    
    Thread.sleep(SLEEP_TIME);

    refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", null);
    assertTrue("Should get two serviceRef to ComponentU", refs != null && refs.length == 2);

    refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", "(vRef.target=\\(vnum=v1\\))");
    assertTrue("Should get one serviceRef to ComponentU with ref v1", refs != null && refs.length == 1);
    org.knopflerfish.service.componentC_test.ComponentU u =
        (org.knopflerfish.service.componentC_test.ComponentU)bc.getService(refs[0]);
    assertEquals("Should get v1 version", "v1", u.getV());

    refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", "(vRef.target=\\(vnum=v2\\))");
    assertTrue("Should get one serviceRef to ComponentU with ref v2", refs != null && refs.length == 1);
    org.knopflerfish.service.componentC_test.ComponentU u2 =
        (org.knopflerfish.service.componentC_test.ComponentU)bc.getService(refs[0]);
    assertNotSame("Services should differ", u, u2);
    assertEquals("Should get v2 version", "v2", u2.getV());
  } catch (Exception e) {
    e.printStackTrace();
    fail("Test11: got unexpected exception " + e);
  } finally {
    if (b1 != null) {
      try {
        if (cmt != null) {
          deleteConfig(cmt.getService(), b1.getLocation());
          cmt.close();
        }
        b1.uninstall();
      } catch (Exception be) {
        be.printStackTrace();
        fail("Test11: got uninstall exception " + be);
      }
    }
  }
}