From 34b18cd7aa3fda3b2bd609fd3593b8a24e8073d4 Mon Sep 17 00:00:00 2001 From: hduelme Date: Fri, 22 Nov 2024 16:48:04 +0100 Subject: [PATCH 1/2] improve diagnose for invalide environment/command line arguments --- .../java/org/mjsip/config/OptionParser.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java b/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java index 4bb5fa4..a5a9995 100644 --- a/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java +++ b/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java @@ -63,6 +63,7 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O } CmdLineException problem = null; + boolean invalidValueGiven = false; try { // First try, use options form environment and command line. Collection arguments = new ArrayList<>(envArgs); @@ -71,13 +72,18 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O } catch (CmdLineException ex) { // Happens, when required configurations are not given on the command line. problem = ex; + String message = ex.getMessage(); + invalidValueGiven = message != null && message.contains(" is not a valid value for "); + if (invalidValueGiven) { + LOG.warn("{}. Reading from environment/command line aborted", message); + } } String argFile = metaConfig.configFile; File file; if (argFile != null && !argFile.isBlank()) { - if ("none".equals(argFile.toLowerCase())) { + if ("none".equalsIgnoreCase(argFile)) { file = null; } else { file = new File(argFile); @@ -87,7 +93,7 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O } } } else if (defaultConfigFile != null) { - String fileName = System.getProperty("user.home") + "/" + defaultConfigFile; + String fileName = System.getProperty("user.home") + File.pathSeparator + defaultConfigFile; file = new File(fileName); if (!file.exists()) { file = null; @@ -97,7 +103,11 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O } if (file!= null) { - LOG.debug("Reading options from: " + file.getAbsolutePath()); + if (invalidValueGiven) { + LOG.warn("Reading options from: {}", file.getAbsolutePath()); + } else { + LOG.debug("Reading options from: {}", file.getAbsolutePath()); + } ConfigFile configFile = new ConfigFile(file); @@ -135,7 +145,7 @@ public static String getValueFromEnvironment(String optionName) { String env = optionName.substring(2).replace('-', '_').toUpperCase(); String value = System.getenv(env); if (value != null) { - LOG.info("Using '" + optionName + "' from environment: " + env + "=" + value); + LOG.info("Using '{}' from environment: {}={}", optionName, env, value); } return value; } From 9bb3532bf98063be469e521236e08855cfade2ae Mon Sep 17 00:00:00 2001 From: Bernhard Haumacher Date: Fri, 22 Nov 2024 18:27:51 +0100 Subject: [PATCH 2/2] Bugfix: Do not ignore environment args when config file is used. --- .../src/main/java/org/mjsip/config/OptionParser.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java b/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java index a5a9995..9adf35a 100644 --- a/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java +++ b/mjsip-util/src/main/java/org/mjsip/config/OptionParser.java @@ -111,9 +111,13 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O ConfigFile configFile = new ConfigFile(file); - // Parse all arguments again to check for required arguments not given but give - // precedence to arguments given on the command line. + // Parse all arguments again to check for required arguments not given and to + // allow overwriting configuration file arguments with environment and command + // line. + // Use the following precedence order: Arguments in the configuration file (lowest), + // arguments from the environment, arguments given on the command line (highest). Collection arguments = new ArrayList<>(configFile.toArguments()); + arguments.addAll(envArgs); arguments.addAll(Arrays.asList(args)); parser.parseArgument(arguments);