5 // Michael Barker (mike@middlesoft.co.uk)
7 // (C) 2008 Michael Barker
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Text.RegularExpressions;
34 namespace Mono.Messaging
36 public sealed class QueueReference
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$";
46 public QueueReference (string host, string queue, bool isPrivate)
49 this.isPrivate = isPrivate;
55 if (host == LOCALHOST)
65 return PRIVATE_STR + @"\" + queue;
71 public bool IsPrivate {
72 get { return isPrivate; }
75 public QueueReference SetHost (string host)
77 return new QueueReference (host, this.queue, this.isPrivate);
80 public QueueReference SetQueue (string queue)
82 return new QueueReference (this.host, queue, this.isPrivate);
86 public override bool Equals (object other)
90 else if (typeof (QueueReference) != other.GetType ())
93 QueueReference qr = (QueueReference) other;
98 public bool Equals (QueueReference other)
100 return host == other.host
101 && isPrivate == other.isPrivate
102 && queue == other.queue;
105 public override int GetHashCode ()
107 return queue == null ? 0 : queue.GetHashCode () + host.GetHashCode ();
110 public static QueueReference Parse (string path)
112 string trimedPath = RemoveLeadingSlashes (path);
113 string[] parts = trimedPath.Split (DELIM, 3);
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);
122 return new QueueReference (parts[0], parts[2], IsPrivateStr (parts[1]));
126 public static bool IsPrivateStr (string s)
128 return PRIVATE_STR == s.ToLower ();
131 public static string RemoveLeadingSlashes (string s)
134 while (idx < s.Length && (s[idx] == '\\'))
136 return s.Substring (idx);
139 public override string ToString ()
142 return Host + "\\" + PRIVATE_STR + "\\" + queue;
144 return Host + "\\" + Queue;
148 public static bool operator == (QueueReference a, QueueReference b)
153 public static bool operator != (QueueReference a, QueueReference b)
155 return !a.Equals (b);