Refine search
@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); } } } }
MutablePropertySources propertySources = environment.getPropertySources(); String subName = name.substring(normalizedPrefix.length()); if (!subProperties.containsKey(subName)) { // take first one Object value = source.getProperty(name); if (value instanceof String) { value = environment.resolvePlaceholders((String) value);
@SuppressWarnings("unchecked") private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) { MutablePropertySources sources = environment.getPropertySources(); PropertySource<?> source = sources.get("server.ports"); if (source == null) { source = new MapPropertySource("server.ports", new HashMap<>()); sources.addFirst(source); } ((Map<String, Object>) source.getSource()).put(propertyName, port); }
private Map<?, ?> getMap(org.springframework.core.env.PropertySource<?> source) { Map<Object, Object> map = new LinkedHashMap<>(); Map<?, ?> input = (Map<?, ?>) source.getSource(); for (Object key : input.keySet()) { // Spring Boot wraps the property values in an "origin" detector, so we need // to extract the string values map.put(key, source.getProperty(key.toString())); } return map; }
@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(); PropertySource<?> existing = propertySources.get(name); if (existing != null) { PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite); propertySources.addLast(propertySource);
MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true)); assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); assertThat(environment.getProperty("pConfig1"), is("pConfig1Value")); assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}") .equalsIgnoreCase("true")) { if (!environment.getPropertySources().contains(this.propertySource.getName())) { environment.getPropertySources().addLast(this.propertySource); } } }
if (environment.containsProperty("logging.file")) { String location = environment.getProperty("logging.file"); servletContext.log("Setting LOG_FILE: " + location); System.setProperty("LOG_FILE", location); } else if (environment.containsProperty("logging.path")) { String location = environment.getProperty("logging.path"); servletContext.log("Setting LOG_PATH: " + location); String location = environment.getProperty("logging.config"); if (location!=null && location.trim().length()>0) { PropertySource<?> environmentPropertySource = environment.getPropertySources().get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); if ((location.startsWith("-D") && environmentPropertySource!=null && location.equals(environmentPropertySource.getProperty("LOGGING_CONFIG")))) { servletContext.log("Ignoring Log Config Location: " + location + ". Location is suspect to be a Tomcat startup script environment variable"); } else {
private void flattenPropertySources(PropertySource<?> propertySource, MutablePropertySources result) { Object source = propertySource.getSource(); if (source instanceof ConfigurableEnvironment) { ConfigurableEnvironment environment = (ConfigurableEnvironment) source; for (PropertySource<?> childSource : environment.getPropertySources()) { flattenPropertySources(childSource, result); } } else { result.addLast(propertySource); } } }
assertThat(sources.get("b").getProperty("p1"), equalTo("bValue")); assertThat(sources.get("d"), not(nullValue())); assertThat(sources.get("d").getProperty("p1"), equalTo("dValue")); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("e")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(5)); assertThat(sources.precedenceOf(PropertySource.named("g")), is(6)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("e")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("g")), is(5)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(6)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
private Map<String, String> getConfigurations(String key, Environment environment) { Object rawProperties = environment.getProperty(key, Object.class); Map<String, String> externalProperties = new HashMap<>(); try { if (rawProperties instanceof Map) { externalProperties.putAll((Map<String, String>) rawProperties); } else if (rawProperties instanceof String) { externalProperties.putAll(ConfigurationUtils.parseProperties((String) rawProperties)); } if (environment instanceof ConfigurableEnvironment && externalProperties.isEmpty()) { ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; PropertySource propertySource = configurableEnvironment.getPropertySources().get(key); if (propertySource != null) { Object source = propertySource.getSource(); if (source instanceof Map) { ((Map<String, Object>) source).forEach((k, v) -> { externalProperties.put(k, (String) v); }); } } } } catch (Exception e) { throw new IllegalStateException(e); } return externalProperties; }
public EnvironmentManager(ConfigurableEnvironment environment) { this.environment = environment; MutablePropertySources sources = environment.getPropertySources(); if (sources.contains(MANAGER_PROPERTY_SOURCE)) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) sources .get(MANAGER_PROPERTY_SOURCE).getSource(); this.map = map; } }
private void copyPropertySources(ConfigurableEnvironment source, StandardEnvironment target) { removePropertySources(target.getPropertySources(), isServletEnvironment(target.getClass(), this.classLoader)); for (PropertySource<?> propertySource : source.getPropertySources()) { if (!SERVLET_ENVIRONMENT_SOURCE_NAMES.contains(propertySource.getName())) { target.getPropertySources().addLast(propertySource); } } }
@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 testProfileSourcesRegisteredOverridesCommon() { Assert.assertTrue(env.getPropertySources().contains(BroadleafEnvironmentConfiguringApplicationListener.FRAMEWORK_SOURCES_NAME)); Assert.assertTrue(env.getPropertySources().contains(BroadleafEnvironmentConfiguringApplicationListener.PROFILE_AWARE_SOURCES_NAME)); String developmentSourceName = new ClassPathResource("common-test-properties/profile-aware-properties/development.properties").getDescription(); CompositePropertySource profileAwareSource = (CompositePropertySource) env.getPropertySources().get(BroadleafEnvironmentConfiguringApplicationListener.PROFILE_AWARE_SOURCES_NAME); Assert.assertTrue(profileAwareSource.getPropertySources().contains(PropertySource.named(developmentSourceName))); }
@SuppressWarnings("unchecked") private void replacePropertySource(ConfigurableEnvironment environment, String sourceName, PropertySource<?> propertySource) { Map<String, Object> originalSource = (Map<String, Object>) propertySource .getSource(); SystemEnvironmentPropertySource source = new OriginAwareSystemEnvironmentPropertySource( sourceName, originalSource); environment.getPropertySources().replace(sourceName, source); }
private static Stream<PropertySource<?>> flatten(PropertySource<?> source) { if (source.getSource() instanceof ConfigurableEnvironment) { return streamPropertySources( ((ConfigurableEnvironment) source.getSource()).getPropertySources()); } return Stream.of(source); }
@Test public void propertySourceOrder() { ConfigurableEnvironment env = new StandardEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.size(), is(2)); }
@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()); }