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