use MOONLIGHT symbol
[mono.git] / mcs / class / System / System.ComponentModel / LicenseManager.cs
1 //
2 // System.ComponentModel.LicenseManager.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 //
9 // (C) 2004 Ivan Hamilton
10 // (C) 2003 Martin Willemoes Hansen
11 // (C) 2003 Andreas Nahr
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 namespace System.ComponentModel
36 {
37         public sealed class LicenseManager
38         {
39                 static LicenseContext mycontext;
40                 static object contextLockUser;
41                 
42                 static object lockObject = new object ();
43
44                 private LicenseManager ()
45                 {
46                 }
47
48                 public static LicenseContext CurrentContext {
49                         get {
50                                 lock (lockObject) {
51                                         //Tests indicate a System.ComponentModel.Design.RuntimeLicenseContext should be returned.
52                                         if  (mycontext==null)
53                                                 mycontext = new Design.RuntimeLicenseContext();
54                                         return mycontext;
55                                 }
56                         } 
57                         set { 
58                                 lock (lockObject) {
59                                         if (contextLockUser==null) {
60                                                 mycontext = value;
61                                         } else {
62                                                 throw new InvalidOperationException("The CurrentContext property of the LicenseManager is currently locked and cannot be changed.");
63                                         }
64                                 }
65                         }
66                 }
67
68                 public static LicenseUsageMode UsageMode {
69                         get {
70                                 return CurrentContext.UsageMode;
71                         }
72                 }
73
74                 public static object CreateWithContext (Type type,
75                                                         LicenseContext creationContext)
76                 {
77                         return CreateWithContext (type, creationContext, new object [0]);
78                 }
79
80                 public static object CreateWithContext (Type type, 
81                                                         LicenseContext creationContext, 
82                                                         object[] args)
83                 {
84                         object newObject = null;
85                         lock (lockObject) {
86                                 object contextUser = new object ();
87                                 LicenseContext oldContext = CurrentContext;
88                                 CurrentContext = creationContext;
89                                 LockContext (contextUser);
90                                 try {
91                                         newObject = Activator.CreateInstance (type, args);
92                                 } catch (Reflection.TargetInvocationException exception) {
93                                         throw exception.InnerException;
94                                 } finally {
95                                         UnlockContext (contextUser);
96                                         CurrentContext = oldContext;
97                                 }
98                         }
99                         return newObject;
100                 }
101
102                 public static bool IsLicensed (Type type)
103                 {
104                         License license = null;
105                         if (!privateGetLicense (type, null, false, out license)) {
106                                 return false;
107                         } else {
108                                 if (license != null)
109                                         license.Dispose ();
110                                 return true;
111                         }
112                 }
113
114                 public static bool IsValid (Type type)
115                 //This method does not throw a LicenseException when it cannot grant a valid License
116                 {
117                         License license=null;
118                         if (!privateGetLicense (type, null, false, out license)) {
119                                 return false;
120                         } else {
121                                 if (license != null)
122                                         license.Dispose ();
123                                 return true;
124                         }
125                 }
126
127                 public static bool IsValid (Type type, object instance, 
128                                             out License license)
129                 //This method does not throw a LicenseException when it cannot grant a valid License
130                 {
131                         return privateGetLicense (type, null, false, out license);
132                 }
133
134                 public static void LockContext (object contextUser)
135                 {
136                         lock (lockObject) {
137                                 contextLockUser = contextUser;
138                         }
139                 }
140
141                 public static void UnlockContext (object contextUser)
142                 {
143                         lock (lockObject) {
144                                 //Ignore if we're not locked
145                                 if (contextLockUser == null)
146                                         return;
147                                 //Don't unlock if not locking user
148                                 if (contextLockUser != contextUser)
149                                         throw new ArgumentException ("The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.");
150                                 //Remove lock
151                                 contextLockUser = null;
152                         }
153                 }
154
155                 public static void Validate (Type type)
156                 // Throws a  LicenseException if the type is licensed, but a License could not be granted. 
157                 {
158                         License license = null;
159                         if (!privateGetLicense (type, null, true, out license))
160                                 throw new LicenseException (type, null);
161                         if (license != null)
162                                 license.Dispose ();
163                 }
164
165                 public static License Validate (Type type, object instance)
166                 // Throws a  LicenseException if the type is licensed, but a License could not be granted. 
167                 {
168                         License license=null;
169                         if (!privateGetLicense(type, instance, true, out license))
170                                 throw new LicenseException(type, instance);
171                         return license;
172                 }
173
174                 private static bool privateGetLicense (Type type, object instance, bool allowExceptions, out License license) 
175                 //Returns if a component is licensed, and the license if provided
176                 {
177                         bool isLicensed = false;
178                         License foundLicense = null;
179                         //Get the LicProc Attrib for our type
180                         LicenseProviderAttribute licenseproviderattribute = (LicenseProviderAttribute) Attribute.GetCustomAttribute(type, typeof (LicenseProviderAttribute), true);
181                         //Check it's got an attrib
182                         if (licenseproviderattribute != null) {
183                                 Type licenseprovidertype = licenseproviderattribute.LicenseProvider;
184                                 //Check the attrib has a type
185                                 if (licenseprovidertype != null) {
186                                         //Create the provider
187                                         LicenseProvider licenseprovider = (LicenseProvider) Activator.CreateInstance(licenseprovidertype);
188                                         //Check we've got the provider
189                                         if (licenseprovider != null) {
190                                                 //Call provider, throw an LicenseException if error.
191                                                 foundLicense = licenseprovider.GetLicense(CurrentContext, type, instance, allowExceptions);
192                                                 if (foundLicense != null)
193                                                         isLicensed = true;
194                                                 //licenseprovider.Dispose();
195                                         } else {
196                                                 //There is was some problem creating the provider
197                                         }
198                                         //licenseprovidertype.Dispose();
199                                 } else {
200                                         //licenseprovidertype is null
201                                 }
202                                 //licenseproviderattribute.Dispose ();
203                         } else {
204                                 //Didn't have a LicenseProviderAttribute, so it's licensed
205                                 isLicensed = true;
206                         }
207                         license = foundLicense;
208                         return isLicensed;
209                 }
210         }
211 }