summaryrefslogtreecommitdiffstats
path: root/wso2/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/DefaultErrorCodeI18n.java
blob: d225e1b01eee75512ce3c38bc1495dcc0d6e07e1 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
 * Copyright 2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.baseservice.i18n;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public final class DefaultErrorCodeI18n implements ErrorCodeI18n {

    static final Logger logger = LoggerFactory.getLogger(DefaultErrorCodeI18n.class);
    private static DefaultErrorCodeI18n singleton;
    private static final Lock lock = new ReentrantLock();
    private Map<Integer, ErrorItemImpl> errorItems;

    private DefaultErrorCodeI18n() {
        try {
            init();
        } catch (Exception e) {
            logger.error("init ErrorCodeI18n failed.", e);
        }
    }

    private void init() throws Exception {
        final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        final Map<Integer, ErrorItemImpl> errorItems = new HashMap<Integer, DefaultErrorCodeI18n.ErrorItemImpl>();
        JsonResourceScanner.findErrorCodePaths().forEach(path -> {
            HashMap<String, Object> fileValues = null;
            try (InputStream ins = systemClassLoader.getResourceAsStream(path)) {
                fileValues = I18nJsonUtil.getInstance().readFromJson(ins, HashMap.class);
                logger.info("load errorcode file success: " + path);
            } catch (IOException ex) {
                logger.info("load errorcode file failed: " + path);
                logger.info(
                        "load errorcode file failed: " + systemClassLoader.getResource(path).toString(),
                        ex);
                return;
            }
            List<?> errcodes = (List<?>) fileValues.get("errcodes");
            if (errcodes == null) {
                logger.info("none errcodes field in: " + path);
                return;
            }

            String fileName = null;
            int i = path.lastIndexOf("/");
            if (i > -1) {
                fileName = path.substring(i + 1);
            } else {
                fileName = path;
            }
            i = fileName.indexOf("-errorcode-");
            String localeSrc = fileName.substring(i + 11, fileName.lastIndexOf("."));
            if (localeSrc.isEmpty()) {
                logger.info("parse errorcode file failed: locale is null");
                return;
            }

            String[] ss = localeSrc.replace("-", "_").split("_");
            String tempLocale = null;
            if (ss.length == 1) {
                tempLocale = new Locale(ss[0]).toString();
            } else if (ss.length == 2) {
                tempLocale = new Locale(ss[0], ss[1]).toString();
            } else {
                logger.info("parse i18n file failed: locale is error \"" + localeSrc + "\"");
                return;
            }
            String locale = tempLocale;
            errcodes.forEach(errorcode -> {
                Map<String, String> errorConfig = (Map<String, String>) errorcode;
                Integer code = Integer.valueOf(errorConfig.get("code"));
                String level = errorConfig.get("level");
                String label = errorConfig.get("label");

                ErrorItemImpl errorItem = errorItems.get(Integer.valueOf(code));
                if (errorItem == null) {
                    errorItem = new ErrorItemImpl();
                    errorItem.errorCode = code.intValue();
                    errorItem.level = ErrorCodeLevelUtil.transfer2Int(level);
                    errorItems.put(code, errorItem);
                }
                errorItem.addLabel(locale, label);
            });
        });

        errorItems.forEach((code, errorItem) -> {
            errorItem.unmodifiable();
        });
        this.errorItems = Collections.unmodifiableMap(errorItems);
    }


    static DefaultErrorCodeI18n getInstance() {
        if (singleton == null) {
            lock.lock();
            try {
                if (singleton == null) {
                    singleton = new DefaultErrorCodeI18n();
                }
            } finally {
                lock.unlock();
            }
        }
        return singleton;
    }


    /*
     * (non-Javadoc)
     *
     * @see com.zte.ums.zenap.i18n.ErrorCodeI18n#getErrorItem(int)
     */
    @Override
    public Optional<ErrorItem> getErrorItem(int errorCode) {
        return Optional.ofNullable(errorItems.get(Integer.valueOf(errorCode)));
    }


    public static class ErrorItemImpl implements ErrorItem {

        private int errorCode;

        private int level;

        private Map<String, String> labels = new HashMap<String, String>();

        private String jsonString = null;

        @Override
        public int getErrorCode() {
            return errorCode;
        }

        @Override
        public int getLevel() {
            return level;
        }

        public Map<String, String> getLabels() {
            return labels;
        }

        private void unmodifiable() {
            if (labels != null) {
                labels = Collections.unmodifiableMap(labels);
            }
        }

        private synchronized void addLabel(String locale, String label) {
            labels.put(locale, label);
        }

        @Override
        public String getLabel(Locale theLocale) {
            if (theLocale == null) {
                return null;
            }
            return labels.get(I18nLocaleTransfer.transfer(theLocale, labels.keySet()));
        }

        @Override
        public String getCanonicalLabels(int errorCode) {
            String jsonString = this.jsonString;
            if (jsonString == null) {
                ErrorItem2 errorItem2 = new ErrorItem2();
                errorItem2.setErrorCode(this.errorCode);
                errorItem2.setLevel(ErrorCodeLevelUtil.transfer2String(this.errorCode));
                errorItem2.setErrlabels(labels);
                try {
                    jsonString = I18nJsonUtil.getInstance().writeToJson(errorItem2);
                } catch (Exception e) {
                    logger.info("getCanonicalLabels failed from with param errorCode " + errorCode
                            + " and this errorCode " + this.errorCode, e);
                    return null;
                }
                this.jsonString = jsonString;
            }
            return jsonString;
        }

    }

    protected static class ErrorItem2 {

        private int errorCode;

        private String level;

        private Map<String, String> errlabels;

        public ErrorItem2() {

        }

        public int getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(int errorCode) {
            this.errorCode = errorCode;
        }

        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public Map<String, String> getErrlabels() {
            return errlabels;
        }

        public void setErrlabels(Map<String, String> errlabels) {
            this.errlabels = errlabels;
        }
    }

    protected static class ErrorCodeLevelUtil {

        public static final int ERROR_LEVEL = javax.swing.JOptionPane.ERROR_MESSAGE;

        public static final int WARN_LEVEL = javax.swing.JOptionPane.WARNING_MESSAGE;

        public static final int INFO_LEVEL = javax.swing.JOptionPane.INFORMATION_MESSAGE;

        protected static String transfer2String(int errorCode) {
            switch (errorCode) {
                case ERROR_LEVEL:
                    return "ERROR";
                case INFO_LEVEL:
                    return "INFO";
                case WARN_LEVEL:
                    return "WARN";
            }
            return null;
        }

        protected static int transfer2Int(String level) {
            switch (level) {
                case "ERROR":
                    return ERROR_LEVEL;
                case "INFO":
                    return INFO_LEVEL;
                case "WARN":
                    return WARN_LEVEL;
            }
            return -1;
        }

    }

}