All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bndtools.utils.ServiceUtils Maven / Gradle / Ivy

The newest version!
package bndtools.utils;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class ServiceUtils {
	public static final  R usingService(BundleContext context, Class clazz,
		ServiceOperation operation) throws E {
		ServiceReference reference = context.getServiceReference(clazz);
		if (reference != null) {
			S service = context.getService(reference);
			if (service != null) {
				try {
					return operation.execute(service);
				} finally {
					context.ungetService(reference);
				}
			}
		}
		return null;
	}
}