private void push(ConfigurableEnvironment environment) { this.iterators.push(environment.getPropertySources().iterator()); }
@Test public void iteratorIsEmptyForEmptySources() { MutablePropertySources sources = new MutablePropertySources(); Iterator<PropertySource<?>> it = sources.iterator(); assertFalse(it.hasNext()); }
@Test public void withExplicitName() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithExplicitName.class); ctx.refresh(); assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("p1")); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); // assert that the property source was added last to the set of sources String name; MutablePropertySources sources = ctx.getEnvironment().getPropertySources(); Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator(); do { name = iterator.next().getName(); } while (iterator.hasNext()); assertThat(name, is("p1")); }
@Test public void iteratorContainsPropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addLast(new MockPropertySource("test")); Iterator<PropertySource<?>> it = sources.iterator(); assertTrue(it.hasNext()); assertEquals("test", it.next().getName()); try { it.remove(); fail("Should have thrown UnsupportedOperationException"); } catch (UnsupportedOperationException ex) { // expected } assertFalse(it.hasNext()); }
@Test @SuppressWarnings("rawtypes") public void addInlinedPropertiesToEnvironmentWithEmptyProperty() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); addInlinedPropertiesToEnvironment(environment, asArray(" ")); assertEquals(1, propertySources.size()); assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size()); }
@Test public void explicitPropertySources() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo")); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
@Test public void explicitPropertySourcesExcludesEnvironment() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env")); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
/** * get All properties */ public Map<String, Object> getAllProperties() { if (properties == null || properties.isEmpty()) { synchronized (SpringEnvironmentProperties.class) { if (properties == null || properties.isEmpty()) { Map<String, Object> map = new HashMap(); for (Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) { PropertySource propertySource = (PropertySource) it.next(); if (propertySource instanceof MapPropertySource) { map.putAll(((MapPropertySource) propertySource).getSource()); } } //decrypt Map<String, Object> decryptedMap = new HashMap(); for (String k : map.keySet()) { decryptedMap.put(k, env.getProperty(k)); } properties = decryptedMap; } } } return properties; }
/** * Aggregate all <code>PropertySource<?><code> objects from the environment into a <code>List</code> */ public static List<PropertySource<?>> getPropertySources(ConfigurableEnvironment env) { Preconditions.checkNotNull(env, "'env' cannot be null"); MutablePropertySources mps = env.getPropertySources(); List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>(); Iterator<PropertySource<?>> itr = mps.iterator(); while (itr.hasNext()) { PropertySource<?> source = itr.next(); sources.add(source); } return sources; }
@Override public Iterator<PropertySource<?>> iterator() { MutablePropertySources result = getFlattened(); return result.iterator(); }
@Override public Iterator<PropertySource<?>> iterator() { MutablePropertySources result = getFlattened(); return result.iterator(); }
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; }
@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); } }
public static Map<String, Object> getAllProperties() { Map<String, Object> retMap = new NoConverterMap<>(); Iterator<PropertySource<?>> ite = ConfigProperties.localenv.getPropertySources() .iterator(); while (ite.hasNext()) { PropertySource<?> s = ite.next(); retMap.putAll(resolvePropertySource(s)); } return retMap; }
/** * get All properties */ public Map<String, Object> getAllProperties() { if (properties == null || properties.isEmpty()) { synchronized (SpringEnvironmentProperties.class) { if (properties == null || properties.isEmpty()) { Map<String, Object> map = new HashMap(); for (Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) { PropertySource propertySource = (PropertySource) it.next(); if (propertySource instanceof MapPropertySource) { map.putAll(((MapPropertySource) propertySource).getSource()); } } //decrypt Map<String, Object> decryptedMap = new HashMap(); for (String k : map.keySet()) { decryptedMap.put(k, env.getProperty(k)); } properties = decryptedMap; } } } return properties; }
@PostConstruct public void init() { id = resolveId(); enabled = resolveEnabled(); port = resolvePort(); host = resolveHost(); if (isHTTPS()) { keyStoreFile = resolveKeyStoreFile(); keyStorePass = resolveKeyStorePass(); certPass = resolveKeyCertPass(); } for (Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) { Object propertySource = it.next(); if (propertySource instanceof MapPropertySource && SpringConstants.APPLICATION_PROPERTIES.equals(((MapPropertySource) propertySource).getName())) { MapPropertySource mapPropertySource = (MapPropertySource) propertySource; for (Map.Entry<String, Object> entry : mapPropertySource.getSource().entrySet()) { String key = entry.getKey(); if (key.startsWith(getScheme()) && key.contains(SpringConstants.PARAMETER_STR)) { parameters.put(key.substring(key.indexOf(SpringConstants.PARAMETER_STR) + 11), (String) entry .getValue()); } } } } }
.iterator(); while (propertySourceIterator.hasNext()) { PropertySource propertySource = propertySourceIterator.next();