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; }
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; }
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { String sourceName = StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME; PropertySource<?> propertySource = environment.getPropertySources() .get(sourceName); if (propertySource != null) { replacePropertySource(environment, sourceName, propertySource); } }
private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) { MutablePropertySources propertySources = environment.getPropertySources(); PropertySource<?> bootstrapPropertySource = propertySources .get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); // not exists or already in the first place if (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) { return; } propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); propertySources.addFirst(bootstrapPropertySource); }
environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); if (ps == null) { ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>());
/** * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with * actual instances populated with the given {@code servletContext} and * {@code servletConfig} objects. * <p>This method is idempotent with respect to the fact it may be called any number * of times but will perform replacement of stub property sources with their * corresponding actual property sources once and only once. * @param sources the {@link MutablePropertySources} to initialize (must not * be {@code null}) * @param servletContext the current {@link ServletContext} (ignored if {@code null} * or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME * servlet context property source} has already been initialized) * @param servletConfig the current {@link ServletConfig} (ignored if {@code null} * or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME * servlet config property source} has already been initialized) * @see org.springframework.core.env.PropertySource.StubPropertySource * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources() */ public static void initServletPropertySources(MutablePropertySources sources, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { Assert.notNull(sources, "'propertySources' must not be null"); String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME; if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletContextPropertySource(name, servletContext)); } name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME; if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletConfigPropertySource(name, servletConfig)); } }
@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); }
/** * Return a set of {@link ConfigurationPropertySource} instances that have previously * been {@link #attach(Environment) attached} to the {@link Environment}. * @param environment the source environment (must be an instance of * {@link ConfigurableEnvironment}) * @return an iterable set of configuration property sources * @throws IllegalStateException if not configuration property sources have been * attached */ public static Iterable<ConfigurationPropertySource> get(Environment environment) { Assert.isInstanceOf(ConfigurableEnvironment.class, environment); MutablePropertySources sources = ((ConfigurableEnvironment) environment) .getPropertySources(); ConfigurationPropertySourcesPropertySource attached = (ConfigurationPropertySourcesPropertySource) sources .get(ATTACHED_PROPERTY_SOURCE_NAME); if (attached == null) { return from(sources); } return attached.getSource(); }
@Test public void propertySourceTypes() { ConfigurableEnvironment env = new StandardEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME), instanceOf(SystemEnvironmentPropertySource.class)); }
PropertySource<?> existing = propertySources.get(name); if (existing != null) { PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
@Test @SuppressWarnings("rawtypes") public void propertyNameOrderingIsPreservedInEnvironment() { final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url", "key.value.1", "key.value.2", "key.value.3" }; EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get( INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); assertArrayEquals(expectedPropertyNames, eps.getPropertyNames()); }
@Test public void getNonExistentPropertySourceReturnsNull() { MutablePropertySources sources = new MutablePropertySources(); assertThat(sources.get("bogus"), nullValue()); }
/** * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with * actual instances populated with the given {@code servletContext} and * {@code servletConfig} objects. * <p>This method is idempotent with respect to the fact it may be called any number * of times but will perform replacement of stub property sources with their * corresponding actual property sources once and only once. * @param sources the {@link MutablePropertySources} to initialize (must not * be {@code null}) * @param servletContext the current {@link ServletContext} (ignored if {@code null} * or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME * servlet context property source} has already been initialized) * @param servletConfig the current {@link ServletConfig} (ignored if {@code null} * or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME * servlet config property source} has already been initialized) * @see org.springframework.core.env.PropertySource.StubPropertySource * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources() */ public static void initServletPropertySources(MutablePropertySources sources, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { Assert.notNull(sources, "'propertySources' must not be null"); String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME; if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletContextPropertySource(name, servletContext)); } name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME; if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletConfigPropertySource(name, servletConfig)); } }
/** * Copy from {@link BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map)} * * @param propertySources {@link MutablePropertySources} * @param map Default RocketMQ Bus Properties */ private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) { MapPropertySource target = null; if (propertySources.contains(PROPERTY_SOURCE_NAME)) { PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME); if (source instanceof MapPropertySource) { target = (MapPropertySource) source; for (String key : map.keySet()) { if (!target.containsProperty(key)) { target.getSource().put(key, map.get(key)); } } } } if (target == null) { target = new MapPropertySource(PROPERTY_SOURCE_NAME, map); } if (!propertySources.contains(PROPERTY_SOURCE_NAME)) { propertySources.addLast(target); } }
PropertySource<?> existing = propertySources.get(name); if (existing != null) { PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
/** * Copy from BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map) * * @param propertySources {@link MutablePropertySources} * @param map Default Dubbo Properties */ private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) { MapPropertySource target = null; if (propertySources.contains(PROPERTY_SOURCE_NAME)) { PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME); if (source instanceof MapPropertySource) { target = (MapPropertySource) source; for (String key : map.keySet()) { if (!target.containsProperty(key)) { target.getSource().put(key, map.get(key)); } } } } if (target == null) { target = new MapPropertySource(PROPERTY_SOURCE_NAME, map); } if (!propertySources.contains(PROPERTY_SOURCE_NAME)) { propertySources.addLast(target); } } }
private static void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) { MapPropertySource target = null; if (propertySources.contains(PROPERTY_SOURCE_NAME)) { PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME); if (source instanceof MapPropertySource) { target = (MapPropertySource) source; for (String key : map.keySet()) { if (!target.containsProperty(key)) { target.getSource().put(key, map.get(key)); } } } } if (target == null) { target = new MapPropertySource(PROPERTY_SOURCE_NAME, map); } if (!propertySources.contains(PROPERTY_SOURCE_NAME)) { propertySources.addLast(target); } }
private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) { MapPropertySource target = null; if (propertySources.contains(PROPERTY_SOURCE_NAME)) { PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME); if (source instanceof MapPropertySource) { target = (MapPropertySource) source; for (String key : map.keySet()) { if (!target.containsProperty(key)) { target.getSource().put(key, map.get(key)); } } } } if (target == null) { target = new MapPropertySource(PROPERTY_SOURCE_NAME, map); } if (!propertySources.contains(PROPERTY_SOURCE_NAME)) { propertySources.addLast(target); } }
@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))); }