#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using HeuristicLab.Clients.Common;
namespace HeuristicLab.Clients.Hive {
public class HiveServiceLocator : IHiveServiceLocator {
private static IHiveServiceLocator instance = null;
public static IHiveServiceLocator Instance {
get {
if (instance == null) {
instance = new HiveServiceLocator();
}
return instance;
}
}
private string username;
public string Username {
get { return username; }
set { username = value; }
}
private string password;
public string Password {
get { return password; }
set { password = value; }
}
private string endpointConfigurationName;
public string EndpointConfigurationName
{
get { return endpointConfigurationName; }
set { endpointConfigurationName = value; }
}
private string remoteAddress;
public string RemoteAddress {
get { return remoteAddress; }
set { remoteAddress = value; }
}
private string identityCertificate;
public string IdentityCertificate {
get { return identityCertificate; }
set { identityCertificate = value; }
}
private HiveServiceClient NewServiceClient() {
HiveServiceClient cl;
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
cl = ClientFactory.CreateClient();
else {
cl = ClientFactory.CreateClient(endpointConfigurationName, null, username, password);
}
if (!string.IsNullOrEmpty(remoteAddress) && !string.IsNullOrEmpty(identityCertificate)) {
X509Certificate2Collection supportingCertificates = new X509Certificate2Collection();
supportingCertificates.Import(Convert.FromBase64String(identityCertificate));
X509Certificate2 primaryCertificate = supportingCertificates[0];
supportingCertificates.RemoveAt(0);
EndpointIdentity ei = EndpointIdentity.CreateX509CertificateIdentity(primaryCertificate, supportingCertificates);
cl.Endpoint.Address = new EndpointAddress(new Uri(remoteAddress), ei);
}
return cl;
}
public T CallHiveService(Func call) {
HiveServiceClient client = NewServiceClient();
HandleAnonymousUser(client);
try {
return call(client);
}
finally {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
}
}
public void CallHiveService(Action call) {
HiveServiceClient client = NewServiceClient();
HandleAnonymousUser(client);
try {
call(client);
}
finally {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
}
}
private void HandleAnonymousUser(HiveServiceClient client) {
if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
throw new AnonymousUserException();
}
}
}
}