for TARGET_J2EE only:
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodCall.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodCall.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.Serialization;
37
38 namespace System.Runtime.Remoting.Messaging {
39
40         [Serializable] [CLSCompliant (false)]
41 #if NET_2_0
42         [System.Runtime.InteropServices.ComVisible (true)]
43 #endif
44         public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
45         {
46                 string _uri;
47                 string _typeName;
48                 string _methodName;
49                 object[] _args;
50                 Type[] _methodSignature;
51                 MethodBase _methodBase;
52                 LogicalCallContext _callContext;
53                 ArgInfo _inArgInfo;
54                 Identity _targetIdentity;
55 #if NET_2_0
56                 Type[] _genericArguments;
57 #endif
58
59                 protected IDictionary ExternalProperties;
60                 protected IDictionary InternalProperties;
61
62                 public MethodCall (Header [] headers)
63                 {
64                         Init();
65
66                         if (headers == null || headers.Length == 0) return;
67
68                         foreach (Header header in headers)
69                                 InitMethodProperty (header.Name, header.Value);
70
71                         ResolveMethod ();
72                 }
73
74                 internal MethodCall (SerializationInfo info, StreamingContext context)
75                 {
76                         Init();
77
78                         foreach (SerializationEntry entry in info)
79                                 InitMethodProperty ((string)entry.Name, entry.Value);
80                 }
81
82                 internal MethodCall (CADMethodCallMessage msg) 
83                 {
84                         _uri = string.Copy (msg.Uri);
85                         
86                         // Get unmarshalled arguments
87                         ArrayList args = msg.GetArguments ();
88
89                         _args = msg.GetArgs (args);
90                         _callContext = msg.GetLogicalCallContext (args);
91                         if (_callContext == null)
92                                 _callContext = new LogicalCallContext ();
93         
94                         _methodBase = msg.GetMethod ();
95                         
96                         Init();
97
98                         if (msg.PropertiesCount > 0)
99                                 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
100                 }
101
102                 public MethodCall (IMessage msg)
103                 {
104                         if (msg is IMethodMessage)
105                                 CopyFrom ((IMethodMessage) msg);
106                         else
107                         {
108                                 foreach (DictionaryEntry entry in msg.Properties)
109                                         InitMethodProperty ((String) entry.Key, entry.Value);
110                                 Init();
111                 }
112                 }
113
114                 internal MethodCall (string uri, string typeName, string methodName, object[] args)
115                 {
116                         _uri = uri;
117                         _typeName = typeName;
118                         _methodName = methodName;
119                         _args = args;
120
121                         Init();
122                         ResolveMethod();
123                 }
124
125                 internal MethodCall ()
126                 {
127                 }
128                 
129                 internal void CopyFrom (IMethodMessage call)
130                 {
131                         _uri = call.Uri;
132                         _typeName = call.TypeName;
133                         _methodName = call.MethodName;
134                         _args = call.Args;
135                         _methodSignature = (Type[]) call.MethodSignature;
136                         _methodBase = call.MethodBase;
137                         _callContext = call.LogicalCallContext;
138                         Init();
139                 }
140                 
141                 internal virtual void InitMethodProperty(string key, object value)
142                 {
143                         switch (key)
144                         {
145                                 case "__TypeName" : _typeName = (string) value; return;
146                                 case "__MethodName" : _methodName = (string) value; return;
147                                 case "__MethodSignature" : _methodSignature = (Type[]) value; return;
148                                 case "__Args" : _args = (object[]) value; return;
149                                 case "__CallContext" : _callContext = (LogicalCallContext) value; return;
150                                 case "__Uri" : _uri = (string) value; return;
151 #if NET_2_0
152                                 case "__GenericArguments" : _genericArguments = (Type[]) value; return;
153 #endif
154                                 default: Properties[key] = value; return;
155                         }
156                 }
157
158                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
159                 {
160                         info.AddValue ("__TypeName", _typeName);
161                         info.AddValue ("__MethodName", _methodName);
162                         info.AddValue ("__MethodSignature", _methodSignature);
163                         info.AddValue ("__Args", _args);
164                         info.AddValue ("__CallContext", _callContext);
165                         info.AddValue ("__Uri", _uri);
166 #if NET_2_0
167                         info.AddValue ("__GenericArguments", _genericArguments);
168 #endif
169
170                         if (InternalProperties != null) {
171                                 foreach (DictionaryEntry entry in InternalProperties)
172                                         info.AddValue ((string) entry.Key, entry.Value);
173                         }
174                 } 
175
176                 public int ArgCount {
177                         get { return _args.Length; }
178                 }
179
180                 public object[] Args {
181                         get { return _args; }
182                 }
183                 
184                 public bool HasVarArgs {
185                         get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
186                 }
187
188                 public int InArgCount 
189                 {
190                         get 
191                         { 
192                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
193                                 return _inArgInfo.GetInOutArgCount();
194                         }
195                 }
196
197                 public object[] InArgs 
198                 {
199                         get 
200                         { 
201                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
202                                 return _inArgInfo.GetInOutArgs (_args);
203                         }
204                 }
205                 
206                 public LogicalCallContext LogicalCallContext {
207                         get {
208                                 if (_callContext == null)
209                                         _callContext = new LogicalCallContext ();
210                                 return _callContext;
211                         }
212                 }
213                 
214                 public MethodBase MethodBase {
215                         get {
216                                 if (_methodBase == null)
217                                         ResolveMethod ();
218                                         
219                                 return _methodBase;
220                         }
221                 }
222
223                 public string MethodName {
224                         get {
225                                 // lazily fill in _methodName from _methodBase
226                                 if (_methodName == null)
227                                         _methodName = _methodBase.Name;
228                                 return _methodName;
229                         }
230                 }
231
232                 public object MethodSignature {
233                         get { 
234                                 if (_methodSignature == null && _methodBase != null)
235                                 {
236                                         ParameterInfo[] parameters = _methodBase.GetParameters();
237                                         _methodSignature = new Type[parameters.Length];
238                                         for (int n=0; n<parameters.Length; n++)
239                                                 _methodSignature[n] = parameters[n].ParameterType;
240                                 }
241                                 return _methodSignature;
242                         }
243                 }
244
245                 public virtual IDictionary Properties {
246                         get 
247                         { 
248                                 if (ExternalProperties == null) InitDictionary ();
249                                 return ExternalProperties; 
250                         }
251                 }
252
253                 internal virtual void InitDictionary()
254                 {
255                         MethodCallDictionary props = new MethodCallDictionary (this);
256                         ExternalProperties = props;
257                         InternalProperties = props.GetInternalProperties();
258                 }
259
260                 public string TypeName 
261                 {
262                         get {
263                                 // lazily fill in _typeName from _methodBase
264                                 if (_typeName == null)
265                                         _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
266                                 return _typeName;
267                         }
268                 }
269
270                 public string Uri {
271                         get { return _uri; }
272                         set { _uri = value; }
273                 }
274
275                 string IInternalMessage.Uri {
276                         get { return Uri; }
277                         set { Uri = value; }
278                 }
279
280                 public object GetArg (int argNum)
281                 {
282                         return _args[argNum];
283                 }
284
285                 public string GetArgName (int index)
286                 {
287                         return _methodBase.GetParameters()[index].Name;
288                 }
289
290                 public object GetInArg (int argNum)
291                 {
292                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
293                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
294                 }
295
296                 public string GetInArgName (int index)
297                 {
298                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
299                         return _inArgInfo.GetInOutArgName(index);
300                 }
301
302                 [MonoTODO]
303                 public virtual object HeaderHandler (Header[] h)
304                 {
305                         throw new NotImplementedException ();
306                 }
307
308                 public virtual void Init ()
309                 {
310                 }
311
312                 public void ResolveMethod ()
313                 {
314                         if (_uri != null)
315                         {
316                                 Type type = RemotingServices.GetServerTypeForUri (_uri);
317                                 if (type == null) {
318                                         string sname = _typeName != null ? " (" + _typeName + ")" : "";
319                                         throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
320                                 }
321
322                                 Type requestType = CastTo (_typeName, type);
323                                 if (requestType == null)
324                                         throw new RemotingException ("Cannot cast from client type '" + _typeName + "' to server type '" + type.FullName + "'");
325
326                                 // Look for the method in the requested type. The method signature is provided
327                                 // only if the method is overloaded in the requested type.
328                                 _methodBase = RemotingServices.GetMethodBaseFromName (requestType, _methodName, _methodSignature);
329
330                                 if (_methodBase == null)
331                                         throw new RemotingException ("Method " + _methodName + " not found in " + requestType);
332
333                                 // If the method is implemented in an interface, look for the method implementation.
334                                 // It can't be done in the previous GetMethodBaseFromName call because at that point we
335                                 // may not yet have the method signature.
336                                 if (requestType != type && requestType.IsInterface && !type.IsInterface) {
337                                         _methodBase = RemotingServices.GetVirtualMethod (type, _methodBase);
338                                         if (_methodBase == null)
339                                                 throw new RemotingException ("Method " + _methodName + " not found in " + type);
340                                 }
341
342                         } else {
343                                 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
344                                 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
345                         }
346
347
348 #if NET_2_0
349                         if (_methodBase.IsGenericMethod && _methodBase.ContainsGenericParameters) {
350                                 if (GenericArguments == null)
351                                         throw new RemotingException ("The remoting infrastructure does not support open generic methods.");
352                                 _methodBase = ((MethodInfo) _methodBase).MakeGenericMethod (GenericArguments);
353                         }
354 #endif
355                 }
356
357                 Type CastTo (string clientType, Type serverType)
358                 {
359                         clientType = GetTypeNameFromAssemblyQualifiedName (clientType);
360                         if (clientType == serverType.FullName) return serverType;
361
362                         // base class hierarchy
363
364                         Type baseType = serverType.BaseType;
365                         while (baseType != null) {
366                                 if (clientType == baseType.FullName) return baseType;
367                                 baseType = baseType.BaseType;
368                         }
369
370                         // Implemented interfaces
371
372                         Type[] interfaces = serverType.GetInterfaces();
373                         foreach (Type itype in interfaces)
374                                 if (clientType == itype.FullName) return itype;
375      
376                         return null;
377                 }
378
379                 static string GetTypeNameFromAssemblyQualifiedName (string aqname)
380                 {
381 #if NET_2_0
382                         int p = aqname.IndexOf ("]]");
383                         int i = aqname.IndexOf(',', p == -1 ? 0 : p + 2);
384 #else
385                         int i = aqname.IndexOf(',');
386 #endif
387                         if (i != -1) aqname = aqname.Substring (0, i).Trim ();
388                         return aqname;
389                 }
390                 
391                 [MonoTODO]
392                 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
393                 {
394                         throw new NotImplementedException ();
395                 }
396
397                 Identity IInternalMessage.TargetIdentity
398                 {
399                         get { return _targetIdentity; }
400                         set { _targetIdentity = value; }
401                 }
402
403 #if !NET_2_0
404                 public override string ToString ()
405                 {
406                         string s = _typeName.Split(',')[0] + "." + _methodName + " (";
407                         if (_args != null)
408                         {
409                                 for (int n=0; n<_args.Length; n++)
410                                 {
411                                         if (n>0) s+= ", ";
412                                         if (_args[n] != null) s += _args[n].GetType().Name + " ";
413                                         s += GetArgName (n);
414                                         if (_args[n] != null) s += " = {" + _args[n] + "}";
415                                         else s+=" = {null}";
416                                 }
417                         }
418                         s += ")";
419                         return s;
420                 }
421 #endif
422
423 #if NET_2_0
424                 Type[] GenericArguments {
425                         get {
426                                 if (_genericArguments != null)
427                                         return _genericArguments;
428
429                                 return _genericArguments = MethodBase.GetGenericArguments ();
430                         }
431                 }
432 #endif
433         }
434 }