/** * Ensure that the given property source is not being added relative to itself. */ protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource<?> propertySource) { String newPropertySourceName = propertySource.getName(); if (relativePropertySourceName.equals(newPropertySourceName)) { throw new IllegalArgumentException( "PropertySource named '" + newPropertySourceName + "' cannot be added relative to itself"); } }
/** * Log the given key as found in the given {@link PropertySource}, resulting in * the given value. * <p>The default implementation writes a debug log message with key and source. * As of 4.3.3, this does not log the value anymore in order to avoid accidental * logging of sensitive settings. Subclasses may override this method to change * the log level and/or log message, including the property's value if desired. * @param key the key found * @param propertySource the {@code PropertySource} that the key has been found in * @param value the corresponding value * @since 4.3.1 */ protected void logKeyFound(String key, PropertySource<?> propertySource, Object value) { if (logger.isDebugEnabled()) { logger.debug("Found key '" + key + "' in PropertySource '" + propertySource.getName() + "' with value of type " + value.getClass().getSimpleName()); } }
/** * Ensure that the given property source is not being added relative to itself. */ protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource<?> propertySource) { String newPropertySourceName = propertySource.getName(); if (relativePropertySourceName.equals(newPropertySourceName)) { throw new IllegalArgumentException( "PropertySource named '" + newPropertySourceName + "' cannot be added relative to itself"); } }
private static void extract(String root, Map<String, PropertySource<?>> map, PropertySource<?> source) { if (source instanceof CompositePropertySource) { for (PropertySource<?> nest : ((CompositePropertySource) source) .getPropertySources()) { extract(source.getName() + ":", map, nest); } } else { map.put(root + source.getName(), source); } }
@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }
@Nullable protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) { if (this.propertySources != null) { for (PropertySource<?> propertySource : this.propertySources) { if (logger.isTraceEnabled()) { logger.trace("Searching for key '" + key + "' in PropertySource '" + propertySource.getName() + "'"); } Object value = propertySource.getProperty(key); if (value != null) { if (resolveNestedPlaceholders && value instanceof String) { value = resolveNestedPlaceholders((String) value); } logKeyFound(key, propertySource, value); return convertValueIfNecessary(value, targetValueType); } } } if (logger.isTraceEnabled()) { logger.trace("Could not find key '" + key + "' in any property source"); } return null; }
private void addPropertySource(PropertySource<?> propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
@Test public void withCustomName() { PropertySource<?> ps = new SimpleCommandLinePropertySource("ps1", new String[0]); assertThat(ps.getName(), equalTo("ps1")); }
@Test public void withDefaultName() { PropertySource<?> ps = new SimpleCommandLinePropertySource(); assertThat(ps.getName(), equalTo(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)); }
@Test public void streamContainsPropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addLast(new MockPropertySource("test")); assertThat(sources.stream(), notNullValue()); assertThat(sources.stream().count(), is(1L)); assertThat(sources.stream().anyMatch(source -> "test".equals(source.getName())), is(true)); assertThat(sources.stream().anyMatch(source -> "bogus".equals(source.getName())), is(false)); }
@Test public void withLocationAndGeneratedNameAndExplicitClassLoader() throws IOException { PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION, getClass().getClassLoader()); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION)); }
@Test public void withLocationAndExplicitNameAndExplicitClassLoader() throws IOException { PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION, getClass().getClassLoader()); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is("ps1")); }
@Test public void withResourceAndGeneratedName() throws IOException { PropertySource<?> ps = new ResourcePropertySource(new ClassPathResource(PROPERTIES_PATH)); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION)); }
@Test public void withResourceAndExplicitName() throws IOException { PropertySource<?> ps = new ResourcePropertySource("ps1", new ClassPathResource(PROPERTIES_PATH)); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is("ps1")); }
@Test public void withLocationAndGeneratedName() throws IOException { PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION)); }
@Test public void withLocationAndExplicitName() throws IOException { PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is("ps1")); }
@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 xmlWithLocationAndGeneratedName() throws IOException { PropertySource<?> ps = new ResourcePropertySource(XML_PROPERTIES_LOCATION); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(XML_PROPERTIES_RESOURCE_DESCRIPTION)); }
@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 public void withResourceHavingNoDescription() throws IOException { PropertySource<?> ps = new ResourcePropertySource(new ByteArrayResource("foo=bar".getBytes(), "")); assertEquals("bar", ps.getProperty("foo")); assertEquals("Byte array resource []", ps.getName()); }