Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
package routines;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
public class OsgiHelper {
private OsgiHelper() {
}
public static <T> T getService(Class<T> clazz, String fullJobName) throws InvalidSyntaxException {
Bundle bundle = FrameworkUtil.getBundle(clazz);
if (bundle != null) {
BundleContext context = bundle.getBundleContext();
ServiceReference[] serviceReferences =
context.getServiceReferences(clazz.getName(), "(&(name=" + fullJobName.substring(fullJobName.lastIndexOf('.') + 1) + ")(type=job))");
if (null != serviceReferences) {
return clazz.cast(context.getService(serviceReferences));
}
}
return null;
}
}