Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Access/3.3/AccessClient.cs @ 11544

Last change on this file since 11544 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
22using System;
23using HeuristicLab.Clients.Common;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26
27namespace 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(CallAccessService<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    #region Events
93    public event EventHandler Refreshing;
94    private void OnRefreshing() {
95      EventHandler handler = Refreshing;
96      if (handler != null) handler(this, EventArgs.Empty);
97    }
98    public event EventHandler Refreshed;
99    private void OnRefreshed() {
100      EventHandler handler = Refreshed;
101      if (handler != null) handler(this, EventArgs.Empty);
102    }
103    #endregion
104
105    #region Helpers
106    public static void CallAccessService(Action<IAccessService> call) {
107      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
108      try {
109        call(client);
110      }
111      finally {
112        try {
113          client.Close();
114        }
115        catch (Exception) {
116          client.Abort();
117        }
118      }
119    }
120    public static T CallAccessService<T>(Func<IAccessService, T> call) {
121      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
122      try {
123        return call(client);
124      }
125      finally {
126        try {
127          client.Close();
128        }
129        catch (Exception) {
130          client.Abort();
131        }
132      }
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.