2 // System.Runtime.Remoting.Messaging.CallContext.cs
4 // Author: Jaime Anguiano Olarra (jaime@gnome.org)
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (c) 2002, Jaime Anguiano Olarra
10 ///Provides several properties that come with the execution code path.
11 ///This class is sealed.
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
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.
38 using System.Threading;
39 using System.Collections;
41 namespace System.Runtime.Remoting.Messaging
45 [System.Runtime.InteropServices.ComVisible (true)]
46 public sealed class CallContext
48 [ThreadStatic] static Header [] Headers;
49 [ThreadStatic] static Hashtable logicalDatastore;
50 [ThreadStatic] static Hashtable datastore;
51 [ThreadStatic] static object hostContext;
53 private CallContext ()
57 public static object HostContext {
58 get { return hostContext; }
59 set { hostContext = value; }
63 public static void FreeNamedDataSlot (string name)
65 Datastore.Remove (name);
66 LogicalDatastore.Remove (name);
69 public static object GetData (string name)
71 if (LogicalDatastore.ContainsKey (name)) {
72 return LogicalDatastore [name];
74 return Datastore [name];
78 public static void SetData (string name, object data)
80 if (data is ILogicalThreadAffinative) {
81 LogicalSetData (name, data);
83 LogicalDatastore.Remove (name);
84 Datastore [name] = data;
88 public static object LogicalGetData (string name)
90 return LogicalDatastore [name];
93 public static void LogicalSetData (string name, object data)
95 Datastore.Remove (name);
96 LogicalDatastore [name] = data;
99 public static Header[] GetHeaders ()
104 public static void SetHeaders (Header[] headers)
109 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
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);
119 if (ctx == null && createEmpty)
120 return new LogicalCallContext ();
125 internal static object SetCurrentCallContext (LogicalCallContext ctx)
127 object oldData = new object[] { datastore, logicalDatastore };
129 if (ctx != null && ctx.HasInfo)
130 logicalDatastore = (Hashtable) ctx.Datastore.Clone ();
132 logicalDatastore = null;
137 internal static void UpdateCurrentLogicalCallContext (LogicalCallContext ctx)
139 Hashtable data = ctx.Datastore;
143 foreach (DictionaryEntry entry in data)
144 LogicalSetData ((string)entry.Key, entry.Value);
147 internal static void RestoreCallContext (object oldContext)
149 object[] contextArray = (object[])oldContext;
150 datastore = (Hashtable)contextArray [0];
151 logicalDatastore = (Hashtable)contextArray [1];
154 private static Hashtable Datastore
157 Hashtable r = datastore;
159 return datastore = new Hashtable ();
164 private static Hashtable LogicalDatastore
167 Hashtable r = logicalDatastore;
169 return logicalDatastore = new Hashtable ();
175 [System.Runtime.InteropServices.ComVisible (true)]
176 public interface ILogicalThreadAffinative