-
Notifications
You must be signed in to change notification settings - Fork 28
/
NMSConnectionFactory.cs
510 lines (455 loc) · 21.3 KB
/
NMSConnectionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using Apache.NMS.Util;
namespace Apache.NMS
{
/// <summary>
/// Provider implementation mapping class.
/// </summary>
public class ProviderFactoryInfo
{
public string assemblyFileName;
public string factoryClassName;
public ProviderFactoryInfo(string aFileName, string fClassName)
{
assemblyFileName = aFileName;
factoryClassName = fClassName;
}
}
/// <summary>
/// Implementation of a factory for <see cref="IConnection" /> instances.
/// </summary>
public class NMSConnectionFactory : IConnectionFactory
{
protected readonly IConnectionFactory factory;
protected static readonly Dictionary<string, ProviderFactoryInfo> schemaProviderFactoryMap;
/// <summary>
/// Static class constructor
/// </summary>
static NMSConnectionFactory()
{
schemaProviderFactoryMap = new Dictionary<string, ProviderFactoryInfo>();
schemaProviderFactoryMap["activemq"] =
new ProviderFactoryInfo("Apache.NMS.ActiveMQ", "Apache.NMS.ActiveMQ.ConnectionFactory");
schemaProviderFactoryMap["activemqnettx"] = new ProviderFactoryInfo("Apache.NMS.ActiveMQ",
"Apache.NMS.ActiveMQ.NetTxConnectionFactory");
schemaProviderFactoryMap["tcp"] =
new ProviderFactoryInfo("Apache.NMS.ActiveMQ", "Apache.NMS.ActiveMQ.ConnectionFactory");
schemaProviderFactoryMap["ems"] =
new ProviderFactoryInfo("Apache.NMS.EMS", "Apache.NMS.EMS.ConnectionFactory");
schemaProviderFactoryMap["mqtt"] =
new ProviderFactoryInfo("Apache.NMS.MQTT", "Apache.NMS.MQTT.ConnectionFactory");
schemaProviderFactoryMap["msmq"] =
new ProviderFactoryInfo("Apache.NMS.MSMQ", "Apache.NMS.MSMQ.ConnectionFactory");
schemaProviderFactoryMap["stomp"] =
new ProviderFactoryInfo("Apache.NMS.Stomp", "Apache.NMS.Stomp.ConnectionFactory");
schemaProviderFactoryMap["xms"] =
new ProviderFactoryInfo("Apache.NMS.XMS", "Apache.NMS.XMS.ConnectionFactory");
schemaProviderFactoryMap["zmq"] =
new ProviderFactoryInfo("Apache.NMS.ZMQ", "Apache.NMS.ZMQ.ConnectionFactory");
schemaProviderFactoryMap["amqp"] =
new ProviderFactoryInfo("Apache.NMS.AMQP", "Apache.NMS.AMQP.ConnectionFactory");
}
/// <summary>
/// The ConnectionFactory object must define a constructor that takes as a minimum a Uri object.
/// Any additional parameters are optional, but will typically include a Client ID string.
/// </summary>
/// <param name="providerURI">The URI for the ActiveMQ provider.</param>
/// <param name="constructorParams">Optional parameters to use when creating the ConnectionFactory.</param>
public NMSConnectionFactory(string providerURI, params object[] constructorParams)
: this(URISupport.CreateCompatibleUri(providerURI), constructorParams)
{
}
/// <summary>
/// The ConnectionFactory object must define a constructor that takes as a minimum a Uri object.
/// Any additional parameters are optional, but will typically include a Client ID string.
/// </summary>
/// <param name="uriProvider">The URI for the ActiveMQ provider.</param>
/// <param name="constructorParams">Optional parameters to use when creating the ConnectionFactory.</param>
public NMSConnectionFactory(Uri uriProvider, params object[] constructorParams)
{
this.factory = CreateConnectionFactory(uriProvider, constructorParams);
}
/// <summary>
/// Create a connection factory that can create connections for the given scheme in the URI.
/// </summary>
/// <param name="uriProvider">The URI for the ActiveMQ provider.</param>
/// <param name="constructorParams">Optional parameters to use when creating the ConnectionFactory.</param>
/// <returns>A <see cref="IConnectionFactory" /> implementation that will be used.</returns>
public static IConnectionFactory CreateConnectionFactory(Uri uriProvider, params object[] constructorParams)
{
IConnectionFactory connectionFactory = null;
try
{
Type factoryType = GetTypeForScheme(uriProvider.Scheme);
// If an implementation was found, try to instantiate it.
if (factoryType != null)
{
#if NETCF
// Compact framework does not allow the activator ta pass parameters to a constructor.
connectionFactory = (IConnectionFactory) Activator.CreateInstance(factoryType);
connectionFactory.BrokerUri = uriProvider;
#else
object[] parameters = MakeParameterArray(uriProvider, constructorParams);
connectionFactory = (IConnectionFactory) Activator.CreateInstance(factoryType, parameters);
#endif
}
if (null == connectionFactory)
{
throw new NMSConnectionException("No IConnectionFactory implementation found for connection URI: " +
uriProvider);
}
}
catch (NMSConnectionException)
{
throw;
}
catch (Exception ex)
{
throw new NMSConnectionException(
"Could not create the IConnectionFactory implementation: " + ex.Message, ex);
}
return connectionFactory;
}
/// <summary>
/// Finds the <see cref="System.Type" /> associated with the given scheme.
/// </summary>
/// <param name="scheme">The scheme (e.g. <c>tcp</c>, <c>activemq</c> or <c>stomp</c>).</param>
/// <returns>The <see cref="System.Type" /> of the ConnectionFactory that will be used
/// to create the connection for the specified <paramref name="scheme" />.</returns>
private static Type GetTypeForScheme(string scheme)
{
string[] paths = GetConfigSearchPaths();
string assemblyFileName;
string factoryClassName;
Type factoryType = null;
Tracer.DebugFormat("Locating provider for scheme: {0}", scheme);
if (LookupConnectionFactoryInfo(paths, scheme, out assemblyFileName, out factoryClassName))
{
Assembly assembly = null;
Tracer.DebugFormat("Attempting to load provider assembly: {0}", assemblyFileName);
try
{
assembly = Assembly.Load(assemblyFileName);
if (null != assembly)
{
Tracer.Debug("Succesfully loaded provider.");
}
}
catch (Exception ex)
{
Tracer.ErrorFormat("Exception loading assembly failed: {0}", ex.Message);
assembly = null;
}
if (null == assembly)
{
foreach (string path in paths)
{
string fullpath = Path.Combine(path, assemblyFileName) + ".dll";
Tracer.DebugFormat("Looking for: {0}", fullpath);
if (File.Exists(fullpath))
{
Tracer.Debug("\tAssembly found! Attempting to load...");
try
{
assembly = Assembly.LoadFrom(fullpath);
}
catch (Exception ex)
{
Tracer.ErrorFormat("Exception loading assembly failed: {0}", ex.Message);
assembly = null;
}
if (null != assembly)
{
Tracer.Debug("Successfully loaded provider.");
break;
}
Tracer.Debug("Failed to load provider. Continuing search...");
}
}
}
if (null != assembly)
{
#if NETCF
factoryType = assembly.GetType(factoryClassName, true);
#else
factoryType = assembly.GetType(factoryClassName, true, true);
#endif
if (null == factoryType)
{
Tracer.Fatal("Failed to load class factory from provider.");
}
}
else
{
Tracer.Fatal("Failed to load provider assembly.");
}
}
return factoryType;
}
/// <summary>
/// Lookup the connection factory assembly filename and class name.
/// Read an external configuration file that maps scheme to provider implementation.
/// Load XML config files named: nmsprovider-{scheme}.config
/// Following is a sample configuration file named nmsprovider-jms.config. Replace
/// the parenthesis with angle brackets for proper XML formatting.
///
/// (?xml version="1.0" encoding="utf-8" ?)
/// (configuration)
/// (provider assembly="MyCompany.NMS.JMSProvider.dll" classFactory="MyCompany.NMS.JMSProvider.ConnectionFactory"/)
/// (/configuration)
///
/// This configuration file would be loaded and parsed when a connection uri with a scheme of 'jms'
/// is used for the provider. In this example the connection string might look like:
/// jms://localhost:7222
///
/// </summary>
/// <param name="paths">Folder paths to look in.</param>
/// <param name="scheme">The scheme.</param>
/// <param name="assemblyFileName">Name of the assembly file.</param>
/// <param name="factoryClassName">Name of the factory class.</param>
/// <returns><c>true</c> if the configuration file for the specified <paramref name="scheme" /> could
/// be found; otherwise, <c>false</c>.</returns>
private static bool LookupConnectionFactoryInfo(string[] paths, string scheme, out string assemblyFileName,
out string factoryClassName)
{
bool foundFactory = false;
string schemeLower = scheme.ToLower();
ProviderFactoryInfo pfi;
// Look for a custom configuration to handle this scheme.
string configFileName = String.Format("nmsprovider-{0}.config", schemeLower);
assemblyFileName = String.Empty;
factoryClassName = String.Empty;
Tracer.DebugFormat("Attempting to locate provider configuration: {0}", configFileName);
foreach (string path in paths)
{
string fullpath = Path.Combine(path, configFileName);
Tracer.DebugFormat("Looking for: {0}", fullpath);
try
{
if (File.Exists(fullpath))
{
Tracer.DebugFormat("\tConfiguration file found in {0}", fullpath);
XmlDocument configDoc = new XmlDocument();
configDoc.Load(fullpath);
XmlElement providerNode = (XmlElement) configDoc.SelectSingleNode("/configuration/provider");
if (null != providerNode)
{
assemblyFileName = providerNode.GetAttribute("assembly");
factoryClassName = providerNode.GetAttribute("classFactory");
if (!String.IsNullOrEmpty(assemblyFileName) && !String.IsNullOrEmpty(factoryClassName))
{
foundFactory = true;
Tracer.DebugFormat("Selected custom provider for {0}: {1}, {2}", schemeLower,
assemblyFileName, factoryClassName);
break;
}
}
}
}
catch (Exception ex)
{
Tracer.DebugFormat("Exception while scanning {0}: {1}", fullpath, ex.Message);
}
}
if (!foundFactory)
{
// Check for standard provider implementations.
if (schemaProviderFactoryMap.TryGetValue(schemeLower, out pfi))
{
assemblyFileName = pfi.assemblyFileName;
factoryClassName = pfi.factoryClassName;
foundFactory = true;
Tracer.DebugFormat("Selected standard provider for {0}: {1}, {2}", schemeLower, assemblyFileName,
factoryClassName);
}
}
return foundFactory;
}
/// <summary>
/// Get an array of search paths to look for config files.
/// </summary>
/// <returns>
/// A collection of search paths, including the current directory, the current AppDomain's
/// BaseDirectory and the current AppDomain's RelativeSearchPath.
/// </returns>
private static string[] GetConfigSearchPaths()
{
ArrayList pathList = new ArrayList();
// Check the current folder first.
pathList.Add("");
#if !NETCF
try
{
AppDomain currentDomain = AppDomain.CurrentDomain;
// Check the folder the assembly is located in.
Assembly executingAssembly = Assembly.GetExecutingAssembly();
try
{
var path = Path.GetDirectoryName(executingAssembly.Location);
if (!string.IsNullOrEmpty(path))
{
pathList.Add(path);
}
}
catch (Exception ex)
{
Tracer.DebugFormat("Error parsing executing assembly location: {0} : {1}",
executingAssembly.Location, ex.Message);
}
if (null != currentDomain.BaseDirectory)
{
pathList.Add(currentDomain.BaseDirectory);
}
if (null != currentDomain.RelativeSearchPath)
{
pathList.Add(currentDomain.RelativeSearchPath);
}
}
catch (Exception ex)
{
Tracer.DebugFormat("Error configuring search paths: {0}", ex.Message);
}
#endif
return (string[]) pathList.ToArray(typeof(string));
}
/// <summary>
/// Converts a <c>params object[]</c> collection into a plain <c>object[]</c>s, to pass to the constructor.
/// </summary>
/// <param name="firstParam">The first parameter in the collection.</param>
/// <param name="varParams">The remaining parameters.</param>
/// <returns>An array of <see cref="Object" /> instances.</returns>
private static object[] MakeParameterArray(object firstParam, params object[] varParams)
{
ArrayList paramList = new ArrayList();
paramList.Add(firstParam);
foreach (object param in varParams)
{
paramList.Add(param);
}
return paramList.ToArray();
}
/// <summary>
/// Creates a new connection.
/// </summary>
/// <returns>An <see cref="IConnection" /> created by the requested ConnectionFactory.</returns>
public IConnection CreateConnection()
{
return this.factory.CreateConnection();
}
/// <summary>
/// Creates a new connection with the given <paramref name="userName" /> and <paramref name="password" /> credentials.
/// </summary>
/// <param name="userName">The username to use when establishing the connection.</param>
/// <param name="password">The password to use when establishing the connection.</param>
/// <returns>An <see cref="IConnection" /> created by the requested ConnectionFactory.</returns>
public IConnection CreateConnection(string userName, string password)
{
return this.factory.CreateConnection(userName, password);
}
public Task<IConnection> CreateConnectionAsync()
{
return this.factory.CreateConnectionAsync();
}
public Task<IConnection> CreateConnectionAsync(string userName, string password)
{
return this.factory.CreateConnectionAsync(userName, password);
}
public INMSContext CreateContext()
{
return this.factory.CreateContext();
}
public INMSContext CreateContext(AcknowledgementMode acknowledgementMode)
{
return this.factory.CreateContext(acknowledgementMode);
}
public INMSContext CreateContext(string userName, string password)
{
return this.factory.CreateContext(userName, password);
}
public INMSContext CreateContext(string userName, string password, AcknowledgementMode acknowledgementMode)
{
return this.factory.CreateContext(userName, password, acknowledgementMode);
}
public Task<INMSContext> CreateContextAsync()
{
return this.factory.CreateContextAsync();
}
public Task<INMSContext> CreateContextAsync(AcknowledgementMode acknowledgementMode)
{
return this.factory.CreateContextAsync(acknowledgementMode);
}
public Task<INMSContext> CreateContextAsync(string userName, string password)
{
return this.factory.CreateContextAsync(userName, password);
}
public Task<INMSContext> CreateContextAsync(string userName, string password, AcknowledgementMode acknowledgementMode)
{
return this.factory.CreateContextAsync(userName, password, acknowledgementMode);
}
/// <summary>
/// Get/or set the broker Uri.
/// </summary>
public Uri BrokerUri
{
get { return ConnectionFactory.BrokerUri; }
set { ConnectionFactory.BrokerUri = value; }
}
/// <summary>
/// The actual IConnectionFactory implementation that is being used. This implementation
/// depends on the scheme of the URI used when constructed.
/// </summary>
public IConnectionFactory ConnectionFactory
{
get { return factory; }
}
/// <summary>
/// Get/or Set the IRedeliveryPolicy instance using the IConnectionFactory implementation
/// that is being used.
/// </summary>
public IRedeliveryPolicy RedeliveryPolicy
{
get { return this.factory.RedeliveryPolicy; }
set { this.factory.RedeliveryPolicy = value; }
}
/// <summary>
/// Get/or Set the ConsumerTransformerDelegate using the IConnectionFactory implementation
/// that is currently being used.
/// </summary>
public ConsumerTransformerDelegate ConsumerTransformer
{
get { return this.factory.ConsumerTransformer; }
set { this.factory.ConsumerTransformer = value; }
}
/// <summary>
/// Get/or Set the ProducerTransformerDelegate using the IConnectionFactory implementation
/// that is currently being used.
/// </summary>
public ProducerTransformerDelegate ProducerTransformer
{
get { return this.factory.ProducerTransformer; }
set { this.factory.ProducerTransformer = value; }
}
}
}