ServicesListProvider implements ResourceListProvider { @Override public Map<String, String> getList(String listName, ResourceListQuery query) throws ListProviderException { ServicesListQuery servicesQuery; try { servicesQuery = (ServicesListQuery) query; } catch (ClassCastException ex) { servicesQuery = new ServicesListQuery(query); } Map<String, String> result = new HashMap<String,String>(); if (LIST_STATUS.equals(listName)) { for (ServiceState s : ServiceState.values()) { result.put(s.toString(), SERVICE_STATUS_FILTER_PREFIX + s.toString()); } return result; } List<ServiceRegistration> serviceRegistrations; try { serviceRegistrations = serviceRegistry.getServiceRegistrations(); } catch (ServiceRegistryException ex) { throw new ListProviderException("Failed to get service registrations.", ex); } for (ServiceRegistration serviceRegistration : serviceRegistrations) { if (servicesQuery.getHostname().isSome() && !StringUtils.equals(servicesQuery.getHostname().get(), serviceRegistration.getHost())) continue; if (servicesQuery.getActions().isSome() && servicesQuery.getActions().get() && serviceRegistration.getServiceState() == ServiceState.NORMAL) continue; result.put(serviceRegistration.getServiceType(), serviceRegistration.getServiceType()); } if (servicesQuery.getLimit().isSome() || servicesQuery.getLimit().isSome()) { int limit = servicesQuery.getLimit().getOrElse(0); int offset = servicesQuery.getOffset().getOrElse(0); result = new SmartIterator(limit, offset).applyLimitAndOffset(result); } return result; }  @Override String[] getListNames(); @Override Map<String, String> getList(String listName, ResourceListQuery query); void setServiceRegistry(ServiceRegistry serviceRegistry); @Override boolean isTranslatable(String listName); @Override String getDefault(); static final String LIST_NAME; static final String LIST_STATUS; static final String SERVICE_STATUS_FILTER_PREFIX; }
@Test public void testListNames() throws ListProviderException { assertEquals(3, servicesListProvider.getList("", servicesQuery).size()); assertEquals(3, servicesListProvider.getList(ServicesListProvider.LIST_NAME, servicesQuery).size()); assertEquals(3, servicesListProvider.getList(ServicesListProvider.LIST_STATUS, servicesQuery).size()); }
@Test public void testNameList() throws ListProviderException { Map<String, String> list = servicesListProvider.getList(ServicesListProvider.LIST_NAME, servicesQuery); assertEquals(SERVICE_TYPE_1, list.get(SERVICE_TYPE_1)); assertEquals(SERVICE_TYPE_2, list.get(SERVICE_TYPE_2)); assertEquals(SERVICE_TYPE_3, list.get(SERVICE_TYPE_3)); }
@Test public void testStatusList() throws ListProviderException { Map<String, String> list = servicesListProvider.getList(ServicesListProvider.LIST_STATUS, servicesQuery); for (ServiceState state : ServiceState.values()) { assertEquals(ServicesListProvider.SERVICE_STATUS_FILTER_PREFIX + state.toString(), list.get(state.toString())); } }