Merge pull request #2023 from juergenhoetzel/master
[mono.git] / mcs / class / Mono.Posix / Mono.Remoting.Channels.Unix / UnixBinaryCore.cs
1 //
2 // Mono.Remoting.Channels.Unix.BinaryCore.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@novell.com)
5 //
6 // 2005 (C) Copyright, Novell, Inc.
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Reflection;
32 using System.Collections;
33 using System.Runtime.Remoting;
34 using System.Runtime.Remoting.Messaging;
35 using System.Runtime.Serialization;
36 using System.Runtime.Serialization.Formatters;
37 using System.Runtime.Serialization.Formatters.Binary;
38
39 namespace Mono.Remoting.Channels.Unix
40 {
41         internal class UnixBinaryCore
42         {
43                 BinaryFormatter _serializationFormatter;
44                 BinaryFormatter _deserializationFormatter;
45                 bool _includeVersions = true;
46                 bool _strictBinding = false;
47                 IDictionary _properties;
48                 
49                 public static UnixBinaryCore DefaultInstance = new UnixBinaryCore ();
50                 
51                 public UnixBinaryCore (object owner, IDictionary properties, string[] allowedProperties)
52                 {
53                         _properties = properties;
54                         
55                         foreach(DictionaryEntry property in properties)
56                         {
57                                 string key = (string) property.Key;
58                                 if (Array.IndexOf (allowedProperties, key) == -1)
59                                         throw new RemotingException (owner.GetType().Name + " does not recognize '" + key + "' configuration property");
60                                 
61                                 switch (key)
62                                 {
63                                         case "includeVersions": 
64                                                 _includeVersions = Convert.ToBoolean (property.Value);
65                                                 break;
66                                                 
67                                         case "strictBinding":
68                                                 _strictBinding = Convert.ToBoolean (property.Value);
69                                                 break;
70                                 }
71                         }
72                         
73                         Init ();
74                 }
75                 
76                 public UnixBinaryCore ()
77                 {
78                         _properties = new Hashtable ();
79                         Init ();
80                 }
81                 
82                 public void Init ()
83                 {
84                         RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
85                         StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
86
87                         _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
88                         _deserializationFormatter = new BinaryFormatter (null, context);
89                         
90                         if (!_includeVersions)
91                         {
92                                 _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
93                                 _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
94                         }
95
96                         if (!_strictBinding)
97                         {
98                                 _serializationFormatter.Binder = SimpleBinder.Instance;
99                                 _deserializationFormatter.Binder = SimpleBinder.Instance;
100                         }
101                 }
102                 
103                 public BinaryFormatter Serializer
104                 {
105                         get { return _serializationFormatter; }
106                 }
107                 
108                 public BinaryFormatter Deserializer
109                 {
110                         get { return _deserializationFormatter; }
111                 }
112                 
113                 public IDictionary Properties
114                 {
115                         get { return _properties; }
116                 }
117         }
118         
119         
120         internal class SimpleBinder: SerializationBinder
121         {
122                 public static SimpleBinder Instance = new SimpleBinder ();
123                  
124                 public override Type BindToType (String assemblyName, string typeName)
125                 {
126                         Assembly asm;
127                         
128                         if (assemblyName.IndexOf (',') != -1)
129                         {
130                                 // Try using the full name
131                                 try
132                                 {
133                                         asm = Assembly.Load (assemblyName);
134                                         if (asm == null) return null;
135                                         Type t = asm.GetType (typeName);
136                                         if (t != null) return t;
137                                 }
138                                 catch {}
139                         }
140                         
141                         // Try using the simple name
142                         asm = Assembly.LoadWithPartialName (assemblyName);
143                         if (asm == null) return null;
144                         return asm.GetType (typeName, true);
145                 }
146         }
147 }
148