#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 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 HiveServiceClient NewServiceClient() {
HiveServiceClient cl;
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
cl = ClientFactory.CreateClient();
else
cl = ClientFactory.CreateClient(null, null, username, password);
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();
}
}
}
}