| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | namespace Ice; |
| | 6 | |
|
| | 7 | | /// <summary>The default implementation of the "Properties" admin facet.</summary> |
| | 8 | | public sealed class NativePropertiesAdmin : PropertiesAdminDisp_ |
| | 9 | | { |
| | 10 | | private readonly Properties _properties; |
| | 11 | | private readonly Logger _logger; |
| 1 | 12 | | private readonly List<Action<Dictionary<string, string>>> _updateCallbacks = new(); |
| 1 | 13 | | private readonly object _mutex = new(); |
| | 14 | | private const string _traceCategory = "Admin.Properties"; |
| | 15 | |
|
| 1 | 16 | | public override string getProperty(string key, Current current) => _properties.getProperty(key); |
| | 17 | |
|
| | 18 | | public override Dictionary<string, string> getPropertiesForPrefix(string prefix, Current current) => |
| 1 | 19 | | _properties.getPropertiesForPrefix(prefix); |
| | 20 | |
|
| | 21 | | public override void setProperties(Dictionary<string, string> newProperties, Current current) |
| | 22 | | { |
| 1 | 23 | | lock (_mutex) |
| | 24 | | { |
| 1 | 25 | | Dictionary<string, string> old = _properties.getPropertiesForPrefix(""); |
| 1 | 26 | | int traceLevel = _properties.getIcePropertyAsInt("Ice.Trace.Admin.Properties"); |
| | 27 | |
|
| | 28 | | // Compute the difference between the new property set and the existing property set: |
| | 29 | | // |
| | 30 | | // 1) Any properties in the new set that were not defined in the existing set. |
| | 31 | | // |
| | 32 | | // 2) Any properties that appear in both sets but with different values. |
| | 33 | | // |
| | 34 | | // 3) Any properties not present in the new set but present in the existing set. |
| | 35 | | // In other words, the property has been removed. |
| | 36 | |
|
| 1 | 37 | | var added = new Dictionary<string, string>(); |
| 1 | 38 | | var changed = new Dictionary<string, string>(); |
| 1 | 39 | | var removed = new Dictionary<string, string>(); |
| 1 | 40 | | foreach (KeyValuePair<string, string> e in newProperties) |
| | 41 | | { |
| 1 | 42 | | string key = e.Key; |
| 1 | 43 | | string value = e.Value; |
| 1 | 44 | | if (!old.ContainsKey(key)) |
| | 45 | | { |
| 1 | 46 | | if (value.Length > 0) |
| | 47 | | { |
| | 48 | | // |
| | 49 | | // This property is new. |
| | 50 | | // |
| 1 | 51 | | added.Add(key, value); |
| | 52 | | } |
| | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 1 | 56 | | if (!old.TryGetValue(key, out string? v) || !value.Equals(v, StringComparison.Ordinal)) |
| | 57 | | { |
| 1 | 58 | | if (value.Length == 0) |
| | 59 | | { |
| | 60 | | // |
| | 61 | | // This property was removed. |
| | 62 | | // |
| 1 | 63 | | removed.Add(key, value); |
| | 64 | | } |
| | 65 | | else |
| | 66 | | { |
| | 67 | | // |
| | 68 | | // This property has changed. |
| | 69 | | // |
| 1 | 70 | | changed.Add(key, value); |
| | 71 | | } |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | old.Remove(key); |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 1 | 78 | | if (traceLevel > 0 && (added.Count > 0 || changed.Count > 0 || removed.Count > 0)) |
| | 79 | | { |
| 0 | 80 | | var message = new System.Text.StringBuilder("Summary of property changes"); |
| | 81 | |
|
| 0 | 82 | | if (added.Count > 0) |
| | 83 | | { |
| 0 | 84 | | message.Append("\nNew properties:"); |
| 0 | 85 | | foreach (KeyValuePair<string, string> e in added) |
| | 86 | | { |
| 0 | 87 | | message.Append("\n "); |
| 0 | 88 | | message.Append(e.Key); |
| 0 | 89 | | if (traceLevel > 1) |
| | 90 | | { |
| 0 | 91 | | message.Append(" = "); |
| 0 | 92 | | message.Append(e.Value); |
| | 93 | | } |
| | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | if (changed.Count > 0) |
| | 98 | | { |
| 0 | 99 | | message.Append("\nChanged properties:"); |
| 0 | 100 | | foreach (KeyValuePair<string, string> e in changed) |
| | 101 | | { |
| 0 | 102 | | message.Append("\n "); |
| 0 | 103 | | message.Append(e.Key); |
| 0 | 104 | | if (traceLevel > 1) |
| | 105 | | { |
| 0 | 106 | | message.Append(" = "); |
| 0 | 107 | | message.Append(e.Value); |
| 0 | 108 | | message.Append(" (old value = "); |
| 0 | 109 | | message.Append(_properties.getProperty(e.Key)); |
| 0 | 110 | | message.Append(')'); |
| | 111 | | } |
| | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | if (removed.Count > 0) |
| | 116 | | { |
| 0 | 117 | | message.Append("\nRemoved properties:"); |
| 0 | 118 | | foreach (KeyValuePair<string, string> e in removed) |
| | 119 | | { |
| 0 | 120 | | message.Append("\n "); |
| 0 | 121 | | message.Append(e.Key); |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | _logger.trace(_traceCategory, message.ToString()); |
| | 126 | | } |
| | 127 | |
|
| | 128 | | // |
| | 129 | | // Update the property set. |
| | 130 | | // |
| | 131 | |
|
| 1 | 132 | | foreach (KeyValuePair<string, string> e in added) |
| | 133 | | { |
| 1 | 134 | | _properties.setProperty(e.Key, e.Value); |
| | 135 | | } |
| | 136 | |
|
| 1 | 137 | | foreach (KeyValuePair<string, string> e in changed) |
| | 138 | | { |
| 1 | 139 | | _properties.setProperty(e.Key, e.Value); |
| | 140 | | } |
| | 141 | |
|
| 1 | 142 | | foreach (KeyValuePair<string, string> e in removed) |
| | 143 | | { |
| 1 | 144 | | _properties.setProperty(e.Key, ""); |
| | 145 | | } |
| | 146 | |
|
| 1 | 147 | | if (_updateCallbacks.Count > 0) |
| | 148 | | { |
| 1 | 149 | | var changes = new Dictionary<string, string>(added); |
| 1 | 150 | | foreach (KeyValuePair<string, string> e in changed) |
| | 151 | | { |
| 1 | 152 | | changes.Add(e.Key, e.Value); |
| | 153 | | } |
| 1 | 154 | | foreach (KeyValuePair<string, string> e in removed) |
| | 155 | | { |
| 1 | 156 | | changes.Add(e.Key, e.Value); |
| | 157 | | } |
| | 158 | |
|
| | 159 | | // Copy callbacks to allow callbacks to update callbacks |
| | 160 | | foreach ( |
| 1 | 161 | | Action<Dictionary<string, string>> callback in |
| 1 | 162 | | new List<System.Action<Dictionary<string, string>>>(_updateCallbacks)) |
| | 163 | | { |
| | 164 | | // The callback should not throw any exception. |
| 1 | 165 | | callback(changes); |
| | 166 | | } |
| | 167 | | } |
| 0 | 168 | | } |
| 1 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary>Registers an update callback that will be invoked when a property update occurs.</summary> |
| | 172 | | /// <param name="cb">The callback.</param> |
| | 173 | | public void addUpdateCallback(System.Action<Dictionary<string, string>> cb) |
| | 174 | | { |
| 1 | 175 | | lock (_mutex) |
| | 176 | | { |
| 1 | 177 | | _updateCallbacks.Add(cb); |
| 1 | 178 | | } |
| 1 | 179 | | } |
| | 180 | |
|
| | 181 | | /// <summary>Unregisters an update callback previously registered with <see cref="addUpdateCallback" /> .</summary> |
| | 182 | | /// <param name="cb">The callback.</param> |
| | 183 | | public void removeUpdateCallback(System.Action<Dictionary<string, string>> cb) |
| | 184 | | { |
| 0 | 185 | | lock (_mutex) |
| | 186 | | { |
| 0 | 187 | | _updateCallbacks.Remove(cb); |
| 0 | 188 | | } |
| 0 | 189 | | } |
| | 190 | |
|
| 1 | 191 | | internal NativePropertiesAdmin(Internal.Instance instance) |
| | 192 | | { |
| 1 | 193 | | _properties = instance.initializationData().properties!; |
| 1 | 194 | | _logger = instance.initializationData().logger!; |
| 1 | 195 | | } |
| | 196 | | } |