/** * 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 collectAcmPropertySources(CompositePropertySource composite, List<AcmPropertySource> result) { for (PropertySource p : composite.getPropertySources()) { if (p instanceof AcmPropertySource) { result.add((AcmPropertySource) p); } else if (p instanceof CompositePropertySource) { collectAcmPropertySources((CompositePropertySource) p, result); } } } }
((ResourcePropertySource) propertySource).withResourceName() : propertySource); if (existing instanceof CompositePropertySource) { ((CompositePropertySource) existing).addFirstPropertySource(newSource); existing = ((ResourcePropertySource) existing).withResourceName(); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, 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))); }
private PropertySource buildPropertySource(ResourceLoader resourceLoader) throws IOException { CompositePropertySource propertySource = new CompositePropertySource(PROPERTY_SOURCE_NAME); appendPropertySource(propertySource, resourceLoader); return propertySource; }
@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; }
compositeSource = new CompositePropertySource(compositeSourceName); if (addBeforeSourceName == null) { environment.getPropertySources().addLast(compositeSource); environment.getPropertySources().addBefore(addBeforeSourceName, compositeSource); LOG.debug("Added new composite property source source " + compositeSource.getName() + " to the environment"); compositeSource.addFirstPropertySource(props); LOG.debug(String.format("Added property source %s at the beginning of the composite source ", props.getName(), compositeSource.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 void loadDiamondDataIfPresent(final CompositePropertySource composite, final String dataId, final String diamondGroup, final boolean groupLevel) { AcmPropertySource ps = acmPropertySourceBuilder.build(dataId, diamondGroup, groupLevel); if (ps != null) { composite.addFirstPropertySource(ps); } } }
@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 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; }
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 addPropertySourceIfNeeded(Function<String, Map<String, String>> contentToMapFunction, String content, String name, CompositePropertySource composite) { Map<String, Object> map = new HashMap<>(); map.putAll(contentToMapFunction.apply(content)); if(map.isEmpty()) { LOG.warn("Property source: " + name + "will be ignored because no properties could be found"); } else { composite.addFirstPropertySource(new MapPropertySource(name, map)); } } }
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); } }
/** * Finds all registered property sources of the given type. */ protected <S extends PropertySource<?>> List<S> findPropertySources( Class<S> sourceClass) { List<S> managedSources = new LinkedList<>(); LinkedList<PropertySource<?>> sources = toLinkedList( environment.getPropertySources()); while (!sources.isEmpty()) { PropertySource<?> source = sources.pop(); if (source instanceof CompositePropertySource) { CompositePropertySource comp = (CompositePropertySource) source; sources.addAll(comp.getPropertySources()); } else if (sourceClass.isInstance(source)) { managedSources.add(sourceClass.cast(source)); } } return managedSources; }
((ResourcePropertySource) propertySource).withResourceName() : propertySource); if (existing instanceof CompositePropertySource) { ((CompositePropertySource) existing).addFirstPropertySource(newSource); existing = ((ResourcePropertySource) existing).withResourceName(); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, 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; }
/** * Property files processed in order of non-profiles first and then profiles, and profiles are first to last * with properties in the last profile overriding properties in previous profiles or non-profiles. * * @param environment Spring environment * @param config Location of config files * @return Merged properties */ private PropertySource<?> loadSettingsByApplicationProfiles(final Environment environment, final File config) { val profiles = ConfigurationPropertiesLoaderFactory.getApplicationProfiles(environment); val resources = scanForConfigurationResources(config, profiles); val composite = new CompositePropertySource("applicationProfilesCompositeProperties"); LOGGER.info("Configuration files found at [{}] are [{}] under profile(s) [{}]", config, resources, profiles); resources.forEach(Unchecked.consumer(f -> { LOGGER.debug("Loading configuration file [{}]", f); val loader = configurationPropertiesLoaderFactory.getLoader(f, "applicationProfilesProperties-" + f.getFilename()); composite.addFirstPropertySource(loader.load()); })); return composite; }
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 loadNacosDataIfPresent(final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) { if (NacosContextRefresher.getRefreshCount() != 0) { NacosPropertySource ps; if (!isRefreshable) { ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); } else { ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); } composite.addFirstPropertySource(ps); } else { NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, isRefreshable); composite.addFirstPropertySource(ps); } }