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