private void reorderSources(ConfigurableEnvironment environment) { PropertySource<?> defaultProperties = environment.getPropertySources() .remove(DEFAULT_PROPERTIES); if (defaultProperties != null) { environment.getPropertySources().addLast(defaultProperties); } }
private void removePropertySources(MutablePropertySources propertySources, boolean isServletEnvironment) { Set<String> names = new HashSet<>(); for (PropertySource<?> propertySource : propertySources) { names.add(propertySource.getName()); } for (String name : names) { if (!isServletEnvironment || !SERVLET_ENVIRONMENT_SOURCE_NAMES.contains(name)) { propertySources.remove(name); } } }
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); }
public static StandardEnvironment prepareEnvironment(Environment environment) { StandardEnvironment standardEnvironment = new StandardEnvironment(); standardEnvironment.getPropertySources() .remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); standardEnvironment.getPropertySources() .remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); standardEnvironment.getPropertySources() .addFirst(new EnvironmentPropertySource(environment)); return standardEnvironment; }
@Test public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); String pair = "key = value"; ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair); ResourceLoader resourceLoader = mock(ResourceLoader.class); when(resourceLoader.getResource(anyString())).thenReturn(resource); addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS); assertEquals(1, propertySources.size()); assertEquals("value", environment.getProperty("key")); }
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder( PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment); builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF); if (!logger.isDebugEnabled()) { // Make the mini-application startup less verbose builder.logStartupInfo(false); } String[] args = getArgs(config, profile, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application() .setListeners(Arrays.asList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
/** * Attach a {@link ConfigurationPropertySource} support to the specified * {@link Environment}. Adapts each {@link PropertySource} managed by the environment * to a {@link ConfigurationPropertySource} and allows classic * {@link PropertySourcesPropertyResolver} calls to resolve using * {@link ConfigurationPropertyName configuration property names}. * <p> * The attached resolver will dynamically track any additions or removals from the * underlying {@link Environment} property sources. * @param environment the source environment (must be an instance of * {@link ConfigurableEnvironment}) * @see #get(Environment) */ public static void attach(Environment environment) { Assert.isInstanceOf(ConfigurableEnvironment.class, environment); MutablePropertySources sources = ((ConfigurableEnvironment) environment) .getPropertySources(); PropertySource<?> attached = sources.get(ATTACHED_PROPERTY_SOURCE_NAME); if (attached != null && attached.getSource() != sources) { sources.remove(ATTACHED_PROPERTY_SOURCE_NAME); attached = null; } if (attached == null) { sources.addFirst(new ConfigurationPropertySourcesPropertySource( ATTACHED_PROPERTY_SOURCE_NAME, new SpringConfigurationPropertySources(sources))); } }
@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()); }
private void reorderSources(ConfigurableEnvironment environment) { PropertySource<?> removed = environment.getPropertySources() .remove(DEFAULT_PROPERTIES); if (removed instanceof ExtendedDefaultPropertySource) { ExtendedDefaultPropertySource defaultProperties = (ExtendedDefaultPropertySource) removed; environment.getPropertySources().addLast(new MapPropertySource( DEFAULT_PROPERTIES, defaultProperties.getSource())); for (PropertySource<?> source : defaultProperties.getPropertySources() .getPropertySources()) { if (!environment.getPropertySources().contains(source.getName())) { environment.getPropertySources().addBefore(DEFAULT_PROPERTIES, source); } } } }
private Environment environment(MutablePropertySources incoming) { StandardEnvironment environment = new StandardEnvironment(); for (PropertySource<?> source : environment.getPropertySources()) { environment.getPropertySources().remove(source.getName()); } for (PropertySource<?> source : incoming) { environment.getPropertySources().addLast(source); } return environment; }
private void mergeAdditionalPropertySources(MutablePropertySources environment, MutablePropertySources bootstrap) { PropertySource<?> defaultProperties = environment.get(DEFAULT_PROPERTIES); ExtendedDefaultPropertySource result = defaultProperties instanceof ExtendedDefaultPropertySource ? (ExtendedDefaultPropertySource) defaultProperties : new ExtendedDefaultPropertySource(DEFAULT_PROPERTIES, defaultProperties); for (PropertySource<?> source : bootstrap) { if (!environment.contains(source.getName())) { result.add(source); } } for (String name : result.getPropertySourceNames()) { bootstrap.remove(name); } addOrReplace(environment, result); addOrReplace(bootstrap, result); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { CompositePropertySource composite = new CompositePropertySource( BOOTSTRAP_PROPERTY_SOURCE_NAME); AnnotationAwareOrderComparator.sort(this.propertySourceLocators); boolean empty = true; ConfigurableEnvironment environment = applicationContext.getEnvironment(); for (PropertySourceLocator locator : this.propertySourceLocators) { PropertySource<?> source = null; source = locator.locate(environment); if (source == null) { continue; } logger.info("Located property source: " + source); composite.addPropertySource(source); empty = false; } if (!empty) { MutablePropertySources propertySources = environment.getPropertySources(); String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME); } insertPropertySources(propertySources, composite); reinitializeLoggingSystem(environment, logConfig, logFile); setLogLevels(applicationContext, environment); handleIncludedProfiles(environment); } }
capture = builder.run(); if (environment.getPropertySources().contains(REFRESH_ARGS_PROPERTY_SOURCE)) { environment.getPropertySources().remove(REFRESH_ARGS_PROPERTY_SOURCE);
/** * Un-Mark this environment as SOFA bootstrap environment * @param environment */ private void unAssemblyEnvironmentMark(ConfigurableEnvironment environment) { environment.getPropertySources().remove(SofaBootInfraConstants.SOFA_BOOTSTRAP); }
private void removeIfParentContains( ConfigurableEnvironment parent, String sourceName ) { if ( parent.getPropertySources().contains( sourceName ) ) { getPropertySources().remove( sourceName ); } } }
private void removeIfParentContains( ConfigurableEnvironment parent, String sourceName ) { if ( parent.getPropertySources().contains( sourceName ) ) { getPropertySources().remove( sourceName ); } } }
@ManagedOperation public void refreshConfiguration() { final PropertySource<?> propertySource = fileSystemLocator.locate(environment); final MutablePropertySources sources = environment.getPropertySources(); if (sources.contains(propertySource.getName())) { sources.remove(propertySource.getName()); } sources.addFirst(propertySource); refreshScope.refreshAll(); } }
@Before public void setup() throws Exception { // Remove the system environment property source so system environment variables don't affect unit tests. getMutablePropertySources().remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); }