yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.qianwen.core.i18n.config;
 
import cn.hutool.core.collection.CollectionUtil;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.i18n.source.WildcardReloadableResourceBundleMessageSource;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.context.MessageSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.StringUtils;
 
@EnableConfigurationProperties
@AutoConfigureBefore({MessageSourceAutoConfiguration.class})
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = {"messageSource"}, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Integer.MIN_VALUE)
@Conditional({CustomMessageSourceAutoConfiguration.ResourceBundleCondition.class})
/* loaded from: blade-starter-i18n-9.3.0.0-SNAPSHOT.jar:org/springblade/core/i18n/config/CustomMessageSourceAutoConfiguration.class */
public class CustomMessageSourceAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(CustomMessageSourceAutoConfiguration.class);
    private static final Resource[] NO_RESOURCES = new Resource[0];
 
    @ConfigurationProperties(prefix = "spring.messages")
    @ConditionalOnMissingBean
    @Bean
    public MessageSourceProperties messageSourceProperties() {
        return new MessageSourceProperties();
    }
 
    @Bean
    @Primary
    public MessageSource messageSource(MessageSourceProperties properties) {
        WildcardReloadableResourceBundleMessageSource messageSource = new WildcardReloadableResourceBundleMessageSource();
        if (StringUtils.hasText(properties.getBasename())) {
            messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
        }
        if (properties.getEncoding() != null) {
            messageSource.setDefaultEncoding(properties.getEncoding().name());
        }
        messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
        Duration cacheDuration = properties.getCacheDuration();
        if (cacheDuration != null) {
            messageSource.setCacheMillis(cacheDuration.toMillis());
        }
        messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
        messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
        return messageSource;
    }
 
    /* loaded from: blade-starter-i18n-9.3.0.0-SNAPSHOT.jar:org/springblade/core/i18n/config/CustomMessageSourceAutoConfiguration$ResourceBundleCondition.class */
    protected static class ResourceBundleCondition extends SpringBootCondition {
        private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
 
        protected ResourceBundleCondition() {
        }
 
        public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
            String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
            ConditionOutcome outcome = (ConditionOutcome) cache.get(basename);
            if (outcome == null) {
                outcome = getMatchOutcomeForBasename(context, basename);
                cache.put(basename, outcome);
            }
            return outcome;
        }
 
        private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
            String[] commaDelimitedListToStringArray;
            Resource[] resources;
            ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle", new Object[0]);
            for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) {
                List<Resource> resourceList = new ArrayList<>();
                for (Resource resource : getResources(context.getClassLoader(), name)) {
                    if (resource.exists()) {
                        CustomMessageSourceAutoConfiguration.log.info("扫描到的包路径" + resource.getFilename());
                        resourceList.add(resource);
                    }
                }
                if (CollectionUtil.isNotEmpty(resourceList)) {
                    return ConditionOutcome.match(message.found("bundle").items(resourceList));
                }
            }
            return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
        }
 
        private Resource[] getResources(ClassLoader classLoader, String name) {
            String target = name.replace('.', '/');
            try {
                return new PathMatchingResourcePatternResolver(classLoader).getResources("classpath*:" + target + ".properties");
            } catch (Exception e) {
                return CustomMessageSourceAutoConfiguration.NO_RESOURCES;
            }
        }
    }
}