Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive/3.3/HiveServiceLocator.cs @ 9450

Last change on this file since 9450 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 2.9 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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;
24
25namespace HeuristicLab.Clients.Hive {
[7132]26  public class HiveServiceLocator : IHiveServiceLocator {
27    private static IHiveServiceLocator instance = null;
28    public static IHiveServiceLocator Instance {
[6976]29      get {
30        if (instance == null) {
[7132]31          instance = new HiveServiceLocator();
[6976]32        }
33        return instance;
34      }
35    }
36
37    private string username;
38    public string Username {
39      get { return username; }
40      set { username = value; }
41    }
42
43    private string password;
44    public string Password {
45      get { return password; }
46      set { password = value; }
47    }
48
49    private HiveServiceClient NewServiceClient() {
50      HiveServiceClient cl;
51      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
52        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>();
53      else
54        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(null, null, username, password);
[7142]55
[6976]56      return cl;
57    }
58
59    public T CallHiveService<T>(Func<IHiveService, T> call) {
60      HiveServiceClient client = NewServiceClient();
[7249]61      HandleAnonymousUser(client);
62
[6976]63      try {
64        return call(client);
[7249]65      }
66      finally {
[6976]67        try {
68          client.Close();
69        }
70        catch (Exception) {
71          client.Abort();
72        }
73      }
74    }
75
76    public void CallHiveService(Action<IHiveService> call) {
77      HiveServiceClient client = NewServiceClient();
[7249]78      HandleAnonymousUser(client);
79
[6976]80      try {
81        call(client);
[7249]82      }
83      finally {
[6976]84        try {
85          client.Close();
86        }
87        catch (Exception) {
88          client.Abort();
89        }
90      }
91    }
[7249]92
93    private void HandleAnonymousUser(HiveServiceClient client) {
94      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
95        try {
96          client.Close();
97        }
98        catch (Exception) {
99          client.Abort();
100        }
101        throw new AnonymousUserException();
102      }
103    }
[6976]104  }
105}
Note: See TracBrowser for help on using the repository browser.