<commit_before>using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Shuttle.Core.Infrastructure;

namespace Shuttle.Esb
{
	public class QueueManager : IQueueManager, IDisposable
	{
		private readonly List<IQueueFactory> _queueFactories = new List<IQueueFactory>();
		private readonly List<IQueue> _queues = new List<IQueue>();

		private readonly ILog _log;
		private static readonly object Padlock = new object();
		private readonly IUriResolver _uriResolver;

		public QueueManager(IUriResolver uriResolver) 
		{
            Guard.AgainstNull(uriResolver, "uriResolver");

            _uriResolver = uriResolver;

            _log = Log.For(this);
		}

		public void Dispose()
		{
			foreach (var queueFactory in _queueFactories)
			{
				queueFactory.AttemptDispose();
			}

			foreach (var queue in _queues)
			{
				queue.AttemptDispose();
			}

			_queueFactories.Clear();
			_queues.Clear();
		}

		public IQueueFactory GetQueueFactory(string scheme)
		{
			Uri uri;

			return Uri.TryCreate(scheme, UriKind.Absolute, out uri)
				? GetQueueFactory(uri)
				: _queueFactories.Find(factory => factory.Scheme.Equals(scheme, StringComparison.InvariantCultureIgnoreCase));
		}

		public IQueueFactory GetQueueFactory(Uri uri)
		{
			foreach (var factory in _queueFactories.Where(factory => factory.CanCreate(uri)))
			{
				return factory;
			}

			throw new QueueFactoryNotFoundException(uri.Scheme);
		}

		public IQueue GetQueue(string uri)
		{
			Guard.AgainstNullOrEmptyString(uri, "uri");

			var queue =
				_queues.Find(
					candidate => Find(candidate, uri));

			if (queue != null)
			{
				return queue;
			}

			lock (Padlock)
			{
				queue =
					_queues.Find(
						candidate => Find(candidate, uri));

				if (queue != null)
				{
					return queue;
				}

				var queueUri = new Uri(uri);

				if (queueUri.Scheme.Equals("resolver"))
				{
					var resolvedQueueUri = _uriResolver.GetTarget(queueUri);

					if (resolvedQueueUri == null)
					{
						throw new KeyNotFoundException(string.Format(EsbResources.UriNameNotFoundException,
							_uriResolver.GetType().FullName,
							uri));
					}

					queue = new ResolvedQueue(CreateQueue(GetQueueFactory(resolvedQueueUri), resolvedQueueUri), queueUri);
				}
				else
				{
					queue = CreateQueue(GetQueueFactory(queueUri), queueUri);
				}

				_queues.Add(queue);

				return queue;
			}
		}

		public IQueue CreateQueue(string uri)
		{
			return CreateQueue(new Uri(uri));
		}

		public IQueue CreateQueue(Uri uri)
		{
			return GetQueueFactory(uri).Create(uri);
		}

		public IEnumerable<IQueueFactory> QueueFactories()
		{
			return new ReadOnlyCollection<IQueueFactory>(_queueFactories);
		}

		public void RegisterQueueFactory(IQueueFactory queueFactory)
		{
			Guard.AgainstNull(queueFactory, "queueFactory");

			var factory = GetQueueFactory(queueFactory.Scheme);

			if (factory != null)
			{
				_queueFactories.Remove(factory);

				_log.Warning(string.Format(EsbResources.DuplicateQueueFactoryReplaced, queueFactory.Scheme,
					factory.GetType().FullName, queueFactory.GetType().FullName));
			}

			_queueFactories.Add(queueFactory);
		}

		public bool ContainsQueueFactory(string scheme)
		{
			return GetQueueFactory(scheme) != null;
		}

	    private IQueue CreateQueue(IQueueFactory queueFactory, Uri queueUri)
		{
			var result = queueFactory.Create(queueUri);

			Guard.AgainstNull(result,
				string.Format(EsbResources.QueueFactoryCreatedNullQueue, queueFactory.GetType().FullName, queueUri));

			return result;
		}

		private bool Find(IQueue candidate, string uri)
		{
			try
			{
				return candidate.Uri.ToString().Equals(uri, StringComparison.InvariantCultureIgnoreCase);
			}
			catch (Exception ex)
			{
				var candidateTypeName = "(candidate is null)";
				var candidateUri = "(candidate is null)";

				if (candidate != null)
				{
					candidateTypeName = candidate.GetType().FullName;
					candidateUri = candidate.Uri != null
						? candidate.Uri.ToString()
						: "(candidate.Uri is null)";
				}

				_log.Error(string.Format(EsbResources.FindQueueException, candidateTypeName, candidateUri,
					uri ?? "(comparison uri is null)", ex.AllMessages()));

				return false;
			}
		}

		public void CreatePhysicalQueues(IServiceBusConfiguration configuration)
		{
			if (configuration.HasInbox)
			{
				CreateQueues(configuration.Inbox);

				if (configuration.Inbox.HasDeferredQueue)
				{
					configuration.Inbox.DeferredQueue.AttemptCreate();
				}
			}

			if (configuration.HasOutbox)
			{
				CreateQueues(configuration.Outbox);
			}

			if (configuration.HasControlInbox)
			{
				CreateQueues(configuration.ControlInbox);
			}
		}

		private void CreateQueues(IWorkQueueConfiguration workQueueConfiguration)
		{
			workQueueConfiguration.WorkQueue.AttemptCreate();

			var errorQueueConfiguration = workQueueConfiguration as IErrorQueueConfiguration;

			if (errorQueueConfiguration != null)
			{
				errorQueueConfiguration.ErrorQueue.AttemptCreate();
			}
		}
	}
}<commit_msg>- added log when registering queue factory<commit_after>using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Shuttle.Core.Infrastructure;

namespace Shuttle.Esb
{
	public class QueueManager : IQueueManager, IDisposable
	{
		private readonly List<IQueueFactory> _queueFactories = new List<IQueueFactory>();
		private readonly List<IQueue> _queues = new List<IQueue>();

		private readonly ILog _log;
		private static readonly object Padlock = new object();
		private readonly IUriResolver _uriResolver;

		public QueueManager(IUriResolver uriResolver) 
		{
            Guard.AgainstNull(uriResolver, "uriResolver");

            _uriResolver = uriResolver;

            _log = Log.For(this);
		}

		public void Dispose()
		{
			foreach (var queueFactory in _queueFactories)
			{
				queueFactory.AttemptDispose();
			}

			foreach (var queue in _queues)
			{
				queue.AttemptDispose();
			}

			_queueFactories.Clear();
			_queues.Clear();
		}

		public IQueueFactory GetQueueFactory(string scheme)
		{
			Uri uri;

			return Uri.TryCreate(scheme, UriKind.Absolute, out uri)
				? GetQueueFactory(uri)
				: _queueFactories.Find(factory => factory.Scheme.Equals(scheme, StringComparison.InvariantCultureIgnoreCase));
		}

		public IQueueFactory GetQueueFactory(Uri uri)
		{
			foreach (var factory in _queueFactories.Where(factory => factory.CanCreate(uri)))
			{
				return factory;
			}

			throw new QueueFactoryNotFoundException(uri.Scheme);
		}

		public IQueue GetQueue(string uri)
		{
			Guard.AgainstNullOrEmptyString(uri, "uri");

			var queue =
				_queues.Find(
					candidate => Find(candidate, uri));

			if (queue != null)
			{
				return queue;
			}

			lock (Padlock)
			{
				queue =
					_queues.Find(
						candidate => Find(candidate, uri));

				if (queue != null)
				{
					return queue;
				}

				var queueUri = new Uri(uri);

				if (queueUri.Scheme.Equals("resolver"))
				{
					var resolvedQueueUri = _uriResolver.GetTarget(queueUri);

					if (resolvedQueueUri == null)
					{
						throw new KeyNotFoundException(string.Format(EsbResources.UriNameNotFoundException,
							_uriResolver.GetType().FullName,
							uri));
					}

					queue = new ResolvedQueue(CreateQueue(GetQueueFactory(resolvedQueueUri), resolvedQueueUri), queueUri);
				}
				else
				{
					queue = CreateQueue(GetQueueFactory(queueUri), queueUri);
				}

				_queues.Add(queue);

				return queue;
			}
		}

