OSGiServiceUtils

public final class OSGiServiceUtils

Utility class for retrieving OSGi services.

Methods

findService

public static <T> T findService(BundleContext bundleContext, Class<T> clazz)

Retrieves the service of the given class from the bundle context. This will retrieve the service with highest priority if there are multiple instances of the service.

Parameters:
  • bundleContext – the bundleContext which will be used for service retrieval
  • clazz – the class of the service to be retrieved
  • <T> – the type of the service to be returned
Returns:

the service found or null if there is no such service in the bundle context

findService

public static <T> T findService(BundleContext bundleContext, String className)

Retrieves the service of the given class from the bundle context. This will retrieve the service with highest priority if there are multiple instances of the service.

Parameters:
  • bundleContext – the bundleContext which will be used for service retrieval
  • className – the class name of the service to be retrieved
  • <T> – the type of the service to be returned
Returns:

the service found or null if there is no such service in the bundle context

findService

public static <T> T findService(BundleContext bundleContext, Class<T> clazz, long timeout)

Retrieves the service of the given class from the bundle context. This will retrieve the service with highest priority if there are multiple instances of the service. Based on the timeout parameter representing the max wait time for the service, the lookup for the service will be performed multiple times in one second intervals. The lookup will be performed at least once.

Parameters:
  • bundleContext – the bundleContext which will be used for service retrieval
  • clazz – the class of the service to be retrieved
  • timeout – the max time that will be spent waiting for the service, in miliseconds
  • <T> – the type of the service to be returned
Returns:

the service found or null if there is no such service in the bundle context

findService

public static <T> T findService(BundleContext bundleContext, String className, long timeout)

Retrieves the service of the given class from the bundle context. This will retrieve the service with highest priority if there are multiple instances of the service. Based on the timeout parameter representing the max wait time for the service, the lookup for the service will be performed multiple times in one second intervals. The lookup will be performed at least once.

Parameters:
  • bundleContext – the bundleContext which will be used for service retrieval
  • className – the class name of the service to be retrieved
  • timeout – the max time that will be spent waiting for the service, in miliseconds
  • <T> – the type of the service to be returned
Returns:

the service found or null if there is no such service in the bundle context