Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[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 datastore;
50                 [ThreadStatic] static object hostContext;
51                 
52                 private CallContext ()
53                 {
54                 }
55
56                 public static object HostContext {
57                         get { return hostContext; }
58                         set { hostContext = value; }
59                 }
60
61                 // public methods
62                 public static void FreeNamedDataSlot (string name) 
63                 {
64                         Datastore.Remove (name);
65                 }
66
67                 public static object GetData (string name) 
68                 {
69                         return Datastore [name];
70                 }
71                 
72                 public static void SetData (string name, object data) 
73                 {
74                         Datastore [name] = data;
75                 }
76
77                 [MonoTODO]
78                 public static object LogicalGetData (string name) 
79                 {
80                         throw new NotImplementedException ();
81                 }
82                 
83                 [MonoTODO]
84                 public static void LogicalSetData (string name, object data) 
85                 {
86                         throw new NotImplementedException ();
87                 }
88
89                 public static Header[] GetHeaders () 
90                 {
91                         return Headers;
92                 }
93                 
94                 public static void SetHeaders (Header[] headers) 
95                 {
96                         Headers = headers;
97                 }
98
99                 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
100                 {
101                         LogicalCallContext ctx = null;
102                         if (datastore != null) {
103                                 foreach (DictionaryEntry entry in datastore)
104                                         if (entry.Value is ILogicalThreadAffinative) {
105                                                 if (ctx == null) ctx = new LogicalCallContext ();
106                                                 ctx.SetData ((string)entry.Key, entry.Value);
107                                         }
108                         }
109
110                         if (ctx == null && createEmpty)
111                                 return new LogicalCallContext ();
112                         else
113                                 return ctx;
114                 }
115
116                 internal static object SetCurrentCallContext (LogicalCallContext ctx)
117                 {
118                         object oldData = datastore;
119
120                         if (ctx != null && ctx.HasInfo)
121                                 datastore = (Hashtable) ctx.Datastore.Clone ();
122                         else
123                                 datastore = null;
124                                 
125                         return oldData;
126                 }
127                 
128                 internal static void UpdateCurrentCallContext (LogicalCallContext ctx)
129                 {
130                         Hashtable data = ctx.Datastore;
131                         foreach (DictionaryEntry entry in data)
132                                 SetData ((string)entry.Key, entry.Value);
133                 }
134                 
135                 internal static void RestoreCallContext (object oldContext)
136                 {
137                         datastore = (Hashtable) oldContext;
138                 }
139
140                 private static Hashtable Datastore
141                 {
142                         get {
143                                 Hashtable r = datastore;
144                                 if (r == null)
145                                         return datastore = new Hashtable ();
146                                 return r;
147                         }
148                 }
149         }
150
151         [System.Runtime.InteropServices.ComVisible (true)]
152         public interface ILogicalThreadAffinative
153         {
154         }
155 }