Context.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // System.Runtime.Remoting.Contexts.Context..cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. // Patrik Torstensson
  8. //
  9. // (C) Ximian, Inc. http://www.ximian.com
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. using System.Threading;
  35. using System.Runtime.Remoting;
  36. using System.Runtime.Remoting.Proxies;
  37. using System.Runtime.Remoting.Activation;
  38. using System.Runtime.Remoting.Messaging;
  39. using System.Runtime.Remoting.Lifetime;
  40. namespace System.Runtime.Remoting.Contexts {
  41. public class Context
  42. {
  43. #region Sync with domain-internals.h
  44. int domain_id;
  45. int context_id;
  46. IntPtr static_data;
  47. #endregion
  48. // Default server context sink chain
  49. static IMessageSink default_server_context_sink;
  50. // The sink chain that has to be used by all calls entering the context
  51. IMessageSink server_context_sink_chain = null;
  52. // The sink chain that has to be used by all calls exiting the context
  53. IMessageSink client_context_sink_chain = null;
  54. Hashtable datastore;
  55. ArrayList context_properties;
  56. bool frozen;
  57. static int global_count;
  58. static Hashtable namedSlots = new Hashtable ();
  59. static DynamicPropertyCollection global_dynamic_properties;
  60. DynamicPropertyCollection context_dynamic_properties;
  61. ContextCallbackObject callback_object = null;
  62. public Context ()
  63. {
  64. domain_id = Thread.GetDomainID();
  65. context_id = 1 + global_count++;
  66. }
  67. ~Context ()
  68. {
  69. }
  70. public static Context DefaultContext {
  71. get {
  72. return AppDomain.InternalGetDefaultContext ();
  73. }
  74. }
  75. public virtual int ContextID {
  76. get {
  77. return context_id;
  78. }
  79. }
  80. public IContextProperty[] ContextProperties
  81. {
  82. get
  83. {
  84. if (context_properties == null) return new IContextProperty[0];
  85. else return (IContextProperty[]) context_properties.ToArray (typeof(IContextProperty[]));
  86. }
  87. }
  88. internal bool IsDefaultContext
  89. {
  90. get { return context_id == 0; }
  91. }
  92. internal bool NeedsContextSink
  93. {
  94. get {
  95. return context_id != 0 ||
  96. (global_dynamic_properties != null && global_dynamic_properties.HasProperties) ||
  97. (context_dynamic_properties != null && context_dynamic_properties.HasProperties);
  98. }
  99. }
  100. public static bool RegisterDynamicProperty(IDynamicProperty prop, ContextBoundObject obj, Context ctx)
  101. {
  102. DynamicPropertyCollection col = GetDynamicPropertyCollection (obj, ctx);
  103. return col.RegisterDynamicProperty (prop);
  104. }
  105. public static bool UnregisterDynamicProperty(string name, ContextBoundObject obj, Context ctx)
  106. {
  107. DynamicPropertyCollection col = GetDynamicPropertyCollection (obj, ctx);
  108. return col.UnregisterDynamicProperty (name);
  109. }
  110. static DynamicPropertyCollection GetDynamicPropertyCollection(ContextBoundObject obj, Context ctx)
  111. {
  112. if (ctx == null && obj != null)
  113. {
  114. if (RemotingServices.IsTransparentProxy(obj))
  115. {
  116. RealProxy rp = RemotingServices.GetRealProxy (obj);
  117. return rp.ObjectIdentity.ClientDynamicProperties;
  118. }
  119. else
  120. return obj.ObjectIdentity.ServerDynamicProperties;
  121. }
  122. else if (ctx != null && obj == null)
  123. {
  124. if (ctx.context_dynamic_properties == null) ctx.context_dynamic_properties = new DynamicPropertyCollection ();
  125. return ctx.context_dynamic_properties;
  126. }
  127. else if (ctx == null && obj == null)
  128. {
  129. if (global_dynamic_properties == null) global_dynamic_properties = new DynamicPropertyCollection ();
  130. return global_dynamic_properties;
  131. }
  132. else
  133. throw new ArgumentException ("Either obj or ctx must be null");
  134. }
  135. internal static void NotifyGlobalDynamicSinks (bool start, IMessage req_msg, bool client_site, bool async)
  136. {
  137. if (global_dynamic_properties != null && global_dynamic_properties.HasProperties)
  138. global_dynamic_properties.NotifyMessage (start, req_msg, client_site, async);
  139. }
  140. internal static bool HasGlobalDynamicSinks
  141. {
  142. get { return (global_dynamic_properties != null && global_dynamic_properties.HasProperties); }
  143. }
  144. internal void NotifyDynamicSinks (bool start, IMessage req_msg, bool client_site, bool async)
  145. {
  146. if (context_dynamic_properties != null && context_dynamic_properties.HasProperties)
  147. context_dynamic_properties.NotifyMessage (start, req_msg, client_site, async);
  148. }
  149. internal bool HasDynamicSinks
  150. {
  151. get { return (context_dynamic_properties != null && context_dynamic_properties.HasProperties); }
  152. }
  153. internal bool HasExitSinks
  154. {
  155. get
  156. {
  157. // Needs to go through the client context sink if there are custom
  158. // client context or dynamic sinks.
  159. return ( !(GetClientContextSinkChain() is ClientContextTerminatorSink) || HasDynamicSinks || HasGlobalDynamicSinks);
  160. }
  161. }
  162. public virtual IContextProperty GetProperty (string name)
  163. {
  164. if (context_properties == null)
  165. return null;
  166. foreach (IContextProperty p in context_properties)
  167. if (p.Name == name)
  168. return p;
  169. return null;
  170. }
  171. public virtual void SetProperty (IContextProperty prop)
  172. {
  173. if (prop == null)
  174. throw new ArgumentNullException ("IContextProperty");
  175. if (this == DefaultContext)
  176. throw new InvalidOperationException ("Can not add properties to " +
  177. "default context");
  178. if (frozen)
  179. throw new InvalidOperationException ("Context is Frozen");
  180. if (context_properties == null)
  181. context_properties = new ArrayList ();
  182. context_properties.Add (prop);
  183. }
  184. public virtual void Freeze ()
  185. {
  186. if (context_properties != null)
  187. {
  188. foreach (IContextProperty prop in context_properties)
  189. prop.Freeze (this);
  190. }
  191. }
  192. public override string ToString()
  193. {
  194. return "ContextID: " + context_id;
  195. }
  196. internal IMessageSink GetServerContextSinkChain()
  197. {
  198. if (server_context_sink_chain == null)
  199. {
  200. if (default_server_context_sink == null)
  201. default_server_context_sink = new ServerContextTerminatorSink();
  202. server_context_sink_chain = default_server_context_sink;
  203. if (context_properties != null) {
  204. // Enumerate in reverse order
  205. for (int n = context_properties.Count-1; n>=0; n--) {
  206. IContributeServerContextSink contributor = context_properties[n] as IContributeServerContextSink;
  207. if (contributor != null)
  208. server_context_sink_chain = contributor.GetServerContextSink (server_context_sink_chain);
  209. }
  210. }
  211. }
  212. return server_context_sink_chain;
  213. }
  214. internal IMessageSink GetClientContextSinkChain()
  215. {
  216. if (client_context_sink_chain == null)
  217. {
  218. client_context_sink_chain = new ClientContextTerminatorSink (this);
  219. if (context_properties != null) {
  220. foreach (IContextProperty prop in context_properties) {
  221. IContributeClientContextSink contributor = prop as IContributeClientContextSink;
  222. if (contributor != null)
  223. client_context_sink_chain = contributor.GetClientContextSink (client_context_sink_chain);
  224. }
  225. }
  226. }
  227. return client_context_sink_chain;
  228. }
  229. internal IMessageSink CreateServerObjectSinkChain (MarshalByRefObject obj, bool forceInternalExecute)
  230. {
  231. IMessageSink objectSink = new StackBuilderSink (obj, forceInternalExecute);
  232. objectSink = new ServerObjectTerminatorSink (objectSink);
  233. objectSink = new Lifetime.LeaseSink (objectSink);
  234. if (context_properties != null)
  235. {
  236. // Contribute object sinks in reverse order
  237. for (int n = context_properties.Count-1; n >= 0; n--)
  238. {
  239. IContextProperty prop = (IContextProperty) context_properties[n];
  240. IContributeObjectSink contributor = prop as IContributeObjectSink;
  241. if (contributor != null)
  242. objectSink = contributor.GetObjectSink (obj, objectSink);
  243. }
  244. }
  245. return objectSink;
  246. }
  247. internal IMessageSink CreateEnvoySink (MarshalByRefObject serverObject)
  248. {
  249. IMessageSink sink = EnvoyTerminatorSink.Instance;
  250. if (context_properties != null)
  251. {
  252. foreach (IContextProperty prop in context_properties)
  253. {
  254. IContributeEnvoySink contributor = prop as IContributeEnvoySink;
  255. if (contributor != null)
  256. sink = contributor.GetEnvoySink (serverObject, sink);
  257. }
  258. }
  259. return sink;
  260. }
  261. internal static Context SwitchToContext (Context newContext)
  262. {
  263. return AppDomain.InternalSetContext (newContext);
  264. }
  265. internal static Context CreateNewContext (IConstructionCallMessage msg)
  266. {
  267. // Create the new context
  268. Context newContext = new Context();
  269. foreach (IContextProperty prop in msg.ContextProperties)
  270. {
  271. if (newContext.GetProperty (prop.Name) == null)
  272. newContext.SetProperty (prop);
  273. }
  274. newContext.Freeze();
  275. // Ask each context property whether the new context is OK
  276. foreach (IContextProperty prop in msg.ContextProperties)
  277. if (!prop.IsNewContextOK (newContext))
  278. throw new RemotingException("A context property did not approve the candidate context for activating the object");
  279. return newContext;
  280. }
  281. public void DoCallBack (CrossContextDelegate deleg)
  282. {
  283. lock (this)
  284. {
  285. if (callback_object == null) {
  286. Context oldContext = Context.SwitchToContext (this);
  287. callback_object = new ContextCallbackObject ();
  288. Context.SwitchToContext (oldContext);
  289. }
  290. }
  291. callback_object.DoCallBack (deleg);
  292. }
  293. public static LocalDataStoreSlot AllocateDataSlot ()
  294. {
  295. return new LocalDataStoreSlot ();
  296. }
  297. public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
  298. {
  299. lock (namedSlots.SyncRoot)
  300. {
  301. LocalDataStoreSlot slot = new LocalDataStoreSlot ();
  302. namedSlots.Add (name, slot);
  303. return slot;
  304. }
  305. }
  306. public static void FreeNamedDataSlot (string name)
  307. {
  308. lock (namedSlots.SyncRoot)
  309. {
  310. namedSlots.Remove (name);
  311. }
  312. }
  313. public static object GetData (LocalDataStoreSlot slot)
  314. {
  315. Context ctx = Thread.CurrentContext;
  316. if (ctx.datastore == null) return null;
  317. lock (ctx.datastore.SyncRoot)
  318. {
  319. return ctx.datastore [slot];
  320. }
  321. }
  322. public static LocalDataStoreSlot GetNamedDataSlot (string name)
  323. {
  324. lock (namedSlots.SyncRoot)
  325. {
  326. LocalDataStoreSlot slot = namedSlots [name] as LocalDataStoreSlot;
  327. if (slot == null) return AllocateNamedDataSlot (name);
  328. else return slot;
  329. }
  330. }
  331. public static void SetData (LocalDataStoreSlot slot, object data)
  332. {
  333. Context ctx = Thread.CurrentContext;
  334. if (ctx.datastore == null)
  335. {
  336. lock (ctx)
  337. {
  338. if (ctx.datastore == null)
  339. ctx.datastore = new Hashtable ();
  340. }
  341. }
  342. lock (ctx.datastore.SyncRoot)
  343. {
  344. ctx.datastore [slot] = data;
  345. }
  346. }
  347. }
  348. class DynamicPropertyCollection
  349. {
  350. ArrayList _properties = new ArrayList();
  351. class DynamicPropertyReg
  352. {
  353. public IDynamicProperty Property;
  354. public IDynamicMessageSink Sink;
  355. }
  356. public bool HasProperties
  357. {
  358. get { return _properties.Count > 0; }
  359. }
  360. public bool RegisterDynamicProperty(IDynamicProperty prop)
  361. {
  362. lock (this)
  363. {
  364. if (FindProperty (prop.Name) != -1)
  365. throw new InvalidOperationException ("Another property by this name already exists");
  366. // Make a copy, do not interfere with threads running dynamic sinks
  367. ArrayList newProps = new ArrayList (_properties);
  368. DynamicPropertyReg reg = new DynamicPropertyReg();
  369. reg.Property = prop;
  370. IContributeDynamicSink contributor = prop as IContributeDynamicSink;
  371. if (contributor != null) reg.Sink = contributor.GetDynamicSink ();
  372. newProps.Add (reg);
  373. _properties = newProps;
  374. return true; // When should be false?
  375. }
  376. }
  377. public bool UnregisterDynamicProperty(string name)
  378. {
  379. lock (this)
  380. {
  381. int i = FindProperty (name);
  382. if (i == -1) throw new RemotingException ("A property with the name " + name + " was not found");
  383. _properties.RemoveAt (i);
  384. return true; // When should be false?
  385. }
  386. }
  387. public void NotifyMessage (bool start, IMessage msg, bool client_site, bool async)
  388. {
  389. ArrayList props = _properties;
  390. if (start)
  391. {
  392. foreach (DynamicPropertyReg reg in props)
  393. if (reg.Sink != null) reg.Sink.ProcessMessageStart (msg, client_site, async);
  394. }
  395. else
  396. {
  397. foreach (DynamicPropertyReg reg in props)
  398. if (reg.Sink != null) reg.Sink.ProcessMessageFinish (msg, client_site, async);
  399. }
  400. }
  401. int FindProperty (string name)
  402. {
  403. for (int n=0; n<_properties.Count; n++)
  404. if (((DynamicPropertyReg)_properties[n]).Property.Name == name)
  405. return n;
  406. return -1;
  407. }
  408. }
  409. class ContextCallbackObject: ContextBoundObject
  410. {
  411. public void DoCallBack (CrossContextDelegate deleg)
  412. {
  413. }
  414. }
  415. }