@Override public String[] getPropertyNames() { Set<String> names = new LinkedHashSet<>(); for (PropertySource<?> propertySource : this.propertySources) { if (!(propertySource instanceof EnumerablePropertySource)) { throw new IllegalStateException( "Failed to enumerate property names due to non-enumerable property source: " + propertySource); } names.addAll(Arrays.asList(((EnumerablePropertySource<?>) propertySource).getPropertyNames())); } return StringUtils.toStringArray(names); }
private static Map<String, Object> doExtraProperties(ConfigurableEnvironment environment) { Map<String, Object> properties = new LinkedHashMap<>(); // orderly Map<String, PropertySource<?>> map = doGetPropertySources(environment); for (PropertySource<?> source : map.values()) { if (source instanceof EnumerablePropertySource) { EnumerablePropertySource propertySource = (EnumerablePropertySource) source; String[] propertyNames = propertySource.getPropertyNames(); if (ObjectUtils.isEmpty(propertyNames)) { continue; } for (String propertyName : propertyNames) { if (!properties.containsKey(propertyName)) { // put If absent properties.put(propertyName, propertySource.getProperty(propertyName)); } } } } return properties; }
@Test public void withDefaultNonOptionArgsNameAndNoNonOptionArgsPresent() { EnumerablePropertySource<?> ps = new SimpleCommandLinePropertySource("--o1=v1", "--o2"); assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.containsProperty("o1"), is(true)); assertThat(ps.containsProperty("o2"), is(true)); assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.getProperty("nonOptionArgs"), nullValue()); assertThat(ps.getPropertyNames().length, is(2)); }
/** * Return whether this {@code PropertySource} contains a property with the given name. * <p>This implementation checks for the presence of the given name within * the {@link #getPropertyNames()} array. * @param name the property to find */ public boolean containsProperty(String name) { Assert.notNull(name, "property name must not be null"); for (String candidate : this.getPropertyNames()) { if (candidate.equals(name)) { if (logger.isDebugEnabled()) { logger.debug(String.format("PropertySource [%s] contains '%s'", getName(), name)); } return true; } } if (logger.isTraceEnabled()) { logger.trace(String.format("PropertySource [%s] does not contain '%s'", getName(), name)); } return false; }
if (source instanceof EnumerablePropertySource) { final EnumerablePropertySource<?> eSource = (EnumerablePropertySource<?>) source; Arrays.stream(eSource.getPropertyNames()).map(v -> { final ConfigurationVo vo = new ConfigurationVo(); vo.setName(v); vo.setSource(eSource.getName()); updateVo(String.valueOf(eSource.getProperty(v)), vo); return vo; }).forEach(vo -> result.put(vo.getName(), vo));
@Override public Object getProperty(String name) { return ps.getProperty(prefix + name); }
@Override public Object getProperty(String name) { // Resolve thru the module first Object result = moduleOptionsPropertySource.getProperty(name); if (result != null) { return result; } // If module could not resolve, but it is *known* to be // a valid module option name, do NOT delegate to the env // as it could end up in false positives if (moduleOptionsPropertySource.containsProperty(name)) { return null; } else { return parent == null ? null : parent.getProperty(name); } } };
public EncryptableEnumerablePropertySourceWrapper(EnumerablePropertySource<T> delegate, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter) { super(delegate.getName(), delegate.getSource()); encryptableDelegate = new CachingDelegateEncryptablePropertySource<>(delegate, resolver, filter); }
/** * Wrap the module options property source so that it can be the only one living in the environment. This will * delegate to the parent environment ONLY IF the key is not a known module option name. */ private PropertySource<?> wrap(final EnumerablePropertySource<?> moduleOptionsPropertySource) { return new PropertySource<Object>(moduleOptionsPropertySource.getName(), moduleOptionsPropertySource) { @Override public Object getProperty(String name) { // Resolve thru the module first Object result = moduleOptionsPropertySource.getProperty(name); if (result != null) { return result; } // If module could not resolve, but it is *known* to be // a valid module option name, do NOT delegate to the env // as it could end up in false positives if (moduleOptionsPropertySource.containsProperty(name)) { return null; } else { return parent == null ? null : parent.getProperty(name); } } }; }
@Test public void withDefaultNonOptionArgsNameAndNoNonOptionArgsPresent() { OptionParser parser = new OptionParser(); parser.acceptsAll(Arrays.asList("o1","option1")).withRequiredArg(); parser.accepts("o2"); OptionSet optionSet = parser.parse("--o1=v1", "--o2"); EnumerablePropertySource<?> ps = new JOptCommandLinePropertySource(optionSet); assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.containsProperty("o1"), is(true)); assertThat(ps.containsProperty("o2"), is(true)); assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.getProperty("nonOptionArgs"), nullValue()); assertThat(ps.getPropertyNames().length, is(2)); }
@Override public Object getProperty(String name) { int separator = name.indexOf(OPTION_SEPARATOR); if (separator == -1) { return null; } String prefix = name.substring(0, separator); EnumerablePropertySource<?> sub = delegates.get(prefix); if (sub == null) { return null; } return sub.getProperty(name.substring(separator + OPTION_SEPARATOR.length())); }
public EncryptableEnumerablePropertySourceWrapper(EnumerablePropertySource<T> delegate, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter) { super(delegate.getName(), delegate.getSource()); encryptableDelegate = new CachingDelegateEncryptablePropertySource<>(delegate, resolver, filter); }
/** * Return whether this {@code PropertySource} contains a property with the given name. * <p>This implementation checks for the presence of the given name within the * {@link #getPropertyNames()} array. * @param name the name of the property to find */ @Override public boolean containsProperty(String name) { return ObjectUtils.containsElement(getPropertyNames(), name); }
public static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) { Map<String, Object> result = new HashMap<>(); if (aPropSource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) aPropSource; cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps))); return result; } if (aPropSource instanceof EnumerablePropertySource<?>) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource; Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key))); return result; } //unable to iterate over it return result; }
@Nullable @Override @SuppressWarnings("all") public Object getProperty(String name) { return getSource().getProperty(name); }
@Override public String[] getPropertyNames() { Set<String> names = new LinkedHashSet<>(); for (PropertySource<?> propertySource : this.propertySources) { if (!(propertySource instanceof EnumerablePropertySource)) { throw new IllegalStateException( "Failed to enumerate property names due to non-enumerable property source: " + propertySource); } names.addAll(Arrays.asList(((EnumerablePropertySource<?>) propertySource).getPropertyNames())); } return StringUtils.toStringArray(names); }
private static Properties getProperties( ConfigurableEnvironment propertyResolver ) { MutablePropertySources propertySources = propertyResolver.getPropertySources(); Properties properties = new Properties(); for ( org.springframework.core.env.PropertySource<?> source : propertySources ) { if ( source instanceof EnumerablePropertySource ) { EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) source; String[] props = enumerablePropertySource.getPropertyNames(); for ( String prop : props ) { properties.put( prop, enumerablePropertySource.getProperty( prop ) ); } } } return properties; }
/** * Return whether this {@code PropertySource} contains a property with the given name. * <p>This implementation checks for the presence of the given name within the * {@link #getPropertyNames()} array. * @param name the name of the property to find */ @Override public boolean containsProperty(String name) { return ObjectUtils.containsElement(getPropertyNames(), name); }
private Map<Object, Object> moduleOptionsToProperties(ModuleOptions moduleOptions) { Map<Object, Object> result = new HashMap<Object, Object>(); EnumerablePropertySource<?> ps = moduleOptions.asPropertySource(); for (String propname : ps.getPropertyNames()) { Object value = ps.getProperty(propname); if (value != null) { result.put(propname, value.toString()); } } return result; }
for (String name : ((EnumerablePropertySource<?>) source).getPropertyNames()) { if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) { String subName = name.substring(normalizedPrefix.length());