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 private static LicenseContext mycontext
= null;
40 private static object contextLockUser
= null;
42 private LicenseManager ()
46 public static LicenseContext CurrentContext
{
48 lock (typeof(LicenseManager
)) {
49 //Tests indicate a System.ComponentModel.Design.RuntimeLicenseContext should be returned.
51 mycontext
= new Design
.RuntimeLicenseContext();
56 lock (typeof(LicenseManager
)) {
57 if (contextLockUser
==null) {
60 throw new InvalidOperationException("The CurrentContext property of the LicenseManager is currently locked and cannot be changed.");
66 public static LicenseUsageMode UsageMode
{
68 return CurrentContext
.UsageMode
;
72 public static object CreateWithContext (Type type
,
73 LicenseContext creationContext
)
75 return CreateWithContext (type
, creationContext
, new object [0]);
78 public static object CreateWithContext (Type type
,
79 LicenseContext creationContext
,
82 object newObject
= null;
83 lock (typeof (LicenseManager
)) {
84 object lockObject
= new object ();
85 LicenseContext oldContext
= CurrentContext
;
86 CurrentContext
= creationContext
;
87 LockContext (lockObject
);
89 newObject
= Activator
.CreateInstance (type
, args
);
90 } catch (Reflection
.TargetInvocationException exception
) {
91 throw exception
.InnerException
;
93 UnlockContext (lockObject
);
94 CurrentContext
= oldContext
;
100 public static bool IsLicensed (Type type
)
102 License license
= null;
103 if (!privateGetLicense (type
, null, false, out license
)) {
112 public static bool IsValid (Type type
)
113 //This method does not throw a LicenseException when it cannot grant a valid License
115 License license
=null;
116 if (!privateGetLicense (type
, null, false, out license
)) {
125 public static bool IsValid (Type type
, object instance
,
127 //This method does not throw a LicenseException when it cannot grant a valid License
129 return privateGetLicense (type
, null, false, out license
);
132 public static void LockContext (object contextUser
)
134 lock (typeof (LicenseManager
)) {
135 contextLockUser
= contextUser
;
139 public static void UnlockContext (object contextUser
)
141 lock (typeof(LicenseManager
)) {
142 //Ignore if we're not locked
143 if (contextLockUser
== null)
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.");
149 contextLockUser
= null;
153 public static void Validate (Type type
)
154 // Throws a LicenseException if the type is licensed, but a License could not be granted.
156 License license
= null;
157 if (!privateGetLicense (type
, null, true, out license
))
158 throw new LicenseException (type
, null);
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.
166 License license
=null;
167 if (!privateGetLicense(type
, instance
, true, out license
))
168 throw new LicenseException(type
, instance
);
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
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)
192 //licenseprovider.Dispose();
194 //There is was some problem creating the provider
196 //licenseprovidertype.Dispose();
198 //licenseprovidertype is null
200 //licenseproviderattribute.Dispose ();
202 //Didn't have a LicenseProviderAttribute, so it's licensed
205 license
= foundLicense
;