private PropertySource createPropertySource(AnnotationAttributes attributes, ConfigurableEnvironment environment, ResourceLoader resourceLoader, EncryptablePropertyResolver resolver, 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(compositePropertySource::addPropertySource); } } return new EncryptableEnumerablePropertySourceWrapper<>(compositePropertySource, resolver); }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { ResourceLoader ac = new DefaultResourceLoader(); MutablePropertySources propertySources = env.getPropertySources(); Stream<AnnotationAttributes> encryptablePropertySourcesMetadata = getEncryptablePropertySourcesMetadata(beanFactory); EncryptablePropertyResolver propertyResolver = beanFactory.getBean(RESOLVER_BEAN_NAME, EncryptablePropertyResolver.class); List<PropertySourceLoader> loaders = initPropertyLoaders(); encryptablePropertySourcesMetadata.forEach(eps -> loadEncryptablePropertySource(eps, env, ac, propertyResolver, propertySources, loaders)); }
private void loadEncryptablePropertySource(AnnotationAttributes encryptablePropertySource, ConfigurableEnvironment env, ResourceLoader resourceLoader, EncryptablePropertyResolver resolver, MutablePropertySources propertySources, List<PropertySourceLoader> loaders) throws BeansException { try { PropertySource ps = createPropertySource(encryptablePropertySource, env, resourceLoader, resolver, loaders); propertySources.addLast(ps); log.info("Created Encryptable Property Source '{}' from locations: {}", ps.getName(), Arrays.asList(encryptablePropertySource.getStringArray("value"))); } catch (Exception e) { throw new ApplicationContextException("Exception Creating PropertySource", e); } }
private Optional<PropertySource<?>> loadPropertySource(List<PropertySourceLoader> loaders, Resource resource, String sourceName) throws IOException { return Optional.of(resource) .filter(this::isFile) .map(res -> loaders.stream() .filter(loader -> canLoadFileExtension(loader, resource)) .findFirst() .map(loader -> load(loader, sourceName, resource)) .orElse(null)); }
private EnumerablePropertySource loadProperties(PropertySourceLoader hoconLoader, String path) throws IOException { return (EnumerablePropertySource) hoconLoader.load("hocon", new ClassPathResource(path), null); }
private List<PropertySourceLoader> initPropertyLoaders() { return SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader()); }
@SneakyThrows private PropertySource<?> load(PropertySourceLoader loader, String sourceName, Resource resource) { return loader.load(sourceName, resource, null); }
private boolean canLoadFileExtension(PropertySourceLoader loader, Resource resource) { return Arrays.stream(loader.getFileExtensions()) .anyMatch(extension -> resource.getFilename().toLowerCase().endsWith("." + extension.toLowerCase())); }