Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10377 was 10171, checked in by pfleck, 11 years ago

#2122

Problem occurs when the retry counter in the HiveServiceLocator is exceeded.
The retry counter is incremented on all exceptions and therefore is incremented on security exceptions (user/password wrong) as well.

Changed the caught exception to EndpointNotFoundException to avoid increment of the retry counter on wrong user/password.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
41    private string username;
42    public string Username {
43      get { return username; }
44      set { username = value; }
45    }
46
47    private string password;
48    public string Password {
49      get { return password; }
50      set { password = value; }
51    }
52
53    public int EndpointRetries { get; private set; }
54
55    public string WorkingEndpoint { get; private set; }
56
57    private HiveServiceClient NewServiceClient() {
58      if (EndpointRetries >= Settings.Default.MaxEndpointRetries) {
59        return CreateClient(WorkingEndpoint);
60      }
61
62      var configurations = Settings.Default.EndpointConfigurationPriorities;
63
64      Exception exception = null;
65      foreach (var endpointConfigurationName in configurations) {
66        try {
67          var cl = CreateClient(endpointConfigurationName);
68          cl.Open();
69          WorkingEndpoint = endpointConfigurationName;
70          return cl;
71        }
72        catch (EndpointNotFoundException exc) {
73          exception = exc;
74          EndpointRetries++;
75        }
76      }
77
78      throw exception ?? new Exception("No endpoint for Hive service found.");
79    }
80
81    private HiveServiceClient CreateClient(string endpointConfigurationName) {
82      HiveServiceClient cl = null;
83
84      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
85        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
86      else
87        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
88
89      return cl;
90    }
91
92    public T CallHiveService<T>(Func<IHiveService, T> call) {
93      HiveServiceClient client = NewServiceClient();
94      HandleAnonymousUser(client);
95
96      try {
97        return call(client);
98      }
99      finally {
100        try {
101          client.Close();
102        }
103        catch (Exception) {
104          client.Abort();
105        }
106      }
107    }
108
109    public void CallHiveService(Action<IHiveService> call) {
110      HiveServiceClient client = NewServiceClient();
111      HandleAnonymousUser(client);
112
113      try {
114        call(client);
115      }
116      finally {
117        try {
118          client.Close();
119        }
120        catch (Exception) {
121          client.Abort();
122        }
123      }
124    }
125
126    private void HandleAnonymousUser(HiveServiceClient client) {
127      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
128        try {
129          client.Close();
130        }
131        catch (Exception) {
132          client.Abort();
133        }
134        throw new AnonymousUserException();
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.