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