@Test public void testGetServiceWithFilter_List() throws InvalidSyntaxException{ Object result = mock(Object.class); String filter = "dummy"; ServiceReference[] refs = new ServiceReference[]{mock(ServiceReference.class), mock(ServiceReference.class)}; when(bc.getServiceReferences(Object.class.getName(), filter)).thenReturn(refs); when(bc.getService(refs[0])).thenReturn(result); List<ServiceReference<?>> list = new ArrayList<ServiceReference<?>>(); Object S = ServiceUtil.getService(bc, Object.class, filter, list); assertTrue(list.get(0).equals(refs[0])); }
public static <S> S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices) { try { ServiceReference<?> ref = bc.getServiceReference(serviceClass.getName()); if ( ref == null ) throw new NullPointerException("ServiceReference is null for: " + serviceClass.getName()); if (gottenServices != null) gottenServices.add(ref); return serviceClass.cast( bc.getService(ref) ); } catch (Exception e) { throw new RuntimeException("Couldn't find service: " + serviceClass.getName(),e); } }
ServiceUtil { public static <S> S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices) { try { ServiceReference<?> ref = bc.getServiceReference(serviceClass.getName()); if ( ref == null ) throw new NullPointerException("ServiceReference is null for: " + serviceClass.getName()); if (gottenServices != null) gottenServices.add(ref); return serviceClass.cast( bc.getService(ref) ); } catch (Exception e) { throw new RuntimeException("Couldn't find service: " + serviceClass.getName(),e); } } }
ServiceUtil { public static <S> S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices) { try { ServiceReference<?> ref = bc.getServiceReference(serviceClass.getName()); if ( ref == null ) throw new NullPointerException("ServiceReference is null for: " + serviceClass.getName()); if (gottenServices != null) gottenServices.add(ref); return serviceClass.cast( bc.getService(ref) ); } catch (Exception e) { throw new RuntimeException("Couldn't find service: " + serviceClass.getName(),e); } } private  ServiceUtil(); }
ServiceUtil { public static <S> S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices) { try { ServiceReference<?> ref = bc.getServiceReference(serviceClass.getName()); if ( ref == null ) throw new NullPointerException("ServiceReference is null for: " + serviceClass.getName()); if (gottenServices != null) gottenServices.add(ref); return serviceClass.cast( bc.getService(ref) ); } catch (Exception e) { throw new RuntimeException("Couldn't find service: " + serviceClass.getName(),e); } } private  ServiceUtil(); static S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices); static S getService(BundleContext bc, Class<S> serviceClass, String filter, List<ServiceReference<?>> gottenServices); static void registerServiceListener(BundleContext bc, Object listener, String registerMethodName, 
			String unregisterMethodName, Class<?> serviceClass, Class<?> methodClass, String additionalFilter, List<CyServiceListener> serviceListeners); static void registerServiceListener(BundleContext bc, BiConsumer<S,Map<String,String>> registerConsumer, 
			BiConsumer<S,Map<String,String>> unregisterConsumer, Class<S> serviceClass, String additionalFilter, List<CyServiceListener> serviceListeners); static void registerService(final BundleContext bc, final Object service, final Class<?> serviceClass, final Properties props, 
			Map<Class,Map<Object,ServiceRegistration>> serviceRegistrations); static Dictionary<String, ?> getDictionary(Properties props); static void registerAllServices(final BundleContext bc, final Object service, final Properties props, 
			Map<Class,Map<Object,ServiceRegistration>> serviceRegistrations); }
ServiceUtil { public static <S> S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices) { try { ServiceReference<?> ref = bc.getServiceReference(serviceClass.getName()); if ( ref == null ) throw new NullPointerException("ServiceReference is null for: " + serviceClass.getName()); if (gottenServices != null) gottenServices.add(ref); return serviceClass.cast( bc.getService(ref) ); } catch (Exception e) { throw new RuntimeException("Couldn't find service: " + serviceClass.getName(),e); } } private  ServiceUtil(); static S getService(BundleContext bc, Class<S> serviceClass, List<ServiceReference<?>> gottenServices); static S getService(BundleContext bc, Class<S> serviceClass, String filter, List<ServiceReference<?>> gottenServices); static void registerServiceListener(BundleContext bc, Object listener, String registerMethodName, 
			String unregisterMethodName, Class<?> serviceClass, Class<?> methodClass, String additionalFilter, List<CyServiceListener> serviceListeners); static void registerServiceListener(BundleContext bc, BiConsumer<S,Map<String,String>> registerConsumer, 
			BiConsumer<S,Map<String,String>> unregisterConsumer, Class<S> serviceClass, String additionalFilter, List<CyServiceListener> serviceListeners); static void registerService(final BundleContext bc, final Object service, final Class<?> serviceClass, final Properties props, 
			Map<Class,Map<Object,ServiceRegistration>> serviceRegistrations); static Dictionary<String, ?> getDictionary(Properties props); static void registerAllServices(final BundleContext bc, final Object service, final Properties props, 
			Map<Class,Map<Object,ServiceRegistration>> serviceRegistrations);  }