Facilitate the merge
[mono.git] / mcs / class / System.Messaging / System.Messaging / MessageQueue.cs
1 //
2 // System.Messaging
3 //
4 // Authors:
5 //        Peter Van Isacker (sclytrack@planetinternet.be)
6 //        Rafael Teixeira   (rafaelteixeirabr@hotmail.com)
7 //
8 // (C) 2003 Peter Van Isacker
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Drawing;
36 using System.Messaging.Design;
37 using System.Threading;
38
39 using Mono.Messaging;
40
41 namespace System.Messaging
42 {
43         [TypeConverter (typeof(MessageQueueConverter))]
44         [Editor ("System.Messaging.Design.QueuePathEditor", "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
45         [Designer ("Microsoft.VisualStudio.Install.MessageQueueInstallableComponentDesigner, " + Consts.AssemblyMicrosoft_VisualStudio)]
46         [InstallerType (typeof(MessageQueueInstaller))]
47         [DefaultEvent ("ReceiveCompleted")]
48         public class MessageQueue : Component, IEnumerable
49         {
50                 #region Fields
51
52                 public static readonly long InfiniteQueueSize;
53                 public static readonly TimeSpan InfiniteTimeout = MessagingProviderLocator.InfiniteTimeout;
54                 private IMessageFormatter formatter;
55                 private MessagePropertyFilter messageReadPropertyFilter = new MessagePropertyFilter ();
56                 private readonly IMessageQueue delegateQueue;
57
58                 #endregion //Fields
59
60
61                 #region Constructor
62
63                 public MessageQueue () : this (GetMessageQueue ())
64                 {
65                 }
66
67                 public MessageQueue (string path) : this (path, false) 
68                 {
69                 }
70
71                 public MessageQueue (string path, bool sharedModeDenyReceive) : 
72                         this (GetMessageQueue (path))
73                 {
74                 }
75                 
76                 public MessageQueue (string path, QueueAccessMode accessMode) :
77                         this (GetMessageQueue (path))
78                 {
79                 }
80
81                 internal MessageQueue (IMessageQueue delegateQueue)
82                 {
83                         this.delegateQueue = delegateQueue;
84                         formatter = new XmlMessageFormatter ();
85                         delegateQueue.PeekCompleted += new CompletedEventHandler (DelegatePeekCompleted);
86                 }
87
88                 #endregion //Constructor
89
90                 #region Properties
91
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93                 [MessagingDescription ("MQ_Authenticate")]
94                 public bool Authenticate {
95                         get {
96                                 return delegateQueue.Authenticate;
97                         }
98                         set {
99                                 delegateQueue.Authenticate = value;
100                         }
101                 }
102
103                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
104                 [MessagingDescription ("MQ_BasePriority")]
105                 public short BasePriority {
106                         get {
107                                 return delegateQueue.BasePriority;
108                         }
109                         set {
110                                 delegateQueue.BasePriority = value;
111                         }
112                 }
113
114                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
115                 [Browsable (false)]
116                 [MessagingDescription ("MQ_CanRead")]
117                 public bool CanRead {
118                         get {
119                                 return delegateQueue.CanRead;
120                         }
121                 }
122
123                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
124                 [Browsable (false)]
125                 [MessagingDescription ("MQ_CanWrite")]
126                 public bool CanWrite {
127                         get {
128                                 return delegateQueue.CanWrite;
129                         }
130                 }
131
132                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
133                 [MessagingDescription ("MQ_Category")]
134                 public Guid Category {
135                         get {
136                                 return delegateQueue.Category;
137                         }
138                         set {
139                                 delegateQueue.Category = value;
140                         }
141                 }
142
143                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
144                 [MessagingDescription ("MQ_CreateTime")]
145                 public DateTime CreateTime {
146                         get {
147                                 return delegateQueue.CreateTime;
148                         }
149                 }
150
151                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
152                 [Browsable (false)]
153                 [MessagingDescription ("MQ_DefaultPropertiesToSend")]
154                 public DefaultPropertiesToSend DefaultPropertiesToSend {
155                         get {
156                                 throw new NotImplementedException ();
157                         }
158                         set {
159                                 throw new NotImplementedException ();
160                         }
161                 }
162
163                 [Browsable (false)]
164                 [DefaultValue (false)]
165                 [MessagingDescription ("MQ_DenySharedReceive")]
166                 public bool DenySharedReceive {
167                         get {
168                                 return delegateQueue.DenySharedReceive;
169                         }
170                         set {
171                                 delegateQueue.DenySharedReceive = value;
172                         }
173                 }
174
175                 [Browsable (false)]
176                 public static bool EnableConnectionCache {
177                         get {
178                                 throw new NotImplementedException ();
179                         }
180                         set {
181                                 throw new NotImplementedException ();
182                         }
183                 }
184
185                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
186                 [MessagingDescription ("MQ_EncryptionRequired")]
187                 public EncryptionRequired EncryptionRequired {
188                         get {
189                                 return (EncryptionRequired) delegateQueue.EncryptionRequired;
190                         }
191                         set {
192                                 delegateQueue.EncryptionRequired = (Mono.Messaging.EncryptionRequired) value;
193                         }
194                 }
195
196                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
197                 [MessagingDescription ("MQ_FormatName")]
198                 public string FormatName {
199                         get {
200                                 throw new NotImplementedException ();
201                         }
202                 }
203
204                 [Browsable (false)]
205                 [DefaultValue (null)]
206                 [TypeConverter (typeof(MessageFormatterConverter))]
207                 [MessagingDescription ("MQ_Formatter")]
208                 public IMessageFormatter Formatter {
209                         get {
210                                 return formatter;
211                         }
212                         set {
213                                 formatter = value;
214                         }
215                 }
216
217                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
218                 [MessagingDescription ("MQ_GuidId")]
219                 public Guid Id {
220                         get {
221                                 return delegateQueue.Id;
222                         }
223                 }
224
225                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
226                 [MessagingDescription ("MQ_Label")]
227                 public string Label {
228                         [MonoTODO]
229                         get {
230                                 //return delegateQueue.Label;
231                                 throw new NotImplementedException ();
232                         }
233                         [MonoTODO]
234                         set {
235                                 //delegateQueue.Label = value;
236                                 throw new NotImplementedException ();
237                         }
238                 }
239
240                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
241                 [MessagingDescription ("MQ_LastModifyTime")]
242                 public DateTime LastModifyTime {
243                         get {
244                                 return delegateQueue.LastModifyTime;
245                         }
246                 }
247
248                 [Browsable (false)]
249                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
250                 [MessagingDescription ("MQ_MachineName")]
251                 public string MachineName {
252                         get {
253                                 return delegateQueue.QRef.Host;
254                         }
255                         set {
256                                 delegateQueue.QRef = delegateQueue.QRef.SetHost (value);
257                         }
258                 }
259
260                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
261                 [TypeConverter (typeof(SizeConverter))]
262                 [MessagingDescription ("MQ_MaximumJournalSize")]
263                 public long MaximumJournalSize {
264                         get {
265                                 return delegateQueue.MaximumJournalSize;
266                         }
267                         set {
268                                 delegateQueue.MaximumJournalSize = value;
269                         }
270                 }
271
272                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
273                 [TypeConverter (typeof(SizeConverter))]
274                 [MessagingDescription ("MQ_MaximumQueueSize")]
275                 public long MaximumQueueSize {
276                         get {
277                                 return delegateQueue.MaximumQueueSize;
278                         }
279                         set {
280                                 delegateQueue.MaximumQueueSize = value;
281                         }
282                 }
283
284                 [Browsable (false)]
285                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
286                 [MessagingDescription ("MQ_MessageReadPropertyFilter")]
287                 public MessagePropertyFilter MessageReadPropertyFilter {
288                         get {
289                                 return messageReadPropertyFilter;
290                         }
291                         set {
292                                 messageReadPropertyFilter = value;
293                         }
294                 }
295
296                 [RecommendedAsConfigurable (true)]
297                 [Editor ("System.Messaging.Design.QueuePathEditor", "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
298                 [Browsable (false)]
299                 [DefaultValue ("")]
300                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
301                 [MessagingDescription ("MQ_Path")]
302                 public string Path {
303                         get {
304                                 return delegateQueue.QRef.ToString ();
305                         }
306                         set {
307                                 delegateQueue.QRef = QueueReference.Parse (value);
308                         }
309                 }
310
311                 [Browsable (false)]
312                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
313                 [MessagingDescription ("MQ_QueueName")]
314                 public string QueueName {
315                         get {
316                                 return delegateQueue.QRef.Queue;
317                         }
318                         set {
319                                 delegateQueue.QRef = delegateQueue.QRef.SetQueue (value);
320                         }
321                 }
322
323                 [Browsable (false)]
324                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
325                 [MessagingDescription ("MQ_ReadHandle")]
326                 public IntPtr ReadHandle {
327                         get {
328                                 return delegateQueue.ReadHandle;
329                         }
330                 }
331
332                 [Browsable (false)]
333                 [DefaultValue (null)]
334                 [MessagingDescription ("MQ_SynchronizingObject")]
335                 public ISynchronizeInvoke SynchronizingObject {
336                         get {
337                                 return delegateQueue.SynchronizingObject;
338                         }
339                         set {
340                                 delegateQueue.SynchronizingObject = value;
341                         }
342                 }
343
344                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
345                 [MessagingDescription ("MQ_Transactional")]
346                 public bool Transactional {
347                         get {
348                                 return delegateQueue.Transactional;
349                         }
350                 }
351
352                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
353                 [MessagingDescription ("MQ_WriteHandle")]
354                 public bool UseJournalQueue {
355                         get {
356                                 return delegateQueue.UseJournalQueue;
357                         }
358                         set {
359                                 delegateQueue.UseJournalQueue = value;
360                         }
361                 }
362
363                 [Browsable (false)]
364                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
365                 [MessagingDescription ("MQ_WriteHandle")]
366                 public IntPtr WriteHandle {
367                         get {
368                                 return delegateQueue.WriteHandle;
369                         }
370                 }
371                 
372                 internal IMessageQueue DelegateQueue {
373                         get {
374                                 return delegateQueue;
375                         }
376                 }
377
378                 #endregion //Properties
379
380                 #region Methods
381
382                 public IAsyncResult BeginPeek ()
383                 {
384                         return delegateQueue.BeginPeek ();
385                 }
386
387                 public IAsyncResult BeginPeek (TimeSpan timeout)
388                 {
389                         return delegateQueue.BeginPeek (timeout);
390                 }
391
392                 public IAsyncResult BeginPeek (TimeSpan timeout, object stateObject)
393                 {
394                         return delegateQueue.BeginPeek (timeout, stateObject);
395                 }
396
397                 public IAsyncResult BeginPeek (TimeSpan timeout,
398                                                                           object stateObject,
399                                                                           AsyncCallback callback)
400                 {
401                         return delegateQueue.BeginPeek (timeout, stateObject, callback);
402                 }               
403
404                 public IAsyncResult BeginReceive ()
405                 {
406                         return delegateQueue.BeginReceive ();
407                 }
408
409                 public IAsyncResult BeginReceive (TimeSpan timeout)
410                 {
411                         return delegateQueue.BeginReceive (timeout);
412                 }
413
414                 public IAsyncResult BeginReceive (TimeSpan timeout, object stateObject)
415                 {
416                         return delegateQueue.BeginReceive (timeout, stateObject);
417                 }
418
419                 public IAsyncResult BeginReceive (TimeSpan timeout, object stateObject, AsyncCallback callback)
420                 {
421                         return delegateQueue.BeginReceive (timeout, stateObject, callback);
422                 }
423                 [MonoTODO]
424                 public static void ClearConnectionCache ()
425                 {
426                         throw new NotImplementedException ();
427                 }
428
429                 public void Close ()
430                 {
431                         delegateQueue.Close ();
432                 }
433
434                 public static MessageQueue Create (string path)
435                 {
436                         QueueReference qRef = QueueReference.Parse (path);
437                         IMessageQueue iMessageQueue = CreateMessageQueue (qRef, false);
438                         return new MessageQueue (iMessageQueue);
439                 }
440
441                 public static MessageQueue Create (string path, bool transactional)
442                 {
443                         QueueReference qRef = QueueReference.Parse (path);
444                         IMessageQueue iMessageQueue = CreateMessageQueue (qRef,
445                                                                           transactional);
446                         return new MessageQueue (iMessageQueue);
447                 }
448
449                 public static void Delete (string path)
450                 {
451                         QueueReference qRef = QueueReference.Parse (path);
452                         MessagingProviderLocator.GetProvider ().DeleteQueue (qRef);
453                 }
454
455                 public Message EndPeek (IAsyncResult asyncResult)
456                 {
457                         if (asyncResult == null)
458                                 throw new ArgumentNullException ();
459                         
460                         try {                           
461                                 IMessage iMsg = delegateQueue.EndPeek (asyncResult);
462                                 if (iMsg == null)
463                                         return null;
464                                 
465                                 return new Message (iMsg, null, Formatter);
466                                 
467                         } catch (ConnectionException e) {
468                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
469                         } catch (MessageUnavailableException e) {
470                                 throw new InvalidOperationException (e.Message, e);
471                         } catch (MonoMessagingException e) {
472                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
473                         }
474                 }
475                 
476                 public Message EndReceive (IAsyncResult asyncResult)
477                 {
478                         if (asyncResult == null)
479                                 throw new ArgumentNullException ();
480                         
481                         try {                           
482                                 IMessage iMsg = delegateQueue.EndReceive (asyncResult);
483                                 if (iMsg == null)
484                                         return null;
485                                 
486                                 return new Message (iMsg, null, Formatter);
487                                 
488                         } catch (ConnectionException e) {
489                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
490                         } catch (MessageUnavailableException e) {
491                                 throw new InvalidOperationException (e.Message, e);
492                         } catch (MonoMessagingException e) {
493                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
494                         }
495                 }
496
497                 public static bool Exists (string path)
498                 {
499                         return Exists (QueueReference.Parse (path));
500                 }
501                 [MonoTODO]
502                 public Message[] GetAllMessages ()
503                 {
504                         throw new NotImplementedException ();
505                 }
506
507                 public IEnumerator GetEnumerator ()
508                 {
509                         return GetMessageEnumerator ();
510                 }
511                 [MonoTODO]
512                 public static Guid GetMachineId (string machineName)
513                 {
514                         throw new NotImplementedException ();
515                 }
516
517                 public MessageEnumerator GetMessageEnumerator ()
518                 {
519                         return new MessageEnumerator (delegateQueue.GetMessageEnumerator (), Formatter);
520                 }
521                 [MonoTODO]
522                 public static MessageQueueEnumerator GetMessageQueueEnumerator ()
523                 {
524                         throw new NotImplementedException ();
525                 }
526                 [MonoTODO]
527                 private static ArrayList filteredQueueList (MessageQueueCriteria criteria)
528                 {
529                         throw new NotImplementedException ();
530                 }
531                 [MonoTODO]
532                 public static MessageQueueEnumerator GetMessageQueueEnumerator (MessageQueueCriteria criteria)
533                 {
534                         throw new NotImplementedException ();
535                 }
536                 [MonoTODO]
537                 public static MessageQueue[] GetPrivateQueuesByMachine (string machineName)
538                 {
539                         throw new NotImplementedException ();
540                 }
541
542                 public static MessageQueue[] GetPublicQueues ()
543                 {
544                         IMessagingProvider provider = MessagingProviderLocator.GetProvider ();
545                         IMessageQueue[] imqs = provider.GetPublicQueues ();
546                         MessageQueue[] mqs = new MessageQueue[imqs.Length];
547                         for (int i = 0; i < imqs.Length; i++)
548                                 mqs[i] = new MessageQueue (imqs[i]);
549                         return mqs;
550                 }
551                 [MonoTODO]
552                 public static MessageQueue[] GetPublicQueues (MessageQueueCriteria criteria)
553                 {
554                         throw new NotImplementedException ();
555                 }
556                 [MonoTODO]
557                 public static MessageQueue[] GetPublicQueuesByCategory (Guid category)
558                 {
559                         throw new NotImplementedException ();
560                 }
561                 [MonoTODO]
562                 public static MessageQueue[] GetPublicQueuesByLabel (string label)
563                 {
564                         throw new NotImplementedException ();
565                 }
566                 [MonoTODO]
567                 public static MessageQueue[] GetPublicQueuesByMachine (string machineName)
568                 {
569                         throw new NotImplementedException ();
570                 }
571
572                 public Message Peek ()
573                 {
574                         try {
575                                 IMessage iMsg = delegateQueue.Peek ();
576                                 if (iMsg == null)
577                                         return null;
578                                 
579                                 return new Message (iMsg, null, Formatter);
580                                 
581                         } catch (ConnectionException e) {
582                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
583                         } catch (MessageUnavailableException e) {
584                                 throw new InvalidOperationException (e.Message, e);
585                         } catch (MonoMessagingException e) {
586                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
587                         }                       
588                 }
589
590                 public Message Peek (TimeSpan timeout)
591                 {
592                         try {
593                                 IMessage iMsg = delegateQueue.Peek (timeout);
594                                 if (iMsg == null)
595                                         return null;
596                                 
597                                 return new Message (iMsg, null, Formatter);
598                                 
599                         } catch (ConnectionException e) {
600                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
601                         } catch (MessageUnavailableException e) {
602                                 throw new InvalidOperationException (e.Message, e);
603                         } catch (MonoMessagingException e) {
604                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
605                         }                       
606                 }
607
608                 public Message PeekByCorrelationId (string correlationId)
609                 {
610                         try {
611                                 IMessage iMsg = delegateQueue.PeekByCorrelationId (correlationId);
612                                 if (iMsg == null)
613                                         return null;
614                                 
615                                 return new Message (iMsg, null, Formatter);
616                                 
617                         } catch (ConnectionException e) {
618                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
619                         } catch (MessageUnavailableException e) {
620                                 throw new InvalidOperationException (e.Message, e);
621                         } catch (MonoMessagingException e) {
622                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
623                         }                       
624                 }
625
626                 public Message PeekByCorrelationId (string correlationId, TimeSpan timeout)
627                 {
628                         try {
629                                 IMessage iMsg = delegateQueue.PeekByCorrelationId (correlationId, timeout);
630                                 if (iMsg == null)
631                                         return null;
632                                 
633                                 return new Message (iMsg, null, Formatter);
634                                 
635                         } catch (ConnectionException e) {
636                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
637                         } catch (MessageUnavailableException e) {
638                                 throw new InvalidOperationException (e.Message, e);
639                         } catch (MonoMessagingException e) {
640                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
641                         }                       
642                 }
643
644                 public Message PeekById (string id)
645                 {
646                         try {
647                                 IMessage iMsg = delegateQueue.PeekById (id);
648                                 if (iMsg == null)
649                                         return null;
650                                 
651                                 return new Message (iMsg, null, Formatter);
652                                 
653                         } catch (ConnectionException e) {
654                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
655                         } catch (MessageUnavailableException e) {
656                                 throw new InvalidOperationException (e.Message, e);
657                         } catch (MonoMessagingException e) {
658                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
659                         }                       
660                 }
661
662                 public Message PeekById (string id, TimeSpan timeout)
663                 {
664                         try {
665                                 IMessage iMsg = delegateQueue.PeekById (id, timeout);
666                                 if (iMsg == null)
667                                         return null;
668                                 
669                                 return new Message (iMsg, null, Formatter);
670                                 
671                         } catch (ConnectionException e) {
672                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
673                         } catch (MessageUnavailableException e) {
674                                 throw new InvalidOperationException (e.Message, e);
675                         } catch (MonoMessagingException e) {
676                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
677                         }                       
678                 }
679
680                 public void Purge ()
681                 {
682                         delegateQueue.Purge ();
683                 }
684
685                 public Message Receive ()
686                 {
687                         try {
688                                 IMessage iMsg = delegateQueue.Receive ();
689                                 if (iMsg == null)
690                                         return null;
691                                 
692                                 return new Message (iMsg, null, Formatter);
693                                 
694                         } catch (ConnectionException e) {
695                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
696                         } catch (MonoMessagingException e) {
697                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
698                         }
699                 }
700
701                 public Message Receive (MessageQueueTransaction transaction)
702                 {
703                         try {
704                                 IMessage iMsg = delegateQueue.Receive (transaction.DelegateTx);
705                                 if (iMsg == null)
706                                         return null;
707                                 
708                                 return new Message (iMsg, null, Formatter);
709                                 
710                         } catch (ConnectionException e) {
711                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
712                         } catch (MessageUnavailableException e) {
713                                 throw new InvalidOperationException (e.Message, e);
714                         } catch (MonoMessagingException e) {
715                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
716                         }
717                 }
718
719                 public Message Receive (MessageQueueTransactionType transactionType)
720                 {
721                         try {
722                                 IMessage iMsg = delegateQueue.Receive ((Mono.Messaging.MessageQueueTransactionType) transactionType);
723                                 if (iMsg == null)
724                                         return null;
725                                 
726                                 return new Message (iMsg, null, Formatter);
727                                 
728                         } catch (ConnectionException e) {
729                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
730                         } catch (MessageUnavailableException e) {
731                                 throw new InvalidOperationException (e.Message, e);
732                         } catch (MonoMessagingException e) {
733                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
734                         }
735                 }
736                 
737                 public Message Receive (TimeSpan timeout)
738                 {
739                         try {
740                                 IMessage iMsg = delegateQueue.Receive (timeout);
741                                 if (iMsg == null)
742                                         return null;
743                                 
744                                 return new Message (iMsg, null, Formatter);
745                                 
746                         } catch (ConnectionException e) {
747                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
748                         } catch (MessageUnavailableException e) {
749                                 throw new InvalidOperationException (e.Message, e);
750                         } catch (MonoMessagingException e) {
751                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
752                         }
753                 }
754
755                 public Message Receive (TimeSpan timeout, MessageQueueTransaction transaction)
756                 {
757                         try {
758                                 IMessage iMsg = delegateQueue.Receive (timeout, transaction.DelegateTx);
759                                 if (iMsg == null)
760                                         return null;
761                                 
762                                 return new Message (iMsg, null, Formatter);
763                                 
764                         } catch (ConnectionException e) {
765                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
766                         } catch (MessageUnavailableException e) {
767                                 throw new InvalidOperationException (e.Message, e);
768                         } catch (MonoMessagingException e) {
769                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
770                         }
771                 }
772
773                 public Message Receive (TimeSpan timeout, MessageQueueTransactionType transactionType)
774                 {
775                         try {
776                                 IMessage iMsg = delegateQueue.Receive (timeout, 
777                                                                        (Mono.Messaging.MessageQueueTransactionType) transactionType);
778                                 if (iMsg == null)
779                                         return null;
780                                 
781                                 return new Message (iMsg, null, Formatter);
782                                 
783                         } catch (ConnectionException e) {
784                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
785                         } catch (MessageUnavailableException e) {
786                                 throw new InvalidOperationException (e.Message, e);
787                         } catch (MonoMessagingException e) {
788                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
789                         }
790                 }
791
792                 public Message ReceiveByCorrelationId (string correlationId)
793                 {
794                         try {
795                                 IMessage iMsg = delegateQueue.ReceiveByCorrelationId (correlationId);
796                                 if (iMsg == null)
797                                         return null;
798                                 
799                                 return new Message (iMsg, null, Formatter);
800
801                         } catch (ConnectionException e) {
802                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
803                         } catch (MessageUnavailableException e) {
804                                 throw new InvalidOperationException (e.Message, e);
805                         } catch (MonoMessagingException e) {
806                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
807                         }
808                 }
809
810                 public Message ReceiveByCorrelationId (string correlationId, MessageQueueTransaction transaction)
811                 {
812                         try {
813                                 IMessage iMsg = delegateQueue.ReceiveByCorrelationId (correlationId, transaction.DelegateTx);
814                                 if (iMsg == null)
815                                         return null;
816                                 
817                                 return new Message (iMsg, null, Formatter);
818                                 
819                         } catch (ConnectionException e) {
820                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
821                         } catch (MessageUnavailableException e) {
822                                 throw new InvalidOperationException (e.Message, e);
823                         } catch (MonoMessagingException e) {
824                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
825                         }                       
826                 }
827
828                 public Message ReceiveByCorrelationId (string correlationId, MessageQueueTransactionType transactionType)
829                 {
830                         try {
831                                 IMessage iMsg = delegateQueue.ReceiveByCorrelationId (correlationId, (Mono.Messaging.MessageQueueTransactionType) transactionType);
832                                 if (iMsg == null)
833                                         return null;
834
835                                 return new Message (iMsg, null, Formatter);
836
837                         } catch (ConnectionException e) {
838                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
839                         } catch (MessageUnavailableException e) {
840                                 throw new InvalidOperationException (e.Message, e);
841                         } catch (MonoMessagingException e) {
842                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
843                         }
844                 }
845
846                 public Message ReceiveByCorrelationId (string correlationId, TimeSpan timeout)
847                 {
848                         try {
849                                 IMessage iMsg = delegateQueue.ReceiveByCorrelationId (correlationId, 
850                                                                                       timeout);
851                                 if (iMsg == null)
852                                         return null;
853                                 
854                                 return new Message (iMsg, null, Formatter);
855                                 
856                         } catch (ConnectionException e) {
857                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
858                         } catch (MessageUnavailableException e) {
859                                 throw new InvalidOperationException (e.Message, e);
860                         } catch (MonoMessagingException e) {
861                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
862                         }
863                 }
864
865                 public Message ReceiveByCorrelationId (string correlationId, TimeSpan timeout, MessageQueueTransaction transaction)
866                 {
867                         try {
868                                 IMessage iMsg = delegateQueue.ReceiveByCorrelationId (correlationId, timeout, transaction.DelegateTx);
869                                 if (iMsg == null)
870                                         return null;
871
872                                 return new Message (iMsg, null, Formatter);
873
874                         } catch (ConnectionException e) {
875                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
876                         } catch (MessageUnavailableException e) {
877                                 throw new InvalidOperationException (e.Message, e);
878                         } catch (MonoMessagingException e) {
879                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
880                         }
881                 }
882
883                 public Message ReceiveByCorrelationId (string correlationId, TimeSpan timeout, MessageQueueTransactionType transactionType)
884                 {
885                         try {
886                                 IMessage iMsg = delegateQueue.ReceiveByCorrelationId (correlationId, timeout, (Mono.Messaging.MessageQueueTransactionType) transactionType);
887                                 if (iMsg == null)
888                                         return null;
889
890                                 return new Message (iMsg, null, Formatter);
891
892                         } catch (ConnectionException e) {
893                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
894                         } catch (MessageUnavailableException e) {
895                                 throw new InvalidOperationException (e.Message, e);
896                         } catch (MonoMessagingException e) {
897                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
898                         }
899                 }
900
901                 public Message ReceiveById (string id)
902                 {
903                         try {
904                                 IMessage iMsg = delegateQueue.ReceiveById (id);
905                                 if (iMsg == null)
906                                         return null;
907
908                                 return new Message (iMsg, null, Formatter);
909
910                         } catch (ConnectionException e) {
911                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
912                         } catch (MessageUnavailableException e) {
913                                 throw new InvalidOperationException (e.Message, e);
914                         } catch (MonoMessagingException e) {
915                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
916                         }
917                 }
918
919                 public Message ReceiveById (string id, MessageQueueTransaction transaction)
920                 {
921                         try {
922                                 IMessage iMsg = delegateQueue.ReceiveById (id, transaction.DelegateTx);
923                                 if (iMsg == null)
924                                         return null;
925
926                                 return new Message (iMsg, null, Formatter);
927
928                         } catch (ConnectionException e) {
929                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
930                         } catch (MessageUnavailableException e) {
931                                 throw new InvalidOperationException (e.Message, e);
932                         } catch (MonoMessagingException e) {
933                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
934                         }
935                 }
936
937                 public Message ReceiveById (string id, MessageQueueTransactionType transactionType)
938                 {
939                         try {
940                                 IMessage iMsg = delegateQueue.ReceiveById (id, (Mono.Messaging.MessageQueueTransactionType) transactionType);
941                                 if (iMsg == null)
942                                         return null;
943
944                                 return new Message (iMsg, null, Formatter);
945
946                         } catch (ConnectionException e) {
947                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
948                         } catch (MessageUnavailableException e) {
949                                 throw new InvalidOperationException (e.Message, e);
950                         } catch (MonoMessagingException e) {
951                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
952                         }
953                 }
954
955                 public Message ReceiveById (string id, TimeSpan timeout)
956                 {
957                         try {
958                                 IMessage iMsg = delegateQueue.ReceiveById (id, timeout);
959                                 if (iMsg == null)
960                                         return null;
961
962                                 return new Message (iMsg, null, Formatter);
963
964                         } catch (ConnectionException e) {
965                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
966                         } catch (MessageUnavailableException e) {
967                                 throw new InvalidOperationException (e.Message, e);
968                         } catch (MonoMessagingException e) {
969                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
970                         }
971                 }
972
973                 public Message ReceiveById (string id, TimeSpan timeout, MessageQueueTransaction transaction)
974                 {
975                         try {
976                                 IMessage iMsg = delegateQueue.ReceiveById (id, timeout, transaction.DelegateTx);
977                                 if (iMsg == null)
978                                         return null;
979
980                                 return new Message (iMsg, null, Formatter);
981
982                         } catch (ConnectionException e) {
983                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
984                         } catch (MessageUnavailableException e) {
985                                 throw new InvalidOperationException (e.Message, e);
986                         } catch (MonoMessagingException e) {
987                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
988                         }
989                 }
990
991                 public Message ReceiveById (string id, TimeSpan timeout, MessageQueueTransactionType transactionType)
992                 {
993                         try {
994                                 IMessage iMsg = delegateQueue.ReceiveById (id, timeout, (Mono.Messaging.MessageQueueTransactionType) transactionType);
995                                 if (iMsg == null)
996                                         return null;
997
998                                 return new Message (iMsg, null, Formatter);
999
1000                         } catch (ConnectionException e) {
1001                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
1002                         } catch (MessageUnavailableException e) {
1003                                 throw new InvalidOperationException (e.Message, e);
1004                         } catch (MonoMessagingException e) {
1005                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
1006                         }
1007                 }
1008                 [MonoTODO]
1009                 public void Refresh ()
1010                 {
1011                         throw new NotImplementedException ();
1012                 }
1013                 [MonoTODO]
1014                 public void ResetPermissions ()
1015                 {
1016                         throw new NotImplementedException ();
1017                 }
1018
1019                 public void Send (object obj)
1020                 {
1021                         if (typeof (Message) == obj.GetType ()) {
1022                                 Message m = (Message) obj;
1023                                 if (m.BodyStream == null) {
1024                                         IMessageFormatter f = (m.Formatter == null) ? Formatter : m.Formatter;
1025                                         f.Write (m, m.Body);
1026                                 }
1027
1028                                 try {
1029                                         delegateQueue.Send (m.DelegateMessage);
1030                                 } catch (ConnectionException e) {
1031                                         throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
1032                                 } catch (MonoMessagingException e) {
1033                                         throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
1034                                 }
1035                         } else {
1036                                 Message m = new Message (obj);
1037                                 Send (m);
1038                         }
1039                 }
1040
1041                 public void Send (object obj, MessageQueueTransaction transaction)
1042                 {
1043                         if (typeof (Message) == obj.GetType ()) {
1044                                 Message m = (Message) obj;
1045                                 if (m.BodyStream == null) {
1046                                         IMessageFormatter f = (m.Formatter == null) ? Formatter : m.Formatter;
1047                                         f.Write (m, m.Body);
1048                                 }
1049
1050                                 try {
1051                                         delegateQueue.Send (m.DelegateMessage, transaction.DelegateTx);
1052                                 } catch (ConnectionException e) {
1053                                         throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
1054                                 } catch (MonoMessagingException e) {
1055                                         throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
1056                                 }
1057                         } else {
1058                                 Message m = new Message (obj);
1059                                 Send (m, transaction);
1060                         }
1061                 }
1062
1063                 public void Send (object obj, MessageQueueTransactionType transactionType)
1064                 {
1065                         if (typeof (Message) == obj.GetType ()) {
1066                                 Message m = (Message) obj;
1067                                 if (m.BodyStream == null) {
1068                                         IMessageFormatter f = (m.Formatter == null) ? Formatter : m.Formatter;
1069                                         f.Write (m, m.Body);
1070                                 }
1071
1072                                 try {
1073                                         delegateQueue.Send (m.DelegateMessage, (Mono.Messaging.MessageQueueTransactionType) transactionType);
1074                                 } catch (ConnectionException e) {
1075                                         throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
1076                                 } catch (MonoMessagingException e) {
1077                                         throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
1078                                 }
1079                         } else {
1080                                 Message m = new Message (obj);
1081                                 Send (m, transactionType);
1082                         }
1083                 }
1084
1085                 public void Send (object obj, string label)
1086                 {
1087                         if (typeof (Message) == obj.GetType ()) {
1088                                 Message m = (Message) obj;
1089                                 m.Label = label;
1090                                 
1091                                 Send (m);
1092                         } else {
1093                                 Message m = new Message (obj);
1094                                 Send (m, label);
1095                         }
1096                 }
1097
1098                 public void Send (object obj, string label, MessageQueueTransaction transaction)
1099                 {
1100                         if (typeof (Message) == obj.GetType ()) {
1101                                 Message m = (Message) obj;
1102                                 m.Label = label;
1103                                 
1104                                 if (m.BodyStream == null) {
1105                                         IMessageFormatter f = (m.Formatter == null) ? Formatter : m.Formatter;
1106                                         f.Write (m, m.Body);
1107                                 }
1108
1109                                 try {
1110                                         delegateQueue.Send (m.DelegateMessage, transaction.DelegateTx);
1111                                 } catch (ConnectionException e) {
1112                                         throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
1113                                 } catch (MonoMessagingException e) {
1114                                         throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
1115                                 }
1116                         } else {
1117                                 Message m = new Message (obj);
1118                                 Send (m, label, transaction);
1119                         }
1120                 }
1121
1122                 public void Send (object obj, string label, MessageQueueTransactionType transactionType)
1123                 {
1124                         if (typeof (Message) == obj.GetType ()) {
1125                                 Message m = (Message) obj;
1126                                 m.Label = label;
1127                                 Send (m, transactionType);
1128                         } else {
1129                                 Message m = new Message (obj);
1130                                 Send (m, label, transactionType);
1131                         }
1132                 }
1133                 [MonoTODO]
1134                 public void SetPermissions (AccessControlList dacl)
1135                 {
1136                         throw new NotImplementedException ();
1137                 }
1138                 [MonoTODO]
1139                 public void SetPermissions (MessageQueueAccessControlEntry ace)
1140                 {
1141                         throw new NotImplementedException ();
1142                 }
1143                 [MonoTODO]
1144                 public void SetPermissions (string user, MessageQueueAccessRights rights)
1145                 {
1146                         throw new NotImplementedException ();
1147                 }
1148                 [MonoTODO]
1149                 public void SetPermissions (string user, MessageQueueAccessRights rights, AccessControlEntryType entryType)
1150                 {
1151                         throw new NotImplementedException ();
1152                 }
1153
1154                 protected override void Dispose (bool disposing)
1155                 {
1156                         //delegateQueue.Dispose ();
1157                 }
1158
1159                 #endregion //Methods
1160
1161                 [MessagingDescription ("MQ_PeekCompleted")]
1162                 public event PeekCompletedEventHandler PeekCompleted;
1163                 
1164                 private void DelegatePeekCompleted (object sender, CompletedEventArgs args)
1165                 {
1166                         if (PeekCompleted == null)
1167                                 return;
1168                         
1169                         PeekCompletedEventArgs newArgs = new PeekCompletedEventArgs (this, args.AsyncResult);                   
1170                         PeekCompleted (sender, newArgs);
1171                 }
1172
1173                 [MessagingDescription ("MQ_ReceiveCompleted")]
1174                 public event ReceiveCompletedEventHandler ReceiveCompleted;
1175                 
1176                 private void DelegateReceiveCompleted (object sender, CompletedEventArgs args)
1177                 {
1178                         if (ReceiveCompleted == null)
1179                                 return;
1180                         
1181                         ReceiveCompletedEventArgs newArgs = new ReceiveCompletedEventArgs (this, args.AsyncResult);                     
1182                         ReceiveCompleted (sender, newArgs);
1183                 }
1184                 
1185                 private static IMessageQueue GetMessageQueue (string path)
1186                 {
1187                         QueueReference qRef = QueueReference.Parse (path);
1188                         IMessageQueue q = MessagingProviderLocator
1189                                 .GetProvider ()
1190                                 .GetMessageQueue (qRef);
1191                         return q;
1192                 }
1193                 
1194                 private static IMessageQueue GetMessageQueue ()
1195                 {
1196                         return MessagingProviderLocator.GetProvider ()
1197                                 .GetMessageQueue (QueueReference.DEFAULT);
1198                 }
1199                 
1200                 private static IMessageQueue CreateMessageQueue (QueueReference qRef,
1201                                                                  bool transactional)
1202                 {
1203                         return MessagingProviderLocator.GetProvider ()
1204                                 .CreateMessageQueue (qRef, transactional);
1205                 }
1206                 
1207                 private static bool Exists (QueueReference qRef)
1208                 {
1209                         return MessagingProviderLocator.GetProvider ().Exists (qRef);
1210                 }               
1211         }
1212 }