LicenseManager.cs 6.4 KB

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