Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.Hive/3.3/HiveServiceLocator.cs @ 15866

Last change on this file since 15866 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 4.6 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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;
[10171]23using System.ServiceModel;
[6976]24using HeuristicLab.Clients.Common;
25
26namespace HeuristicLab.Clients.Hive {
[7132]27  public class HiveServiceLocator : IHiveServiceLocator {
28    private static IHiveServiceLocator instance = null;
29    public static IHiveServiceLocator Instance {
[6976]30      get {
31        if (instance == null) {
[7132]32          instance = new HiveServiceLocator();
[6976]33        }
34        return instance;
35      }
36    }
37
[13060]38    private HiveServiceLocator() { }
[9665]39
[6976]40    private string username;
41    public string Username {
42      get { return username; }
43      set { username = value; }
44    }
45
46    private string password;
47    public string Password {
48      get { return password; }
49      set { password = value; }
50    }
51
[9665]52    public int EndpointRetries { get; private set; }
53
54    public string WorkingEndpoint { get; private set; }
55
[13060]56
57    public string GetEndpointInformation() {
58      string message = "Configured endpoints: " + Environment.NewLine;
59
60      var configurations = Settings.Default.EndpointConfigurationPriorities;
61      foreach (var endpointConfigurationName in configurations) {
62        var cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
63        message += endpointConfigurationName + ": " + cl.Endpoint.Address + Environment.NewLine;
64      }
65
66      if (string.IsNullOrEmpty(WorkingEndpoint)) {
67        message += "No working endpoint found, check you configuration.";
68      } else {
69        message += "Used endpoint: " + WorkingEndpoint;
70      }
71
72      return message;
73    }
74
[6976]75    private HiveServiceClient NewServiceClient() {
[9665]76      if (EndpointRetries >= Settings.Default.MaxEndpointRetries) {
77        return CreateClient(WorkingEndpoint);
78      }
79
80      var configurations = Settings.Default.EndpointConfigurationPriorities;
81
82      Exception exception = null;
83      foreach (var endpointConfigurationName in configurations) {
84        try {
85          var cl = CreateClient(endpointConfigurationName);
86          cl.Open();
87          WorkingEndpoint = endpointConfigurationName;
88          return cl;
[14927]89        } catch (EndpointNotFoundException exc) {
[9665]90          exception = exc;
91          EndpointRetries++;
92        }
93      }
94
95      throw exception ?? new Exception("No endpoint for Hive service found.");
96    }
97
98    private HiveServiceClient CreateClient(string endpointConfigurationName) {
99      HiveServiceClient cl = null;
100
[6976]101      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
[9665]102        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
[6976]103      else
[9665]104        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
[7142]105
[6976]106      return cl;
107    }
108
109    public T CallHiveService<T>(Func<IHiveService, T> call) {
110      HiveServiceClient client = NewServiceClient();
[7249]111      HandleAnonymousUser(client);
112
[6976]113      try {
114        return call(client);
[14927]115      } finally {
[6976]116        try {
117          client.Close();
[14927]118        } catch (Exception) {
[6976]119          client.Abort();
120        }
121      }
122    }
123
124    public void CallHiveService(Action<IHiveService> call) {
125      HiveServiceClient client = NewServiceClient();
[7249]126      HandleAnonymousUser(client);
127
[6976]128      try {
129        call(client);
[14927]130      } finally {
[6976]131        try {
132          client.Close();
[14927]133        } catch (Exception) {
[6976]134          client.Abort();
135        }
136      }
137    }
[7249]138
139    private void HandleAnonymousUser(HiveServiceClient client) {
140      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
141        try {
142          client.Close();
[14927]143        } catch (Exception) {
[7249]144          client.Abort();
145        }
146        throw new AnonymousUserException();
147      }
148    }
[6976]149  }
150}
Note: See TracBrowser for help on using the repository browser.