blob: b53b5fa4e4e8c831b1a215b4a4d1c6d620cb75a1 [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 {
10 @Override
11 public <T> T get(Class<T> serviceClass) {
12 BundleContext bc = FrameworkUtil.getBundle(serviceClass).getBundleContext();
13 T impl = bc.getService(bc.getServiceReference(serviceClass));
14 if (impl == null) {
15 throw new ServiceNotFoundException("Service " + serviceClass.getName() + " not found");
16 }
17 return impl;
18 }
19}