tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 1 | package org.onlab.osgi; |
| 2 | |
| 3 | import org.osgi.framework.BundleContext; |
| 4 | import org.osgi.framework.FrameworkUtil; |
| 5 | |
| 6 | /** |
| 7 | * Default implementation of the service directory using OSGi framework utilities. |
| 8 | */ |
| 9 | public 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 | } |