1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Clients.Common;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Clients.Access {
|
---|
28 | [Item("AccessClient", "Access client.")]
|
---|
29 | public sealed class AccessClient : IContent {
|
---|
30 | private static AccessClient instance;
|
---|
31 | public static AccessClient Instance {
|
---|
32 | get {
|
---|
33 | if (instance == null) instance = new AccessClient();
|
---|
34 | return instance;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | #region Properties
|
---|
39 | private ItemList<UserGroupBase> usersAndGroups;
|
---|
40 | public ItemList<UserGroupBase> UsersAndGroups {
|
---|
41 | get { return usersAndGroups; }
|
---|
42 | }
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | private AccessClient() { }
|
---|
46 |
|
---|
47 | #region Refresh
|
---|
48 | public void Refresh() {
|
---|
49 | usersAndGroups = new ItemList<UserGroupBase>();
|
---|
50 | usersAndGroups.AddRange(CallRunCreationService<ItemList<UserGroupBase>>(s => new ItemList<UserGroupBase>(s.GetAllLeightweightUsersAndGroups())));
|
---|
51 | }
|
---|
52 | public void RefreshAsync(Action<Exception> exceptionCallback) {
|
---|
53 | var call = new Func<Exception>(delegate() {
|
---|
54 | try {
|
---|
55 | OnRefreshing();
|
---|
56 | Refresh();
|
---|
57 | }
|
---|
58 | catch (Exception ex) {
|
---|
59 | return ex;
|
---|
60 | }
|
---|
61 | finally {
|
---|
62 | OnRefreshed();
|
---|
63 | }
|
---|
64 | return null;
|
---|
65 | });
|
---|
66 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
67 | Exception ex = call.EndInvoke(result);
|
---|
68 | if (ex != null) exceptionCallback(ex);
|
---|
69 | }, null);
|
---|
70 | }
|
---|
71 | public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
|
---|
72 | var call = new Func<Exception>(delegate() {
|
---|
73 | try {
|
---|
74 | OnRefreshing();
|
---|
75 | action();
|
---|
76 | }
|
---|
77 | catch (Exception ex) {
|
---|
78 | return ex;
|
---|
79 | }
|
---|
80 | finally {
|
---|
81 | OnRefreshed();
|
---|
82 | }
|
---|
83 | return null;
|
---|
84 | });
|
---|
85 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
86 | Exception ex = call.EndInvoke(result);
|
---|
87 | if (ex != null) exceptionCallback(ex);
|
---|
88 | }, null);
|
---|
89 | }
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | public static void Store(IAccessItem item) {
|
---|
93 | //TODO: prevent storing of lightweight users
|
---|
94 | }
|
---|
95 |
|
---|
96 | #region Events
|
---|
97 | public event EventHandler Refreshing;
|
---|
98 | private void OnRefreshing() {
|
---|
99 | EventHandler handler = Refreshing;
|
---|
100 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
101 | }
|
---|
102 | public event EventHandler Refreshed;
|
---|
103 | private void OnRefreshed() {
|
---|
104 | EventHandler handler = Refreshed;
|
---|
105 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
106 | }
|
---|
107 | #endregion
|
---|
108 |
|
---|
109 | #region Helpers
|
---|
110 | public static void CallRunCreationService(Action<IAccessService> call) {
|
---|
111 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
112 | try {
|
---|
113 | call(client);
|
---|
114 | }
|
---|
115 | finally {
|
---|
116 | try {
|
---|
117 | client.Close();
|
---|
118 | }
|
---|
119 | catch (Exception) {
|
---|
120 | client.Abort();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | public static T CallRunCreationService<T>(Func<IAccessService, T> call) {
|
---|
125 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
126 | try {
|
---|
127 | return call(client);
|
---|
128 | }
|
---|
129 | finally {
|
---|
130 | try {
|
---|
131 | client.Close();
|
---|
132 | }
|
---|
133 | catch (Exception) {
|
---|
134 | client.Abort();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | #endregion
|
---|
139 | }
|
---|
140 | }
|
---|