e9770b65c374f36ac54e3cc9160cf6654ef4ba9e
[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
45                 public QueueReference (string host, string queue, bool isPrivate)
46                 {
47                         this.host = host;
48                         this.isPrivate = isPrivate;
49                         this.queue = queue;
50                 }
51                 
52                 public string Host {
53                         get { 
54                                 if (host == LOCALHOST) 
55                                         return "localhost";
56                                 else
57                                         return host;
58                         }
59                 }
60                 
61                 public string Queue {
62                         get { 
63                                 if (isPrivate)
64                                         return @"private$\" + queue;
65                                 else
66                                         return queue;
67                         }
68                 }
69                 
70                 public bool IsPrivate {
71                         get { return isPrivate; }
72                 }
73                 
74                 public QueueReference SetHost (string host)
75                 {
76                         return new QueueReference (host, this.queue, this.isPrivate);
77                 }
78                 
79                 public QueueReference SetQueue (string queue)
80                 {
81                         return new QueueReference (this.host, queue, this.isPrivate);
82                 }
83
84
85                 public override bool Equals (object other)
86                 {
87                         Console.Write("Equals Called\n");
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$" == 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\\" + 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