2004-04-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Activation / ContextLevelActivator.cs
1 //
2 // System.Runtime.Remoting.Activation.ContextLevelActivator.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2003, Lluis Sanchez Gual
7 //
8
9 using System;
10 using System.Runtime.Remoting.Messaging;
11 using System.Runtime.Remoting.Contexts;
12
13 namespace System.Runtime.Remoting.Activation
14 {
15         [Serializable]
16         internal class ContextLevelActivator: IActivator
17         {
18                 IActivator m_NextActivator;
19
20                 public ContextLevelActivator (IActivator next)
21                 {
22                         m_NextActivator = next;
23                 }
24
25                 public ActivatorLevel Level 
26                 {
27                         get { return ActivatorLevel.Context; }
28                 }
29
30                 public IActivator NextActivator 
31                 {
32                         get { return m_NextActivator; }
33                         set { m_NextActivator = value; }
34                 }
35
36                 public IConstructionReturnMessage Activate (IConstructionCallMessage ctorCall)
37                 {
38                         ServerIdentity identity = RemotingServices.CreateContextBoundObjectIdentity (ctorCall.ActivationType);
39                         RemotingServices.SetMessageTargetIdentity (ctorCall, identity);
40
41                         ConstructionCall call = ctorCall as ConstructionCall;
42                         if (call == null || !call.IsContextOk)
43                         {
44                                 identity.Context = Context.CreateNewContext (ctorCall);
45                                 Context oldContext = Context.SwitchToContext (identity.Context);
46
47                                 try
48                                 {
49                                         return m_NextActivator.Activate (ctorCall);
50                                 }
51                                 finally
52                                 {
53                                         Context.SwitchToContext (oldContext);
54                                 }
55                         }
56                         else
57                                 return m_NextActivator.Activate (ctorCall);
58                 }
59         }
60 }