This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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         public sealed class CallContext 
46         {
47                 internal const string HeadersKey = "__Headers";
48                 internal const string ContextKey = "__CallContext";
49                 
50                 private CallContext ()
51                 {
52                 }
53
54                 // public methods
55                 public static void FreeNamedDataSlot (string name) 
56                 {
57                         Datastore.Remove (name);
58                 }
59
60                 public static object GetData (string name) 
61                 {
62                         return Datastore [name];
63                 }
64
65                 public static Header[] GetHeaders () 
66                 {
67                         return (Header[]) Datastore [HeadersKey];
68                 }
69
70                 public static void SetData (string name, object data) 
71                 {
72                         Datastore [name] = data;
73                 }
74
75                 public static void SetHeaders (Header[] headers) 
76                 {
77                         Datastore [HeadersKey] = headers;
78                 }
79
80                 internal static LogicalCallContext CreateLogicalCallContext ()
81                 {
82                         LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
83                         Hashtable res = (Hashtable) Thread.GetData (ds);
84
85                         LogicalCallContext ctx = new LogicalCallContext();
86                         if (res == null) return ctx;
87
88                         foreach (DictionaryEntry entry in res)
89                                 if (entry.Value is ILogicalThreadAffinative)
90                                         ctx.SetData ((string)entry.Key, entry.Value);
91
92                         return ctx;
93                 }
94
95                 internal static object SetCurrentCallContext (LogicalCallContext ctx)
96                 {
97                         LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
98                         object oldData = Thread.GetData (ds);
99
100                         if (ctx != null && ctx.HasInfo)
101                         {
102                                 Hashtable newData = new Hashtable();
103                                 Hashtable data = ctx.Datastore;
104                                 
105                                 foreach (DictionaryEntry entry in data)
106                                         newData [(string)entry.Key] = entry.Value;
107         
108                                 Thread.SetData (ds, newData);
109                         }
110                         else
111                                 Thread.SetData (ds, null);
112                                 
113                         return oldData;
114                 }
115                 
116                 internal static void UpdateCurrentCallContext (LogicalCallContext ctx)
117                 {
118                         Hashtable data = ctx.Datastore;
119                         foreach (DictionaryEntry entry in data)
120                                 SetData ((string)entry.Key, entry.Value);
121                 }
122                 
123                 internal static void RestoreCallContext (object oldContext)
124                 {
125                         LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
126                         Thread.SetData (ds, oldContext);
127                 }
128
129                 private static Hashtable Datastore
130                 {
131                         get 
132                         {
133                                 LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
134                                 Hashtable res = (Hashtable) Thread.GetData (ds);
135                                 if (res == null) {
136                                         res = new Hashtable ();
137                                         Thread.SetData (ds, res);
138                                 }
139                                 return res;
140                         }
141                 }
142         }
143
144         public interface ILogicalThreadAffinative
145         {
146         }
147 }