CatalogUtil.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.InteropServices;
  9. using System.Runtime;
  10. class ComCatalogObject
  11. {
  12. ICatalogObject catalogObject;
  13. ICatalogCollection catalogCollection;
  14. public ComCatalogObject(ICatalogObject catalogObject,
  15. ICatalogCollection catalogCollection)
  16. {
  17. this.catalogObject = catalogObject;
  18. this.catalogCollection = catalogCollection;
  19. }
  20. public object GetValue(string key)
  21. {
  22. return this.catalogObject.GetValue(key);
  23. }
  24. public string Name
  25. {
  26. get
  27. {
  28. return (string)(this.catalogObject.Name());
  29. }
  30. }
  31. public ComCatalogCollection GetCollection(string collectionName)
  32. {
  33. ICatalogCollection collection;
  34. collection = (ICatalogCollection)this.catalogCollection.GetCollection(
  35. collectionName,
  36. this.catalogObject.Key());
  37. collection.Populate();
  38. return new ComCatalogCollection(collection);
  39. }
  40. }
  41. class ComCatalogCollection
  42. {
  43. ICatalogCollection catalogCollection;
  44. public ComCatalogCollection(ICatalogCollection catalogCollection)
  45. {
  46. this.catalogCollection = catalogCollection;
  47. }
  48. public int Count
  49. {
  50. get
  51. {
  52. return this.catalogCollection.Count();
  53. }
  54. }
  55. // (Not a property because I make a new object every time.)
  56. public ComCatalogObject Item(int index)
  57. {
  58. ICatalogObject catalogObject;
  59. catalogObject = (ICatalogObject)this.catalogCollection.Item(index);
  60. return new ComCatalogObject(catalogObject, this.catalogCollection);
  61. }
  62. public Enumerator GetEnumerator()
  63. {
  64. return new Enumerator(this);
  65. }
  66. // This is kind of a half-baked IEnumerator implementation. It
  67. // lets you use foreach(), but don't expect fancy things like
  68. // InvalidOperationExceptions and such.
  69. //
  70. public struct Enumerator
  71. {
  72. ComCatalogCollection collection;
  73. ComCatalogObject current;
  74. int count;
  75. public Enumerator(ComCatalogCollection collection)
  76. {
  77. this.collection = collection;
  78. this.current = null;
  79. this.count = -1;
  80. }
  81. public ComCatalogObject Current
  82. {
  83. get { return this.current; }
  84. }
  85. public bool MoveNext()
  86. {
  87. this.count++;
  88. if (this.count >= collection.Count)
  89. return false;
  90. this.current = this.collection.Item(this.count);
  91. return true;
  92. }
  93. public void Reset()
  94. {
  95. this.count = -1;
  96. }
  97. }
  98. }
  99. internal static class CatalogUtil
  100. {
  101. internal static string[] GetRoleMembers(
  102. ComCatalogObject application,
  103. ComCatalogCollection rolesCollection)
  104. {
  105. ComCatalogCollection applicationRoles;
  106. applicationRoles = application.GetCollection("Roles");
  107. // This is inefficient. If it turns into a
  108. // performance problem, then we'll need to put a cache in
  109. // somewhere.
  110. //
  111. List<string> roleMembers = new List<string>();
  112. foreach (ComCatalogObject role in rolesCollection)
  113. {
  114. string roleName = (string)role.GetValue("Name");
  115. // Find the role in the app roles list.
  116. //
  117. foreach (ComCatalogObject appRole in applicationRoles)
  118. {
  119. string appRoleName = (string)appRole.GetValue("Name");
  120. if (roleName == appRoleName)
  121. {
  122. // Found it, put all of the user names into
  123. // the role members list.
  124. //
  125. ComCatalogCollection users;
  126. users = appRole.GetCollection("UsersInRole");
  127. foreach (ComCatalogObject userObject in users)
  128. {
  129. string user = (string)userObject.GetValue("User");
  130. roleMembers.Add(user);
  131. }
  132. break;
  133. }
  134. }
  135. }
  136. return roleMembers.ToArray();
  137. }
  138. internal static ComCatalogObject FindApplication(Guid applicationId)
  139. {
  140. ICatalog2 catalog = (ICatalog2)(new xCatalog());
  141. ICatalogObject appObject = null;
  142. ICatalogCollection partitionCollection = null;
  143. try
  144. {
  145. partitionCollection = (ICatalogCollection)catalog.GetCollection(
  146. "Partitions");
  147. partitionCollection.Populate();
  148. }
  149. catch (COMException comException)
  150. {
  151. if (comException.ErrorCode != HR.COMADMIN_E_PARTITIONS_DISABLED)
  152. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(comException);
  153. }
  154. if (partitionCollection != null)
  155. {
  156. for (int i = 0; i < partitionCollection.Count(); i++)
  157. {
  158. ICatalogObject partition;
  159. partition = (ICatalogObject)partitionCollection.Item(i);
  160. ICatalogCollection appCollection;
  161. appCollection = (ICatalogCollection)partitionCollection.GetCollection(
  162. "Applications",
  163. partition.Key());
  164. appCollection.Populate();
  165. appObject = FindApplication(appCollection, applicationId);
  166. if (appObject != null)
  167. return new ComCatalogObject(appObject, appCollection);
  168. }
  169. }
  170. else
  171. {
  172. ICatalogCollection appCollection;
  173. appCollection = (ICatalogCollection)catalog.GetCollection(
  174. "Applications");
  175. appCollection.Populate();
  176. appObject = FindApplication(appCollection, applicationId);
  177. if (appObject != null)
  178. return new ComCatalogObject(appObject, appCollection);
  179. }
  180. return null;
  181. }
  182. static ICatalogObject FindApplication(ICatalogCollection appCollection,
  183. Guid applicationId)
  184. {
  185. ICatalogObject appObject = null;
  186. for (int i = 0; i < appCollection.Count(); i++)
  187. {
  188. appObject = (ICatalogObject)appCollection.Item(i);
  189. Guid id = Fx.CreateGuid((string)appObject.GetValue("ID"));
  190. if (id == applicationId)
  191. return appObject;
  192. }
  193. return null;
  194. }
  195. }
  196. }