Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14927 was 14927, checked in by gkronber, 7 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 System.ServiceModel;
24using HeuristicLab.Clients.Common;
25
26namespace HeuristicLab.Clients.Hive {
27  public class HiveServiceLocator : IHiveServiceLocator {
28    private static IHiveServiceLocator instance = null;
29    public static IHiveServiceLocator Instance {
30      get {
31        if (instance == null) {
32          instance = new HiveServiceLocator();
33        }
34        return instance;
35      }
36    }
37
38    private HiveServiceLocator() { }
39
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
52    public int EndpointRetries { get; private set; }
53
54    public string WorkingEndpoint { get; private set; }
55
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
75    private HiveServiceClient NewServiceClient() {
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;
89        } catch (EndpointNotFoundException exc) {
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
101      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
102        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
103      else
104        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
105
106      return cl;
107    }
108
109    public T CallHiveService<T>(Func<IHiveService, T> call) {
110      HiveServiceClient client = NewServiceClient();
111      HandleAnonymousUser(client);
112
113      try {
114        return call(client);
115      } finally {
116        try {
117          client.Close();
118        } catch (Exception) {
119          client.Abort();
120        }
121      }
122    }
123
124    public void CallHiveService(Action<IHiveService> call) {
125      HiveServiceClient client = NewServiceClient();
126      HandleAnonymousUser(client);
127
128      try {
129        call(client);
130      } finally {
131        try {
132          client.Close();
133        } catch (Exception) {
134          client.Abort();
135        }
136      }
137    }
138
139    private void HandleAnonymousUser(HiveServiceClient client) {
140      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
141        try {
142          client.Close();
143        } catch (Exception) {
144          client.Abort();
145        }
146        throw new AnonymousUserException();
147      }
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.