| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | namespace Ice; |
| | 4 | |
|
| | 5 | | // The base class for all ImplicitContext implementations |
| | 6 | | public abstract class ImplicitContextI : ImplicitContext |
| | 7 | | { |
| | 8 | | public static ImplicitContextI create(string kind) |
| | 9 | | { |
| 1 | 10 | | if (kind == "None" || kind.Length == 0) |
| | 11 | | { |
| 1 | 12 | | return null; |
| | 13 | | } |
| 1 | 14 | | else if (kind == "Shared") |
| | 15 | | { |
| 1 | 16 | | return new SharedImplicitContext(); |
| | 17 | | } |
| 1 | 18 | | else if (kind == "PerThread") |
| | 19 | | { |
| 1 | 20 | | return new PerThreadImplicitContext(); |
| | 21 | | } |
| | 22 | | else |
| | 23 | | { |
| 0 | 24 | | throw new InitializationException("'" + kind + "' is not a valid value for Ice.ImplicitContext"); |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public abstract Dictionary<string, string> getContext(); |
| | 29 | |
|
| | 30 | | public abstract void setContext(Dictionary<string, string> newContext); |
| | 31 | |
|
| | 32 | | public abstract bool containsKey(string key); |
| | 33 | |
|
| | 34 | | public abstract string get(string key); |
| | 35 | |
|
| | 36 | | public abstract string put(string key, string value); |
| | 37 | |
|
| | 38 | | public abstract string remove(string key); |
| | 39 | |
|
| | 40 | | public abstract void write(Dictionary<string, string> prxContext, OutputStream os); |
| | 41 | |
|
| | 42 | | internal abstract Dictionary<string, string> combine(Dictionary<string, string> prxContext); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | internal class SharedImplicitContext : ImplicitContextI |
| | 46 | | { |
| | 47 | | public override Dictionary<string, string> getContext() |
| | 48 | | { |
| | 49 | | lock (_mutex) |
| | 50 | | { |
| | 51 | | return new Dictionary<string, string>(_context); |
| | 52 | | } |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public override void setContext(Dictionary<string, string> context) |
| | 56 | | { |
| | 57 | | lock (_mutex) |
| | 58 | | { |
| | 59 | | if (context != null && context.Count != 0) |
| | 60 | | { |
| | 61 | | _context = new Dictionary<string, string>(context); |
| | 62 | | } |
| | 63 | | else |
| | 64 | | { |
| | 65 | | _context.Clear(); |
| | 66 | | } |
| | 67 | | } |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public override bool containsKey(string key) |
| | 71 | | { |
| | 72 | | lock (_mutex) |
| | 73 | | { |
| | 74 | | key ??= ""; |
| | 75 | |
|
| | 76 | | return _context.ContainsKey(key); |
| | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public override string get(string key) |
| | 81 | | { |
| | 82 | | lock (_mutex) |
| | 83 | | { |
| | 84 | | key ??= ""; |
| | 85 | |
|
| | 86 | | string val = _context[key] ?? ""; |
| | 87 | | return val; |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public override string put(string key, string value) |
| | 92 | | { |
| | 93 | | lock (_mutex) |
| | 94 | | { |
| | 95 | | key ??= ""; |
| | 96 | | value ??= ""; |
| | 97 | |
|
| | 98 | | _context.TryGetValue(key, out string oldVal); |
| | 99 | | oldVal ??= ""; |
| | 100 | | _context[key] = value; |
| | 101 | |
|
| | 102 | | return oldVal; |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public override string remove(string key) |
| | 107 | | { |
| | 108 | | lock (_mutex) |
| | 109 | | { |
| | 110 | | key ??= ""; |
| | 111 | |
|
| | 112 | | string val = _context[key]; |
| | 113 | |
|
| | 114 | | if (val == null) |
| | 115 | | { |
| | 116 | | val = ""; |
| | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| | 120 | | _context.Remove(key); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | return val; |
| | 124 | | } |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public override void write(Dictionary<string, string> prxContext, OutputStream os) |
| | 128 | | { |
| | 129 | | if (prxContext.Count == 0) |
| | 130 | | { |
| | 131 | | lock (_mutex) |
| | 132 | | { |
| | 133 | | ContextHelper.write(os, _context); |
| | 134 | | } |
| | 135 | | } |
| | 136 | | else |
| | 137 | | { |
| | 138 | | Dictionary<string, string> ctx = null; |
| | 139 | | lock (_mutex) |
| | 140 | | { |
| | 141 | | ctx = _context.Count == 0 ? prxContext : combine(prxContext); |
| | 142 | | } |
| | 143 | | ContextHelper.write(os, ctx); |
| | 144 | | } |
| | 145 | | } |
| | 146 | |
|
| | 147 | | internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext) |
| | 148 | | { |
| | 149 | | lock (_mutex) |
| | 150 | | { |
| | 151 | | var combined = new Dictionary<string, string>(prxContext); |
| | 152 | | foreach (KeyValuePair<string, string> e in _context) |
| | 153 | | { |
| | 154 | | try |
| | 155 | | { |
| | 156 | | combined.Add(e.Key, e.Value); |
| | 157 | | } |
| | 158 | | catch (System.ArgumentException) |
| | 159 | | { |
| | 160 | | // Ignore. |
| | 161 | | } |
| | 162 | | } |
| | 163 | | return combined; |
| | 164 | | } |
| | 165 | | } |
| | 166 | |
|
| | 167 | | private Dictionary<string, string> _context = new Dictionary<string, string>(); |
| | 168 | | private readonly object _mutex = new(); |
| | 169 | | } |
| | 170 | |
|
| | 171 | | internal class PerThreadImplicitContext : ImplicitContextI |
| | 172 | | { |
| | 173 | | public override Dictionary<string, string> getContext() |
| | 174 | | { |
| | 175 | | Dictionary<string, string> threadContext = null; |
| | 176 | | Thread currentThread = Thread.CurrentThread; |
| | 177 | | lock (_mutex) |
| | 178 | | { |
| | 179 | | if (_map.TryGetValue(currentThread, out Dictionary<string, string> value)) |
| | 180 | | { |
| | 181 | | threadContext = value; |
| | 182 | | } |
| | 183 | | } |
| | 184 | |
|
| | 185 | | threadContext ??= new Dictionary<string, string>(); |
| | 186 | | return threadContext; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | public override void setContext(Dictionary<string, string> context) |
| | 190 | | { |
| | 191 | | if (context == null || context.Count == 0) |
| | 192 | | { |
| | 193 | | lock (_mutex) |
| | 194 | | { |
| | 195 | | _map.Remove(Thread.CurrentThread); |
| | 196 | | } |
| | 197 | | } |
| | 198 | | else |
| | 199 | | { |
| | 200 | | var threadContext = new Dictionary<string, string>(context); |
| | 201 | |
|
| | 202 | | lock (_mutex) |
| | 203 | | { |
| | 204 | | _map.Add(Thread.CurrentThread, threadContext); |
| | 205 | | } |
| | 206 | | } |
| | 207 | | } |
| | 208 | |
|
| | 209 | | public override bool containsKey(string key) |
| | 210 | | { |
| | 211 | | key ??= ""; |
| | 212 | |
|
| | 213 | | Dictionary<string, string> threadContext = null; |
| | 214 | | lock (_mutex) |
| | 215 | | { |
| | 216 | | if (!_map.TryGetValue(Thread.CurrentThread, out threadContext)) |
| | 217 | | { |
| | 218 | | return false; |
| | 219 | | } |
| | 220 | | } |
| | 221 | |
|
| | 222 | | return threadContext.ContainsKey(key); |
| | 223 | | } |
| | 224 | |
|
| | 225 | | public override string get(string key) |
| | 226 | | { |
| | 227 | | key ??= ""; |
| | 228 | |
|
| | 229 | | Dictionary<string, string> threadContext = null; |
| | 230 | | lock (_mutex) |
| | 231 | | { |
| | 232 | | if (!_map.TryGetValue(Thread.CurrentThread, out threadContext)) |
| | 233 | | { |
| | 234 | | return ""; |
| | 235 | | } |
| | 236 | | } |
| | 237 | |
|
| | 238 | | string val = threadContext[key] ?? ""; |
| | 239 | | return val; |
| | 240 | | } |
| | 241 | |
|
| | 242 | | public override string put(string key, string value) |
| | 243 | | { |
| | 244 | | key ??= ""; |
| | 245 | | value ??= ""; |
| | 246 | |
|
| | 247 | | Dictionary<string, string> threadContext = null; |
| | 248 | | lock (_mutex) |
| | 249 | | { |
| | 250 | | if (!_map.TryGetValue(Thread.CurrentThread, out threadContext)) |
| | 251 | | { |
| | 252 | | threadContext = new Dictionary<string, string>(); |
| | 253 | | _map.Add(Thread.CurrentThread, threadContext); |
| | 254 | | } |
| | 255 | | } |
| | 256 | |
|
| | 257 | | if (!threadContext.TryGetValue(key, out string oldVal)) |
| | 258 | | { |
| | 259 | | oldVal = ""; |
| | 260 | | } |
| | 261 | |
|
| | 262 | | threadContext[key] = value; |
| | 263 | | return oldVal; |
| | 264 | | } |
| | 265 | |
|
| | 266 | | public override string remove(string key) |
| | 267 | | { |
| | 268 | | key ??= ""; |
| | 269 | |
|
| | 270 | | Dictionary<string, string> threadContext = null; |
| | 271 | | lock (_mutex) |
| | 272 | | { |
| | 273 | | if (!_map.TryGetValue(Thread.CurrentThread, out threadContext)) |
| | 274 | | { |
| | 275 | | return ""; |
| | 276 | | } |
| | 277 | | } |
| | 278 | |
|
| | 279 | | if (!threadContext.TryGetValue(key, out string val)) |
| | 280 | | { |
| | 281 | | val = ""; |
| | 282 | | } |
| | 283 | | else |
| | 284 | | { |
| | 285 | | threadContext.Remove(key); |
| | 286 | | } |
| | 287 | | return val; |
| | 288 | | } |
| | 289 | |
|
| | 290 | | public override void write(Dictionary<string, string> prxContext, OutputStream os) |
| | 291 | | { |
| | 292 | | Dictionary<string, string> threadContext = null; |
| | 293 | | lock (_mutex) |
| | 294 | | { |
| | 295 | | _map.TryGetValue(Thread.CurrentThread, out threadContext); |
| | 296 | | } |
| | 297 | |
|
| | 298 | | if (threadContext == null || threadContext.Count == 0) |
| | 299 | | { |
| | 300 | | ContextHelper.write(os, prxContext); |
| | 301 | | } |
| | 302 | | else if (prxContext.Count == 0) |
| | 303 | | { |
| | 304 | | ContextHelper.write(os, threadContext); |
| | 305 | | } |
| | 306 | | else |
| | 307 | | { |
| | 308 | | var combined = new Dictionary<string, string>(prxContext); |
| | 309 | | foreach (KeyValuePair<string, string> e in threadContext) |
| | 310 | | { |
| | 311 | | try |
| | 312 | | { |
| | 313 | | combined.Add(e.Key, e.Value); |
| | 314 | | } |
| | 315 | | catch (System.ArgumentException) |
| | 316 | | { |
| | 317 | | // Ignore. |
| | 318 | | } |
| | 319 | | } |
| | 320 | | ContextHelper.write(os, combined); |
| | 321 | | } |
| | 322 | | } |
| | 323 | |
|
| | 324 | | internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext) |
| | 325 | | { |
| | 326 | | Dictionary<string, string> threadContext = null; |
| | 327 | | lock (_mutex) |
| | 328 | | { |
| | 329 | | if (!_map.TryGetValue(Thread.CurrentThread, out threadContext)) |
| | 330 | | { |
| | 331 | | return new Dictionary<string, string>(prxContext); |
| | 332 | | } |
| | 333 | | } |
| | 334 | |
|
| | 335 | | var combined = new Dictionary<string, string>(prxContext); |
| | 336 | | foreach (KeyValuePair<string, string> e in threadContext) |
| | 337 | | { |
| | 338 | | combined.Add(e.Key, e.Value); |
| | 339 | | } |
| | 340 | | return combined; |
| | 341 | | } |
| | 342 | |
|
| | 343 | | // |
| | 344 | | // map Thread -> Context |
| | 345 | | // |
| | 346 | | private readonly Dictionary<Thread, Dictionary<string, string>> _map = new(); |
| | 347 | | private readonly object _mutex = new(); |
| | 348 | | } |