private void appendPropertySource(CompositePropertySource propertySource, ResourceLoader resourceLoader) throws IOException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(resourceLoader); Resource[] resources = resourcePatternResolver.getResources(RESOURCE_LOCATION_PATTERN); for (Resource resource : resources) { // Add if exists if (resource.exists()) { String internalName = String.valueOf(resource.getURL()); propertySource.addPropertySource(new ResourcePropertySource(internalName, new EncodedResource(resource, FILE_ENCODING))); } } }
composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite);
/** * Initialize Apollo Configurations Just after environment is ready. * * @param environment */ protected void initialize(ConfigurableEnvironment environment) { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { //already initialized return; } String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION); logger.debug("Apollo bootstrap namespaces: {}", namespaces); List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces); CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); for (String namespace : namespaceList) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } environment.getPropertySources().addFirst(composite); }
private void initializePropertySources() { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) { //already initialized return; } CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME); //sort by order asc ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet()); Iterator<Integer> iterator = orders.iterator(); while (iterator.hasNext()) { int order = iterator.next(); for (String namespace : NAMESPACE_NAMES.get(order)) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } } // clean up NAMESPACE_NAMES.clear(); // add after the bootstrap property source or to the first if (environment.getPropertySources() .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { // ensure ApolloBootstrapPropertySources is still the first ensureBootstrapPropertyPrecedence(environment); environment.getPropertySources() .addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite); } else { environment.getPropertySources().addFirst(composite); } }
@Test public void addFirst() { PropertySource<?> p1 = new MapPropertySource("p1", Collections.emptyMap()); PropertySource<?> p2 = new MapPropertySource("p2", Collections.emptyMap()); PropertySource<?> p3 = new MapPropertySource("p3", Collections.emptyMap()); CompositePropertySource composite = new CompositePropertySource("c"); composite.addPropertySource(p2); composite.addPropertySource(p3); composite.addPropertySource(p1); composite.addFirstPropertySource(p1); String s = composite.toString(); int i1 = s.indexOf("name='p1'"); int i2 = s.indexOf("name='p2'"); int i3 = s.indexOf("name='p3'"); assertTrue("Bad order: " + s, ((i1 < i2) && (i2 < i3))); }
composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite);
/** * Add, remove or re-order any {@link PropertySource}s in this application's * environment. * @param environment this application's environment * @param args arguments passed to the {@code run} method * @see #configureEnvironment(ConfigurableEnvironment, String[]) */ protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) { MutablePropertySources sources = environment.getPropertySources(); if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { sources.addLast( new MapPropertySource("defaultProperties", this.defaultProperties)); } if (this.addCommandLineProperties && args.length > 0) { String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME; if (sources.contains(name)) { PropertySource<?> source = sources.get(name); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(new SimpleCommandLinePropertySource( "springApplicationCommandLineArgs", args)); composite.addPropertySource(source); sources.replace(name, composite); } else { sources.addFirst(new SimpleCommandLinePropertySource(args)); } } }
@Override public org.springframework.core.env.PropertySource<?> locate(Environment environment) { CompositePropertySource composite = new CompositePropertySource("configService"); for (PropertySource source : repository.findOne(name, profiles, label) .getPropertySources()) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource(); composite.addPropertySource(new MapPropertySource(source.getName(), map)); } return composite; }
Map<String, Object> map = (Map<String, Object>) source .getSource(); composite.addPropertySource( new MapPropertySource(source.getName(), map));
public void add(PropertySource<?> source) { if (source instanceof EnumerablePropertySource && !this.names.contains(source.getName())) { this.sources.addPropertySource(source); this.names.add(source.getName()); } }
@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); } }
/** * Create a {@link CompositePropertySource} given a {@link List} of * {@link PropertySource}s. * * @param propertySourceName the property source name. * @param propertySources the property sources. * @return the {@link CompositePropertySource} to use. */ protected CompositePropertySource doCreateCompositePropertySource( String propertySourceName, List<PropertySource<?>> propertySources) { CompositePropertySource compositePropertySource = new CompositePropertySource( propertySourceName); for (PropertySource<?> propertySource : propertySources) { compositePropertySource.addPropertySource(propertySource); } return compositePropertySource; }
/** * Create a {@link CompositePropertySource} given a {@link List} of * {@link PropertySource}s. * * @param propertySourceName the property source name. * @param propertySources the property sources. * @return the {@link CompositePropertySource} to use. */ protected CompositePropertySource doCreateCompositePropertySource( String propertySourceName, List<PropertySource<?>> propertySources) { CompositePropertySource compositePropertySource = new CompositePropertySource( propertySourceName); for (PropertySource<?> propertySource : propertySources) { compositePropertySource.addPropertySource(propertySource); } return compositePropertySource; }
public void add(PropertySource<?> source) { if (source instanceof EnumerablePropertySource && !this.names.contains(source.getName())) { this.sources.addPropertySource(source); this.names.add(source.getName()); } }
private void appendPropertySource(CompositePropertySource propertySource, ResourceLoader resourceLoader) throws IOException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(resourceLoader); Resource[] resources = resourcePatternResolver.getResources(RESOURCE_LOCATION_PATTERN); for (Resource resource : resources) { // Add if exists if (resource.exists()) { String internalName = String.valueOf(resource.getURL()); propertySource.addPropertySource(new ResourcePropertySource(internalName, new EncodedResource(resource, FILE_ENCODING))); } } }
private PropertySource<?> loadPropertySource(Resource resource, String path) { if (resource.exists()) { try { List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(path, resource); if (sources != null) { logger.info("Loaded YAML properties from: " + resource); } else if (sources == null || sources.isEmpty()){ return null; } CompositePropertySource composite = new CompositePropertySource("cli-sources"); for (PropertySource propertySource : sources) { composite.addPropertySource(propertySource); } return composite; } catch (IOException e) { } } return null; }
/** * Adding items to composite property source which contains property sources processed in order, first one wins. * First Priority: Standalone configuration file * Second Priority: Configuration files in config dir, profiles override non-profiles, last profile overrides first * Third Priority: classpath:/application.yml * * @param environment the environment * @param resourceLoader the resource loader * @return CompositePropertySource containing sources listed above */ @Override public PropertySource<?> locate(final Environment environment, final ResourceLoader resourceLoader) { val compositePropertySource = new CompositePropertySource("casCompositePropertySource"); val configFile = casConfigurationPropertiesEnvironmentManager.getStandaloneProfileConfigurationFile(); if (configFile != null) { val sourceStandalone = loadSettingsFromStandaloneConfigFile(environment, configFile); compositePropertySource.addPropertySource(sourceStandalone); } val config = casConfigurationPropertiesEnvironmentManager.getStandaloneProfileConfigurationDirectory(); LOGGER.debug("Located CAS standalone configuration directory at [{}]", config); if (config != null && config.isDirectory() && config.exists()) { val sourceProfiles = loadSettingsByApplicationProfiles(environment, config); compositePropertySource.addPropertySource(sourceProfiles); } else { LOGGER.info("Configuration directory [{}] is not a directory or cannot be found at the specific path", config); } val sourceYaml = loadEmbeddedYamlOverriddenProperties(resourceLoader); compositePropertySource.addPropertySource(sourceYaml); return compositePropertySource; }
composite.addPropertySource(create(propertySourceContext)); } catch (Exception e) { if (this.properties.isFailFast()) {
private void addToPropertySource(org.springframework.cloud.config.environment.Environment loadedEnvs, CompositePropertySource propertySource) { for (org.springframework.cloud.config.environment.PropertySource source : loadedEnvs.getPropertySources()) { @SuppressWarnings("unchecked") Map<String, Object> map = decrypt((Map<String, Object>) source.getSource()); propertySource.addPropertySource(new MapPropertySource(source.getName(), map)); } }
@Override public PropertySource<?> locate(Environment environment) { if (environment instanceof ConfigurableEnvironment) { final ConfigurableEnvironment env = (ConfigurableEnvironment) environment; final String[] profiles = env.getActiveProfiles(); final List<String> contexts = new ArrayList<>(); setupContext(contexts, profiles, this.properties.getPrefix(), this.properties.getDefaultContext()); setupContext(contexts, profiles, this.properties.getPrefix(), env.getProperty(EtcdConstants.PROPERTY_SPRING_APPLICATION_NAME)); CompositePropertySource composite = new CompositePropertySource( EtcdConstants.NAME); Collections.reverse(contexts); for (String context : contexts) { EtcdPropertySource propertySource = new EtcdPropertySource(context, etcd, properties); propertySource.init(); composite.addPropertySource(propertySource); } return composite; } return null; }