private PropertySource buildPropertySource(ResourceLoader resourceLoader) throws IOException { CompositePropertySource propertySource = new CompositePropertySource(PROPERTY_SOURCE_NAME); appendPropertySource(propertySource, resourceLoader); return propertySource; }
/** * 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); } }
existing = ((ResourcePropertySource) existing).withResourceName(); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing);
existing = ((ResourcePropertySource) existing).withResourceName(); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing);
@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))); }
compositeSource = new CompositePropertySource(compositeSourceName); if (addBeforeSourceName == null) { environment.getPropertySources().addLast(compositeSource);
@Override public PropertySource locate(Environment environment) { if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment env = (ConfigurableEnvironment) environment; List<ConfigMapConfigProperties.NormalizedSource> sources = properties .determineSources(); CompositePropertySource composite = new CompositePropertySource( "composite-configmap"); if (properties.isEnableApi()) { sources.forEach(s -> composite.addFirstPropertySource( getMapPropertySourceForSingleConfigMap(env, s))); } addPropertySourcesFromPaths(environment, composite); return composite; } return null; }
@Override public PropertySource<?> locate(Environment env) { ConfigService configService = nacosConfigProperties.configServiceInstance(); if (null == configService) { log.warn("no instance of config service found, can't load config from nacos"); return null; } long timeout = nacosConfigProperties.getTimeout(); nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); String name = nacosConfigProperties.getName(); String nacosGroup = nacosConfigProperties.getGroup(); String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = name; } if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = env.getProperty("spring.application.name"); } List<String> profiles = Arrays.asList(env.getActiveProfiles()); nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); String fileExtension = nacosConfigProperties.getFileExtension(); CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); loadSharedConfiguration(composite); loadExtConfiguration(composite); loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); return composite; }
@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; }
@Override public PropertySource<?> locate(Environment environment) { CompositePropertySource compositePropertySource = new CompositePropertySource( DIAMOND_PROPERTY_SOURCE_NAME); for (String dataId : acmIntegrationProperties.getGroupConfigurationDataIds()) { loadDiamondDataIfPresent(compositePropertySource, dataId, acmIntegrationProperties.getAcmProperties().getGroup(), true); } for (String dataId : acmIntegrationProperties .getApplicationConfigurationDataIds()) { loadDiamondDataIfPresent(compositePropertySource, dataId, acmIntegrationProperties.getAcmProperties().getGroup(), false); } return compositePropertySource; }
/** * 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)); } } }
org.springframework.core.env.Environment environment) { ConfigClientProperties properties = this.defaultProperties.override(environment); CompositePropertySource composite = new CompositePropertySource("configService"); RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties)
public ExtendedDefaultPropertySource(String name, PropertySource<?> propertySource) { super(name, findMap(propertySource)); this.sources = new CompositePropertySource(name); }
private PropertySource createPropertySource(AnnotationAttributes attributes, ConfigurableEnvironment environment, ResourceLoader resourceLoader, EncryptablePropertyResolver resolver, EncryptablePropertyFilter propertyFilter, List<PropertySourceLoader> loaders) throws Exception { String name = generateName(attributes.getString("name")); String[] locations = attributes.getStringArray("value"); boolean ignoreResourceNotFound = attributes.getBoolean("ignoreResourceNotFound"); CompositePropertySource compositePropertySource = new CompositePropertySource(name); Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required"); for (String location : locations) { String resolvedLocation = environment.resolveRequiredPlaceholders(location); Resource resource = resourceLoader.getResource(resolvedLocation); if (!resource.exists()) { if (!ignoreResourceNotFound) { throw new IllegalStateException(String.format("Encryptable Property Source '%s' from location: %s Not Found", name, resolvedLocation)); } else { log.info("Ignoring NOT FOUND Encryptable Property Source '{}' from locations: {}", name, resolvedLocation); } } else { String actualName = name + "#" + resolvedLocation; loadPropertySource(loaders, resource, actualName) .ifPresent(psources -> psources.forEach(compositePropertySource::addPropertySource)); } } return new EncryptableEnumerablePropertySourceWrapper<>(compositePropertySource, resolver, propertyFilter); }
@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); } }
public ExtendedDefaultPropertySource(String name, PropertySource<?> propertySource) { super(name, findMap(propertySource)); this.sources = new CompositePropertySource(name); }
private PropertySource buildPropertySource(ResourceLoader resourceLoader) throws IOException { CompositePropertySource propertySource = new CompositePropertySource(PROPERTY_SOURCE_NAME); appendPropertySource(propertySource, resourceLoader); return propertySource; }
/** * 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; }
@Override public PropertySource<?> locate(org.springframework.core.env.Environment environment) { final ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; final NativeEnvironmentRepository springEnv = new NativeEnvironmentRepository(configurableEnvironment); final List<File> propertiesPath = getConfigDirs(); //https://github.com/spring-cloud/spring-cloud-config/issues/338 springEnv.setSearchLocations(toSearchLocations(propertiesPath)); CompositePropertySource composite = new CompositePropertySource(FileSystemLocator.class.getSimpleName()); addToPropertySource(returnPropertiesForName(springEnv, appCoordinates.getApplicationNameWithCountry()), composite); addToPropertySource(returnPropertiesForName(springEnv, appCoordinates.getApplicationNameWithoutCountry()), composite); addToPropertySource(returnPropertiesForName(springEnv, ConfigLocations.BASE_FILENAME_FOR_GLOBAL_CONFIG), composite); return composite; }