LicenseManager.cs 5.4 KB

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