From b34720ddfa5a2a35c873ccc0d376248b6d787503 Mon Sep 17 00:00:00 2001 From: MASES Public Developers Team <94312179+masesdevelopers@users.noreply.github.com> Date: Mon, 27 Jan 2025 19:24:18 +0100 Subject: [PATCH] JNet is able to manage environment variables with the pattern $(ENV_VAR) (#601) --- src/net/JNet/JNetCoreBase.cs | 34 ++++++++++++++++++++-- src/net/JNetReflector/JNetReflectorCore.cs | 10 +++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/net/JNet/JNetCoreBase.cs b/src/net/JNet/JNetCoreBase.cs index cab5a63861..c432dc7560 100644 --- a/src/net/JNet/JNetCoreBase.cs +++ b/src/net/JNet/JNetCoreBase.cs @@ -179,6 +179,36 @@ protected override string[] ProcessCommandLine() /// Adds options to the JVM /// protected abstract IDictionary Options { get; } + /// + /// Replace environment variable in + /// + /// The string where the environemnt variables shall be replaced + /// The string with replaced environment varibales + /// Each environment variable is expected in the form $(ENV_VAR) + public static string ReplaceEnvironmentVariable(string item) + { + const string startTemplate = "$("; + const string endTemplate = ")"; + + if (!string.IsNullOrWhiteSpace(item)) + { + int firstIndex = 0; + while ((firstIndex = item.IndexOf(startTemplate, firstIndex)) != -1) + { + var secondIndex = item.IndexOf(endTemplate, firstIndex); + if (secondIndex != -1) + { + var envVar = item.Substring(firstIndex + startTemplate.Length, secondIndex - (firstIndex + startTemplate.Length)); + if (Environment.GetEnvironmentVariable(envVar) != null) + { + item.Replace($"{startTemplate}{envVar}{endTemplate}", Environment.GetEnvironmentVariable(envVar)); + } + } + firstIndex = secondIndex; + } + } + return item; + } /// public sealed override IEnumerable> JVMOptions @@ -190,7 +220,7 @@ public sealed override IEnumerable> JVMOptions { foreach (var item in base.JVMOptions) { - opt.Add(item); + opt.Add(new KeyValuePair(ReplaceEnvironmentVariable(item.Key), ReplaceEnvironmentVariable(item.Value))); } } if (Options != null) @@ -199,7 +229,7 @@ public sealed override IEnumerable> JVMOptions { try { - opt.Add(item); + opt.Add(new KeyValuePair(ReplaceEnvironmentVariable(item.Key), ReplaceEnvironmentVariable(item.Value))); } catch (Exception e) { diff --git a/src/net/JNetReflector/JNetReflectorCore.cs b/src/net/JNetReflector/JNetReflectorCore.cs index 9450929c66..337c60de92 100644 --- a/src/net/JNetReflector/JNetReflectorCore.cs +++ b/src/net/JNetReflector/JNetReflectorCore.cs @@ -69,11 +69,11 @@ public struct JVMOption { public JVMOption(string optionName, string optionValue) { - OptionName = optionName; - OptionValue = optionValue; + Name = optionName; + Value = optionValue; } - public string OptionName { get; set; } - public string OptionValue { get; set; } + public string Name { get; set; } + public string Value { get; set; } } public IEnumerable JVMOptions { get; set; } @@ -844,7 +844,7 @@ protected override IDictionary Options Dictionary dict = new(); foreach (var item in _ConfigurationFromFile.JVMOptions) { - dict.Add(item.OptionName, item.OptionValue); + dict.Add(item.Name, item.Value); } return dict; }