yangys
2024-04-01 86cdd920b68274185233383f69ddb974052b6b6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.qianwen.core.tool.yml;
 
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import com.qianwen.core.tool.utils.StringUtil;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.lang.Nullable;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/yml/YmlPropertyLoaderFactory.class */
public class YmlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {
        if (encodedResource == null) {
            return emptyPropertySource(name);
        }
        Resource resource = encodedResource.getResource();
        String fileName = resource.getFilename();
        List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(fileName, resource);
        if (sources.isEmpty()) {
            return emptyPropertySource(fileName);
        }
        Map<String, Object> ymlDataMap = new HashMap<>(32);
        /*
        Iterator<PropertySource<?>> it = sources.iterator();
        while (it.hasNext()) {
            ymlDataMap.putAll((Map) ((MapPropertySource) it.next()).getSource());
        }*/
        for (PropertySource<?> source : sources) {
            ymlDataMap.putAll((Map<? extends String, ?>)((MapPropertySource)source).getSource()); 
        }
        return new OriginTrackedMapPropertySource(getSourceName(fileName, name), ymlDataMap);
    }
 
    private static PropertySource<?> emptyPropertySource(@Nullable String name) {
        return new MapPropertySource(getSourceName(name), Collections.emptyMap());
    }
 
    private static String getSourceName(String... names) {
       
        return Stream.<String>of(names)
                  .filter(StringUtil::isNotBlank)
                  .findFirst()
                  .orElse("BladeYmlPropertySource");
    }
}