MessageQueueCriteria.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //------------------------------------------------------------------------------
  2. // <copyright file="MessageQueueCriteria.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using Experimental.System.Messaging.Interop;
  7. using System;
  8. using System.ComponentModel;
  9. using System.Globalization; //for CultureInfo
  10. namespace Experimental.System.Messaging
  11. {
  12. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria"]/*' />
  13. /// <devdoc>
  14. /// <para>
  15. /// This class
  16. /// is used to filter MessageQueues when performing a
  17. /// query in the network, through MessageQueue.GetPublicQueues method.
  18. /// </para>
  19. /// </devdoc>
  20. public class MessageQueueCriteria
  21. {
  22. private DateTime createdAfter;
  23. private DateTime createdBefore;
  24. private string label;
  25. private string machine;
  26. private DateTime modifiedAfter;
  27. private DateTime modifiedBefore;
  28. private Guid category;
  29. private CriteriaPropertyFilter filter = new CriteriaPropertyFilter();
  30. private Restrictions restrictions;
  31. private Guid machineId;
  32. private static DateTime minDate = new DateTime(1970, 1, 1);
  33. private static DateTime maxDate = new DateTime(2038, 1, 19);
  34. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.CreatedAfter"]/*' />
  35. /// <devdoc>
  36. /// Specifies the lower bound of the interval
  37. /// that will be used as the queue creation time
  38. /// search criteria.
  39. /// </devdoc>
  40. public DateTime CreatedAfter
  41. {
  42. get
  43. {
  44. if (!this.filter.CreatedAfter)
  45. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  46. return this.createdAfter;
  47. }
  48. set
  49. {
  50. if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
  51. throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
  52. this.createdAfter = value;
  53. if (this.filter.CreatedBefore && this.createdAfter > this.createdBefore)
  54. this.createdBefore = this.createdAfter;
  55. this.filter.CreatedAfter = true;
  56. }
  57. }
  58. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.CreatedBefore"]/*' />
  59. /// <devdoc>
  60. /// Specifies the upper bound of the interval
  61. /// that will be used as the queue creation time
  62. /// search criteria.
  63. /// </devdoc>
  64. public DateTime CreatedBefore
  65. {
  66. get
  67. {
  68. if (!this.filter.CreatedBefore)
  69. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  70. return this.createdBefore;
  71. }
  72. set
  73. {
  74. if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
  75. throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
  76. this.createdBefore = value;
  77. if (this.filter.CreatedAfter && this.createdAfter > this.createdBefore)
  78. this.createdAfter = this.createdBefore;
  79. this.filter.CreatedBefore = true;
  80. }
  81. }
  82. internal bool FilterMachine
  83. {
  84. get
  85. {
  86. return this.filter.MachineName;
  87. }
  88. }
  89. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.Label"]/*' />
  90. /// <devdoc>
  91. /// Specifies the label that that will be used as
  92. /// the criteria to search queues in the network.
  93. /// </devdoc>
  94. public string Label
  95. {
  96. get
  97. {
  98. if (!this.filter.Label)
  99. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  100. return this.label;
  101. }
  102. set
  103. {
  104. if (value == null)
  105. throw new ArgumentNullException("value");
  106. this.label = value;
  107. this.filter.Label = true;
  108. }
  109. }
  110. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.MachineName"]/*' />
  111. /// <devdoc>
  112. /// <para>
  113. /// Specifies the machine name that will be used
  114. /// as the criteria to search queues in the network.
  115. /// </para>
  116. /// </devdoc>
  117. public string MachineName
  118. {
  119. get
  120. {
  121. if (!this.filter.MachineName)
  122. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  123. return this.machine;
  124. }
  125. set
  126. {
  127. if (!SyntaxCheck.CheckMachineName(value))
  128. throw new ArgumentException(Res.GetString(Res.InvalidProperty, "MachineName", value));
  129. try
  130. {
  131. this.machineId = MessageQueue.GetMachineId(value);
  132. }
  133. finally
  134. {
  135. }
  136. this.machine = value;
  137. this.filter.MachineName = true;
  138. }
  139. }
  140. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ModifiedAfter"]/*' />
  141. /// <devdoc>
  142. /// Specifies the lower bound of the interval
  143. /// that will be used as the queue modified time
  144. /// search criteria.
  145. /// </devdoc>
  146. public DateTime ModifiedAfter
  147. {
  148. get
  149. {
  150. if (!this.filter.ModifiedAfter)
  151. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  152. return this.modifiedAfter;
  153. }
  154. set
  155. {
  156. if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
  157. throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
  158. this.modifiedAfter = value;
  159. if (this.filter.ModifiedBefore && this.modifiedAfter > this.modifiedBefore)
  160. this.modifiedBefore = this.modifiedAfter;
  161. this.filter.ModifiedAfter = true;
  162. }
  163. }
  164. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ModifiedBefore"]/*' />
  165. /// <devdoc>
  166. /// Specifies the upper bound of the interval
  167. /// that will be used as the queue modified time
  168. /// search criteria.
  169. /// </devdoc>
  170. public DateTime ModifiedBefore
  171. {
  172. get
  173. {
  174. if (!this.filter.ModifiedBefore)
  175. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  176. return this.modifiedBefore;
  177. }
  178. set
  179. {
  180. if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
  181. throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
  182. this.modifiedBefore = value;
  183. if (this.filter.ModifiedAfter && this.modifiedAfter > this.modifiedBefore)
  184. this.modifiedAfter = this.modifiedBefore;
  185. this.filter.ModifiedBefore = true;
  186. }
  187. }
  188. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.Reference"]/*' />
  189. /// <internalonly/>
  190. internal Restrictions.MQRESTRICTION Reference
  191. {
  192. get
  193. {
  194. int size = 0;
  195. if (this.filter.CreatedAfter)
  196. ++size;
  197. if (this.filter.CreatedBefore)
  198. ++size;
  199. if (this.filter.Label)
  200. ++size;
  201. if (this.filter.ModifiedAfter)
  202. ++size;
  203. if (this.filter.ModifiedBefore)
  204. ++size;
  205. if (this.filter.Category)
  206. ++size;
  207. restrictions = new Restrictions(size);
  208. if (this.filter.CreatedAfter)
  209. restrictions.AddI4(NativeMethods.QUEUE_PROPID_CREATE_TIME, Restrictions.PRGT, ConvertTime(this.createdAfter));
  210. if (this.filter.CreatedBefore)
  211. restrictions.AddI4(NativeMethods.QUEUE_PROPID_CREATE_TIME, Restrictions.PRLE, ConvertTime(this.createdBefore));
  212. if (this.filter.Label)
  213. restrictions.AddString(NativeMethods.QUEUE_PROPID_LABEL, Restrictions.PREQ, this.label);
  214. if (this.filter.ModifiedAfter)
  215. restrictions.AddI4(NativeMethods.QUEUE_PROPID_MODIFY_TIME, Restrictions.PRGT, ConvertTime(this.modifiedAfter));
  216. if (this.filter.ModifiedBefore)
  217. restrictions.AddI4(NativeMethods.QUEUE_PROPID_MODIFY_TIME, Restrictions.PRLE, ConvertTime(this.modifiedBefore));
  218. if (this.filter.Category)
  219. restrictions.AddGuid(NativeMethods.QUEUE_PROPID_TYPE, Restrictions.PREQ, this.category);
  220. return this.restrictions.GetRestrictionsRef();
  221. }
  222. }
  223. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.Category"]/*' />
  224. /// <devdoc>
  225. /// Specifies the Category that will be used
  226. /// as the criteria to search queues in the network.
  227. /// </devdoc>
  228. public Guid Category
  229. {
  230. get
  231. {
  232. if (!this.filter.Category)
  233. throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
  234. return this.category;
  235. }
  236. set
  237. {
  238. this.category = value;
  239. this.filter.Category = true;
  240. }
  241. }
  242. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ClearAll"]/*' />
  243. /// <devdoc>
  244. /// Resets all the current instance settings.
  245. /// </devdoc>
  246. public void ClearAll()
  247. {
  248. this.filter.ClearAll();
  249. }
  250. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ConvertTime"]/*' />
  251. /// <internalonly/>
  252. private int ConvertTime(DateTime time)
  253. {
  254. time = time.ToUniversalTime();
  255. return (int)(time - MessageQueueCriteria.minDate).TotalSeconds;
  256. }
  257. /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.CriteriaPropertyFilter"]/*' />
  258. /// <internalonly/>
  259. private class CriteriaPropertyFilter
  260. {
  261. public bool CreatedAfter;
  262. public bool CreatedBefore;
  263. public bool Label;
  264. public bool MachineName;
  265. public bool ModifiedAfter;
  266. public bool ModifiedBefore;
  267. public bool Category;
  268. public void ClearAll()
  269. {
  270. this.CreatedAfter = false;
  271. this.CreatedBefore = false;
  272. this.Label = false;
  273. this.MachineName = false;
  274. this.ModifiedAfter = false;
  275. this.ModifiedBefore = false;
  276. this.Category = false;
  277. }
  278. }
  279. }
  280. }