Merge branch 'patch-1' of https://github.com/ReubenBond/mono into ReubenBond-patch-1
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels / BinaryCore.cs
1 //
2 // System.Runtime.Remoting.Channels.BinaryCore.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2003 (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.Collections;
31 using System.Runtime.Remoting.Messaging;
32 using System.Runtime.Serialization;
33 using System.Runtime.Serialization.Formatters;
34 using System.Runtime.Serialization.Formatters.Binary;
35
36 namespace System.Runtime.Remoting.Channels
37 {
38         internal class BinaryCore
39         {
40                 BinaryFormatter _serializationFormatter;
41                 BinaryFormatter _deserializationFormatter;
42                 bool _includeVersions = true;
43                 bool _strictBinding = false;
44                 IDictionary _properties;
45                 
46                 TypeFilterLevel _filterLevel = TypeFilterLevel.Low;
47                 
48                 public static BinaryCore DefaultInstance = new BinaryCore ();
49                 
50                 public BinaryCore (object owner, IDictionary properties, string[] allowedProperties)
51                 {
52                         _properties = properties;
53                         
54                         if (_properties == null)
55                         {
56                                 _properties = new Hashtable(10);
57                         }
58                         
59                         foreach(DictionaryEntry property in properties)
60                         {
61                                 string key = (string) property.Key;
62                                 if (Array.IndexOf (allowedProperties, key) == -1)
63                                         throw new RemotingException (owner.GetType().Name + " does not recognize '" + key + "' configuration property");
64                                 
65                                 switch (key)
66                                 {
67                                         case "includeVersions": 
68                                                 _includeVersions = Convert.ToBoolean (property.Value);
69                                                 break;
70                                                 
71                                         case "strictBinding":
72                                                 _strictBinding = Convert.ToBoolean (property.Value);
73                                                 break;
74                                                 
75                                         case "typeFilterLevel":
76                                                 if (property.Value is TypeFilterLevel)
77                                                         _filterLevel = (TypeFilterLevel) property.Value;
78                                                 else {
79                                                         string s = (string) property.Value;
80                                                         _filterLevel = (TypeFilterLevel) Enum.Parse (typeof(TypeFilterLevel), s);
81                                                 }
82                                                 break;
83                                                 
84                                 }
85                         }
86                         
87                         Init ();
88                 }
89                 
90                 public BinaryCore ()
91                 {
92                         _properties = new Hashtable ();
93                         Init ();
94                 }
95                 
96                 public void Init ()
97                 {
98                         RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
99                         StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
100
101                         _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
102                         _deserializationFormatter = new BinaryFormatter (null, context);
103                         
104                         _serializationFormatter.FilterLevel = _filterLevel;
105                         _deserializationFormatter.FilterLevel = _filterLevel;
106                         
107                         if (!_includeVersions)
108                         {
109                                 _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
110                                 _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
111                         }
112
113                         if (!_strictBinding)
114                         {
115                                 _serializationFormatter.Binder = ChannelCore.SimpleBinder;
116                                 _deserializationFormatter.Binder = ChannelCore.SimpleBinder;
117                         }
118                 }
119                 
120                 public BinaryFormatter Serializer
121                 {
122                         get { return _serializationFormatter; }
123                 }
124                 
125                 public BinaryFormatter Deserializer
126                 {
127                         get { return _deserializationFormatter; }
128                 }
129                 
130                 public IDictionary Properties
131                 {
132                         get { return _properties; }
133                 }
134                 
135                 public TypeFilterLevel TypeFilterLevel
136                 {
137                         get { return _filterLevel; }
138                 }
139         }
140 }
141