001package co.codewizards.cloudstore.core.util; 002 003import static co.codewizards.cloudstore.core.util.Util.*; 004 005import java.util.ArrayList; 006import java.util.List; 007import java.util.Locale; 008 009public final class MainArgsUtil { 010// Logging is usually not yet initialised, when this class is used => commented out logger, it makes no sense. 011// private static final Logger logger = LoggerFactory.getLogger(MainArgsUtil.class); 012 013 private MainArgsUtil() { } 014 015 public static String[][] separateSystemPropertiesFromOtherArgs(String[] args) { 016// if (logger.isDebugEnabled()) 017// logger.debug("separateSystemPropertiesFromOtherArgs: args={}", Arrays.toString(args)); 018 019 List<String> sysPropArgs = new ArrayList<String>(args.length); 020 List<String> otherArgs = new ArrayList<String>(args.length); 021 022 for (String arg : args) { 023 if (arg.startsWith("-D")) 024 sysPropArgs.add(arg); 025 else 026 otherArgs.add(arg); 027 } 028 return new String[][] { 029 sysPropArgs.toArray(new String[sysPropArgs.size()]), 030 otherArgs.toArray(new String[otherArgs.size()]) 031 }; 032 } 033 034 public static String[] extractAndApplySystemPropertiesReturnOthers(String[] args) { 035 String[][] sysPropArgsAndOtherArgs = separateSystemPropertiesFromOtherArgs(args); 036 String[] sysPropArgs = sysPropArgsAndOtherArgs[0]; 037 String[] otherArgs = sysPropArgsAndOtherArgs[1]; 038 for (String arg : sysPropArgs) { 039 if (!arg.startsWith("-D")) 040 throw new IllegalStateException("sysPropArgs contains element not starting with '-D': " + arg); 041 042 String kv = arg.substring(2); 043 int equalsIndex = kv.indexOf('='); 044 if (equalsIndex >= 0) { 045 String k = kv.substring(0, equalsIndex); 046 String v = kv.substring(equalsIndex + 1); 047// logger.debug("extractAndApplySystemPropertiesReturnOthers: k='{}' v='{}'", k, v); 048 System.setProperty(k, v); 049 } 050 else { 051// logger.debug("extractAndApplySystemPropertiesReturnOthers: kv='{}'", kv); 052 System.setProperty(kv, ""); 053 } 054 } 055 applyLocaleIfNeeded(); 056 return otherArgs; 057 } 058 059 /** 060 * (Re)Applies the {@linkplain Locale#setDefault(Locale) default locale}, if the 061 * system properties passed as normal program arguments cause a change. 062 * <p> 063 * Since the {@code Locale.default} property is already initialised (really early!), 064 * when {@link #extractAndApplySystemPropertiesReturnOthers(String[])} is called, the 065 * newly set system properties don't have any effect on the default {@code Locale}. 066 * Therefore, we must check the default {@code Locale}, now, and change it if needed. 067 */ 068 private static void applyLocaleIfNeeded() { 069 final String userLanguage = System.getProperty("user.language"); 070 final String userCountry = System.getProperty("user.country"); 071 Locale locale = Locale.getDefault(); 072 if (! (equal(userLanguage, locale.getLanguage()) && equal(userCountry, locale.getCountry()))) { 073 Locale.setDefault(new Locale(userLanguage, userCountry)); 074 } 075 } 076}