Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14364 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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;
89        }
[10171]90        catch (EndpointNotFoundException exc) {
[9665]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
[6976]102      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
[9665]103        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
[6976]104      else
[9665]105        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
[7142]106
[6976]107      return cl;
108    }
109
110    public T CallHiveService<T>(Func<IHiveService, T> call) {
111      HiveServiceClient client = NewServiceClient();
[7249]112      HandleAnonymousUser(client);
113
[6976]114      try {
115        return call(client);
[7249]116      }
117      finally {
[6976]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();
[7249]129      HandleAnonymousUser(client);
130
[6976]131      try {
132        call(client);
[7249]133      }
134      finally {
[6976]135        try {
136          client.Close();
137        }
138        catch (Exception) {
139          client.Abort();
140        }
141      }
142    }
[7249]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    }
[6976]155  }
156}
Note: See TracBrowser for help on using the repository browser.