Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcosblandim/2e9029ee7e2acb459d787e05e13b33a9 to your computer and use it in GitHub Desktop.
Save marcosblandim/2e9029ee7e2acb459d787e05e13b33a9 to your computer and use it in GitHub Desktop.
Liferay: get OSGi Service with ServiceTracker in Groovy
import org.osgi.framework.Bundle
import org.osgi.framework.BundleContext
import org.osgi.framework.FrameworkUtil
import org.osgi.util.tracker.ServiceTracker
static <T> T getService(Class<T> clazz) {
Bundle bundle = FrameworkUtil.getBundle(clazz)
BundleContext bundleContext = bundle.getBundleContext()
ServiceTracker serviceTracker = new ServiceTracker(bundleContext, clazz, null)
serviceTracker.open()
T service = (T) serviceTracker.waitForService(500)
serviceTracker.close()
return service
}
println getService(any.osgi.service.package.AnOSGIService.class)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment