TransferClientFactory
using Relativity.Transfer.Resources;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Relativity.Transfer
{
internal class TransferClientFactory : ITransferClientFactory
{
private readonly RelativityConnectionInfo connectionInfo;
private readonly ITransferLog transferLog;
public TransferClientFactory(RelativityConnectionInfo connectionInfo, ITransferLog log)
{
if (connectionInfo == null)
throw new ArgumentNullException("connectionInfo");
if (log == null)
throw new ArgumentNullException("log");
this.connectionInfo = connectionInfo;
transferLog = log;
}
public ITransferClient CreateTransferClient(ClientConfiguration configuration)
{
if (configuration == null)
throw new ArgumentNullException("configuration");
WellKnownTransferClient client = configuration.Client;
Guid clientId = configuration.ClientId;
if (client != 0 || !(clientId == Guid.Empty)) {
ValidateParameters();
try {
DirectoryCatalog2 directoryCatalog = new DirectoryCatalog2(transferLog);
try {
using (TransferClientPluginService transferClientPluginService = new TransferClientPluginService(directoryCatalog, transferLog)) {
List<Lazy<ITransferClient, ITransferClientMetadata>> list = transferClientPluginService.Search(connectionInfo, configuration).ToList();
ValidateTransferClients(list);
Lazy<ITransferClient, ITransferClientMetadata> lazy = list.FirstOrDefault(delegate(Lazy<ITransferClient, ITransferClientMetadata> x) {
if (!(new Guid(x.Metadata.Id) == clientId))
return x.Metadata.Client == client;
return true;
});
if (lazy == null)
throw CreatePluginNotFoundException(client, clientId);
ITransferClient value = lazy.Value;
configuration.Client = lazy.Metadata.Client;
configuration.ClientId = new Guid(lazy.Metadata.Id);
return value;
}
} finally {
((IDisposable)directoryCatalog)?.Dispose();
}
} catch (TransferException) {
throw;
} catch (Exception exception) {
throw CreatePluginLoadException(client, clientId, exception);
}
}
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ClientStrings.TransferClientIdArgumentExceptionMessage, "client-id"), "configuration");
}
[AsyncStateMachine(typeof(<CreateTransferClientAsync>d__4))]
public Task<ITransferClient> CreateTransferClientAsync(ClientConfiguration configuration, ITransferClientStrategy strategy, CancellationToken token)
{
<CreateTransferClientAsync>d__4 stateMachine = default(<CreateTransferClientAsync>d__4);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<ITransferClient>.Create();
stateMachine.<>4__this = this;
stateMachine.configuration = configuration;
stateMachine.strategy = strategy;
stateMachine.token = token;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
private static MruClient GetMruClient(RelativityConnectionInfo connectionInfo, params object[] data)
{
string mruClientKey = ClientMemoryCacheKeys.GetMruClientKey(connectionInfo, data);
using (MemoryCacheRepository memoryCacheRepository = new MemoryCacheRepository(TransferCache.Default))
return memoryCacheRepository.SelectByKey<MruClient>(mruClientKey);
}
private static void SetMruClient(RelativityConnectionInfo connectionInfo, MruClient item, params object[] data)
{
string mruClientKey = ClientMemoryCacheKeys.GetMruClientKey(connectionInfo, data);
using (MemoryCacheRepository memoryCacheRepository = new MemoryCacheRepository(TransferCache.Default))
memoryCacheRepository.Upsert(mruClientKey, item, TimeSpan.FromDays(1));
}
private static void RemoveMruClient(RelativityConnectionInfo connectionInfo, params object[] data)
{
string mruClientKey = ClientMemoryCacheKeys.GetMruClientKey(connectionInfo, data);
using (MemoryCacheRepository memoryCacheRepository = new MemoryCacheRepository(TransferCache.Default))
memoryCacheRepository.Delete(mruClientKey);
}
private static void ValidateTransferClients(ICollection<Lazy<ITransferClient, ITransferClientMetadata>> clients)
{
if (clients.Count != 0)
return;
throw new TransferException(string.Format(CultureInfo.CurrentCulture, ClientStrings.NoTransferClientsFoundExceptionMessage, GlobalSettings.Instance.PluginDirectory));
}
private void ValidateParameters()
{
if (connectionInfo.Credential == null)
throw new InvalidOperationException(CoreStrings.RelativityConnectionCredentialsArgumentExceptionMessage);
if (connectionInfo.Host == (Uri)null)
throw new InvalidOperationException(CoreStrings.RelativityConnectionHostArgumentExceptionMessage);
if (connectionInfo.WorkspaceId < 1 && connectionInfo.WorkspaceId != -1)
throw new InvalidOperationException(CoreStrings.RelativityConnectionWorkspaceArgumentExceptionMessage);
}
private static TransferException CreatePluginNotFoundException(WellKnownTransferClient client, Guid clientId)
{
string displayName = clientId.ToString();
if (client != 0)
displayName = client.ToString();
return CreatePluginNotFoundException(displayName);
}
private static TransferException CreatePluginNotFoundException(string displayName)
{
return new TransferException(string.Format(CultureInfo.CurrentCulture, ClientStrings.TransferClientNotFoundExceptionMessage, displayName, GlobalSettings.Instance.PluginDirectory));
}
private static TransferException CreatePluginLoadException(WellKnownTransferClient client, Guid clientId, Exception exception)
{
string arg = clientId.ToString();
if (client != 0)
arg = client.ToString();
return new TransferException(string.Format(CultureInfo.CurrentCulture, ClientStrings.TransferClientMefExceptionMessage, arg, GlobalSettings.Instance.PluginDirectory), exception);
}
private static TransferException CreateNoTransferClientsSupportedException(Exception exception)
{
return new TransferException(string.Format(CultureInfo.CurrentCulture, ClientStrings.NoTransferClientsSupportedMessage, GlobalSettings.Instance.PluginDirectory), exception, true);
}
private static TransferException CreateTransferClientMetadataMefException(Exception exception)
{
throw new TransferException(string.Format(CultureInfo.CurrentCulture, ClientStrings.TransferClientMetadataMefExceptionMessage, GlobalSettings.Instance.PluginDirectory), exception, true);
}
}
}