77c0661308b4a5806ec848069e9d25a53da87a82
[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                 private static LicenseContext mycontext = null;
40                 private static object contextLockUser = null;
41
42                 private LicenseManager ()
43                 {
44                 }
45
46                 public static LicenseContext CurrentContext {
47                         get {
48                                 lock (typeof(LicenseManager)) {
49                                         //Tests indicate a System.ComponentModel.Design.RuntimeLicenseContext should be returned.
50                                         if  (mycontext==null)
51                                                 mycontext = new Design.RuntimeLicenseContext();
52                                         return mycontext;
53                                 }
54                         } 
55                         set { 
56                                 lock (typeof(LicenseManager)) {
57                                         if (contextLockUser==null) {
58                                                 mycontext = value;
59                                         } else {
60                                                 throw new InvalidOperationException("The CurrentContext property of the LicenseManager is currently locked and cannot be changed.");
61                                         }
62                                 }
63                         }
64                 }
65
66                 public static LicenseUsageMode UsageMode {
67                         get {
68                                 return CurrentContext.UsageMode;
69                         }
70                 }
71
72                 public static object CreateWithContext (Type type,
73                                                         LicenseContext creationContext)
74                 {
75                         return CreateWithContext (type, creationContext, new object [0]);
76                 }
77
78                 public static object CreateWithContext (Type type, 
79                                                         LicenseContext creationContext, 
80                                                         object[] args)
81                 {
82                         object newObject = null;
83                         lock (typeof (LicenseManager)) {
84                                 object lockObject = new object ();
85                                 LicenseContext oldContext = CurrentContext;
86                                 CurrentContext = creationContext;
87                                 LockContext (lockObject);
88                                 try {
89                                         newObject = Activator.CreateInstance (type, args);
90                                 } catch (Reflection.TargetInvocationException exception) {
91                                         throw exception.InnerException;
92                                 } finally {
93                                         UnlockContext (lockObject);
94                                         CurrentContext = oldContext;
95                                 }
96                         }
97                         return newObject;
98                 }
99
100                 public static bool IsLicensed (Type type)
101                 {
102                         License license = null;
103                         if (!privateGetLicense (type, null, false, out license)) {
104                                 return false;
105                         } else {
106                                 if (license != null)
107                                         license.Dispose ();
108                                 return true;
109                         }
110                 }
111
112                 public static bool IsValid (Type type)
113                 //This method does not throw a LicenseException when it cannot grant a valid License
114                 {
115                         License license=null;
116                         if (!privateGetLicense (type, null, false, out license)) {
117                                 return false;
118                         } else {
119                                 if (license != null)
120                                         license.Dispose ();
121                                 return true;
122                         }
123                 }
124
125                 public static bool IsValid (Type type, object instance, 
126                                             out License license)
127                 //This method does not throw a LicenseException when it cannot grant a valid License
128                 {
129                         return privateGetLicense (type, null, false, out license);
130                 }
131
132                 public static void LockContext (object contextUser)
133                 {
134                         lock (typeof (LicenseManager)) {
135                                 contextLockUser = contextUser;
136                         }
137                 }
138
139                 public static void UnlockContext (object contextUser)
140                 {
141                         lock (typeof(LicenseManager)) {
142                                 //Ignore if we're not locked
143                                 if (contextLockUser == null)
144                                         return;
145                                 //Don't unlock if not locking user
146                                 if (contextLockUser != contextUser)
147                                         throw new ArgumentException ("The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.");
148                                 //Remove lock
149                                 contextLockUser = null;
150                         }
151                 }
152
153                 public static void Validate (Type type)
154                 // Throws a  LicenseException if the type is licensed, but a License could not be granted. 
155                 {
156                         License license = null;
157                         if (!privateGetLicense (type, null, true, out license))
158                                 throw new LicenseException (type, null);
159                         if (license != null)
160                                 license.Dispose ();
161                 }
162
163                 public static License Validate (Type type, object instance)
164                 // Throws a  LicenseException if the type is licensed, but a License could not be granted. 
165                 {
166                         License license=null;
167                         if (!privateGetLicense(type, instance, true, out license))
168                                 throw new LicenseException(type, instance);
169                         return license;
170                 }
171
172                 private static bool privateGetLicense (Type type, object instance, bool allowExceptions, out License license) 
173                 //Returns if a component is licensed, and the license if provided
174                 {
175                         bool isLicensed = false;
176                         License foundLicense = null;
177                         //Get the LicProc Attrib for our type
178                         LicenseProviderAttribute licenseproviderattribute = (LicenseProviderAttribute) Attribute.GetCustomAttribute(type, typeof (LicenseProviderAttribute), true);
179                         //Check it's got an attrib
180                         if (licenseproviderattribute != null) {
181                                 Type licenseprovidertype = licenseproviderattribute.LicenseProvider;
182                                 //Check the attrib has a type
183                                 if (licenseprovidertype != null) {
184                                         //Create the provider
185                                         LicenseProvider licenseprovider = (LicenseProvider) Activator.CreateInstance(licenseprovidertype);
186                                         //Check we've got the provider
187                                         if (licenseprovider != null) {
188                                                 //Call provider, throw an LicenseException if error.
189                                                 foundLicense = licenseprovider.GetLicense(CurrentContext, type, instance, allowExceptions);
190                                                 if (foundLicense != null)
191                                                         isLicensed = true;
192                                                 //licenseprovider.Dispose();
193                                         } else {
194                                                 //There is was some problem creating the provider
195                                         }
196                                         //licenseprovidertype.Dispose();
197                                 } else {
198                                         //licenseprovidertype is null
199                                 }
200                                 //licenseproviderattribute.Dispose ();
201                         } else {
202                                 //Didn't have a LicenseProviderAttribute, so it's licensed
203                                 isLicensed = true;
204                         }
205                         license = foundLicense;
206                         return isLicensed;
207                 }
208         }
209 }