Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Clients.Hive/3.3/HiveServiceLocator.cs @ 11297

Last change on this file since 11297 was 9665, checked in by ascheibe, 11 years ago

#2030 merged hive performance branch back into trunk

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 HeuristicLab.Clients.Common;
24
25namespace HeuristicLab.Clients.Hive {
26  public class HiveServiceLocator : IHiveServiceLocator {
27    private static IHiveServiceLocator instance = null;
28    public static IHiveServiceLocator Instance {
29      get {
30        if (instance == null) {
31          instance = new HiveServiceLocator();
32        }
33        return instance;
34      }
35    }
36
37    private HiveServiceLocator() {
38    }
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    private HiveServiceClient NewServiceClient() {
57      if (EndpointRetries >= Settings.Default.MaxEndpointRetries) {
58        return CreateClient(WorkingEndpoint);
59      }
60
61      var configurations = Settings.Default.EndpointConfigurationPriorities;
62
63      Exception exception = null;
64      foreach (var endpointConfigurationName in configurations) {
65        try {
66          var cl = CreateClient(endpointConfigurationName);
67          cl.Open();
68          WorkingEndpoint = endpointConfigurationName;
69          return cl;
70        }
71        catch (Exception exc) {
72          exception = exc;
73          EndpointRetries++;
74        }
75      }
76
77      throw exception ?? new Exception("No endpoint for Hive service found.");
78    }
79
80    private HiveServiceClient CreateClient(string endpointConfigurationName) {
81      HiveServiceClient cl = null;
82
83      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
84        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
85      else
86        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
87
88      return cl;
89    }
90
91    public T CallHiveService<T>(Func<IHiveService, T> call) {
92      HiveServiceClient client = NewServiceClient();
93      HandleAnonymousUser(client);
94
95      try {
96        return call(client);
97      }
98      finally {
99        try {
100          client.Close();
101        }
102        catch (Exception) {
103          client.Abort();
104        }
105      }
106    }
107
108    public void CallHiveService(Action<IHiveService> call) {
109      HiveServiceClient client = NewServiceClient();
110      HandleAnonymousUser(client);
111
112      try {
113        call(client);
114      }
115      finally {
116        try {
117          client.Close();
118        }
119        catch (Exception) {
120          client.Abort();
121        }
122      }
123    }
124
125    private void HandleAnonymousUser(HiveServiceClient client) {
126      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
127        try {
128          client.Close();
129        }
130        catch (Exception) {
131          client.Abort();
132        }
133        throw new AnonymousUserException();
134      }
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.