* ActivationServices.cs: make AllocateUninitializedClassInstance public,
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Activation / ActivationServices.cs
1 //
2 // System.Runtime.Remoting.ActivationServices.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2002, Lluis Sanchez Gual
7 //
8
9 using System;
10 using System.Threading;
11 using System.Runtime.Remoting.Messaging;
12 using System.Runtime.Remoting.Activation;
13 using System.Runtime.Remoting.Contexts;
14 using System.Runtime.Remoting.Proxies;
15 using System.Reflection;
16 using System.Runtime.CompilerServices;
17 using System.Collections;
18 using System.Runtime.Remoting.Channels;
19
20 namespace System.Runtime.Remoting.Activation
21 {
22         internal class ActivationServices
23         {
24                 static IActivator _constructionActivator = new ConstructionLevelActivator ();
25
26                 public static IMessage Activate (RemotingProxy proxy, ConstructionCall ctorCall)
27                 {
28                         IMessage response;
29
30                         if (Thread.CurrentContext.HasExitSinks && !ctorCall.IsContextOk)
31                                 response = Thread.CurrentContext.GetClientContextSinkChain ().SyncProcessMessage (ctorCall);
32                         else
33                                 response = RemoteActivate (ctorCall);
34
35                         if (response is IConstructionReturnMessage)
36                         {
37                                 Identity identity = RemotingServices.GetMessageTargetIdentity (ctorCall);
38                                 proxy.AttachIdentity (identity);
39                         }
40
41                         return response;
42                 }
43
44                 public static IMessage RemoteActivate (IConstructionCallMessage ctorCall)
45                 {
46                         try \r
47                         {
48                                 return ctorCall.Activator.Activate (ctorCall);
49                         }
50                         catch (Exception ex) \r
51                         {
52                                 return new ReturnMessage (ex, ctorCall);
53                         }               \r
54                 }
55
56                 public static object CreateProxyFromAttributes (Type type, object[] activationAttributes)
57                 {
58                         string activationUrl = null;
59                         foreach (object attr in activationAttributes)
60                         {
61                                 if (!(attr is IContextAttribute)) throw new RemotingException ("Activation attribute does not implement the IContextAttribute interface");
62                                 if (attr is UrlAttribute) activationUrl = ((UrlAttribute)attr).UrlValue;
63                         }
64
65                         if (activationUrl != null)
66                                 return RemotingServices.CreateClientProxy (type, activationUrl, activationAttributes);
67
68                         ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType (type);
69                         if (activatedEntry != null)
70                                 return RemotingServices.CreateClientProxy (activatedEntry, activationAttributes);
71
72                         if (type.IsContextful)
73                                 return RemotingServices.CreateClientProxyForContextBound (type, activationAttributes);
74                         
75                         return null;
76                 }
77
78                 public static ConstructionCall CreateConstructionCall (Type type, string activationUrl, object[] activationAttributes)
79                 {
80                         ConstructionCall ctorCall = new ConstructionCall (type);
81
82                         if (!type.IsContextful) 
83                         {
84                                 // Must be a remote activated object
85                                 ctorCall.Activator = new AppDomainLevelActivator (activationUrl, _constructionActivator);
86                                 ctorCall.IsContextOk = false;   // It'll be activated in a remote context
87                                 return ctorCall;
88                         }
89
90                         // It is a CBO. Need collect context properties and
91                         // check if a new context is needed.
92
93                         IActivator activatorChain = _constructionActivator;
94                         activatorChain = new ContextLevelActivator (activatorChain);
95
96                         ArrayList attributes = new ArrayList ();
97                         if (activationAttributes != null) attributes.AddRange (activationAttributes);
98
99                         bool isContextOk = (activationUrl == ChannelServices.CrossContextUrl);  // Remote CBOs are always created in a new context
100                         Context currentContext = Threading.Thread.CurrentContext;
101
102                         if (isContextOk) 
103                         {
104                                 foreach (IContextAttribute attr in attributes) 
105                                 {
106                                         if (!attr.IsContextOK (currentContext, ctorCall)) 
107                                         {
108                                                 isContextOk = false;
109                                                 break;
110                                         }
111                                 }
112                         }
113
114                         object[] typeAttributes = type.GetCustomAttributes (true);
115                         foreach (object attr in typeAttributes) 
116                         {
117                                 if (attr is IContextAttribute) 
118                                 {
119                                         isContextOk = isContextOk && ((IContextAttribute)attr).IsContextOK (currentContext, ctorCall);
120                                         attributes.Add (attr);
121                                 }
122                         }
123
124                         if (!isContextOk)
125                         {
126                                 // A new context is needed. Collect the context properties and chain
127                                 // the context level activator.
128
129                                 ctorCall.SetActivationAttributes (attributes.ToArray());
130
131                                 foreach (IContextAttribute attr in attributes)
132                                         attr.GetPropertiesForNewContext (ctorCall);
133                         }
134
135                         if (activationUrl != ChannelServices.CrossContextUrl)
136                                 activatorChain = new AppDomainLevelActivator (activationUrl, activatorChain);
137                         
138                         ctorCall.Activator = activatorChain;
139                         ctorCall.IsContextOk = isContextOk;
140
141                         return ctorCall;
142                 }
143
144                 public static IMessage CreateInstanceFromMessage (IConstructionCallMessage ctorCall)
145                 {
146                         object obj = AllocateUninitializedClassInstance (ctorCall.ActivationType);
147                         ctorCall.MethodBase.Invoke (obj, ctorCall.Args);
148
149                         ServerIdentity identity = (ServerIdentity) RemotingServices.GetMessageTargetIdentity (ctorCall);
150
151                         identity.AttachServerObject ((MarshalByRefObject) obj, Threading.Thread.CurrentContext);
152
153                         return new ConstructionResponse (obj, null, ctorCall);
154                 }
155
156                 public static object CreateProxyForType (Type type)
157                 {
158                         // Called by the runtime when creating an instance of a type
159                         // that has been registered as remotely activated.
160
161                         // First of all check for remote activation. If the object is not remote, then
162                         // it may be contextbound.
163
164                         ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType (type);
165                         if (activatedEntry != null)
166                                 return RemotingServices.CreateClientProxy (activatedEntry, null);
167
168                         WellKnownClientTypeEntry wellknownEntry = RemotingConfiguration.IsWellKnownClientType (type);
169                         if (wellknownEntry != null)
170                                 return RemotingServices.CreateClientProxy (wellknownEntry);
171
172                         if (type.IsContextful)
173                                 return RemotingServices.CreateClientProxyForContextBound (type, null);
174
175                         return null;
176                 }
177
178                 // Allocates an uninitialized instance. It never creates proxies.
179                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
180                 public static extern object AllocateUninitializedClassInstance (Type type);
181
182                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183                 public extern static void EnableProxyActivation (Type type, bool enable);
184         }
185 }