2009-05-23 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Messaging / System.Messaging / MessageEnumerator.cs
1 //
2 // System.Messaging
3 //
4 // Authors:
5 //      Peter Van Isacker (sclytrack@planetinternet.be)
6 //      Michael Barker (mike@middlesoft.co.uk)
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
35 using Mono.Messaging;
36
37 namespace System.Messaging 
38 {
39         public class MessageEnumerator: MarshalByRefObject, IEnumerator, IDisposable 
40         {
41                 private IMessageEnumerator delegateEnumerator;
42                 private IMessageFormatter formatter;
43                 
44                 internal MessageEnumerator (IMessageEnumerator delegateEnumerator, IMessageFormatter formatter)
45                 {
46                         this.delegateEnumerator = delegateEnumerator;
47                         this.formatter = formatter;
48                 }
49
50                 public Message Current {
51                         get {
52                                 IMessage iMsg = delegateEnumerator.Current;
53                                 if (iMsg == null)
54                                         return null;
55                                 
56                                 return new Message (iMsg, null, formatter);
57                         }
58                 }
59                 
60                 object IEnumerator.Current {
61                         get { return Current; }
62                 }
63                 
64                 public IntPtr CursorHandle {
65                         get { return delegateEnumerator.CursorHandle; }
66                 }
67
68                 public void Close()
69                 {
70                         delegateEnumerator.Close ();
71                 }
72
73                 public void Dispose()
74                 {
75                         Dispose (true);
76                         GC.SuppressFinalize (this);
77                 }
78
79                 protected virtual void Dispose(bool disposing)
80                 {
81                         delegateEnumerator.Dispose ();
82                         Close();
83                 }
84
85
86                 public bool MoveNext()
87                 {
88                         return delegateEnumerator.MoveNext ();
89                 }
90
91                 public bool MoveNext (TimeSpan timeout)
92                 {
93                         return delegateEnumerator.MoveNext (timeout);
94                 }
95                 
96                 public Message RemoveCurrent()
97                 {
98                         try {
99                                 IMessage iMsg = delegateEnumerator.RemoveCurrent ();
100                                 if (iMsg == null)
101                                         return null;
102                                 return new Message (iMsg, null, formatter);
103                                 
104                         } catch (ConnectionException e) {
105                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
106                         } catch (MessageUnavailableException e) {
107                                 throw new InvalidOperationException (e.Message, e);
108                         } catch (MonoMessagingException e) {
109                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
110                         }                       
111                 }
112
113                 public Message RemoveCurrent (MessageQueueTransaction transaction)
114                 {
115                         try {
116                                 IMessage iMsg = delegateEnumerator.RemoveCurrent (transaction.DelegateTx);
117                                 if (iMsg == null)
118                                         return null;
119                                 return new Message (iMsg, null, formatter);
120                                 
121                         } catch (ConnectionException e) {
122                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
123                         } catch (MessageUnavailableException e) {
124                                 throw new InvalidOperationException (e.Message, e);
125                         } catch (MonoMessagingException e) {
126                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
127                         }                       
128                 }
129
130                 public Message RemoveCurrent(MessageQueueTransactionType transactionType)
131                 {
132                         try {
133                                 IMessage iMsg = delegateEnumerator.RemoveCurrent ((Mono.Messaging.MessageQueueTransactionType) transactionType);
134                                 if (iMsg == null)
135                                         return null;
136                                 return new Message (iMsg, null, formatter);
137                                 
138                         } catch (ConnectionException e) {
139                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
140                         } catch (MessageUnavailableException e) {
141                                 throw new InvalidOperationException (e.Message, e);
142                         } catch (MonoMessagingException e) {
143                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
144                         }                       
145                 }
146
147                 public Message RemoveCurrent (TimeSpan timeout)
148                 {
149                         try {
150                                 IMessage iMsg = delegateEnumerator.RemoveCurrent (timeout);
151                                 if (iMsg == null)
152                                         return null;
153                                 return new Message (iMsg, null, formatter);
154                                 
155                         } catch (ConnectionException e) {
156                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
157                         } catch (MessageUnavailableException e) {
158                                 throw new InvalidOperationException (e.Message, e);
159                         } catch (MonoMessagingException e) {
160                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
161                         }                       
162                 }
163
164                 public Message RemoveCurrent (TimeSpan timeout,
165                                               MessageQueueTransaction transaction)
166                 {
167                         try {
168                                 IMessage iMsg = delegateEnumerator.RemoveCurrent (timeout,
169                                                                                   transaction.DelegateTx);
170                                 if (iMsg == null)
171                                         return null;
172                                 return new Message (iMsg, null, formatter);
173                                 
174                         } catch (ConnectionException e) {
175                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
176                         } catch (MessageUnavailableException e) {
177                                 throw new InvalidOperationException (e.Message, e);
178                         } catch (MonoMessagingException e) {
179                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
180                         }                       
181                 }
182
183                 public Message RemoveCurrent (TimeSpan timeout, MessageQueueTransactionType transactionType)
184                 {
185                         try {
186                                 IMessage iMsg = delegateEnumerator.RemoveCurrent (timeout, 
187                                                                                   (Mono.Messaging.MessageQueueTransactionType) transactionType);
188                                 if (iMsg == null)
189                                         return null;
190                                 return new Message (iMsg, null, formatter);
191                                 
192                         } catch (ConnectionException e) {
193                                 throw new MessageQueueException (MessageQueueErrorCode.QueueNotAvailable, e.Message);
194                         } catch (MessageUnavailableException e) {
195                                 throw new InvalidOperationException (e.Message, e);
196                         } catch (MonoMessagingException e) {
197                                 throw new MessageQueueException (MessageQueueErrorCode.Generic, e.Message);
198                         }                       
199                 }
200
201                 public void Reset()
202                 {
203                         Close ();
204                 }
205
206                 ~MessageEnumerator()
207                 {
208                         Dispose(false);
209                 }
210         }
211 }