2 // System.ComponentModel.LicenseManager.cs
5 // Ivan Hamilton (ivan@chimerical.com.au)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2004 Ivan Hamilton
10 // (C) 2003 Martin Willemoes Hansen
11 // (C) 2003 Andreas Nahr
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 namespace System.ComponentModel
37 public sealed class LicenseManager
39 static LicenseContext mycontext;
40 static object contextLockUser;
42 static object lockObject = new object ();
44 private LicenseManager ()
48 public static LicenseContext CurrentContext {
51 //Tests indicate a System.ComponentModel.Design.RuntimeLicenseContext should be returned.
53 mycontext = new Design.RuntimeLicenseContext();
59 if (contextLockUser==null) {
62 throw new InvalidOperationException("The CurrentContext property of the LicenseManager is currently locked and cannot be changed.");
68 public static LicenseUsageMode UsageMode {
70 return CurrentContext.UsageMode;
74 public static object CreateWithContext (Type type,
75 LicenseContext creationContext)
77 return CreateWithContext (type, creationContext, new object [0]);
80 public static object CreateWithContext (Type type,
81 LicenseContext creationContext,
84 object newObject = null;
86 object contextUser = new object ();
87 LicenseContext oldContext = CurrentContext;
88 CurrentContext = creationContext;
89 LockContext (contextUser);
91 newObject = Activator.CreateInstance (type, args);
92 } catch (Reflection.TargetInvocationException exception) {
93 throw exception.InnerException;
95 UnlockContext (contextUser);
96 CurrentContext = oldContext;
102 public static bool IsLicensed (Type type)
104 License license = null;
105 if (!privateGetLicense (type, null, false, out license)) {
114 public static bool IsValid (Type type)
115 //This method does not throw a LicenseException when it cannot grant a valid License
117 License license=null;
118 if (!privateGetLicense (type, null, false, out license)) {
127 public static bool IsValid (Type type, object instance,
129 //This method does not throw a LicenseException when it cannot grant a valid License
131 return privateGetLicense (type, null, false, out license);
134 public static void LockContext (object contextUser)
137 contextLockUser = contextUser;
141 public static void UnlockContext (object contextUser)
144 //Ignore if we're not locked
145 if (contextLockUser == null)
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.");
151 contextLockUser = null;
155 public static void Validate (Type type)
156 // Throws a LicenseException if the type is licensed, but a License could not be granted.
158 License license = null;
159 if (!privateGetLicense (type, null, true, out license))
160 throw new LicenseException (type, null);
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.
168 License license=null;
169 if (!privateGetLicense(type, instance, true, out license))
170 throw new LicenseException(type, instance);
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
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)
194 //licenseprovider.Dispose();
196 //There is was some problem creating the provider
198 //licenseprovidertype.Dispose();
200 //licenseprovidertype is null
202 //licenseproviderattribute.Dispose ();
204 //Didn't have a LicenseProviderAttribute, so it's licensed
207 license = foundLicense;