2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.baseservice.i18n;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Locale;
25 import java.util.Optional;
26 import java.util.concurrent.locks.Lock;
27 import java.util.concurrent.locks.ReentrantLock;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 public final class DefaultErrorCodeI18n implements ErrorCodeI18n {
34 static final Logger logger = LoggerFactory.getLogger(DefaultErrorCodeI18n.class);
35 private static DefaultErrorCodeI18n singleton;
36 private static final Lock lock = new ReentrantLock();
37 private Map<Integer, ErrorItemImpl> errorItems;
39 private DefaultErrorCodeI18n() {
42 } catch (Exception e) {
43 logger.error("init ErrorCodeI18n failed.", e);
47 private void init() throws Exception {
48 final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
49 final Map<Integer, ErrorItemImpl> errorItems = new HashMap<Integer, DefaultErrorCodeI18n.ErrorItemImpl>();
50 JsonResourceScanner.findErrorCodePaths().forEach(path -> {
51 HashMap<String, Object> fileValues = null;
52 try (InputStream ins = systemClassLoader.getResourceAsStream(path)) {
53 fileValues = I18nJsonUtil.getInstance().readFromJson(ins, HashMap.class);
54 logger.info("load errorcode file success: " + path);
55 } catch (IOException ex) {
56 logger.info("load errorcode file failed: " + path);
58 "load errorcode file failed: " + systemClassLoader.getResource(path).toString(),
62 List<?> errcodes = (List<?>) fileValues.get("errcodes");
63 if (errcodes == null) {
64 logger.info("none errcodes field in: " + path);
68 String fileName = null;
69 int i = path.lastIndexOf("/");
71 fileName = path.substring(i + 1);
75 i = fileName.indexOf("-errorcode-");
76 String localeSrc = fileName.substring(i + 11, fileName.lastIndexOf("."));
77 if (localeSrc.isEmpty()) {
78 logger.info("parse errorcode file failed: locale is null");
82 String[] ss = localeSrc.replace("-", "_").split("_");
83 String tempLocale = null;
85 tempLocale = new Locale(ss[0]).toString();
86 } else if (ss.length == 2) {
87 tempLocale = new Locale(ss[0], ss[1]).toString();
89 logger.info("parse i18n file failed: locale is error \"" + localeSrc + "\"");
92 String locale = tempLocale;
93 errcodes.forEach(errorcode -> {
94 Map<String, String> errorConfig = (Map<String, String>) errorcode;
95 Integer code = Integer.valueOf(errorConfig.get("code"));
96 String level = errorConfig.get("level");
97 String label = errorConfig.get("label");
99 ErrorItemImpl errorItem = errorItems.get(Integer.valueOf(code));
100 if (errorItem == null) {
101 errorItem = new ErrorItemImpl();
102 errorItem.errorCode = code.intValue();
103 errorItem.level = ErrorCodeLevelUtil.transfer2Int(level);
104 errorItems.put(code, errorItem);
106 errorItem.addLabel(locale, label);
110 errorItems.forEach((code, errorItem) -> {
111 errorItem.unmodifiable();
113 this.errorItems = Collections.unmodifiableMap(errorItems);
117 static DefaultErrorCodeI18n getInstance() {
118 if (singleton == null) {
121 if (singleton == null) {
122 singleton = new DefaultErrorCodeI18n();
135 * @see com.zte.ums.zenap.i18n.ErrorCodeI18n#getErrorItem(int)
138 public Optional<ErrorItem> getErrorItem(int errorCode) {
139 return Optional.ofNullable(errorItems.get(Integer.valueOf(errorCode)));
143 public static class ErrorItemImpl implements ErrorItem {
145 private int errorCode;
149 private Map<String, String> labels = new HashMap<String, String>();
151 private String jsonString = null;
154 public int getErrorCode() {
159 public int getLevel() {
163 public Map<String, String> getLabels() {
167 private void unmodifiable() {
168 if (labels != null) {
169 labels = Collections.unmodifiableMap(labels);
173 private synchronized void addLabel(String locale, String label) {
174 labels.put(locale, label);
178 public String getLabel(Locale theLocale) {
179 if (theLocale == null) {
182 return labels.get(I18nLocaleTransfer.transfer(theLocale, labels.keySet()));
186 public String getCanonicalLabels(int errorCode) {
187 String jsonString = this.jsonString;
188 if (jsonString == null) {
189 ErrorItem2 errorItem2 = new ErrorItem2();
190 errorItem2.setErrorCode(this.errorCode);
191 errorItem2.setLevel(ErrorCodeLevelUtil.transfer2String(this.errorCode));
192 errorItem2.setErrlabels(labels);
194 jsonString = I18nJsonUtil.getInstance().writeToJson(errorItem2);
195 } catch (Exception e) {
196 logger.info("getCanonicalLabels failed from with param errorCode " + errorCode
197 + " and this errorCode " + this.errorCode, e);
200 this.jsonString = jsonString;
207 protected static class ErrorItem2 {
209 private int errorCode;
211 private String level;
213 private Map<String, String> errlabels;
215 public ErrorItem2() {
219 public int getErrorCode() {
223 public void setErrorCode(int errorCode) {
224 this.errorCode = errorCode;
227 public String getLevel() {
231 public void setLevel(String level) {
235 public Map<String, String> getErrlabels() {
239 public void setErrlabels(Map<String, String> errlabels) {
240 this.errlabels = errlabels;
244 protected static class ErrorCodeLevelUtil {
246 public static final int ERROR_LEVEL = javax.swing.JOptionPane.ERROR_MESSAGE;
248 public static final int WARN_LEVEL = javax.swing.JOptionPane.WARNING_MESSAGE;
250 public static final int INFO_LEVEL = javax.swing.JOptionPane.INFORMATION_MESSAGE;
252 protected static String transfer2String(int errorCode) {
264 protected static int transfer2Int(String level) {