Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Hive/3.3/HiveServiceLocator.cs @ 11829

Last change on this file since 11829 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 3.9 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[11170]3 * Copyright (C) 2002-2014 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;
[10172]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
[9700]38    private HiveServiceLocator() {
39    }
40
[6976]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
[9700]53    public int EndpointRetries { get; private set; }
54
55    public string WorkingEndpoint { get; private set; }
56
[6976]57    private HiveServiceClient NewServiceClient() {
[9700]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        }
[10172]72        catch (EndpointNotFoundException exc) {
[9700]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
[6976]84      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
[9700]85        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
[6976]86      else
[9700]87        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
[7142]88
[6976]89      return cl;
90    }
91
92    public T CallHiveService<T>(Func<IHiveService, T> call) {
93      HiveServiceClient client = NewServiceClient();
[7249]94      HandleAnonymousUser(client);
95
[6976]96      try {
97        return call(client);
[7249]98      }
99      finally {
[6976]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();
[7249]111      HandleAnonymousUser(client);
112
[6976]113      try {
114        call(client);
[7249]115      }
116      finally {
[6976]117        try {
118          client.Close();
119        }
120        catch (Exception) {
121          client.Abort();
122        }
123      }
124    }
[7249]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    }
[6976]137  }
138}
Note: See TracBrowser for help on using the repository browser.