blob: d4839913a5789e7a3d1739cf2ed4471a51acf057 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.onlab.osgi;
2
3import org.osgi.framework.BundleContext;
4import org.osgi.framework.FrameworkUtil;
5
6/**
7 * Default implementation of the service directory using OSGi framework utilities.
8 */
9public class DefaultServiceDirectory implements ServiceDirectory {
tom0872a172014-09-23 11:24:26 -070010
11 /**
12 * Returns the reference to the implementation of the specified service.
13 *
14 * @param serviceClass service class
15 * @param <T> type of service
16 * @return service implementation
17 */
18 public static <T> T getService(Class<T> serviceClass) {
tom0eb04ca2014-08-25 14:34:51 -070019 BundleContext bc = FrameworkUtil.getBundle(serviceClass).getBundleContext();
20 T impl = bc.getService(bc.getServiceReference(serviceClass));
21 if (impl == null) {
22 throw new ServiceNotFoundException("Service " + serviceClass.getName() + " not found");
23 }
24 return impl;
25 }
tom0872a172014-09-23 11:24:26 -070026
27 @Override
28 public <T> T get(Class<T> serviceClass) {
29 return getService(serviceClass);
30 }
31
tom0eb04ca2014-08-25 14:34:51 -070032}