2009-01-06 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / Mono.Messaging / Mono.Messaging / QueueReference.cs
1 //
2 // Mono.Messaging
3 //
4 // Authors:
5 //              Michael Barker (mike@middlesoft.co.uk)
6 //
7 // (C) 2008 Michael Barker
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Text.RegularExpressions;
33
34 namespace Mono.Messaging
35 {
36         public sealed class QueueReference
37         {
38                 private static readonly char[] DELIM = new char[] { '\\' };
39                 private readonly string host;
40                 private readonly bool isPrivate;
41                 private readonly string queue;
42                 public static readonly string LOCALHOST = ".";
43                 public static readonly QueueReference DEFAULT = new QueueReference (LOCALHOST, null, false);
44                 private static readonly string PRIVATE_STR = "private$";
45
46                 public QueueReference (string host, string queue, bool isPrivate)
47                 {
48                         this.host = host;
49                         this.isPrivate = isPrivate;
50                         this.queue = queue;
51                 }
52                 
53                 public string Host {
54                         get { 
55                                 if (host == LOCALHOST) 
56                                         return "localhost";
57                                 else
58                                         return host;
59                         }
60                 }
61                 
62                 public string Queue {
63                         get { 
64                                 if (isPrivate)
65                                         return PRIVATE_STR + @"\" + queue;
66                                 else
67                                         return queue;
68                         }
69                 }
70                 
71                 public bool IsPrivate {
72                         get { return isPrivate; }
73                 }
74                 
75                 public QueueReference SetHost (string host)
76                 {
77                         return new QueueReference (host, this.queue, this.isPrivate);
78                 }
79                 
80                 public QueueReference SetQueue (string queue)
81                 {
82                         return new QueueReference (this.host, queue, this.isPrivate);
83                 }
84
85
86                 public override bool Equals (object other)
87                 {
88                         if (other == null)
89                                 return false;
90                         else if (typeof (QueueReference) != other.GetType ())
91                                 return false;
92                         else {
93                                 QueueReference qr = (QueueReference) other;
94                                 return Equals (qr);
95                         }
96                 }
97
98                 public bool Equals (QueueReference other)
99                 {
100                         return host == other.host 
101                                 && isPrivate == other.isPrivate
102                                 && queue == other.queue;
103                 }
104                 
105                 public override int GetHashCode ()
106                 {
107                         return queue == null ? 0 : queue.GetHashCode () + host.GetHashCode ();
108                 }
109                 
110                 public static QueueReference Parse (string path)
111                 {
112                         string trimedPath = RemoveLeadingSlashes (path);
113                         string[] parts = trimedPath.Split (DELIM, 3);
114                         
115                         if (parts.Length == 0) {
116                                 throw new ArgumentException ();
117                         } else if (parts.Length == 1) {
118                                 return new QueueReference (QueueReference.LOCALHOST, parts[0], false);
119                         } else if (parts.Length == 2) {
120                                 return new QueueReference (parts[0], parts[1], false);
121                         } else {
122                                 return new QueueReference (parts[0], parts[2], IsPrivateStr (parts[1]));
123                         }
124                 }
125                 
126                 public static bool IsPrivateStr (string s)
127                 {
128                         return PRIVATE_STR == s.ToLower ();
129                 }
130                 
131                 public static string RemoveLeadingSlashes (string s)
132                 {
133                         int idx = 0;
134                         while (idx < s.Length && (s[idx] == '\\'))
135                                 idx++;
136                         return s.Substring (idx);
137                 }
138                 
139                 public override string ToString ()
140                 {
141                         if (IsPrivate) {
142                                 return Host + "\\" + PRIVATE_STR + "\\" + queue;
143                         } else {
144                                 return Host + "\\" + Queue;
145                         }
146                 }
147
148                 public static bool operator == (QueueReference a, QueueReference b)
149                 {
150                         return a.Equals (b);
151                 }
152                 
153                 public static bool operator != (QueueReference a, QueueReference b)
154                 {
155                         return !a.Equals (b);
156                 }
157         }
158 }
159