Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17078 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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        }
90        catch (EndpointNotFoundException exc) {
91          exception = exc;
92          EndpointRetries++;
93        }
94      }
95
96      throw exception ?? new Exception("No endpoint for Hive service found.");
97    }
98
99    private HiveServiceClient CreateClient(string endpointConfigurationName) {
100      HiveServiceClient cl = null;
101
102      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
103        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
104      else
105        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
106
107      return cl;
108    }
109
110    public T CallHiveService<T>(Func<IHiveService, T> call) {
111      HiveServiceClient client = NewServiceClient();
112      HandleAnonymousUser(client);
113
114      try {
115        return call(client);
116      }
117      finally {
118        try {
119          client.Close();
120        }
121        catch (Exception) {
122          client.Abort();
123        }
124      }
125    }
126
127    public void CallHiveService(Action<IHiveService> call) {
128      HiveServiceClient client = NewServiceClient();
129      HandleAnonymousUser(client);
130
131      try {
132        call(client);
133      }
134      finally {
135        try {
136          client.Close();
137        }
138        catch (Exception) {
139          client.Abort();
140        }
141      }
142    }
143
144    private void HandleAnonymousUser(HiveServiceClient client) {
145      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
146        try {
147          client.Close();
148        }
149        catch (Exception) {
150          client.Abort();
151        }
152        throw new AnonymousUserException();
153      }
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.