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; }
@Override public Object getProperty(String name) { return ps.getProperty(prefix + 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; }
@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())); }
@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)); }
@Nullable @Override @SuppressWarnings("all") public Object getProperty(String name) { return getSource().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) { // 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); } } };
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; }
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; }
static Properties extractZipkinProperties(ConfigurableEnvironment env) { Properties properties = new Properties(); Iterator<PropertySource<?>> it = env.getPropertySources().iterator(); while (it.hasNext()) { PropertySource<?> next = it.next(); if (!(next instanceof EnumerablePropertySource)) continue; EnumerablePropertySource source = (EnumerablePropertySource) next; for (String name : source.getPropertyNames()) { if (name.startsWith("zipkin")) properties.put(name, source.getProperty(name)); } } return properties; }
/** * Convert an EnumerablePropertySource into a Properties object. */ @Deprecated public static Properties convert(EnumerablePropertySource<?> source) { Properties properties = new Properties(); String[] names = source.getPropertyNames(); for (String name : names) { Object object = source.getProperty(name); if (object != null) { String value = object.toString(); properties.setProperty(name, value); } else { logger.warn("Property [{}] is null", name); } } return properties; }
@Override public PollResult poll(boolean initial, Object checkPoint) throws Exception { Map<String, Object> result = new HashMap<>(); environment.getPropertySources().iterator().forEachRemaining(source -> { if (source instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source; for (String key : enumerable.getPropertyNames()) { result.putIfAbsent(key, enumerable.getProperty(key)); } } }); return PollResult.createFull(result); } }
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; }
protected 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; } return result; }
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; } // note: Most descendants of PropertySource are EnumerablePropertySource. There are some // few others like JndiPropertySource or StubPropertySource LOGGER.debug( "Given PropertySource is instanceof " + aPropSource.getClass().getName() + " and cannot be iterated" ); return result; }
public 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; } return result; }
/** * Copy the key/value pairs from <code>source</code> into a <code>java.util.Properties</code> object. * * @throws <code>IllegalArgumentException</code> if any value is <code>null</code> or is not a <code>java.lang.String</code> */ public static Properties convert(EnumerablePropertySource<?> source) { Assert.notNull(source, "source is null"); Properties properties = new Properties(); String[] names = source.getPropertyNames(); for (String name : names) { Object object = source.getProperty(name); Assert.notNull(object, "[" + name + "] is null"); Assert.isTrue(object instanceof String, "[" + name + "] is not a string"); properties.setProperty(name, (String) object); } return properties; }
protected static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) { Map<String, Object> result = new HashMap<String, Object>(); if (aPropSource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) aPropSource; for (PropertySource<?> propertySource : cps.getPropertySources()) { addAll(result, getAllProperties(propertySource)); } return result; } if (aPropSource instanceof EnumerablePropertySource<?>) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource; for (String propertyName : ps.getPropertyNames()) { result.put(propertyName, ps.getProperty(propertyName)); } return result; } return result; }
protected static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) { Map<String, Object> result = new HashMap<String, Object>(); if (aPropSource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) aPropSource; for (PropertySource<?> propertySource : cps.getPropertySources()) { addAll(result, getAllProperties(propertySource)); } return result; } if (aPropSource instanceof EnumerablePropertySource<?>) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource; for (String propertyName : ps.getPropertyNames()) { result.put(propertyName, ps.getProperty(propertyName)); } return result; } return result; }