935090232@qq.com
2021-03-19 81a3369395a7cccceb7cd36f5238cc6fe2aa88e5
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.matrix.component.ueditor;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
 
import com.matrix.component.ueditor.define.ActionMap;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
 
import com.matrix.core.tools.PropertiesUtil;
 
/**
 * 配置管理器
 * 
 * @author hancong03@baidu.com
 *
 */
public final class ConfigManager {
 
    private final String rootPath;
    private final String originalPath;
    private final String contextPath;
    private static final String configFileName = "config.json";
    private String parentPath = null;
    private JSONObject jsonConfig = null;
    // 涂鸦上传filename定义
    private final static String SCRAWL_FILE_NAME = "scrawl";
    // 远程图片抓取filename定义
    private final static String REMOTE_FILE_NAME = "remote";
    
    private final static String FILES_TORAGE_PATH ="file_storage_path";
 
    /*
     * 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
     */
    private ConfigManager(String rootPath, String contextPath, String uri) throws FileNotFoundException, IOException {
 
        rootPath = rootPath.replace("\\", "/");
 
        this.rootPath = rootPath;
        this.contextPath = contextPath;
        if (contextPath.length() > 0) {
            this.originalPath = this.rootPath + uri.substring(contextPath.length());
        } else {
            this.originalPath = this.rootPath + uri;
        }
        this.initEnv();
 
    }
 
    /**
     * 配置管理器构造工厂
     * 
     * @param rootPath
     *            服务器根路径
     * @param contextPath
     *            服务器所在项目路径
     * @param uri
     *            当前访问的uri
     * @return 配置管理器实例或者null
     */
    public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {
 
        try {
            return new ConfigManager(rootPath, contextPath, uri);
        } catch (Exception e) {
            return null;
        }
 
    }
 
    // 验证配置文件加载是否正确
    public boolean valid() {
        return this.jsonConfig != null;
    }
 
    public JSONObject getAllConfig() {
 
        return this.jsonConfig;
 
    }
 
    public Map<String, Object> getConfig(int type) {
 
        Map<String, Object> conf = new HashMap<String, Object>();
        String savePath = null;
 
        switch (type) {
 
        case ActionMap.UPLOAD_FILE:
            conf.put("isBase64", "false");
            conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
            conf.put("allowFiles", this.getArray("fileAllowFiles"));
            conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
            savePath = this.jsonConfig.getString("filePathFormat");
            break;
 
        case ActionMap.UPLOAD_IMAGE:
            conf.put("isBase64", "false");
            conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
            conf.put("allowFiles", this.getArray("imageAllowFiles"));
            conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
            savePath = this.jsonConfig.getString("imagePathFormat");
            break;
 
        case ActionMap.UPLOAD_VIDEO:
            conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"));
            conf.put("allowFiles", this.getArray("videoAllowFiles"));
            conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
            savePath = this.jsonConfig.getString("videoPathFormat");
            break;
 
        case ActionMap.UPLOAD_SCRAWL:
            conf.put("filename", ConfigManager.SCRAWL_FILE_NAME);
            conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
            conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
            conf.put("isBase64", "true");
            savePath = this.jsonConfig.getString("scrawlPathFormat");
            break;
 
        case ActionMap.CATCH_IMAGE:
            conf.put("filename", ConfigManager.REMOTE_FILE_NAME);
            conf.put("filter", this.getArray("catcherLocalDomain"));
            conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
            conf.put("allowFiles", this.getArray("catcherAllowFiles"));
            conf.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]");
            savePath = this.jsonConfig.getString("catcherPathFormat");
            break;
 
        case ActionMap.LIST_IMAGE:
            conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
            conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
            conf.put("count", this.jsonConfig.getInt("imageManagerListSize"));
            break;
 
        case ActionMap.LIST_FILE:
            conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
            conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
            conf.put("count", this.jsonConfig.getInt("fileManagerListSize"));
            break;
 
        }
 
        conf.put("savePath", savePath);
        conf.put("rootPath", PropertiesUtil.getString(this.FILES_TORAGE_PATH));
 
        return conf;
 
    }
 
    private void initEnv() throws FileNotFoundException, IOException {
 
        File file = new File(this.originalPath);
 
        if (!file.isAbsolute()) {
            file = new File(file.getAbsolutePath());
        }
 
        this.parentPath = file.getParent();
        //String configContent = this.readFile(this.getConfigPath());
        String configContent = this.filter(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("config/config.json")));
 
        try {
            JSONObject jsonConfig = new JSONObject(configContent);
            this.jsonConfig = jsonConfig;
        } catch (Exception e) {
            this.jsonConfig = null;
        }
 
    }
 
    private String getConfigPath() {
        //return this.parentPath + File.separator + ConfigManager.configFileName;
        try {
            //获取classpath下的config.json路径
            return this.getClass().getClassLoader().getResource("config.json").toURI().getPath();
        } catch (URISyntaxException e) {
            return null;
        }
 
    }
 
    private String[] getArray(String key) {
 
        JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
        String[] result = new String[jsonArray.length()];
 
        for (int i = 0, len = jsonArray.length(); i < len; i++) {
            result[i] = jsonArray.getString(i);
        }
 
        return result;
 
    }
 
    private String readFile(String path) throws IOException {
 
        StringBuilder builder = new StringBuilder();
 
        try {
 
            InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
            BufferedReader bfReader = new BufferedReader(reader);
 
            String tmpContent = null;
 
            while ((tmpContent = bfReader.readLine()) != null) {
                builder.append(tmpContent);
            }
 
            bfReader.close();
 
        } catch (UnsupportedEncodingException e) {
            // 忽略
        }
 
        return this.filter(builder.toString());
 
    }
 
    // 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
    private String filter(String input) {
 
        return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
 
    }
 
}