2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.ComponentModel.Design / RuntimeLicenseContext.cs
blob99a17ed2b0382538622b6ccf06b350dfc1144fb4
1 //
2 // System.ComponentModel.Design.RuntimeLicenseContext.cs
3 //
4 // Authors:
5 // Ivan Hamilton (ivan@chimerical.com.au)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // Carlo Kok (ck@remobjects.com)
9 //
10 // (C) 2004 Ivan Hamilton
11 // (C) 2003 Martin Willemoes Hansen
12 // (C) 2003 Andreas Nahr
13 // (C) 2009 Carlo Kok
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 //
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 //
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.ComponentModel;
38 using System.Reflection;
39 using System.Collections;
40 using System.IO;
41 using System.Runtime.Serialization.Formatters.Binary;
43 namespace System.ComponentModel.Design
45 internal class RuntimeLicenseContext : LicenseContext
47 private Hashtable extraassemblies;
48 private Hashtable keys;
50 public RuntimeLicenseContext () : base ()
54 void LoadKeys ()
56 if (keys != null)
57 return;
58 keys = new Hashtable ();
60 Assembly asm = Assembly.GetEntryAssembly ();
61 if (asm != null)
62 LoadAssemblyLicenses (keys, asm);
63 else {
64 foreach (Assembly asmnode in AppDomain.CurrentDomain.GetAssemblies ()) {
65 LoadAssemblyLicenses (keys, asmnode);
70 void LoadAssemblyLicenses (Hashtable targetkeys, Assembly asm)
72 if (asm is System.Reflection.Emit.AssemblyBuilder) return;
73 string asmname = Path.GetFileName (asm.Location);
74 string resourcename = asmname + ".licenses";
75 try {
76 foreach (string name in asm.GetManifestResourceNames ()) {
77 if (name != resourcename)
78 continue;
79 using (Stream stream = asm.GetManifestResourceStream (name)) {
80 BinaryFormatter formatter = new BinaryFormatter ();
81 object[] res = formatter.Deserialize (stream) as object[];
82 if (String.Compare ((string) res[0], asmname, true) == 0) {
83 Hashtable table = (Hashtable) res[1];
84 foreach (DictionaryEntry et in table)
85 targetkeys.Add (et.Key, et.Value);
90 } catch (InvalidCastException) {
94 public override string GetSavedLicenseKey (Type type, Assembly resourceAssembly)
96 if (type == null)
97 throw new ArgumentNullException ("type");
98 if (resourceAssembly != null) {
99 if (extraassemblies == null)
100 extraassemblies = new Hashtable ();
101 Hashtable ht = extraassemblies [resourceAssembly.FullName] as Hashtable;
102 if (ht == null) {
103 ht = new Hashtable ();
104 LoadAssemblyLicenses (ht, resourceAssembly);
105 extraassemblies [resourceAssembly.FullName] = ht;
107 return (string) ht [type.AssemblyQualifiedName];
109 LoadKeys ();
110 return (string) keys [type.AssemblyQualifiedName];
113 public override void SetSavedLicenseKey (Type type, string key)
115 if (type == null)
116 throw new ArgumentNullException ("type");
117 LoadKeys ();
118 keys [type.AssemblyQualifiedName] = key;