		public IQueue CreateQueue(string uri)
		{
			return CreateQueue(new Uri(uri));
		}

		public IQueue CreateQueue(Uri uri)
		{
			return GetQueueFactory(uri).Create(uri);
		}

		public IEnumerable<IQueueFactory> QueueFactories()
		{
			return new ReadOnlyCollection<IQueueFactory>(_queueFactories);
		}

		public void RegisterQueueFactory(IQueueFactory queueFactory)
		{
			Guard.AgainstNull(queueFactory, "queueFactory");

			var factory = GetQueueFactory(queueFactory.Scheme);

			if (factory != null)
			{
				_queueFactories.Remove(factory);

				_log.Warning(string.Format(EsbResources.DuplicateQueueFactoryReplaced, queueFactory.Scheme,
					factory.GetType().FullName, queueFactory.GetType().FullName));
			}

			_queueFactories.Add(queueFactory);

		    if (Log.IsTraceEnabled)
		    {
                _log.Trace(string.Format(EsbResources.QueueFactoryRegistered, queueFactory.Scheme,
                    queueFactory.GetType().FullName));
            }
        }

		public bool ContainsQueueFactory(string scheme)
		{
			return GetQueueFactory(scheme) != null;
		}

	    private IQueue CreateQueue(IQueueFactory queueFactory, Uri queueUri)
		{
			var result = queueFactory.Create(queueUri);

			Guard.AgainstNull(result,
				string.Format(EsbResources.QueueFactoryCreatedNullQueue, queueFactory.GetType().FullName, queueUri));

			return result;
		}

		private bool Find(IQueue candidate, string uri)
		{
			try
			{
				return candidate.Uri.ToString().Equals(uri, StringComparison.InvariantCultureIgnoreCase);
			}
			catch (Exception ex)
			{
				var candidateTypeName = "(candidate is null)";
				var candidateUri = "(candidate is null)";

				if (candidate != null)
				{
					candidateTypeName = candidate.GetType().FullName;
					candidateUri = candidate.Uri != null
						? candidate.Uri.ToString()
						: "(candidate.Uri is null)";
				}

				_log.Error(string.Format(EsbResources.FindQueueException, candidateTypeName, candidateUri,
					uri ?? "(comparison uri is null)", ex.AllMessages()));

				return false;
			}
		}

		public void CreatePhysicalQueues(IServiceBusConfiguration configuration)
		{
			if (configuration.HasInbox)
			{
				CreateQueues(configuration.Inbox);

				if (configuration.Inbox.HasDeferredQueue)
				{
					configuration.Inbox.DeferredQueue.AttemptCreate();
				}
			}

			if (configuration.HasOutbox)
			{
				CreateQueues(configuration.Outbox);
			}

			if (configuration.HasControlInbox)
			{
				CreateQueues(configuration.ControlInbox);
			}
		}

		private void CreateQueues(IWorkQueueConfiguration workQueueConfiguration)
		{
			workQueueConfiguration.WorkQueue.AttemptCreate();

			var errorQueueConfiguration = workQueueConfiguration as IErrorQueueConfiguration;

			if (errorQueueConfiguration != null)
			{
				errorQueueConfiguration.ErrorQueue.AttemptCreate();
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.10086872586872588, 'fraction_numerical': 0.0, 'mean_word_length': 3.582485625829279, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1345284', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3034, 'n_words': 597}