#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Clients.Common;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Clients.OKB.Authentication {
[Item("AuthenticationClient", "OKB authentication client.")]
public sealed class AuthenticationClient : IContent {
private static AuthenticationClient instance;
public static AuthenticationClient Instance {
get {
if (instance == null) instance = new AuthenticationClient();
return instance;
}
}
#region Properties
private IEnumerable users;
public IEnumerable Users {
get { return users; }
}
#endregion
private AuthenticationClient() { }
#region Refresh
public void Refresh() {
OnRefreshing();
try {
users = CallAuthenticationService>(s => s.GetUsers()).OrderBy(x => x.Name);
}
finally {
OnRefreshed();
}
}
public void RefreshAsync(Action exceptionCallback) {
var call = new Func(delegate() {
try {
Refresh();
}
catch (Exception ex) {
return ex;
}
return null;
});
call.BeginInvoke(delegate(IAsyncResult result) {
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#endregion
#region User Methods
public static IEnumerable GetUsers() {
return CallAuthenticationService>(s => s.GetUsers()).OrderBy(x => x.Name);
}
#endregion
#region Events
public event EventHandler Refreshing;
private void OnRefreshing() {
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed() {
EventHandler handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Helpers
private static T CallAuthenticationService(Func call) {
AuthenticationServiceClient client = ClientFactory.CreateClient();
try {
return call(client);
}
finally {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
}
}
#endregion
}
}