Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / runtime / remoting / contextproperty.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** File:    ContextProperty.cs
9 **
10 ** A contextProperty is a name-value pair holding the property
11 ** name and the object representing the property in a context.
12 ** An array of these is returned by Context::GetContextProperties()
13 ** 
14 **
15 **
16 ===========================================================*/
17
18 namespace System.Runtime.Remoting.Contexts {
19     
20     using System;
21     using System.Threading;
22     using System.Reflection;
23     using System.Runtime.InteropServices;    
24     using System.Runtime.CompilerServices;
25     using System.Runtime.Remoting.Activation;  
26     using System.Security.Permissions;
27     using System.Diagnostics.Contracts;
28     
29     /// <internalonly/>
30     [System.Security.SecurityCritical]  // auto-generated_required
31     [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.Infrastructure)]    
32     [System.Runtime.InteropServices.ComVisible(true)]
33     public class ContextProperty {
34         internal String _name;           // property name
35         internal Object _property;       // property object
36     
37     /// <internalonly/>
38         public virtual String Name {
39             get {
40                 return _name;
41             }
42         }
43     
44     /// <internalonly/>
45         public virtual Object Property {
46             get {
47                 return _property;
48             }
49         }
50     
51         /* can't create outside the package */
52         internal ContextProperty(String name, Object prop)
53         {
54             _name = name;
55             _property = prop;
56         }
57     }
58     
59     //  The IContextAttribute interface is implemented by attribute classes.
60     //  The attributes contribute a property which resides in a context and
61     //  enforces a specific policy for the objects created in that context.
62     /// <internalonly/>
63 [System.Runtime.InteropServices.ComVisible(true)]
64     public interface IContextAttribute
65     {
66     /// <internalonly/>
67         [System.Security.SecurityCritical]  // auto-generated_required
68         bool IsContextOK(Context ctx, IConstructionCallMessage msg);
69     /// <internalonly/>
70         [System.Security.SecurityCritical]  // auto-generated_required
71         void GetPropertiesForNewContext(IConstructionCallMessage msg);
72     }
73     
74    //   This interface is exposed by the property contributed to a context
75    //   by an attribute. By default, it is also implemented by the ContextAttribute
76    //   base class which every attribute class must extend from.
77     /// <internalonly/>
78 [System.Runtime.InteropServices.ComVisible(true)]
79     public interface IContextProperty
80     {
81     /// <internalonly/>
82        //   This is the name under which the property will be added
83        //   to the {name,property} table in a context.
84         String Name 
85         {
86             [System.Security.SecurityCritical]  // auto-generated_required
87             get;
88         }
89     /// <internalonly/>
90        //   After forming the newCtx, we ask each property if it is happy
91        //   with the context. We expect most implementations to say yes.
92         [System.Security.SecurityCritical]  // auto-generated_required
93         bool IsNewContextOK(Context newCtx);
94     /// <internalonly/>
95
96         // New method. All properties are notified when the context
97         // they are in is frozen.
98         [System.Security.SecurityCritical]  // auto-generated_required
99         void Freeze(Context newContext);
100     }  
101
102     /// <internalonly/>
103 [System.Runtime.InteropServices.ComVisible(true)]
104     public interface IContextPropertyActivator
105     {
106     /// <internalonly/>
107         // This method lets properties in the current context have a say in 
108         // whether an activation may be done 'here' or not.
109         [System.Security.SecurityCritical]  // auto-generated_required
110         bool IsOKToActivate(IConstructionCallMessage msg);
111     /// <internalonly/>
112         [System.Security.SecurityCritical]  // auto-generated_required
113         void CollectFromClientContext(IConstructionCallMessage msg);
114     /// <internalonly/>
115         [System.Security.SecurityCritical]  // auto-generated_required
116         bool DeliverClientContextToServerContext(IConstructionCallMessage msg);
117     /// <internalonly/>
118         [System.Security.SecurityCritical]  // auto-generated_required
119         void CollectFromServerContext(IConstructionReturnMessage msg);
120     /// <internalonly/>
121         [System.Security.SecurityCritical]  // auto-generated_required
122         bool DeliverServerContextToClientContext(IConstructionReturnMessage msg);
123     }
124     
125     
126    //   All context attribute classes must extend from this base class.
127    //   This class provides the base implementations which the derived
128    //   classes are free to over-ride. The base implementations provide
129    //   the default answers to various questions.
130      
131     /// <internalonly/>
132     [System.Security.SecurityCritical]  // auto-generated_required
133     [Serializable]
134     [AttributeUsage(AttributeTargets.Class)]
135     [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.Infrastructure)]    
136     [System.Runtime.InteropServices.ComVisible(true)]
137     public class ContextAttribute
138         : Attribute, IContextAttribute, IContextProperty
139     {
140     /// <internalonly/>
141         protected String AttributeName;
142     
143         // The derived class must call: base(name);
144     /// <internalonly/>
145         public ContextAttribute(String name)
146         {
147             AttributeName = name;
148         }
149         
150         // IContextPropery::Name
151         // Default implementation provides AttributeName as the property name.
152     /// <internalonly/>
153         public virtual String Name
154         {
155             [System.Security.SecurityCritical]
156             get { return AttributeName; }
157         }
158
159         // IContextProperty::IsNewContextOK
160     /// <internalonly/>
161         [System.Security.SecurityCritical]
162         public virtual bool IsNewContextOK(Context newCtx)
163         {
164             // This will be called before entering the newCtx
165             // Default implementation says OK.
166             return true;
167         }    
168
169         // IContextProperty::Freeze
170         // Default implementation does nothing
171     /// <internalonly/>
172         [System.Security.SecurityCritical]
173         public virtual void Freeze(Context newContext)
174         {
175             BCLDebug.Log("ContextAttribute::ContextProperty::Freeze"+
176                         " for context " + newContext );
177         }
178                 
179         // Object::Equals
180         // Default implementation just compares the names
181     /// <internalonly/>
182         [System.Security.SecuritySafeCritical] // overrides public transparent method
183         public override bool Equals(Object o)
184         {
185             IContextProperty prop = o as IContextProperty;
186             return  (null != prop) && AttributeName.Equals(prop.Name);
187         }
188
189     /// <internalonly/>
190         [System.Security.SecuritySafeCritical] // overrides public transparent method
191         public override int GetHashCode()
192         {
193             return this.AttributeName.GetHashCode();
194         }
195
196         // IContextAttribute::IsContextOK
197         // Default calls Object::Equals on the property and does not
198         // bother with the ctorMsg.
199     /// <internalonly/>
200         [System.Security.SecurityCritical]  // auto-generated
201         public virtual bool IsContextOK(
202             Context ctx, IConstructionCallMessage ctorMsg)
203         {
204             if (ctx == null) 
205                 throw new ArgumentNullException("ctx");
206             if (ctorMsg == null) 
207                 throw new ArgumentNullException("ctorMsg");
208             Contract.EndContractBlock();
209                 
210             Contract.Assert(ctorMsg.ActivationType.IsMarshalByRef, "Activation on a non MarshalByRef object");
211
212             if (!ctorMsg.ActivationType.IsContextful)
213             {
214                 return true;
215             }
216
217             Object prop = ctx.GetProperty(AttributeName);
218             if ((prop!=null) && (Equals(prop)))
219             {
220                 return true;
221             }
222             return false;
223         }
224         
225         // IContextAttribute::GetPropertiesForNewContext
226         // Default adds the attribute itself w/o regard to the current
227         // list of properties
228     /// <internalonly/>
229         [System.Security.SecurityCritical]  // auto-generated
230         public virtual void GetPropertiesForNewContext(
231             IConstructionCallMessage ctorMsg)
232         {
233             if (ctorMsg == null)
234                 throw new ArgumentNullException("ctorMsg");
235             Contract.EndContractBlock();
236             ctorMsg.ContextProperties.Add((IContextProperty)this);
237         }
238     }
239
240 #if SIMPLEXAACTIVATION
241     /// <internalonly/>
242         [AttributeUsage(AttributeTargets.Class)]
243 [System.Runtime.InteropServices.ComVisible(true)]
244     public class new_appdomain : ContextAttribute
245     {
246
247         internal static int _domain_no = 0;
248
249     /// <internalonly/>
250         public new_appdomain() : base("new_appdomain") {}
251
252     /// <internalonly/>
253 [System.Runtime.InteropServices.ComVisible(true)]
254         public override bool IsContextOK(Context ctx, IConstructionCallMessage msg)
255         {
256             return false;
257         }
258
259     /// <internalonly/>
260 [System.Runtime.InteropServices.ComVisible(true)]
261         public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
262         {
263             ctorMsg.GetProperties()["__new_appdomain"] = true;
264         }
265
266     /// <internalonly/>
267 [System.Runtime.InteropServices.ComVisible(true)]
268         public static IConstructionReturnMessage DoSimpleXADActivation(IConstructionCallMessage msg)
269         {
270             int domain_no = Interlocked.Increment(ref _domain_no);
271             AppDomain ad = AppDomain.CreateDomain("AutoDomain #" + domain_no, null, null);
272
273             activator a = (activator) (ad.CreateInstance(null, typeof(activator).FullName)).Unwrap();
274             
275             return a.Activate(msg);
276         }
277
278     /// <internalonly/>
279 [System.Runtime.InteropServices.ComVisible(true)]
280         public class activator : MarshalByRefObject, IActivator
281         {
282         /// <internalonly/>
283 [System.Runtime.InteropServices.ComVisible(true)]
284             public IConstructionReturnMessage Activate(IConstructionCallMessage msg)
285             {
286                 return RemotingServices.DoCrossContextActivation(msg);
287             }
288
289         /// <internalonly/>
290             [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
291             public void LoadAssembly(AssemblyName an)
292             {
293                 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
294                 Assembly a = Assembly.InternalLoad(an, false, null, ref stackMark);
295                 if (a == null)
296                 {
297                     throw new RemotingException(
298                         String.Format(
299                             Environment.GetResourceString(
300                                 "Remoting_AssemblyLoadFailed"),
301                             an));                                
302                 }
303             }
304         }
305     }
306     
307     /// <internalonly/>
308         [new_appdomain]
309 [System.Runtime.InteropServices.ComVisible(true)]
310         public class MBR : MarshalByRefObject
311         {
312         /// <internalonly/>
313             public String MyAppDomain()
314             {
315                 return Thread.GetDomain().GetFriendlyName();
316             }
317         }
318         
319     /// <internalonly/>
320         [new_appdomain]
321 [System.Runtime.InteropServices.ComVisible(true)]
322         public class CB : ContextBoundObject
323         {
324         /// <internalonly/>
325             public String MyAppDomain()
326             {
327                 return Thread.GetDomain().GetFriendlyName();
328             }
329         }
330 #endif        
331     
332 }