[corlib] Improve flow of ExecutionContext (few tests taken from https://github.com...
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / CallContext.cs
1 // 
2 // System.Runtime.Remoting.Messaging.CallContext.cs
3 //
4 // Authors: Jaime Anguiano Olarra (jaime@gnome.org)
5 //         Lluis Sanchez Gual (lluis@ximian.com)
6 //          Marek Safar (marek.safar@gmail.com)
7 //
8 // (c) 2002, Jaime Anguiano Olarra
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Threading;
34 using System.Collections;
35 using System.Collections.Generic;
36
37 namespace System.Runtime.Remoting.Messaging 
38 {       
39         [Serializable]
40         [System.Runtime.InteropServices.ComVisible (true)]
41         public sealed class CallContext 
42         {
43                 [ThreadStatic] static Header [] Headers;
44                 [ThreadStatic] static object hostContext;
45                 
46                 private CallContext ()
47                 {
48                 }
49
50                 public static object HostContext {
51                         get { return hostContext; }
52                         set { hostContext = value; }
53                 }
54
55                 public static void FreeNamedDataSlot (string name)
56                 {
57                         ExecutionContext.FreeNamedDataSlot (name);
58                 }
59
60                 public static object GetData (string name) 
61                 {
62                         var value = LogicalGetData (name);
63                         if (value == null)
64                                 Datastore.TryGetValue (name, out value);
65
66                         return value;
67                 }
68                 
69                 public static void SetData (string name, object data) 
70                 {
71                         if (data is ILogicalThreadAffinative) {
72                                 LogicalSetData (name, data);
73                         } else {
74                                 LogicalContext.FreeNamedDataSlot (name);
75                                 Datastore [name] = data;
76                         }
77                 }
78                 
79                 public static object LogicalGetData (string name) 
80                 {
81                         return LogicalContext.GetData (name);
82                 }
83
84                 public static void LogicalSetData (string name, object data) 
85                 {
86                         Datastore.Remove (name);
87                         LogicalContext.SetData (name, data);
88                 }
89
90                 public static Header[] GetHeaders () 
91                 {
92                         return Headers;
93                 }
94                 
95                 public static void SetHeaders (Header[] headers) 
96                 {
97                         Headers = headers;
98                 }
99
100                 internal static object SetCurrentCallContext (LogicalCallContext ctx)
101                 {
102                         object oldData = new object[] { Datastore, LogicalContext.Datastore };
103
104                         Hashtable logicalDatastore;
105                         if (ctx != null && ctx.HasInfo)
106                                 logicalDatastore = (Hashtable) ctx.Datastore.Clone ();
107                         else
108                                 logicalDatastore = null;
109                                 
110                         LogicalContext.Datastore = logicalDatastore;
111                         return oldData;
112                 }
113
114                 internal static void UpdateCurrentLogicalCallContext (LogicalCallContext ctx)
115                 {
116                         Hashtable data = ctx.Datastore;
117                         if (data == null)
118                                 return;
119
120                         foreach (DictionaryEntry entry in data)
121                                 LogicalSetData ((string)entry.Key, entry.Value);
122                 }
123                 
124                 internal static void RestoreCallContext (object oldContext)
125                 {
126                         object[] contextArray = (object[])oldContext;
127                         ExecutionContext.DataStore = (Dictionary<string, object>)contextArray [0];
128                         LogicalContext.Datastore = (Hashtable)contextArray [1];
129                 }
130
131                 static Dictionary<string, object> Datastore {
132                         get {
133                                 return ExecutionContext.DataStore;
134                         }
135                 }
136
137                 static LogicalCallContext LogicalContext {
138                         get {
139                                 return ExecutionContext.LogicalCallContext;
140                         }
141                 }
142
143                 static ExecutionContext ExecutionContext {
144                         get {
145                                 return ExecutionContext.Current;
146                         }
147                 }
148         }
149
150         [System.Runtime.InteropServices.ComVisible (true)]
151         public interface ILogicalThreadAffinative
152         {
153         }
154 }