Created
August 2, 2023 23:29
-
-
Save marcosblandim/2e9029ee7e2acb459d787e05e13b33a9 to your computer and use it in GitHub Desktop.
Liferay: get OSGi Service with ServiceTracker in Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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