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); } } } }
/** * 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; }
/** * Returns a list of MapPropertySource that correspond to the current state of the * system This only handles the PropertySource objects that are returned */ protected List<MapPropertySource> locateMapPropertySources( PropertySourceLocator propertySourceLocator, Environment environment) { List<MapPropertySource> result = new ArrayList<>(); PropertySource propertySource = propertySourceLocator.locate(environment); if (propertySource instanceof MapPropertySource) { result.add((MapPropertySource) propertySource); } else if (propertySource instanceof CompositePropertySource) { result.addAll(((CompositePropertySource) propertySource).getPropertySources() .stream().filter(p -> p instanceof MapPropertySource) .map(p -> (MapPropertySource) p).collect(Collectors.toList())); } else { log.debug("Found property source that cannot be handled: " + propertySource.getClass()); } return result; }
private static void extract(String root, Map<String, PropertySource<?>> map, PropertySource<?> source) { if (source instanceof CompositePropertySource) { for (PropertySource<?> nest : ((CompositePropertySource) source) .getPropertySources()) { extract(source.getName() + ":", map, nest); } } else { map.put(root + source.getName(), source); } }
public static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) { Map<String, Object> result = new HashMap<>(); if (aPropSource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) aPropSource; cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps))); return result; } if (aPropSource instanceof EnumerablePropertySource<?>) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource; Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key))); return result; } //unable to iterate over it return result; }
@Override protected void doHealthCheck(Builder builder) throws Exception { PropertySource<?> propertySource = getPropertySource(); builder.up(); if (propertySource instanceof CompositePropertySource) { List<String> sources = new ArrayList<>(); for (PropertySource<?> ps : ((CompositePropertySource) propertySource).getPropertySources()) { sources.add(ps.getName()); } builder.withDetail("propertySources", sources); } else if (propertySource!=null) { builder.withDetail("propertySources", propertySource.toString()); } else { builder.unknown().withDetail("error", "no property sources located"); } }
@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))); }
private void refreshPropertySource(PropertySource<?> propertySource) { if (propertySource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) propertySource; cps.getPropertySources().forEach(this::refreshPropertySource); } else if (propertySource instanceof EncryptablePropertySource){ EncryptablePropertySource eps = (EncryptablePropertySource) propertySource; eps.refresh(); } } }
/** * 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; }
private void extract(String root, Map<String, PropertySource<?>> map, PropertySource<?> source) { if (source instanceof CompositePropertySource) { for (PropertySource<?> nest : ((CompositePropertySource) source) .getPropertySources()) { extract(source.getName() + ":", map, nest); } } else { map.put(root + source.getName(), source); } }
.getPropertySources()) { collectEncryptedProperties(nested, overrides);
private Set<String> addIncludedProfilesTo(Set<String> profiles, PropertySource<?> propertySource) { if (propertySource instanceof CompositePropertySource) { for (PropertySource<?> nestedPropertySource : ((CompositePropertySource) propertySource) .getPropertySources()) { addIncludedProfilesTo(profiles, nestedPropertySource); } } else { Collections.addAll(profiles, getProfilesForValue(propertySource.getProperty( ConfigFileApplicationListener.INCLUDE_PROFILES_PROPERTY))); } return profiles; }
private void extract(PropertySource<?> parent, Map<String, Object> result) { if (parent instanceof CompositePropertySource) { try { List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>(); for (PropertySource<?> source : ((CompositePropertySource) parent) .getPropertySources()) { sources.add(0, source); } for (PropertySource<?> source : sources) { extract(source, result); } } catch (Exception e) { return; } } else if (parent instanceof EnumerablePropertySource) { for (String key : ((EnumerablePropertySource<?>) parent).getPropertyNames()) { result.put(key, parent.getProperty(key)); } } }
/** * Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)} */ private void getProperties(ConfigurableEnvironment environment, PropertySource<?> propertySource, Map<String, Object> configFromSpringBoot) { if (propertySource instanceof CompositePropertySource) { // recursively get EnumerablePropertySource CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource; compositePropertySource.getPropertySources().forEach(ps -> getProperties(environment, ps, configFromSpringBoot)); return; } if (propertySource instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource; for (String propertyName : enumerablePropertySource.getPropertyNames()) { configFromSpringBoot.put(propertyName, environment.getProperty(propertyName, Object.class)); } return; } LOGGER.debug("a none EnumerablePropertySource is ignored, propertySourceName = [{}]", propertySource.getName()); } }
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 void refreshPropertySource(PropertySource<?> propertySource) { if (propertySource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) propertySource; cps.getPropertySources().forEach(this::refreshPropertySource); } else if (propertySource instanceof EncryptablePropertySource){ EncryptablePropertySource eps = (EncryptablePropertySource) propertySource; eps.refresh(); } } }
/** * Initialize nested {@link PropertySource}s inside the * {@link CompositePropertySource}. * @param propertySource the {@link CompositePropertySource} to initialize. */ protected void initialize(CompositePropertySource propertySource) { for (PropertySource<?> source : propertySource.getPropertySources()) { ((VaultPropertySource) source).init(); } }
/** * Initialize nested {@link PropertySource}s inside the * {@link CompositePropertySource}. * @param propertySource the {@link CompositePropertySource} to initialize. */ protected void initialize(CompositePropertySource propertySource) { for (PropertySource<?> source : propertySource.getPropertySources()) { ((VaultPropertySource) source).init(); } }
private void extract(String root, Map<String, PropertySource<?>> map, PropertySource<?> source) { if (source instanceof CompositePropertySource) { for (PropertySource<?> nest : ((CompositePropertySource) source) .getPropertySources()) { extract(source.getName() + ":", map, nest); } } else { map.put(root + source.getName(), source); } } }
private void extract(String root, Map<String, PropertySource<?>> map, PropertySource<?> source) { if (source instanceof CompositePropertySource) { for (PropertySource<?> nest : ((CompositePropertySource) source) .getPropertySources()) { extract(source.getName() + ":", map, nest); } } else { map.put(root + source.getName(), source); } }