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