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