1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007, 2008 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd.,
39 // Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
41 // Portions created by LShift Ltd., Cohesive Financial Technologies
42 // LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007, 2008
43 // LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
46 // All Rights Reserved.
48 // Contributor(s): ______________________________________.
50 //---------------------------------------------------------------------------
52 using System.Reflection;
53 using System.Configuration;
55 namespace RabbitMQ.Client
57 ///<summary>Concrete, predefined IProtocol instances ready for use
58 ///with ConnectionFactory.</summary>
61 /// Applications will in the common case use the FromEnvironment()
62 /// method to search a fallback-chain of configuration sources for
63 /// the IProtocol instance to use. However, in some cases, the
64 /// default fallback-chain is not appropriate; in these cases,
65 /// other methods such as FromConfiguration(string) or
66 /// SafeLookup(string) may suffice.
69 public class Protocols
71 // Hide the constructor - no instances of Protocols needed.
72 // We'd make this class static, but for MS's .NET 1.1 compilers
73 private Protocols() {}
75 ///<summary>The default App.config appSettings key used by
76 ///FromConfiguration and FromEnvironment. At the time of
77 ///writing, "AMQP_PROTOCOL".</summary>
78 public readonly static string DefaultAppSettingsKey = "AMQP_PROTOCOL";
80 ///<summary>The environment variable read by
81 ///FromEnvironmentVariable() and FromEnvironment(). At the
82 ///time of writing, "AMQP_PROTOCOL".</summary>
83 public readonly static string EnvironmentVariable = "AMQP_PROTOCOL";
85 ///<summary>Protocol version 0-8 as standardised.</summary>
86 public static IProtocol AMQP_0_8
88 get { return new RabbitMQ.Client.Framing.v0_8.Protocol(); }
90 ///<summary>Protocol version 0-8, as modified by QPid.</summary>
91 public static IProtocol AMQP_0_8_QPID
93 get { return new RabbitMQ.Client.Framing.v0_8qpid.Protocol(); }
95 ///<summary>Protocol version 0-9 as standardised (omitting
96 ///sections marked "WIP", "work in progress", including in
97 ///particular the Message class of operations).</summary>
98 public static IProtocol AMQP_0_9
100 get { return new RabbitMQ.Client.Framing.v0_9.Protocol(); }
103 ///<summary>Retrieve the current default protocol variant
104 ///(currently AMQP_0_8)</summary>
105 public static IProtocol DefaultProtocol
107 get { return AMQP_0_8; }
110 ///<summary>Low-level method for retrieving a protocol version
111 ///by name (of one of the static properties on this
115 /// Returns null if no suitable property could be found.
118 /// In many cases, FromEnvironment() will be a more
119 /// appropriate method for applications to call; this method
120 /// is provided for cases where the caller wishes to know the
121 /// answer to the question "does a suitable IProtocol property
122 /// with this name exist, and if so, what is its value?"
125 public static IProtocol Lookup(string name)
127 PropertyInfo pi = typeof(Protocols).GetProperty(name,
128 BindingFlags.Public |
129 BindingFlags.Static);
134 return pi.GetValue(null, new object[0]) as IProtocol;
137 ///<summary>Retrieve a protocol version by name (of one of the
138 ///static properties on this class)</summary>
141 /// If the argument is null, Protocols.DefaultProtocol is
142 /// used. If the protocol variant named is not found,
143 /// ConfigurationException is thrown.
146 /// In many cases, FromEnvironment() will be a more
147 /// appropriate method for applications to call; this method
148 /// is provided for cases where the caller wishes to know the
149 /// answer to the question "does a suitable IProtocol property
150 /// with this name exist, and if so, what is its value?", with
151 /// the additional guarantee that if a suitable property does
152 /// not exist, a ConfigurationException will be thrown.
155 ///<exception cref="ConfigurationException"/>
156 public static IProtocol SafeLookup(string name)
160 IProtocol p = Lookup(name);
167 throw new ConfigurationException("Unsupported protocol variant name: " + name);
170 return DefaultProtocol;
173 private static string ReadEnvironmentVariable()
175 return Environment.GetEnvironmentVariable(EnvironmentVariable);
178 ///<summary>Uses the process environment variable
179 ///<code>EnvironmentVariable</code> to retrieve an IProtocol
180 ///instance.</summary>
182 ///If the environment variable is unset,
183 ///Protocols.DefaultProtocol is used. If the protocol variant
184 ///named is not found, ConfigurationException is thrown.
186 ///<exception cref="ConfigurationException"/>
187 public static IProtocol FromEnvironmentVariable()
189 return SafeLookup(ReadEnvironmentVariable());
192 ///<summary>Uses App.config's appSettings section to retrieve
193 ///an IProtocol instance.</summary>
195 ///If the appSettings key is missing,
196 ///Protocols.DefaultProtocol is used. If the protocol variant
197 ///named is not found, ConfigurationException is thrown.
199 ///<exception cref="ConfigurationException"/>
200 public static IProtocol FromConfiguration(string appSettingsKey)
202 // FIXME: ConfigurationSettings.AppSettings is
203 // obsolete. Use ConfigurationManager.AppSettings once we
204 // decide that supporting .NET 1.1 is no longer required.
205 string name = ConfigurationSettings.AppSettings[appSettingsKey];
206 return SafeLookup(name);
209 ///<summary>Returns FromConfiguration(DefaultAppSettingsKey).</summary>
210 public static IProtocol FromConfiguration()
212 return FromConfiguration(DefaultAppSettingsKey);
215 ///<summary>Tries FromConfiguration() first, followed by
216 ///FromEnvironmentVariable() if no setting was found in the
217 ///App.config.</summary>
218 ///<exception cref="ConfigurationException"/>
219 public static IProtocol FromEnvironment(string appSettingsKey)
221 // FIXME: ConfigurationSettings.AppSettings is
222 // obsolete. Use ConfigurationManager.AppSettings once we
223 // decide that supporting .NET 1.1 is no longer required.
224 string name = ConfigurationSettings.AppSettings[appSettingsKey];
226 name = ReadEnvironmentVariable();
228 return SafeLookup(name);
231 ///<summary>Returns FromEnvironment(DefaultAppSettingsKey).</summary>
232 public static IProtocol FromEnvironment()
234 return FromEnvironment(DefaultAppSettingsKey);