init code
[vfc/nfvo/wfengine.git] / wso2 / baseservice-i18n / src / main / java / org / openo / baseservice / i18n / I18nContainer.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.baseservice.i18n;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Optional;
28
29 final class I18nContainer {
30
31   private static Logger logger = LoggerFactory.getLogger(I18nContainer.class);
32
33   private Map<String, I18n> i18ns;
34
35   private I18nContainer() {
36     init();
37   }
38
39
40   private void init() {
41     Map<String, Map<String, I18nItem>> i18nTemps = generateI18ns();
42
43     i18ns = new HashMap<String, I18n>();
44     for (Entry<String, Map<String, I18nItem>> entry : i18nTemps.entrySet()) {
45       String name = entry.getKey();
46       Map<String, I18nItem> items = entry.getValue();
47       for (Entry<String, I18nItem> i18nItemEntry : items.entrySet()) {
48         i18nItemEntry.getValue().unmodifiable();
49       }
50
51       I18n i18n = new I18nImpl(name, items);
52       i18ns.put(name, i18n);
53     }
54   }
55
56   @SuppressWarnings("unchecked")
57   private Map<String, Map<String, I18nItem>> generateI18ns() {
58     Map<String, Map<String, I18nItem>> i18nTemps = new HashMap<String, Map<String, I18nItem>>();
59
60     ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
61     JsonResourceScanner.findI18nPaths().forEach((path) -> {
62       HashMap<String, String> fileValues = null;
63       try (InputStream ins = systemClassLoader.getResourceAsStream(path)) {
64         fileValues = I18nJsonUtil.getInstance().readFromJson(ins, HashMap.class);
65         logger.info("load i18n file success: " + path);
66       } catch (IOException ex) {
67         logger.info("load i18n file failed: " + path);
68         logger.info("load i18n file failed: " + systemClassLoader.getResource(path).toString(), ex);
69         return;
70       }
71
72       if (!validateI18nFileValues(fileValues, path)) {
73         return;
74       }
75
76       String fileName = null;
77       int i = path.lastIndexOf("/");
78       if (i > -1) {
79         fileName = path.substring(i + 1);
80       } else {
81         fileName = path;
82       }
83       i = fileName.indexOf("-i18n-");
84       String name = fileName.substring(0, i);
85       String localeSrc = fileName.substring(i + 6, fileName.lastIndexOf("."));
86       if (name.isEmpty()) {
87         logger.info("parse i18n file failed: name is null");
88         return;
89       } else if (localeSrc.isEmpty()) {
90         logger.info("parse i18n file failed: locale is null");
91         return;
92       }
93
94       String[] ss = localeSrc.replace("-", "_").split("_");
95       String locale = null;
96       if (ss.length == 1) {
97         locale = new Locale(ss[0]).toString();
98       } else if (ss.length == 2) {
99         locale = new Locale(ss[0], ss[1]).toString();
100       } else {
101         logger.info("parse i18n file failed: locale is error \"" + localeSrc + "\"");
102         return;
103       }
104
105       Map<String, I18nItem> i18nItems = i18nTemps.get(name);
106       if (i18nItems == null) {
107         i18nItems = new HashMap<String, I18nItem>();
108         i18nTemps.put(name, i18nItems);
109       }
110
111       for (Entry<String, String> entry : fileValues.entrySet()) {
112         String label = entry.getKey();
113
114         I18nItem i18nItem = i18nItems.get(label);
115         if (i18nItem == null) {
116           i18nItem = new I18nItem(label);
117           i18nItems.put(label, i18nItem);
118         }
119
120         i18nItem.addValue(locale, entry.getValue());
121       }
122     });
123     return i18nTemps;
124   }
125
126   private boolean validateI18nFileValues(HashMap<String, String> fileValues, String path) {
127     for (Entry<String, String> entry : fileValues.entrySet()) {
128       if (entry.getValue() != null && !String.class.isInstance(entry.getValue())) {
129         logger.info("parse i18n file failed: " + path + " field's[" + entry.getKey()
130             + "] value is not string type");
131         return false;
132       }
133     }
134     return true;
135   }
136
137   protected static I18nContainer getInstance() {
138     return I18nContainerSingleton.singleton;
139   }
140
141   Optional<I18n> getI18n(String name) {
142     return Optional.ofNullable(this.i18ns.get(name));
143   }
144
145   static class I18nContainerSingleton {
146     private static final I18nContainer singleton = new I18nContainer();
147   }
148 }