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      private static final Logger LOGGER = LogManager.getRootLogger();
25  
26      private final Properties properties = new Properties();
27  
28      public PropertiesUtil() {
29          var filename = "config.properties";
30          try (InputStream input = PropertiesUtil.class.getClassLoader().getResourceAsStream(filename)) {
31              if (input == null) {
32                  LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Properties file {} not found", filename);
33                  return;
34              }
35              this.properties.load(input);  // load a properties file from class path, inside static method
36          } catch (IOException e) {
37              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
38          }
39      }
40  
41      public void displayStatus(Locale newLocale) {
42          AtomicInteger countJvm = new AtomicInteger();
43          var statusJvm = StringUtils.EMPTY;
44          AtomicInteger countGui = new AtomicInteger();
45          var statusGui = StringUtils.EMPTY;
46          var propertiesRoot = PropertiesUtil.getProperties("i18n/jsql.properties");
47  
48          if (!Arrays.asList(StringUtils.EMPTY, "en").contains(newLocale.getLanguage())) {
49              var bundleUser = PropertiesUtil.getProperties("i18n/jsql_" + newLocale.getLanguage() + ".properties");
50              propertiesRoot.entrySet().stream()
51              .filter(key -> bundleUser.isEmpty() || !bundleUser.containsKey(key.getKey()))
52              .forEach(key -> countGui.getAndIncrement());
53              statusGui = String.format("gui %s %s%% %s",
54                  newLocale.getDisplayLanguage(newLocale),
55                  countGui.get() * 100 / propertiesRoot.entrySet().size(),
56                  countGui.get() <= 0 ? StringUtils.EMPTY : "("+ countGui.get() +" items)"
57              );
58          }
59  
60          if (!Locale.getDefault().getLanguage().equals("en")) {
61              var propertiesJvm = PropertiesUtil.getProperties("i18n/jsql_"+ Locale.getDefault().getLanguage() +".properties");
62              propertiesRoot.entrySet().stream()
63              .filter(key -> propertiesJvm.isEmpty() || !propertiesJvm.containsKey(key.getKey()))
64              .forEach(key -> countJvm.getAndIncrement());
65              statusJvm = String.format("jvm %s %s%% %s",
66                  Locale.getDefault().getDisplayLanguage(newLocale),
67                  countJvm.get() * 100 / propertiesRoot.entrySet().size(),
68                  countJvm.get() <= 0 ? StringUtils.EMPTY : " ("+ countJvm.get() +" items)"
69              );
70          }
71  
72          if (countJvm.get() > 0 || countGui.get() > 0) {
73              LOGGER.info(
74                  "i18n status: {}{}",
75                  countJvm.get() > 0 ? statusJvm : StringUtils.EMPTY,
76                  countGui.get() > 0 ? statusGui : StringUtils.EMPTY
77              );
78          }
79      }
80  
81      private static Properties getProperties(String name) {
82          var properties = new Properties();
83          try {
84              var uri = ClassLoader.getSystemResource(name).toURI();
85              var path = Paths.get(uri);
86              byte[] root = Files.readAllBytes(path);
87              var rootI18n = new String(root, StandardCharsets.UTF_8);
88              String rootI18nFixed = Pattern.compile("\\\\[\n\r]+")
89                  .matcher(Matcher.quoteReplacement(rootI18n))
90                  .replaceAll("a");
91              properties.load(new StringReader(rootI18nFixed));
92          } catch (IOException | URISyntaxException e) {
93              throw new JSqlRuntimeException(e);
94          }
95          return properties;
96      }
97  
98      public String getVersionJsql() {
99          return this.properties.getProperty("jsql.version");
100     }
101 
102     public String getProperty(String property) {
103         return this.properties.getProperty(property);
104     }
105 }