static List<UriBasedServiceData> readServicesData(LinkedHashMap<String, Properties> propertySources) { // we'll turn this into KVPs to return but need to eliminate duplicates first Map<String, String> collectedServices = new HashMap<String, String>(); // iterate over the property sources in order, extracting matching properties for (Map.Entry<String, Properties> propertySource : propertySources.entrySet()) { if (propertySource.getValue().isEmpty()) { logger.info("no " + propertySource.getKey()); continue; } logger.info("reading services from " + propertySource.getKey()); Map<String, String> services = readServices(propertySource.getValue()); // add each of the found services to the list, warning about duplicates for (Map.Entry<String, String> service : services.entrySet()) { String oldUri = collectedServices.put(service.getKey(), service.getValue()); if (oldUri == null) logger.info("added service '" + service.getKey() + "' from " + propertySource.getKey()); else logger.warning("replaced service '" + service.getKey() + "' with new URI from " + propertySource.getKey()); } } // now we have a collated set of service IDs and URIs List<UriBasedServiceData> serviceData = new ArrayList<UriBasedServiceData>(collectedServices.size()); for (Map.Entry<String, String> serviceInfo : collectedServices.entrySet()) { serviceData.add(new UriBasedServiceData(serviceInfo.getKey(), serviceInfo.getValue())); } return serviceData; }
@Override protected List<UriBasedServiceData> getServicesData() { if (fileProperties == null) throw new IllegalStateException("isInMatchingCloud() must be called first to initialize connector"); LinkedHashMap<String, Properties> propertySources = new LinkedHashMap<String, Properties>(); propertySources.put("properties from file", fileProperties); try { propertySources.put("system properties", env.getSystemProperties()); } catch (SecurityException e) { logger.log(Level.WARNING, "couldn't read system properties; no service definitions from system properties will be applied", e); } return LocalConfigUtil.readServicesData(propertySources); }
@Override protected List<UriBasedServiceData> getServicesData() { if (fileProperties == null) throw new IllegalStateException("isInMatchingCloud() must be called first to initialize connector"); LinkedHashMap<String, Properties> propertySources = new LinkedHashMap<String, Properties>(); propertySources.put("properties from file", fileProperties); try { propertySources.put("system properties", env.getSystemProperties()); } catch (SecurityException e) { logger.log(Level.WARNING, "couldn't read system properties; no service definitions from system properties will be applied", e); } return LocalConfigUtil.readServicesData(propertySources); }
static List<UriBasedServiceData> readServicesData(LinkedHashMap<String, Properties> propertySources) { // we'll turn this into KVPs to return but need to eliminate duplicates first Map<String, String> collectedServices = new HashMap<String, String>(); // iterate over the property sources in order, extracting matching properties for (Map.Entry<String, Properties> propertySource : propertySources.entrySet()) { if (propertySource.getValue().isEmpty()) { logger.info("no " + propertySource.getKey()); continue; } logger.info("reading services from " + propertySource.getKey()); Map<String, String> services = readServices(propertySource.getValue()); // add each of the found services to the list, warning about duplicates for (Map.Entry<String, String> service : services.entrySet()) { String oldUri = collectedServices.put(service.getKey(), service.getValue()); if (oldUri == null) logger.info("added service '" + service.getKey() + "' from " + propertySource.getKey()); else logger.warning("replaced service '" + service.getKey() + "' with new URI from " + propertySource.getKey()); } } // now we have a collated set of service IDs and URIs List<UriBasedServiceData> serviceData = new ArrayList<UriBasedServiceData>(collectedServices.size()); for (Map.Entry<String, String> serviceInfo : collectedServices.entrySet()) { serviceData.add(new UriBasedServiceData(serviceInfo.getKey(), serviceInfo.getValue())); } return serviceData; }
@Test public void testOverride() { first.setProperty("spring.cloud.duplicate", "firstUri"); second.setProperty("spring.cloud.duplicate", "secondUri"); List<UriBasedServiceData> serviceData = LocalConfigUtil.readServicesData(propertySources); assertEquals(1, serviceData.size()); UriBasedServiceData kvp = serviceData.get(0); assertEquals("duplicate", kvp.getKey()); assertEquals("secondUri", kvp.getUri()); } }
@Test public void testPropertyParsing() { first.setProperty("spring.cloud.appId", "should skip me because I'm meta"); first.setProperty("spring.cloud.service1", "one"); first.setProperty("spring.cloud.", "should skip me because I don't have an ID"); first.setProperty("spring.cloud.service.two", "two"); first.setProperty("foobar", "should skip me because I don't match the prefix"); Map<String, String> services = LocalConfigUtil.readServices(first); assertEquals(2, services.size()); assertEquals("one", services.get("service1")); assertEquals("two", services.get("service.two")); }
@Test public void testCollation() { first.setProperty("spring.cloud.first", "firstUri"); second.setProperty("spring.cloud.second", "secondUri"); List<UriBasedServiceData> serviceData = LocalConfigUtil.readServicesData(propertySources); assertEquals(2, serviceData.size()); boolean foundFirst = false; for (UriBasedServiceData kvp : serviceData) { if (kvp.getKey().equals("first")) { assertEquals("firstUri", kvp.getUri()); foundFirst = true; } } assertTrue(foundFirst); }