1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Clients.Common;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Clients.OKB.Authentication {
|
---|
31 | [Item("AuthenticationClient", "OKB authentication client.")]
|
---|
32 | public sealed class AuthenticationClient : IContent {
|
---|
33 | private static AuthenticationClient instance;
|
---|
34 | public static AuthenticationClient Instance {
|
---|
35 | get {
|
---|
36 | if (instance == null) instance = new AuthenticationClient();
|
---|
37 | return instance;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | #region Properties
|
---|
42 | private IEnumerable<User> users;
|
---|
43 | public IEnumerable<User> Users {
|
---|
44 | get { return users; }
|
---|
45 | }
|
---|
46 | #endregion
|
---|
47 |
|
---|
48 | private AuthenticationClient() { }
|
---|
49 |
|
---|
50 | #region Refresh
|
---|
51 | public void Refresh() {
|
---|
52 | OnRefreshing();
|
---|
53 | try {
|
---|
54 | users = CallAuthenticationService<List<User>>(s => s.GetUsers()).OrderBy(x => x.Name);
|
---|
55 | }
|
---|
56 | finally {
|
---|
57 | OnRefreshed();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | public void RefreshAsync(Action<Exception> exceptionCallback) {
|
---|
61 | var call = new Func<Exception>(delegate() {
|
---|
62 | try {
|
---|
63 | Refresh();
|
---|
64 | }
|
---|
65 | catch (Exception ex) {
|
---|
66 | return ex;
|
---|
67 | }
|
---|
68 | return null;
|
---|
69 | });
|
---|
70 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
71 | Exception ex = call.EndInvoke(result);
|
---|
72 | if (ex != null) exceptionCallback(ex);
|
---|
73 | }, null);
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | #region User Methods
|
---|
78 | public static IEnumerable<User> GetUsers() {
|
---|
79 | return CallAuthenticationService<List<User>>(s => s.GetUsers()).OrderBy(x => x.Name);
|
---|
80 | }
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | #region Events
|
---|
84 | public event EventHandler Refreshing;
|
---|
85 | private void OnRefreshing() {
|
---|
86 | EventHandler handler = Refreshing;
|
---|
87 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
88 | }
|
---|
89 | public event EventHandler Refreshed;
|
---|
90 | private void OnRefreshed() {
|
---|
91 | EventHandler handler = Refreshed;
|
---|
92 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | #region Helpers
|
---|
97 | private static T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
|
---|
98 | AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
|
---|
99 | try {
|
---|
100 | return call(client);
|
---|
101 | }
|
---|
102 | finally {
|
---|
103 | try {
|
---|
104 | client.Close();
|
---|
105 | }
|
---|
106 | catch (Exception) {
|
---|
107 | client.Abort();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 | }
|
---|
113 | }
|
---|