List of usage examples for org.springframework.core.env EnumerablePropertySource EnumerablePropertySource
public EnumerablePropertySource(String name, T source)
From source file:org.springframework.xd.module.options.FlattenedCompositeModuleOptionsMetadata.java
@Override public ModuleOptions interpolate(Map<String, String> raw) throws BindException { @SuppressWarnings("serial") Map<ModuleOptionsMetadata, Map<String, String>> distributed = new HashMap<ModuleOptionsMetadata, Map<String, String>>() { @Override/*from w ww. j a v a 2 s.c o m*/ public Map<String, String> get(Object key) { Map<String, String> result = super.get(key); if (result == null) { result = new HashMap<String, String>(); this.put((ModuleOptionsMetadata) key, result); } return result; } }; Map<String, String> copy = new HashMap<String, String>(raw); // First, distribute raw input values to their corresponding MOM for (String key : raw.keySet()) { for (ModuleOptionsMetadata mom : momToSupportedOptions.keySet()) { Set<String> optionNames = momToSupportedOptions.get(mom); if (optionNames.contains(key)) { distributed.get(mom).put(key, copy.remove(key)); break; } } } if (copy.size() > 0) { // We're just interested in a container for errors BindingResult bindingResult = new MapBindingResult(new HashMap<String, Object>(), "flattened"); for (String pty : copy.keySet()) { bindingResult.addError( new FieldError("flattened", pty, String.format("option named '%s' is not supported", pty))); } throw new BindException(bindingResult); } // Then, interpolate per-MOM and remember which MOM is responsible for which exposed value // TODO: should be multimap, as several MOMs could expose the same value final Map<String, ModuleOptions> nameToOptions = new HashMap<String, ModuleOptions>(); for (ModuleOptionsMetadata mom : momToSupportedOptions.keySet()) { Map<String, String> rawValuesSubset = distributed.get(mom); ModuleOptions mo = mom.interpolate(rawValuesSubset); EnumerablePropertySource<?> propertySource = mo.asPropertySource(); for (String optionName : propertySource.getPropertyNames()) { /* * XD-1472: InputType treated as a special case. If the module defines a default value, do not replace * it with a null value (the global default) */ if (optionName.equals(INPUT_TYPE)) { if (propertySource.getProperty(optionName) != null) { nameToOptions.put(optionName, mo); } } else { nameToOptions.put(optionName, mo); } } } final Set<ModuleOptions> uniqueModuleOptions = new HashSet<ModuleOptions>(nameToOptions.values()); return new ModuleOptions() { @Override public EnumerablePropertySource<FlattenedCompositeModuleOptionsMetadata> asPropertySource() { String psName = String.format("flattened-%d", System.identityHashCode(FlattenedCompositeModuleOptionsMetadata.this)); return new EnumerablePropertySource<FlattenedCompositeModuleOptionsMetadata>(psName, FlattenedCompositeModuleOptionsMetadata.this) { @Override public String[] getPropertyNames() { String[] result = nameToOptions.keySet().toArray(new String[nameToOptions.keySet().size()]); FlattenedCompositeModuleOptionsMetadata.logger.debug(String.format( "returning propertyNames: %s", StringUtils.arrayToCommaDelimitedString(result))); return result; } @Override public Object getProperty(String name) { ModuleOptions moduleOptions = nameToOptions.get(name); return moduleOptions == null ? null : moduleOptions.asPropertySource().getProperty(name); } }; } @Override public String[] profilesToActivate() { List<String> result = new ArrayList<String>(); for (ModuleOptions delegate : uniqueModuleOptions) { result.addAll(Arrays.asList(delegate.profilesToActivate())); } return result.toArray(new String[result.size()]); } @Override public void validate() { for (ModuleOptions delegate : uniqueModuleOptions) { delegate.validate(); } } }; }