1
zj
2024-06-13 8eea5be3b36875bd4ffe70e6c3a5bb07b1d829bf
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.yami.trading.common.util;
 
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Properties;
 
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ResourceUtils;
 
/**
 * @author JORGE
 * @description 配置配置文件
 */
public class PropertiesReader {
    /**
     * 路径解析器
     * 配置文件(*.yml|*.properties)
     */
    private static ResourcePatternResolver pathResolver=new PathMatchingResourcePatternResolver();
    
    /**
     * 字母序列
     */
    private static final String LETTERS="abcdefghijklmnopqrstuvwxyz"+"abcdefghijklmnopqrstuvwxyz".toUpperCase();
    
    /**
     * 读取属性配置文件
     * @param absolutePath 配置文件绝对路径
     * @return 属性字典
     */
    public static Properties getPropertiesByRealPath(String absolutePath){
        return getProperties(new File(absolutePath));
    }
    
    /**
     * 读取属性配置文件
     * @param fileUrl 配置文件路径
     * @return 属性字典
     */
    public static Properties getProperties(URL fileUrl){
        if(!ResourceUtils.isFileURL(fileUrl)) return null;
        return getProperties(new FileUrlResource(fileUrl));
    }
    
    /**
     * 读取属性配置文件
     * @param fileUri 配置文件路径
     * @return 属性字典
     */
    public static Properties getProperties(URI fileUri){
        URL url=null;
        try {
            url = fileUri.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        
        if(!ResourceUtils.isFileURL(url)) return null;
        return getProperties(new FileUrlResource(url));
    }
    
    /**
     * 读取属性配置文件
     * @param confFile 配置文件
     * @return 属性字典
     */
    public static Properties getProperties(File confFile){
        if(!confFile.exists() || confFile.isDirectory()) return null;
        
        URL url=null;
        try {
            url = confFile.toURI().toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        
        if(!ResourceUtils.isFileURL(url)) return null;
        return getProperties(new FileUrlResource(url));
    }
    
    /**
     * 判断是否为绝对路径
     * @param resourcePath 资源路径
     * @return 是否为绝对路径
     */
    public static boolean isAbsolutePath(String resourcePath){
        if(null==resourcePath || resourcePath.trim().isEmpty()) return false;
        char firstChar=resourcePath.charAt(0);
        if(firstChar=='/' || firstChar=='\\' || firstChar==File.separatorChar) return true;
        int count=LETTERS.length();
        for(int i=0;i<count;i++) if(resourcePath.startsWith(LETTERS.charAt(i)+":")) return true;
        return false;
    }
    
    /**
     * 根据可选的资源路径获取资源描述
     * @param resourceURI 资源路径
     * @return 资源描述
     */
    public static Resource getResource(String... resourceURI){
        Resource resource=null;
        if(null==resourceURI || 0==resourceURI.length || null==resourceURI[0] || resourceURI[0].trim().isEmpty()) {
            resource = pathResolver.getResource("classpath:application.yml");
            if(null==resource || !resource.exists()) resource = pathResolver.getResource("classpath:application.yaml");
            if(null==resource || !resource.exists()) resource = pathResolver.getResource("classpath:application.properties");
        } else {
            String resourcePath=resourceURI[0].trim();
            if(resourcePath.startsWith("classpath:") || resourcePath.startsWith("classpath*:") || resourcePath.startsWith("file:")){
                resource = pathResolver.getResource(resourcePath);
            }else if(isAbsolutePath(resourcePath)){
                resource = pathResolver.getResource("file:"+resourcePath);
            }else{
                resource = pathResolver.getResource(resourcePath);
            }
        }
        if(null==resource || !resource.exists()) return null;
        return resource;
    }
    
    /**
     * 从资源配置中获取属性字典表
     * @param resourceURI 资源路径
     * @return 属性字典
     */
    public static Properties getProperties(String... resourceURI){
        Resource resource=getResource(resourceURI);
        if(null==resource) return null;
        return getProperties(resource);
    }
    
    /**
     * 从资源配置中获取属性字典表
     * @param resource 资源描述
     * @return 属性字典
     */
    public static Properties getProperties(Resource resource){
        String fileName = resource.getFilename();
        if(null==fileName || fileName.trim().isEmpty()) return null;
        Properties properties = null;
        if (fileName.endsWith(".yml") || fileName.endsWith(".yaml")) {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties=yamlFactory.getObject();
        }else{
            try {
                properties=PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }
    
    /**
     * 从资源路径指向的配置文件中获取指定键的值
     * @param key 键
     * @param defaultValue 默认值
     * @param resourceURI 资源路径
     * @return 值
     */
    public static String getProperty(String key,String defaultValue,String... resourceURI){
        Resource resource=getResource(resourceURI);
        if(null==resource) return null;
        Properties properties=getProperties(resource);
        if(null==properties) return null;
        String value = properties.getProperty(key);
        return null==value?defaultValue:value;
    }
    
    /**
     * 从资源路径指向的配置文件中获取指定键的值
     * @param key 键
     * @param resourceURI 资源路径
     * @return 值
     */
    public static String getProperty(String key,String... resourceURI){
        return getProperty(key,null,resourceURI);
    }
}