| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using Ice.Internal; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Text; |
| | 8 | | using System.Text.RegularExpressions; |
| | 9 | |
|
| | 10 | | namespace Ice; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// A property set used to configure Ice and Ice applications. Properties are key/value pairs, with both keys and |
| | 14 | | /// values being strings. |
| | 15 | | /// </summary> |
| | 16 | | public sealed class Properties |
| | 17 | | { |
| | 18 | | private class PropertyValue |
| | 19 | | { |
| 1 | 20 | | public string value { get; set; } |
| | 21 | |
|
| 1 | 22 | | public bool used { get; set; } |
| | 23 | |
|
| 1 | 24 | | public PropertyValue(string value, bool used) |
| | 25 | | { |
| 1 | 26 | | this.value = value; |
| 1 | 27 | | this.used = used; |
| 1 | 28 | | } |
| | 29 | |
|
| 1 | 30 | | public PropertyValue Clone() => new(value, used); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="Properties" /> class. The property set is initially empty. |
| | 35 | | /// </summary> |
| 1 | 36 | | public Properties() |
| | 37 | | { |
| 1 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Initializes a new instance of the <see cref="Properties" /> class with a list of opt-in prefixes. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="optInPrefixes">A list of opt-in prefixes to allow in the property set.</param> |
| 1 | 44 | | public Properties(List<string> optInPrefixes) => _optInPrefixes = optInPrefixes; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Initializes a new instance of the <see cref="Properties" /> class. The property set is initialized from the |
| | 48 | | /// provided argument vector. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="args">A command-line argument vector, possibly containing options to set properties. If the |
| | 51 | | /// command-line options include a --Ice.Config option, the corresponding configuration files are parsed. If the |
| | 52 | | /// same property is set in a configuration file and in the argument vector, the argument vector takes precedence. |
| | 53 | | /// This method modifies the argument vector by removing any Ice-related options.</param> |
| | 54 | | /// <param name="defaults">Default values for the property set. Settings in configuration files and args override |
| | 55 | | /// these defaults. May be null.</param> |
| 1 | 56 | | public Properties(ref string[] args, Properties? defaults = null) |
| | 57 | | { |
| 1 | 58 | | if (defaults is not null) |
| | 59 | | { |
| 1 | 60 | | foreach (KeyValuePair<string, PropertyValue> entry in defaults._properties) |
| | 61 | | { |
| 1 | 62 | | _properties[entry.Key] = entry.Value.Clone(); |
| | 63 | | } |
| | 64 | |
|
| 1 | 65 | | _optInPrefixes.AddRange(defaults._optInPrefixes); |
| | 66 | | } |
| | 67 | |
|
| 1 | 68 | | if (_properties.TryGetValue("Ice.ProgramName", out PropertyValue? pv)) |
| | 69 | | { |
| 1 | 70 | | pv.used = true; |
| | 71 | | } |
| | 72 | | else |
| | 73 | | { |
| 1 | 74 | | _properties["Ice.ProgramName"] = new PropertyValue(AppDomain.CurrentDomain.FriendlyName, true); |
| | 75 | | } |
| | 76 | |
|
| 1 | 77 | | bool loadConfigFiles = false; |
| | 78 | |
|
| 1 | 79 | | for (int i = 0; i < args.Length; i++) |
| | 80 | | { |
| 1 | 81 | | if (args[i].StartsWith("--Ice.Config", StringComparison.Ordinal)) |
| | 82 | | { |
| 1 | 83 | | string line = args[i]; |
| 1 | 84 | | if (!line.Contains('=', StringComparison.Ordinal)) |
| | 85 | | { |
| 0 | 86 | | line += "=1"; |
| | 87 | | } |
| 1 | 88 | | parseLine(line[2..]); |
| 1 | 89 | | loadConfigFiles = true; |
| | 90 | |
|
| 1 | 91 | | string[] arr = new string[args.Length - 1]; |
| 1 | 92 | | Array.Copy(args, 0, arr, 0, i); |
| 1 | 93 | | if (i < args.Length - 1) |
| | 94 | | { |
| 1 | 95 | | Array.Copy(args, i + 1, arr, i, args.Length - i - 1); |
| | 96 | | } |
| 1 | 97 | | args = arr; |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| 1 | 101 | | if (!loadConfigFiles) |
| | 102 | | { |
| | 103 | | // |
| | 104 | | // If Ice.Config is not set, load from ICE_CONFIG (if set) |
| | 105 | | // |
| 1 | 106 | | loadConfigFiles = !_properties.ContainsKey("Ice.Config"); |
| | 107 | | } |
| | 108 | |
|
| 1 | 109 | | if (loadConfigFiles) |
| | 110 | | { |
| 1 | 111 | | loadConfig(); |
| | 112 | | } |
| | 113 | |
|
| 1 | 114 | | args = parseIceCommandLineOptions(args); |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Get a property by key. |
| | 119 | | /// If the property is not set, an empty string is returned. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="key">The property key.</param> |
| | 122 | | /// <returns>The property value.</returns> |
| | 123 | | public string getProperty(string key) |
| | 124 | | { |
| 1 | 125 | | lock (_mutex) |
| | 126 | | { |
| 1 | 127 | | string result = ""; |
| 1 | 128 | | if (_properties.TryGetValue(key, out PropertyValue? pv)) |
| | 129 | | { |
| 1 | 130 | | pv.used = true; |
| 1 | 131 | | result = pv.value; |
| | 132 | | } |
| 1 | 133 | | return result; |
| | 134 | | } |
| 1 | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Get an Ice property by key. |
| | 139 | | /// If the property is not set, its default value is returned. |
| | 140 | | /// </summary> |
| | 141 | | /// <param name="key">The property key.</param> |
| | 142 | | /// <returns>The property value or the default value.</returns> |
| | 143 | | /// <exception name="PropertyException">Thrown if the property is not a known Ice property.</exception> |
| 1 | 144 | | public string getIceProperty(string key) => getPropertyWithDefault(key, getDefaultProperty(key)); |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// Get a property by key. |
| | 148 | | /// If the property is not set, the given default value is returned. |
| | 149 | | /// </summary> |
| | 150 | | /// <param name="key">The property key.</param> |
| | 151 | | /// <param name="value">The default value to use if the property does not exist.</param> |
| | 152 | | /// <returns>The property value or the default value.</returns> |
| | 153 | | public string getPropertyWithDefault(string key, string value) |
| | 154 | | { |
| 1 | 155 | | lock (_mutex) |
| | 156 | | { |
| 1 | 157 | | string result = value; |
| 1 | 158 | | if (_properties.TryGetValue(key, out PropertyValue? pv)) |
| | 159 | | { |
| 1 | 160 | | pv.used = true; |
| 1 | 161 | | result = pv.value; |
| | 162 | | } |
| 1 | 163 | | return result; |
| | 164 | | } |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | /// <summary> |
| | 168 | | /// Get a property as an integer. |
| | 169 | | /// If the property is not set, 0 is returned. |
| | 170 | | /// </summary> |
| | 171 | | /// <param name="key">The property key.</param> |
| | 172 | | /// <returns>The property value interpreted as an integer.</returns> |
| | 173 | | /// <exception name="PropertyException">Thrown if the property value is not a valid integer.</exception> |
| 1 | 174 | | public int getPropertyAsInt(string key) => getPropertyAsIntWithDefault(key, 0); |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Get an Ice property as an integer. |
| | 178 | | /// If the property is not set, its default value is returned. |
| | 179 | | /// </summary> |
| | 180 | | /// <param name="key">The property key.</param> |
| | 181 | | /// <returns>The property value interpreted as an integer, or the default value.</returns> |
| | 182 | | /// <exception name="PropertyException">Thrown if the property is not a known Ice property or the value is not a |
| | 183 | | /// valid integer.</exception> |
| | 184 | | public int getIcePropertyAsInt(string key) |
| | 185 | | { |
| 1 | 186 | | string defaultValueString = getDefaultProperty(key); |
| 1 | 187 | | int defaultValue = 0; |
| 1 | 188 | | if (defaultValueString.Length > 0) |
| | 189 | | { |
| 1 | 190 | | defaultValue = int.Parse(defaultValueString, CultureInfo.InvariantCulture); |
| | 191 | | } |
| | 192 | |
|
| 1 | 193 | | return getPropertyAsIntWithDefault(key, defaultValue); |
| | 194 | | } |
| | 195 | |
|
| | 196 | | /// <summary> |
| | 197 | | /// Get a property as an integer. |
| | 198 | | /// If the property is not set, the given default value is returned. |
| | 199 | | /// </summary> |
| | 200 | | /// <param name="key">The property key.</param> |
| | 201 | | /// <param name="value">The default value to use if the property does not exist.</param> |
| | 202 | | /// <returns>The property value interpreted as an integer, or the default value.</returns> |
| | 203 | | /// <exception name="PropertyException">Thrown if the property value is not a valid integer.</exception> |
| | 204 | | public int getPropertyAsIntWithDefault(string key, int value) |
| | 205 | | { |
| 1 | 206 | | lock (_mutex) |
| | 207 | | { |
| 1 | 208 | | if (!_properties.TryGetValue(key, out PropertyValue? pv)) |
| | 209 | | { |
| 1 | 210 | | return value; |
| | 211 | | } |
| 1 | 212 | | pv.used = true; |
| | 213 | | try |
| | 214 | | { |
| 1 | 215 | | return int.Parse(pv.value, CultureInfo.InvariantCulture); |
| | 216 | | } |
| 0 | 217 | | catch (FormatException) |
| | 218 | | { |
| 0 | 219 | | throw new PropertyException($"property '{key}' has an invalid integer value: '{pv.value}'"); |
| | 220 | | } |
| | 221 | | } |
| 1 | 222 | | } |
| | 223 | |
|
| | 224 | | /// <summary> |
| | 225 | | /// Get a property as a list of strings. |
| | 226 | | /// The strings must be separated by whitespace or comma. If the property is |
| | 227 | | /// not set, an empty list is returned. The strings in the list can contain whitespace and commas if they are |
| | 228 | | /// enclosed in single or double quotes. If quotes are mismatched, an empty list is returned. Within single quotes |
| | 229 | | /// or double quotes, you can escape the quote in question with a backslash, e.g. O'Reilly can be written as |
| | 230 | | /// O'Reilly, "O'Reilly" or 'O\'Reilly'. |
| | 231 | | /// </summary> |
| | 232 | | /// <param name="key">The property key.</param> |
| | 233 | | /// <returns>The property value interpreted as a list of strings.</returns> |
| 0 | 234 | | public string[] getPropertyAsList(string key) => getPropertyAsListWithDefault(key, []); |
| | 235 | |
|
| | 236 | | /// <summary> |
| | 237 | | /// Get an Ice property as a list of strings. |
| | 238 | | /// The strings must be separated by whitespace or comma. If the property is |
| | 239 | | /// not set, its default list is returned. The strings in the list can contain whitespace and commas if they are |
| | 240 | | /// enclosed in single or double quotes. If quotes are mismatched, the default list is returned. Within single |
| | 241 | | /// quotes or double quotes, you can escape the quote in question with a backslash, e.g. O'Reilly can be written as |
| | 242 | | /// O'Reilly, "O'Reilly" or 'O\'Reilly'. |
| | 243 | | /// </summary> |
| | 244 | | /// <param name="key">The property key.</param> |
| | 245 | | /// <returns>The property value interpreted as list of strings, or the default value.</returns> |
| | 246 | | /// <exception cref="PropertyException">Thrown if the property is not a known Ice property.</exception> |
| | 247 | | public string[] getIcePropertyAsList(string key) |
| | 248 | | { |
| 1 | 249 | | string[] defaultList = UtilInternal.StringUtil.splitString(getDefaultProperty(key), ", \t\r\n"); |
| 1 | 250 | | return getPropertyAsListWithDefault(key, defaultList); |
| | 251 | | } |
| | 252 | |
|
| | 253 | | /// <summary> |
| | 254 | | /// Get a property as a list of strings. |
| | 255 | | /// The strings must be separated by whitespace or comma. If the property is |
| | 256 | | /// not set, the default list is returned. The strings in the list can contain whitespace and commas if they are |
| | 257 | | /// enclosed in single or double quotes. If quotes are mismatched, the default list is returned. Within single |
| | 258 | | /// quotes or double quotes, you can escape the quote in question with a backslash, e.g. O'Reilly can be written as |
| | 259 | | /// O'Reilly, "O'Reilly" or 'O\'Reilly'. |
| | 260 | | /// </summary> |
| | 261 | | /// <param name="key">The property key.</param> |
| | 262 | | /// <param name="value">The default value to use if the property is not set.</param> |
| | 263 | | /// <returns>The property value interpreted as list of strings, or the default value.</returns> |
| | 264 | | public string[] getPropertyAsListWithDefault(string key, string[] value) |
| | 265 | | { |
| 1 | 266 | | value ??= []; |
| | 267 | |
|
| 1 | 268 | | lock (_mutex) |
| | 269 | | { |
| 1 | 270 | | if (!_properties.TryGetValue(key, out PropertyValue? pv)) |
| | 271 | | { |
| 1 | 272 | | return value; |
| | 273 | | } |
| | 274 | |
|
| 1 | 275 | | pv.used = true; |
| | 276 | |
|
| 1 | 277 | | string[] result = Ice.UtilInternal.StringUtil.splitString(pv.value, ", \t\r\n"); |
| 1 | 278 | | if (result == null) |
| | 279 | | { |
| 0 | 280 | | Util.getProcessLogger().warning( |
| 0 | 281 | | $"mismatched quotes in property {key}'s value, returning default value"); |
| 0 | 282 | | return value; |
| | 283 | | } |
| | 284 | | else |
| | 285 | | { |
| 1 | 286 | | return result; |
| | 287 | | } |
| | 288 | | } |
| 1 | 289 | | } |
| | 290 | |
|
| | 291 | | /// <summary> |
| | 292 | | /// Get all properties whose keys begins with prefix. |
| | 293 | | /// If prefix is an empty string, then all |
| | 294 | | /// properties are returned. |
| | 295 | | /// </summary> |
| | 296 | | /// <param name="prefix">The prefix to search for (empty string if none).</param> |
| | 297 | | /// <returns>The matching property set.</returns> |
| | 298 | | public Dictionary<string, string> getPropertiesForPrefix(string prefix) |
| | 299 | | { |
| 1 | 300 | | lock (_mutex) |
| | 301 | | { |
| 1 | 302 | | var result = new Dictionary<string, string>(); |
| | 303 | |
|
| 1 | 304 | | foreach (string s in _properties.Keys) |
| | 305 | | { |
| 1 | 306 | | if (prefix.Length == 0 || s.StartsWith(prefix, StringComparison.Ordinal)) |
| | 307 | | { |
| 1 | 308 | | PropertyValue pv = _properties[s]; |
| 1 | 309 | | pv.used = true; |
| 1 | 310 | | result[s] = pv.value; |
| | 311 | | } |
| | 312 | | } |
| 1 | 313 | | return result; |
| | 314 | | } |
| 1 | 315 | | } |
| | 316 | |
|
| | 317 | | /// <summary> |
| | 318 | | /// Set a property. |
| | 319 | | /// To unset a property, set it to the empty string. |
| | 320 | | /// </summary> |
| | 321 | | /// <param name="key">The property key.</param> |
| | 322 | | /// <param name="value">The property value.</param> |
| | 323 | | public void setProperty(string key, string value) |
| | 324 | | { |
| | 325 | | // |
| | 326 | | // Trim whitespace |
| | 327 | | // |
| 1 | 328 | | if (key != null) |
| | 329 | | { |
| 1 | 330 | | key = key.Trim(); |
| | 331 | | } |
| 1 | 332 | | if (key == null || key.Length == 0) |
| | 333 | | { |
| 0 | 334 | | throw new InitializationException("Attempt to set property with empty key"); |
| | 335 | | } |
| | 336 | |
|
| | 337 | | // Check if the property is in an Ice property prefix. If so, check that it's a valid property. |
| 1 | 338 | | if (findIcePropertyArray(key) is PropertyArray propertyArray) |
| | 339 | | { |
| 1 | 340 | | if (propertyArray.isOptIn && !_optInPrefixes.Contains(propertyArray.name)) |
| | 341 | | { |
| 1 | 342 | | throw new PropertyException( |
| 1 | 343 | | $"Unable to set '{key}': property prefix '{propertyArray.name}' is opt-in and must be explicitly ena |
| | 344 | | } |
| | 345 | |
|
| 1 | 346 | | Property prop = findProperty(key[(propertyArray.name.Length + 1)..], propertyArray) ?? |
| 1 | 347 | | throw new PropertyException($"unknown Ice property: {key}"); |
| | 348 | |
|
| | 349 | | // If the property is deprecated, log a warning. |
| 1 | 350 | | if (prop.deprecated) |
| | 351 | | { |
| 0 | 352 | | Util.getProcessLogger().warning($"setting deprecated property: {key}"); |
| | 353 | | } |
| | 354 | | } |
| | 355 | |
|
| 1 | 356 | | lock (_mutex) |
| | 357 | | { |
| | 358 | | // Set or clear the property. |
| 1 | 359 | | if (value is not null && value.Length > 0) |
| | 360 | | { |
| 1 | 361 | | if (_properties.TryGetValue(key, out PropertyValue? pv)) |
| | 362 | | { |
| 1 | 363 | | pv.value = value; |
| | 364 | | } |
| | 365 | | else |
| | 366 | | { |
| 1 | 367 | | pv = new PropertyValue(value, false); |
| | 368 | | } |
| 1 | 369 | | _properties[key] = pv; |
| | 370 | | } |
| | 371 | | else |
| | 372 | | { |
| 1 | 373 | | _properties.Remove(key); |
| | 374 | | } |
| 1 | 375 | | } |
| 1 | 376 | | } |
| | 377 | |
|
| | 378 | | /// <summary> |
| | 379 | | /// Get a sequence of command-line options that is equivalent to this property set. |
| | 380 | | /// Each element of the returned |
| | 381 | | /// sequence is a command-line option of the form --key=value. |
| | 382 | | /// </summary> |
| | 383 | | /// <returns>The command line options for this property set.</returns> |
| | 384 | | public string[] getCommandLineOptions() |
| | 385 | | { |
| 0 | 386 | | lock (_mutex) |
| | 387 | | { |
| 0 | 388 | | string[] result = new string[_properties.Count]; |
| 0 | 389 | | int i = 0; |
| 0 | 390 | | foreach (KeyValuePair<string, PropertyValue> entry in _properties) |
| | 391 | | { |
| 0 | 392 | | result[i++] = "--" + entry.Key + "=" + entry.Value.value; |
| | 393 | | } |
| 0 | 394 | | return result; |
| | 395 | | } |
| 0 | 396 | | } |
| | 397 | |
|
| | 398 | | /// <summary> |
| | 399 | | /// Convert a sequence of command-line options into properties. |
| | 400 | | /// All options that begin with |
| | 401 | | /// --prefix. are converted into properties. If the prefix is empty, all options that begin with |
| | 402 | | /// -- are converted to properties. |
| | 403 | | /// </summary> |
| | 404 | | /// <param name="prefix">The property prefix, or an empty string to convert all options starting with --.</param> |
| | 405 | | /// <param name="options">The command-line options.</param> |
| | 406 | | /// <returns>The command-line options that do not start with the specified prefix, in their original order.</returns |
| | 407 | | public string[] parseCommandLineOptions(string prefix, string[] options) |
| | 408 | | { |
| 1 | 409 | | if (prefix.Length > 0 && prefix[^1] != '.') |
| | 410 | | { |
| 1 | 411 | | prefix += '.'; |
| | 412 | | } |
| 1 | 413 | | prefix = "--" + prefix; |
| | 414 | |
|
| 1 | 415 | | List<string> result = []; |
| 1 | 416 | | for (int i = 0; i < options.Length; i++) |
| | 417 | | { |
| 1 | 418 | | string opt = options[i]; |
| 1 | 419 | | if (opt.StartsWith(prefix, StringComparison.Ordinal)) |
| | 420 | | { |
| 1 | 421 | | if (!opt.Contains('=', StringComparison.Ordinal)) |
| | 422 | | { |
| 0 | 423 | | opt += "=1"; |
| | 424 | | } |
| | 425 | |
|
| 1 | 426 | | parseLine(opt[2..]); |
| | 427 | | } |
| | 428 | | else |
| | 429 | | { |
| 1 | 430 | | result.Add(opt); |
| | 431 | | } |
| | 432 | | } |
| 1 | 433 | | string[] arr = new string[result.Count]; |
| 1 | 434 | | if (arr.Length != 0) |
| | 435 | | { |
| 1 | 436 | | result.CopyTo(arr); |
| | 437 | | } |
| 1 | 438 | | return arr; |
| | 439 | | } |
| | 440 | |
|
| | 441 | | /// <summary> |
| | 442 | | /// Convert a sequence of command-line options into properties. |
| | 443 | | /// All options that begin with one of the following |
| | 444 | | /// prefixes are converted into properties: --Ice, --IceBox, --IceGrid, |
| | 445 | | /// --IceSSL, --IceStorm, and --Glacier2. |
| | 446 | | /// </summary> |
| | 447 | | /// <param name="options">The command-line options.</param> |
| | 448 | | /// <returns>The command-line options that do not start with one of the listed prefixes, in their original order. |
| | 449 | | /// </returns> |
| | 450 | | public string[] parseIceCommandLineOptions(string[] options) |
| | 451 | | { |
| 1 | 452 | | string[] args = options; |
| 1 | 453 | | foreach (string? name in PropertyNames.validProps.Select(p => p.name)) |
| | 454 | | { |
| 1 | 455 | | args = parseCommandLineOptions(name, args); |
| | 456 | | } |
| 1 | 457 | | return args; |
| | 458 | | } |
| | 459 | |
|
| | 460 | | /// <summary> |
| | 461 | | /// Load properties from a file. |
| | 462 | | /// </summary> |
| | 463 | | /// <param name="file">The property file.</param> |
| | 464 | | public void load(string file) |
| | 465 | | { |
| | 466 | | try |
| | 467 | | { |
| 1 | 468 | | using var sr = new StreamReader(file); |
| 1 | 469 | | parse(sr); |
| 1 | 470 | | } |
| 0 | 471 | | catch (IOException ex) |
| | 472 | | { |
| 0 | 473 | | throw new FileException($"Cannot read '{file}'", ex); |
| | 474 | | } |
| 1 | 475 | | } |
| | 476 | |
|
| | 477 | | /// <summary> |
| | 478 | | /// Create a copy of this property set. |
| | 479 | | /// </summary> |
| | 480 | | /// <returns>A copy of this property set.</returns> |
| | 481 | | public Properties Clone() |
| | 482 | | { |
| 1 | 483 | | var clonedProperties = new Properties(); |
| 1 | 484 | | lock (_mutex) |
| | 485 | | { |
| 1 | 486 | | foreach ((string key, PropertyValue value) in _properties) |
| | 487 | | { |
| 1 | 488 | | clonedProperties._properties[key] = value.Clone(); |
| | 489 | | } |
| | 490 | | } |
| 1 | 491 | | return clonedProperties; |
| | 492 | | } |
| | 493 | |
|
| | 494 | | /// <summary> |
| | 495 | | /// Alias for <see cref="Clone" />. Provided for source compatibility with Ice 3.7 and earlier versions. |
| | 496 | | /// </summary> |
| | 497 | | /// <returns>A copy of this property set.</returns> |
| | 498 | | [Obsolete("Use Clone instead.")] |
| 0 | 499 | | public Properties ice_clone_() => Clone(); |
| | 500 | |
|
| | 501 | | /// <summary> |
| | 502 | | /// Get the unused properties in the property set. |
| | 503 | | /// </summary> |
| | 504 | | /// <returns>A list containing the names of the unused properties in this property set.</returns> |
| | 505 | | public List<string> getUnusedProperties() |
| | 506 | | { |
| 0 | 507 | | lock (_mutex) |
| | 508 | | { |
| 0 | 509 | | var unused = new List<string>(); |
| 0 | 510 | | foreach ((string key, PropertyValue value) in _properties) |
| | 511 | | { |
| 0 | 512 | | if (!value.used) |
| | 513 | | { |
| 0 | 514 | | unused.Add(key); |
| | 515 | | } |
| | 516 | | } |
| 0 | 517 | | return unused; |
| | 518 | | } |
| 0 | 519 | | } |
| | 520 | |
|
| | 521 | | /// <summary> |
| | 522 | | /// Validate the properties for the given prefix. |
| | 523 | | /// </summary> |
| | 524 | | /// <param name="prefix">The property prefix to validate.</param> |
| | 525 | | /// <param name="properties">The properties to consider. </param> |
| | 526 | | /// <param name="propertyArray">The property array to search against.</param> |
| | 527 | | /// <exception cref="PropertyException"> Thrown if unknown properties are found.</exception> |
| | 528 | | internal static void validatePropertiesWithPrefix( |
| | 529 | | string prefix, |
| | 530 | | Properties properties, |
| | 531 | | PropertyArray propertyArray) |
| | 532 | | { |
| | 533 | | // Do not check for unknown properties if Ice prefix, ie Ice, Glacier2, etc |
| 1 | 534 | | foreach (string? name in PropertyNames.validProps.Select(p => p.name)) |
| | 535 | | { |
| 1 | 536 | | if (prefix.StartsWith($"{name}.", StringComparison.Ordinal)) |
| | 537 | | { |
| 1 | 538 | | return; |
| | 539 | | } |
| | 540 | | } |
| | 541 | |
|
| 1 | 542 | | var unknownProps = new List<string>(); |
| 1 | 543 | | Dictionary<string, string> props = properties.getPropertiesForPrefix($"{prefix}."); |
| | 544 | |
|
| 1 | 545 | | foreach (string p in props.Keys) |
| | 546 | | { |
| | 547 | | // Plus one to include the dot. |
| 1 | 548 | | if (findProperty(p[(prefix.Length + 1)..], propertyArray) is null) |
| | 549 | | { |
| 1 | 550 | | unknownProps.Add(p); |
| | 551 | | } |
| | 552 | | } |
| | 553 | |
|
| 1 | 554 | | if (unknownProps.Count > 0) |
| | 555 | | { |
| 1 | 556 | | var message = new StringBuilder($"Found unknown properties for {propertyArray.name} : '{prefix}'"); |
| 1 | 557 | | foreach (string s in unknownProps) |
| | 558 | | { |
| 1 | 559 | | message.Append("\n "); |
| 1 | 560 | | message.Append(s); |
| | 561 | | } |
| | 562 | |
|
| 1 | 563 | | throw new PropertyException(message.ToString()); |
| | 564 | | } |
| 1 | 565 | | } |
| | 566 | |
|
| | 567 | | /// <summary> |
| | 568 | | /// Find the Ice property array for a given property name. |
| | 569 | | /// </summary> |
| | 570 | | /// <param name="key">The property key.</param> |
| | 571 | | /// <returns>The Ice property array if found, else null.</returns> |
| | 572 | | private static PropertyArray? findIcePropertyArray(string key) |
| | 573 | | { |
| 1 | 574 | | int dotPos = key.IndexOf('.', StringComparison.Ordinal); |
| | 575 | |
|
| | 576 | | // If the key doesn't contain a dot, it's not a valid Ice property |
| 1 | 577 | | if (dotPos == -1) |
| | 578 | | { |
| 1 | 579 | | return null; |
| | 580 | | } |
| | 581 | |
|
| 1 | 582 | | string prefix = key[..dotPos]; |
| | 583 | |
|
| | 584 | | // Search for the property list that matches the prefix |
| 1 | 585 | | return PropertyNames.validProps.FirstOrDefault(properties => prefix == properties.name); |
| | 586 | | } |
| | 587 | |
|
| | 588 | | /// <summary> |
| | 589 | | /// Find a property in the property array. |
| | 590 | | /// </summary> |
| | 591 | | /// <param name="key">The property key.</param> |
| | 592 | | /// <param name="propertyArray">The property array to check.</param> |
| | 593 | | /// <returns>The <see cref="Property" /> if found, null otherwise.</returns> |
| | 594 | | private static Property? findProperty(string key, PropertyArray propertyArray) |
| | 595 | | { |
| 1 | 596 | | foreach (Property prop in propertyArray.properties) |
| | 597 | | { |
| | 598 | | // If the key is an exact match, return the property unless it has a property class which is prefix only. |
| | 599 | | // If the key is a regex match, return the property. A property cannot have a property class and use regex. |
| 1 | 600 | | if (key == prop.pattern) |
| | 601 | | { |
| 1 | 602 | | if (prop.propertyArray is not null && prop.propertyArray.prefixOnly) |
| | 603 | | { |
| 0 | 604 | | return null; |
| | 605 | | } |
| 1 | 606 | | return prop; |
| | 607 | | } |
| 1 | 608 | | else if (prop.usesRegex && Regex.IsMatch(key, prop.pattern)) |
| | 609 | | { |
| 1 | 610 | | return prop; |
| | 611 | | } |
| | 612 | |
|
| | 613 | | // If the property has a property class, check if the key is a prefix of the property. |
| 1 | 614 | | if (prop.propertyArray is not null) |
| | 615 | | { |
| | 616 | | // Check if the key is a prefix of the property. |
| | 617 | | // The key must be: |
| | 618 | | // - shorter than the property pattern |
| | 619 | | // - the property pattern must start with the key |
| | 620 | | // - the pattern character after the key must be a dot |
| 1 | 621 | | if (key.Length > prop.pattern.Length && key.StartsWith($"{prop.pattern}.", StringComparison.Ordinal)) |
| | 622 | | { |
| | 623 | | // Plus one to skip the dot. |
| 1 | 624 | | string substring = key[(prop.pattern.Length + 1)..]; |
| | 625 | |
|
| | 626 | | // Check if the suffix is a valid property. If so, return it. If it's not, continue searching |
| | 627 | | // the current property array. |
| 1 | 628 | | if (findProperty(substring, prop.propertyArray) is Property subProp) |
| | 629 | | { |
| 1 | 630 | | return subProp; |
| | 631 | | } |
| | 632 | | } |
| | 633 | | } |
| | 634 | | } |
| 1 | 635 | | return null; |
| | 636 | | } |
| | 637 | |
|
| | 638 | | /// <summary> |
| | 639 | | /// Gets the default value for a given Ice property. |
| | 640 | | /// </summary> |
| | 641 | | /// <param name="key">The Ice property key.</param> |
| | 642 | | /// <returns>The default property value, or an empty string the default is unspecified.</returns> |
| | 643 | | /// <exception cref="PropertyException">Thrown if the property is not a known Ice property.</exception> |
| | 644 | | private static string getDefaultProperty(string key) |
| | 645 | | { |
| | 646 | | // Find the property, don't log any warnings. |
| 1 | 647 | | PropertyArray propertyArray = findIcePropertyArray(key) ?? |
| 1 | 648 | | throw new PropertyException($"unknown Ice property: {key}"); |
| | 649 | |
|
| | 650 | | // Find the property in the property array. |
| 1 | 651 | | Property prop = findProperty(key[(propertyArray.name.Length + 1)..], propertyArray) ?? |
| 1 | 652 | | throw new PropertyException($"unknown Ice property: {key}"); |
| | 653 | |
|
| 1 | 654 | | return prop.defaultValue; |
| | 655 | | } |
| | 656 | |
|
| | 657 | | private void parse(StreamReader input) |
| | 658 | | { |
| | 659 | | try |
| | 660 | | { |
| | 661 | | string? line; |
| 1 | 662 | | while ((line = input.ReadLine()) is not null) |
| | 663 | | { |
| 1 | 664 | | parseLine(line); |
| | 665 | | } |
| 1 | 666 | | } |
| 0 | 667 | | catch (IOException ex) |
| | 668 | | { |
| 0 | 669 | | throw new SyscallException(ex); |
| | 670 | | } |
| 1 | 671 | | } |
| | 672 | |
|
| | 673 | | private const int ParseStateKey = 0; |
| | 674 | | private const int ParseStateValue = 1; |
| | 675 | |
|
| | 676 | | private void parseLine(string line) |
| | 677 | | { |
| 1 | 678 | | string key = ""; |
| 1 | 679 | | string val = ""; |
| | 680 | |
|
| 1 | 681 | | int state = ParseStateKey; |
| | 682 | |
|
| 1 | 683 | | string whitespace = ""; |
| 1 | 684 | | string escapedSpace = ""; |
| 1 | 685 | | bool finished = false; |
| 1 | 686 | | for (int i = 0; i < line.Length; ++i) |
| | 687 | | { |
| 1 | 688 | | char c = line[i]; |
| | 689 | | switch (state) |
| | 690 | | { |
| | 691 | | case ParseStateKey: |
| | 692 | | { |
| | 693 | | switch (c) |
| | 694 | | { |
| | 695 | | case '\\': |
| 1 | 696 | | if (i < line.Length - 1) |
| | 697 | | { |
| 1 | 698 | | c = line[++i]; |
| | 699 | | switch (c) |
| | 700 | | { |
| | 701 | | case '\\': |
| | 702 | | case '#': |
| | 703 | | case '=': |
| 1 | 704 | | key += whitespace; |
| 1 | 705 | | whitespace = ""; |
| 1 | 706 | | key += c; |
| 1 | 707 | | break; |
| | 708 | |
|
| | 709 | | case ' ': |
| 1 | 710 | | if (key.Length != 0) |
| | 711 | | { |
| 1 | 712 | | whitespace += c; |
| | 713 | | } |
| 1 | 714 | | break; |
| | 715 | |
|
| | 716 | | default: |
| 1 | 717 | | key += whitespace; |
| 1 | 718 | | whitespace = ""; |
| 1 | 719 | | key += '\\'; |
| 1 | 720 | | key += c; |
| 1 | 721 | | break; |
| | 722 | | } |
| | 723 | | } |
| | 724 | | else |
| | 725 | | { |
| 0 | 726 | | key += whitespace; |
| 0 | 727 | | key += c; |
| | 728 | | } |
| 0 | 729 | | break; |
| | 730 | |
|
| | 731 | | case ' ': |
| | 732 | | case '\t': |
| | 733 | | case '\r': |
| | 734 | | case '\n': |
| 1 | 735 | | if (key.Length != 0) |
| | 736 | | { |
| 1 | 737 | | whitespace += c; |
| | 738 | | } |
| 1 | 739 | | break; |
| | 740 | |
|
| | 741 | | case '=': |
| 1 | 742 | | whitespace = ""; |
| 1 | 743 | | state = ParseStateValue; |
| 1 | 744 | | break; |
| | 745 | |
|
| | 746 | | case '#': |
| 1 | 747 | | finished = true; |
| 1 | 748 | | break; |
| | 749 | |
|
| | 750 | | default: |
| 1 | 751 | | key += whitespace; |
| 1 | 752 | | whitespace = ""; |
| 1 | 753 | | key += c; |
| 1 | 754 | | break; |
| | 755 | | } |
| | 756 | | break; |
| | 757 | | } |
| | 758 | |
|
| | 759 | | case ParseStateValue: |
| | 760 | | { |
| | 761 | | switch (c) |
| | 762 | | { |
| | 763 | | case '\\': |
| 1 | 764 | | if (i < line.Length - 1) |
| | 765 | | { |
| 1 | 766 | | c = line[++i]; |
| | 767 | | switch (c) |
| | 768 | | { |
| | 769 | | case '\\': |
| | 770 | | case '#': |
| | 771 | | case '=': |
| 1 | 772 | | val += val.Length == 0 ? escapedSpace : whitespace; |
| 1 | 773 | | whitespace = ""; |
| 1 | 774 | | escapedSpace = ""; |
| 1 | 775 | | val += c; |
| 1 | 776 | | break; |
| | 777 | |
|
| | 778 | | case ' ': |
| 1 | 779 | | whitespace += c; |
| 1 | 780 | | escapedSpace += c; |
| 1 | 781 | | break; |
| | 782 | |
|
| | 783 | | default: |
| 1 | 784 | | val += val.Length == 0 ? escapedSpace : whitespace; |
| 1 | 785 | | whitespace = ""; |
| 1 | 786 | | escapedSpace = ""; |
| 1 | 787 | | val += '\\'; |
| 1 | 788 | | val += c; |
| 1 | 789 | | break; |
| | 790 | | } |
| | 791 | | } |
| | 792 | | else |
| | 793 | | { |
| 0 | 794 | | val += val.Length == 0 ? escapedSpace : whitespace; |
| 0 | 795 | | val += c; |
| | 796 | | } |
| 0 | 797 | | break; |
| | 798 | |
|
| | 799 | | case ' ': |
| | 800 | | case '\t': |
| | 801 | | case '\r': |
| | 802 | | case '\n': |
| 1 | 803 | | if (val.Length != 0) |
| | 804 | | { |
| 1 | 805 | | whitespace += c; |
| | 806 | | } |
| 1 | 807 | | break; |
| | 808 | |
|
| | 809 | | case '#': |
| 1 | 810 | | finished = true; |
| 1 | 811 | | break; |
| | 812 | |
|
| | 813 | | default: |
| 1 | 814 | | val += val.Length == 0 ? escapedSpace : whitespace; |
| 1 | 815 | | whitespace = ""; |
| 1 | 816 | | escapedSpace = ""; |
| 1 | 817 | | val += c; |
| | 818 | | break; |
| | 819 | | } |
| | 820 | | break; |
| | 821 | | } |
| | 822 | | } |
| 1 | 823 | | if (finished) |
| | 824 | | { |
| | 825 | | break; |
| | 826 | | } |
| | 827 | | } |
| 1 | 828 | | val += escapedSpace; |
| | 829 | |
|
| 1 | 830 | | if ((state == ParseStateKey && key.Length != 0) || (state == ParseStateValue && key.Length == 0)) |
| | 831 | | { |
| 0 | 832 | | Util.getProcessLogger().warning("invalid config file entry: \"" + line + "\""); |
| 0 | 833 | | return; |
| | 834 | | } |
| 1 | 835 | | else if (key.Length == 0) |
| | 836 | | { |
| 1 | 837 | | return; |
| | 838 | | } |
| | 839 | |
|
| 1 | 840 | | setProperty(key, val); |
| 1 | 841 | | } |
| | 842 | |
|
| | 843 | | private void loadConfig() |
| | 844 | | { |
| 1 | 845 | | string val = getIceProperty("Ice.Config"); |
| 1 | 846 | | if (val.Length == 0 || val == "1") |
| | 847 | | { |
| 1 | 848 | | string? s = Environment.GetEnvironmentVariable("ICE_CONFIG"); |
| 1 | 849 | | if (s != null && s.Length != 0) |
| | 850 | | { |
| 0 | 851 | | val = s; |
| | 852 | | } |
| | 853 | | } |
| | 854 | |
|
| 1 | 855 | | if (val.Length > 0) |
| | 856 | | { |
| 1 | 857 | | char[] separator = { ',' }; |
| 1 | 858 | | string[] files = val.Split(separator); |
| 1 | 859 | | for (int i = 0; i < files.Length; i++) |
| | 860 | | { |
| 1 | 861 | | load(files[i].Trim()); |
| | 862 | | } |
| | 863 | |
|
| 1 | 864 | | _properties["Ice.Config"] = new PropertyValue(val, true); |
| | 865 | | } |
| 1 | 866 | | } |
| | 867 | |
|
| 1 | 868 | | private readonly Dictionary<string, PropertyValue> _properties = []; |
| 1 | 869 | | private readonly List<string> _optInPrefixes = []; |
| 1 | 870 | | private readonly object _mutex = new(); |
| | 871 | | } |