View Javadoc
1   package com.jsql.util;
2   
3   import com.jsql.model.exception.JSqlRuntimeException;
4   import org.apache.commons.lang3.StringUtils;
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.io.StringReader;
11  import java.net.URISyntaxException;
12  import java.nio.charset.StandardCharsets;
13  import java.nio.file.Files;
14  import java.nio.file.Paths;
15  import java.util.Arrays;
16  import java.util.Locale;
17  import java.util.Properties;
18  import java.util.concurrent.atomic.AtomicInteger;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  public class PropertiesUtil {
23      
24      /**
25       * Log4j logger sent to view.
26       */
27      private static final Logger LOGGER = LogManager.getRootLogger();
28  
29      private final Properties properties = new Properties();
30  
31      public PropertiesUtil() {
32          var filename = "config.properties";
33          try (InputStream input = PropertiesUtil.class.getClassLoader().getResourceAsStream(filename)) {
34              if (input == null) {
35                  LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Properties file {} not found", filename);
36                  return;
37              }
38              this.properties.load(input);  // load a properties file from class path, inside static method
39          } catch (IOException e) {
40              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
41          }
42      }
43  
44      public void displayStatus(Locale newLocale) {
45          AtomicInteger countJvm = new AtomicInteger();
46          var statusJvm = StringUtils.EMPTY;
47          AtomicInteger countGui = new AtomicInteger();
48          var statusGui = StringUtils.EMPTY;
49          var propertiesRoot = PropertiesUtil.getProperties("i18n/jsql.properties");
50  
51          if (!Arrays.asList(StringUtils.EMPTY, "en").contains(newLocale.getLanguage())) {
52              var bundleUser = PropertiesUtil.getProperties("i18n/jsql_" + newLocale.getLanguage() + ".properties");
53              propertiesRoot.entrySet().stream()
54              .filter(key -> bundleUser.isEmpty() || !bundleUser.containsKey(key.getKey()))
55              .forEach(key -> countGui.getAndIncrement());
56              statusGui = String.format("gui %s %s%% %s",
57                  newLocale.getDisplayLanguage(newLocale),
58                  (countGui.get() * 100) / propertiesRoot.entrySet().size(),
59                  countGui.get() <= 0 ? StringUtils.EMPTY : "("+ countGui.get() +" items)"
60              );
61          }
62  
63          if (!Locale.getDefault().getLanguage().equals("en")) {
64              var propertiesJvm = PropertiesUtil.getProperties("i18n/jsql_"+ Locale.getDefault().getLanguage() +".properties");
65              propertiesRoot.entrySet().stream()
66              .filter(key -> propertiesJvm.isEmpty() || !propertiesJvm.containsKey(key.getKey()))
67              .forEach(key -> countJvm.getAndIncrement());
68              statusJvm = String.format("jvm %s %s%% %s",
69                  Locale.getDefault().getDisplayLanguage(newLocale),
70                  (countJvm.get() * 100) / propertiesRoot.entrySet().size(),
71                  countJvm.get() <= 0 ? StringUtils.EMPTY : " ("+ countJvm.get() +" items)"
72              );
73          }
74  
75          if (countJvm.get() > 0 || countGui.get() > 0) {
76              LOGGER.info(
77                  "i18n status: {}{}",
78                  countJvm.get() > 0 ? statusJvm : StringUtils.EMPTY,
79                  countGui.get() > 0 ? statusGui : StringUtils.EMPTY
80              );
81          }
82      }
83  
84      private static Properties getProperties(String name) {
85          var properties = new Properties();
86          try {
87              var uri = ClassLoader.getSystemResource(name).toURI();
88              var path = Paths.get(uri);
89              byte[] root = Files.readAllBytes(path);
90              var rootI18n = new String(root, StandardCharsets.UTF_8);
91              String rootI18nFixed = Pattern.compile("\\\\[\n\r]+")
92                  .matcher(Matcher.quoteReplacement(rootI18n))
93                  .replaceAll("a");
94              properties.load(new StringReader(rootI18nFixed));
95          } catch (IOException | URISyntaxException e) {
96              throw new JSqlRuntimeException(e);
97          }
98          return properties;
99      }
100 
101     public String getVersionJsql() {
102         return this.properties.getProperty("jsql.version");
103     }
104 
105     public String getProperty(String property) {
106         return this.properties.getProperty(property);
107     }
108 }